(Simple) Tracing with Ruby

Hi Rubyists !

I'm using ruby 1.6.8 (2002-12-24) [i586-mswin32]
What I tinkered for tracing is (simplified) this:

#begin definition
def traceEntry (*args)
    puts "\n"
    puts ("ENTRY " + caller[0])
    args.each {|arg| puts ("ARG: " + arg.to_s)}
end

def traceExit
    puts ("EXIT " + caller[0])
    puts "\n"
end
#end definition

#begin usage
def method1 (arg1 = 0, arg2 = false, arg3 = 'hello')
traceEntry (arg1, arg2, arg3)
    # do some stuff
traceExit
end

method1
method1 (1, true, 'world')
#end usage

It produces this:

#begin output

ENTRY SimpleTracingWithRuby.rb:18:in `method1'
ARG: 0
ARG: false
ARG: hello
EXIT SimpleTracingWithRuby.rb:20:in `method1'

ENTRY SimpleTracingWithRuby.rb:18:in `method1'
ARG: 1
ARG: true
ARG: world
EXIT SimpleTracingWithRuby.rb:20:in `method1'

#end output

My question is:
How do I know the names of the formal arguments of the actual caller of
TraceEntry ?
Or by Example:

#begin desired usage
def method1 (arg1 = 1, arg2 = true, arg3 = 'hello')
traceEntry
    # do some stuff
traceExit
end
#end desired usage

#begin desired output

ENTRY SimpleTracingWithRuby.rb:18:in `method1'
arg1: 0
arg2: false
arg3: hello
EXIT SimpleTracingWithRuby.rb:20:in `method1'

ENTRY SimpleTracingWithRuby.rb:18:in `method1'
arg1: 1
arg2: true
arg3: world
EXIT SimpleTracingWithRuby.rb:20:in `method1'

#end desired output

Any ideas welcomed !
Chris

"Christoph Neubauer" <christoph.neubauer@siemens.com> schrieb im
Newsbeitrag news:cdghds$plc$1@news.siemens.at...

Hi Rubyists !

I'm using ruby 1.6.8 (2002-12-24) [i586-mswin32]
What I tinkered for tracing is (simplified) this:

Do you know Kernel#set_trace_func() ? That'll do what you need much more
easily IMHO.
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Kernel.html#M001573

(It's already present in 1.6.8)

My question is:
How do I know the names of the formal arguments of the actual caller of
TraceEntry ?

You can't other that providing them explicitely, for example by using a
hash:

def traceEntry (args={})
    puts "\n"
    puts ("ENTRY " + caller[0])
    args.each {|name, arg| puts "ARG: #{name}=#{arg}"}
end

def traceExit
    puts ("EXIT " + caller[0])
    puts "\n"
end

and then do

def method1 (arg1 = 0, arg2 = false, arg3 = 'hello')
  traceEntry ("arg1" => arg1, "arg2" => arg2, "arg3" => arg3)
  begin
    # do some stuff
  ensure
    # record even in case of an exception
    traceExit
  end
end

Kind regards

    robert

Christoph Neubauer wrote:

Hi Rubyists !

Moin!

I'm using ruby 1.6.8 (2002-12-24) [i586-mswin32]
What I tinkered for tracing is (simplified) this:

Ruby 1.8.0 contains a tracer.rb as part of the Standard Library. I'm not sure whether it already existed in Ruby 1.6.8, but maybe you can make some usage of it or its source code.

Regards,
Florian Gross

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:2m215lFi754cU1@uni-berlin.de...
<snip>

Do you know Kernel#set_trace_func() ? That'll do what you need much more
easily IMHO.
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Kernel.html#M001573

(It's already present in 1.6.8)

Thanks, I'll follow that link

> My question is:
> How do I know the names of the formal arguments of the actual caller of
> TraceEntry ?

You can't other that providing them explicitely, for example by using a
hash:

I already thougt about a hash-solution, but I hoped to get something like a
hash implicitely,
such minimizing work and possible typo errors.

Many thanks for fast answer,
Chris

"Christoph Neubauer" <christoph.neubauer@siemens.com> schrieb im
Newsbeitrag news:cdgjre$ps6$1@news.siemens.at...

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:2m215lFi754cU1@uni-berlin.de...
<snip>

>
> Do you know Kernel#set_trace_func() ? That'll do what you need much

more

> easily IMHO.
> http://www.ruby-doc.org/docs/rdoc/1.9/classes/Kernel.html#M001573
>
> (It's already present in 1.6.8)

Thanks, I'll follow that link

>
> > My question is:
> > How do I know the names of the formal arguments of the actual caller

of

> > TraceEntry ?
>
> You can't other that providing them explicitely, for example by using

a

> hash:
>

I already thougt about a hash-solution, but I hoped to get something

like a

hash implicitely,
such minimizing work and possible typo errors.

Not without doing work somewhere else. You could somehow evaluate
__FILE__ and __LINE__ or caller to get the source line number and try to
extract argument names from the source file. But this is error prone and
doesn't work well with eval and such.

Many thanks for fast answer,

You're welcome!

    robert