------------------------------------------------------------------------
-- |
-- Module           : What4.Utils.Streams
-- Description      : IO stream utilities
-- Copyright        : (c) Galois, Inc 2013-2020
-- License          : BSD3
-- Maintainer       : Joe Hendrix <jhendrix@galois.com>
-- Stability        : provisional
------------------------------------------------------------------------
module What4.Utils.Streams
( logErrorStream
) where

import qualified Data.ByteString.UTF8 as UTF8
import qualified System.IO.Streams as Streams

-- | Write from input stream to a logging function.
logErrorStream :: Streams.InputStream UTF8.ByteString
               -> (String -> IO ()) -- ^ Logging function
               -> IO ()
logErrorStream :: InputStream ByteString -> (String -> IO ()) -> IO ()
logErrorStream InputStream ByteString
err_stream String -> IO ()
logFn = do
  -- Have err_stream log complete lines to logLn
  let write_err :: Maybe String -> IO ()
write_err Maybe String
Nothing = forall (m :: Type -> Type) a. Monad m => a -> m a
return ()
      write_err (Just String
b) = String -> IO ()
logFn String
b
  OutputStream String
err_output <- forall a. (Maybe a -> IO ()) -> IO (OutputStream a)
Streams.makeOutputStream Maybe String -> IO ()
write_err
  InputStream String
lns <- forall a b. (a -> b) -> InputStream a -> IO (InputStream b)
Streams.map ByteString -> String
UTF8.toString forall (m :: Type -> Type) a b. Monad m => (a -> m b) -> m a -> m b
=<< InputStream ByteString -> IO (InputStream ByteString)
Streams.lines InputStream ByteString
err_stream
  forall a. InputStream a -> OutputStream a -> IO ()
Streams.connect InputStream String
lns OutputStream String
err_output