Erlang: How to view the output of io: format / 2 calls in processes spawned on remote nodes

I am working on a decentralized Erlang application. I am currently working on the same PC and create several nodes by initializing erl with the -sname flag.

When I create a process using spawn/4 in my home node, I can see the output generated by io:format/2 calls inside this process in its home erl instance.

When I start the process remotely using spawn/4 in combination with register_name , the output of io:format/2 sometimes redirected back to the erl instance where the remote spawn/4 call was deleted, and sometimes completely invisible.

Similarly, when I use rpc:call/4 , the output of io:format/2 calls is redirected back to the erl instance, where the call to `rpc: call / 4 'is made.

How do you get the process to output debug output back to the parent erl instance?

+4
source share
3 answers

You can specify the 1st argument in io: format / 3 format in the second node using the result of erlang: group_leader () from the first node.

Starting first with a node, registering the local group of the shell process group globally:

 erl -sname a ( a@localhost )1> global:register_name(global_io_srv, group_leader()). yes 

Starting from the second node, connecting using a globally registered process as an io device

 erl -sname b ( b@localhost )1> net_kernel:connect( a@localhost ). true ( b@localhost )2> io:format(global:whereis_name(global_io_srv),"test output",[]). ok 

You will see the test output in the first node. This is just as the Christian suggested, a little more explicit. That way you can have error_logger for production logging and io: format / 3 for quick debugging only.

+7
source

What you see are processes with their group leader set to pid on the node from which they were created. See erlang: group_leader . The leader of the group is the place where they send their result.

You call this output "debug output", so you are sure you do not want to run the sasl application on the nodes and use error_logger ?

+4
source

See my answer to the Erlang question : RPC for node with output to this node for some details on how to get output to different places. What is not mentioned, you can start the remote shell even when the shell starts. Just press Ctrl+G ( ^G hint at startup), and then you have help in h (press h and Enter ).

Example. Suppose you have erlang node running on erl -sname foo . Than:

 $ erl -sname bar Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.7.5 (abort with ^G) ( bar@hynek-notebook )1> User switch command --> r ' foo@hynek-notebook ' --> j 1 {shell,start,[init]} 2* {' foo@hynek-notebook ',shell,start,[]} --> h c [nn] - connect to job i [nn] - interrupt job k [nn] - kill job j - list all jobs s [shell] - start local shell r [node [shell]] - start remote shell q - quit erlang ? | h - this message --> c Eshell V5.7.5 (abort with ^G) ( foo@hynek-notebook )1> 
+3
source

Source: https://habr.com/ru/post/1306155/


All Articles