[erlang-questions] Extending the io server

Vlad Dumitrescu vladdu55@REDACTED
Fri Nov 3 13:54:37 CET 2006


Well, what's a lunch break without some heavyweight coding? :-) I have
implemented a module that at runtime modifies the io server to send
copies of all requests to a specified process.

The implementation uses Smerl (thanks Yariv! :-) and will be included
in Erlide. For now you can get it from TrapExit
http://forum.trapexit.org/viewtopic.php?t=6664.

Usage:
*stdio_handler:install(Handler) when is_atom(Handler)
Handler has to be a process' name because we can't get an abstract
form for pids...
*stdio_handler:uninstall()

We change the second clause of io:request/2 from

  request(Pid, Request) when pid(Pid) ->
     Mref = erlang:monitor(process,Pid),
     Pid ! {io_request,self(),Pid,io_request(Pid, Request)},
     wait_io_mon_reply(Pid,Mref);

with

  request(Pid, Request) when pid(Pid) ->
     case proccess_info(Pid, group_leader} of
         {group_leader, Pid} ->
             catch *Handler* ! {request, Pid, Request, self(), erlang:now()};
         _ ->
             ok
     end,
     Mref = erlang:monitor(process,Pid),
     Pid ! {io_request,self(),Pid,io_request(Pid, Request)},
     wait_io_mon_reply(Pid,Mref);

We only want to capture standard_io output, i.e. where the
group_leader is its own group_leader. So now your process will get
notified when something gets read or written.

best regards,
Vlad



More information about the erlang-questions mailing list