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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With