Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Erlang, how can I independently capture stdout and stderr of a subprocess?

Tags:

erlang

I'm trying to figure out how to pull the stdout and stderr from a system subprocess in Erlang. (Not to be confused with an Erlang process.) The gotcha is I'm trying to pull the output of the streams independently.

open_port/2 seems to get me most of the way there, however it doesn't seem to provide a way to differentiate between the two streams. There is the stderr_to_stdout option, but that's not what I want; I want to get data from both data streams but be able to distinguish the two streams.

Any suggestions? Thanks.

Update: I'm ideally looking for a solution that will work on both Windows and Linux.

like image 322
Vultaire Avatar asked Jan 21 '16 19:01

Vultaire


1 Answers

Try this :

Path = filename:join(["./priv", "log", "log_file_name"]),
{ok, F} = file:open(Path, [write]),
group_leader(F, self()),
erlang:display("Anything this process outputs now gets redirected").
like image 124
Vans S Avatar answered Sep 28 '22 01:09

Vans S