From: Joel VanderWerf Date: 2007-09-13T02:37:43+09:00 Subject: Re: Logging all method calls? Gustav Paul wrote: > Hey > > Is there a way to log all the method calls that occur throughout the > execution of some script? Something like an hook that gets called whenever a > method executes would be ideal. > > Eg. > > class Object > def before_method_called(method_name) > p class.name + method_name.to_s > end > end There's a heavy speed cost to this, but it's what your asking for... ri set_trace_func | cat -------------------------------------------------- Kernel#set_trace_func set_trace_func(proc) => proc set_trace_func(nil) => nil ------------------------------------------------------------------------ Establishes proc as the handler for tracing, or disables tracing if the parameter is nil. proc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. proc is invoked whenever an event occurs. Events are: c-call (call a C-language routine), c-return (return from a C-language routine), call (call a Ruby method), class (start a class or module definition), end (finish a class or module definition), line (execute code on a new line), raise (raise an exception), and return (return from a Ruby method). Tracing is disabled within the context of proc. class Test def test a = 1 b = 2 end end set_trace_func proc { |event, file, line, id, binding, classname| printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname } t = Test.new t.test line prog.rb:11 false c-call prog.rb:11 new Class c-call prog.rb:11 initialize Object c-return prog.rb:11 initialize Object c-return prog.rb:11 new Class line prog.rb:12 false call prog.rb:2 test Test line prog.rb:3 test Test line prog.rb:4 test Test return prog.rb:4 test Test -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407