Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine Handles in Haskell?

I'd like to have something like bash's 2>&1 redirect in Haskell that combines stdout and stderr from a process into a single Handle. It would be nice to do this directly with System.Process.createProcess or a similar library function, particularly if it used the same semantics as the bash redirect w.r.t. interleaving input from the handles.

The flexibility offered by createProcess seems promising at first: one can specify a Handle to use for the standard file descriptors, so the same Handle could be given for both stdout and stderr. However, the Handle arguments must already exist before the call. Without the ability to create a Handle from thin air before calling the function, I'm not sure the problem can be solved this way.

Edit: The solution needs to work regardless of platform.

like image 232
acfoltzer Avatar asked Jul 18 '11 17:07

acfoltzer


1 Answers

Getting your hands on a Handle isn't too hard: System.IO offers constants stdin,stdout,stderr :: Handle and functions withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r and openFile :: FilePath -> IOMode -> IO Handle.

Alternately, you could request new pipes from createProcess and set yourself up as a forwarding service (reading from the new stdout and stderr handles of your child and sending both to wherever you like).

like image 105
Daniel Wagner Avatar answered Oct 19 '22 11:10

Daniel Wagner