io-capture-1.0.0: Capture IO actions' stdout and stderr

Safe HaskellNone
LanguageHaskell2010

System.IO.Capture

Synopsis

Documentation

capture :: IO a -> IO (ByteString, ByteString, ByteString, Maybe ProcessStatus) Source

Capture an IO action's stdout, stderr, and any thrown exception. Waits for the action to complete before returning.

> (out, _, _, _) <- capture (putStrLn "foo")
> print out
"foo"

> (_, err, _, _) <- capture (hPutStrLn stderr "bar")
> print err
"bar"

> (_, _, exc, _) <- capture undefined
> print exc
"Prelude.undefined"

captureStream :: IO a -> IO (ByteString IO (), ByteString IO (), ByteString IO (), ProcessID) Source

Stream an IO action's stdout, stderr, and any thrown exception. Also returns the spawned process's pid to wait on (otherwise the process will become a zombie after terminating), kill, etc.

import qualified Data.ByteString.Streaming as S

> let action = putStrLn "foo" >> threadDelay 1000000
> (out, _, _, pid) <- captureStream (replicateM_ 5 action)
> S.stdout out
foo
foo
foo
foo
foo
> getProcessStatus True True pid
Just (Exited ExitSuccess)