[erlang-questions] Logging interprocess communication

32 views
Skip to first unread message

Yash Ganthe

unread,
Dec 9, 2012, 8:35:32 AM12/9/12
to erlang-q...@erlang.org

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-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Loïc Hoguin

unread,
Dec 9, 2012, 8:44:07 AM12/9/12
to Yash Ganthe, erlang-q...@erlang.org
On 12/09/2012 02:35 PM, Yash Ganthe 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?

I would look at Erlang support for DTrace/Systemtap depending on the
system you are running. For Linux, Systemtap needs a special kernel
build but that's not hard to setup (at least on Arch Linux).

--
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu

Jachym Holecek

unread,
Dec 9, 2012, 8:39:51 AM12/9/12
to Yash Ganthe, erlang-q...@erlang.org
# Yash Ganthe 2012-12-09:
> 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?

There's a builtin tracing facility inside the VM, controlled by
erlang:trace/3 and friends. The dbg(3) module builds on top of
it, supports dumping traces to file among other things. Not sure
if there are more elaborate frontends or step-by-step documents.

BR,
-- Jachym

Alex Shneyderman

unread,
Dec 9, 2012, 9:11:06 AM12/9/12
to Yash Ganthe, erlang-q...@erlang.org
You might find parse_transforms useful for this sort of things:

http://stackoverflow.com/questions/2416192/is-there-a-good-complete-tutorial-on-erlang-parse-transforms-available

I am not sure your particular use case can be weaved around but it
would be hard to believe if it is not.

Cheers.

Joe Armstrong

unread,
Dec 9, 2012, 9:13:31 AM12/9/12
to Yash Ganthe, erlang-q...@erlang.org
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 <yas...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages