hyperboreean
hyperboreean

Reputation: 8333

erlang - inspect mailbox messages once at a time

I am trying to inspect the messages a node receives from other nodes, but in some other manner other than flush(), because the message size is rather big and it doesn't help. Also, I can see the messages with erlang:process_info(self(), messages_queue_len)., but I would like some way of extracting one message at a time in some kind of variable for debugging purposes.

Upvotes: 4

Views: 1881

Answers (3)

Gustavo Chaín
Gustavo Chaín

Reputation: 933

or you can use:

1> F = fun() -> receive X -> {message, X} after 0 -> no_message end end.
#Fun<erl_eval.20.111823515>
2> F().
no_message
3> self() ! foo.
foo
4> self() ! bar.
bar
5> F().
{message, foo}
6> F().
{message, bar}

... to prevent blocking

Upvotes: 2

Roberto Aloi
Roberto Aloi

Reputation: 30985

You might want to have a look to the dbg module in Erlang.

Start the tracer:

dbg:tracer().

Trace all messages received (r) by a process (in this case self()):

dbg:p(self(), r).

More information here.

Upvotes: 7

cthulahoops
cthulahoops

Reputation: 3835

receive is the erlang primitive for taking messages from the mailbox.

See: http://www.erlang.org/doc/getting_started/conc_prog.html#id2263965

If you just want to get the first message in the shell for debugging, you could try defining a fun like this:

1> self() ! foo.
foo
2> F = fun() -> receive X -> X end end.
#Fun<erl_eval.20.67289768>
3> F().
foo

Upvotes: 1

Related Questions