# Logging all method calls?

**URL:** <https://rubytalk.org/t/logging-all-method-calls/40571>\
**Category:** ruby-talk\
**Created:** [12 September 2007 17:16 UTC](https://rubytalk.org/t/logging-all-method-calls/40571 "2007-09-12T17:16:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gustav\_Railist](https://avatars.discourse-cdn.com/v4/letter/g/d2c977/32.png) [@Gustav\_Railist](https://rubytalk.org/u/Gustav_Railist)\
**Post date:** [12 September 2007 17:16 UTC](https://rubytalk.org/t/logging-all-method-calls/40571/1 "2007-09-12T17:16:39Z")

</div>

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  
&nbsp;&nbsp;def before\_method\_called(method\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;p [class.name](http://class.name) + method\_name.to\_s  
&nbsp;&nbsp;end  
end

Thanks in advance,  
Gustav Paul

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [12 September 2007 17:35 UTC](https://rubytalk.org/t/logging-all-method-calls/40571/2 "2007-09-12T17:35:21Z")

</div>

Check out profile.rb (in the standard lib), it will help you out with this.

Jason

> **···**
>
> On 9/12/07, Gustav Paul \<gustav@rails.co.za\> 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  
> > &nbsp;&nbsp;def before\_method\_called(method\_name)  
> > &nbsp;&nbsp;&nbsp;&nbsp;p class.name + method\_name.to\_s  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Thanks in advance,  
> > Gustav Paul

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [12 September 2007 17:37 UTC](https://rubytalk.org/t/logging-all-method-calls/40571/3 "2007-09-12T17:37:43Z")

</div>

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  
> &nbsp;&nbsp;def before\_method\_called(method\_name)  
> &nbsp;&nbsp;&nbsp;&nbsp;p class.name + method\_name.to\_s  
> &nbsp;&nbsp;end  
> end

There's a heavy speed cost to this, but it's what your asking for...

&nbsp;&nbsp;ri set\_trace\_func | cat  
-------------------------------------------------- Kernel#set\_trace\_func  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_trace\_func(proc) =\> proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_trace\_func(nil) =\> nil

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Establishes proc as the handler for tracing, or disables tracing  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if the parameter is nil. proc takes up to six parameters: an event  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name, a filename, a line number, an object id, a binding, and the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name of a class. proc is invoked whenever an event occurs. Events  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;are: c-call (call a C-language routine), c-return (return from a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C-language routine), call (call a Ruby method), class (start a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class or module definition), end (finish a class or module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;definition), line (execute code on a new line), raise (raise an  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exception), and return (return from a Ruby method). Tracing is  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disabled within the context of proc.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = 2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_trace\_func proc { |event, file, line, id, binding, classname|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = Test.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.test
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line prog.rb:11 false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c-call prog.rb:11 new Class  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c-call prog.rb:11 initialize Object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c-return prog.rb:11 initialize Object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c-return prog.rb:11 new Class  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line prog.rb:12 false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call prog.rb:2 test Test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line prog.rb:3 test Test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line prog.rb:4 test Test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return prog.rb:4 test Test
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [13 September 2007 20:41 UTC](https://rubytalk.org/t/logging-all-method-calls/40571/4 "2007-09-13T20:41:46Z")

</div>

ruby-prof

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
