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'
Cheers
/Joe