[erlang-questions] Logging interprocess communication

Joe Armstrong erlang@REDACTED
Sun Dec 9 15:13:31 CET 2012


This is *exactly* what the trace BIFs are for

erlang:trace(PidSpec, Bool, TraceFlags) does what you want.

Here is an example:

-module(tracer).
-export([start/0, loop/0, watch/1]).

start() ->
    Pid = spawn(tracer, loop, []),
    spawn(tracer, watch, [Pid]),
    Pid.

loop() ->
    receive
Any ->
    io:format("I got:~p~n",[Any]),
    loop()
    end.

watch(Pid) ->
    erlang:trace(Pid, true, [send, timestamp]),
    watch().

watch() ->
    receive
Any ->
    io:format("watch got:~p~n",[Any]),
    loop()
    end.

In the shell

2> c(tracer).
{ok,tracer}
3> Pid = tracer:start().
<0.43.0>
4> Pid ! hello.
I got:hello
hello
watch got:{trace_ts,<0.43.0>,send,
              {io_request,<0.43.0>,<0.24.0>,
                  {put_chars,unicode,io_lib,format,["I got:~p~n",[hello]]}},
              <0.24.0>,
              {1355,61952,458716}}

The line

 erlang:trace(Pid, true, [send, timestamp]),

means

"trace the process Pid and tell me about all messages it sends, an include a
time stamp. "Tell me" here means 'send me a message'

This is described (with examples) in my book and in Francesco and Simon's
book, and at http://www.erlang.org/doc/man/erlang.html#trace-3

Cheers

/Joe




On Sun, Dec 9, 2012 at 2:35 PM, Yash Ganthe <yashgt@REDACTED> wrote:

>
> Hi,
>
> 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.
>
> What is the best way to achieve this?
>
> Thanks,
> Yash
> --
> Using Opera's revolutionary email client: http://www.opera.com/mail/
> ______________________________**_________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/**listinfo/erlang-questions<http://erlang.org/mailman/listinfo/erlang-questions>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121209/408ea35e/attachment.htm>


More information about the erlang-questions mailing list