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

后端 未结 3 1309
天涯浪人
天涯浪人 2021-02-10 15:31

I am working on a decentralized Erlang application. I am currently working on a single PC and creating multiple nodes by initializing erl with the -sname

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 16:18

    You can supply 1st argument to io:format/3 on the second node, using result of erlang:group_leader() from the first node.

    Starting first node, registering local shell process group leader globally:

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

    Starting second node, connecting, using globally registered process as 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 test output in the first node. This is the same way that Christian suggested, just a bit more explicit. So you can have error_logger for production logging and io:format/3 just for quick debugging.

提交回复
热议问题