Skip to content

Instantly share code, notes, and snippets.

@ngocdaothanh
Created December 11, 2012 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngocdaothanh/4255689 to your computer and use it in GitHub Desktop.
Save ngocdaothanh/4255689 to your computer and use it in GitHub Desktop.
[erlang-questions] Logging interprocess communication
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment