(Simple) Tracing with Ruby

C

Christoph Neubauer

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
 
R

Robert Klemme

Christoph Neubauer said:
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
 
C

Christoph Neubauer

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
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
 
R

Robert Klemme

Christoph Neubauer said:
Thanks, I'll follow that link


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
 
F

Florian Gross

Christoph said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top