This is *exactly* what the trace BIFs are for<div><br></div><div>erlang:trace(PidSpec, Bool, TraceFlags) does what you want.</div><div><br></div><div>Here is an example:</div><div><br></div><div><div>-module(tracer).</div>
<div>-export([start/0, loop/0, watch/1]).</div><div><br></div><div>start() -></div><div>    Pid = spawn(tracer, loop, []),</div><div>    spawn(tracer, watch, [Pid]),</div><div>    Pid.</div><div><br></div><div>loop() -></div>
<div>    receive</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>Any -></div><div><span class="Apple-tab-span" style="white-space:pre">    </span>    io:format("I got:~p~n",[Any]),</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>    loop()</div><div>    end.</div><div><br></div><div>watch(Pid) -></div><div>    erlang:trace(Pid, true, [send, timestamp]),</div><div>    watch().</div><div>
<br></div><div>watch() -></div><div>    receive</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>Any -></div><div><span class="Apple-tab-span" style="white-space:pre">    </span>    io:format("watch got:~p~n",[Any]),</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>    loop()</div><div>    end.</div></div><div><br></div><div>In the shell</div><div><br></div><div><div>2> c(tracer).</div><div>{ok,tracer}</div><div>3> Pid = tracer:start().</div>
<div><0.43.0></div><div>4> Pid ! hello.</div><div>I got:hello</div><div>hello</div><div>watch got:{trace_ts,<0.43.0>,send,</div><div>              {io_request,<0.43.0>,<0.24.0>,</div><div>                  {put_chars,unicode,io_lib,format,["I got:~p~n",[hello]]}},</div>
<div>              <0.24.0>,</div><div>              {1355,61952,458716}}</div></div><div><br></div><div>The line</div><div><br></div><div> erlang:trace(Pid, true, [send, timestamp]),</div><div><br></div><div>means</div>
<div><br></div><div>"trace the process Pid and tell me about all messages it sends, an include a</div><div>time stamp. "Tell me" here means 'send me a message'</div><div><br></div><div>This is described (with examples) in my book and in Francesco and Simon's book, and at <a href="http://www.erlang.org/doc/man/erlang.html#trace-3">http://www.erlang.org/doc/man/erlang.html#trace-3</a></div>
<div><br></div><div>Cheers</div><div><br></div><div>/Joe</div><div><br></div><div><br></div><div><br><br><div class="gmail_quote">On Sun, Dec 9, 2012 at 2:35 PM, Yash Ganthe <span dir="ltr"><<a href="mailto:yashgt@gmail.com" target="_blank">yashgt@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi,<br>
<br>
In an Erlang VM when one process send a message to another using: Pid ! MyMessage we would like to log this interaction to a log file. We need to do this without having the developer introduce explicit log statements all over the code wherever one process communicates with another.<br>

<br>
What is the best way to achieve this?<br>
<br>
Thanks,<br>
Yash<span class="HOEnZb"><font color="#888888"><br>
-- <br>
Using Opera's revolutionary email client: <a href="http://www.opera.com/mail/" target="_blank">http://www.opera.com/mail/</a><br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</font></span></blockquote></div><br></div>