{-# LANGUAGE CPP #-}
module Colog.Core.IO
(
logStringStdout
, logStringStderr
, logStringHandle
, withLogStringFile
, logPrint
, logPrintStderr
, logPrintHandle
, withLogPrintFile
, liftLogIO
) where
import Colog.Core.Action (LogAction (..))
import Control.Monad.IO.Class (MonadIO, liftIO)
import System.IO (Handle, IOMode (AppendMode), hPrint, hPutStrLn, stderr, withFile)
logStringStdout :: MonadIO m => LogAction m String
logStringStdout :: LogAction m String
logStringStdout = (String -> m ()) -> LogAction m String
forall (m :: * -> *) msg. (msg -> m ()) -> LogAction m msg
LogAction (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (String -> IO ()) -> String -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ()
putStrLn)
{-# INLINE logStringStdout #-}
{-# SPECIALIZE logStringStdout :: LogAction IO String #-}
logStringStderr :: MonadIO m => LogAction m String
logStringStderr :: LogAction m String
logStringStderr = Handle -> LogAction m String
forall (m :: * -> *). MonadIO m => Handle -> LogAction m String
logStringHandle Handle
stderr
{-# INLINE logStringStderr #-}
{-# SPECIALIZE logStringStderr :: LogAction IO String #-}
logStringHandle :: MonadIO m => Handle -> LogAction m String
logStringHandle :: Handle -> LogAction m String
logStringHandle handle :: Handle
handle = (String -> m ()) -> LogAction m String
forall (m :: * -> *) msg. (msg -> m ()) -> LogAction m msg
LogAction ((String -> m ()) -> LogAction m String)
-> (String -> m ()) -> LogAction m String
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (String -> IO ()) -> String -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> String -> IO ()
hPutStrLn Handle
handle
{-# INLINE logStringHandle #-}
{-# SPECIALIZE logStringHandle :: Handle -> LogAction IO String #-}
withLogStringFile :: MonadIO m => FilePath -> (LogAction m String -> IO r) -> IO r
withLogStringFile :: String -> (LogAction m String -> IO r) -> IO r
withLogStringFile path :: String
path action :: LogAction m String -> IO r
action = String -> IOMode -> (Handle -> IO r) -> IO r
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
path IOMode
AppendMode ((Handle -> IO r) -> IO r) -> (Handle -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ LogAction m String -> IO r
action (LogAction m String -> IO r)
-> (Handle -> LogAction m String) -> Handle -> IO r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> LogAction m String
forall (m :: * -> *). MonadIO m => Handle -> LogAction m String
logStringHandle
{-# INLINE withLogStringFile #-}
{-# SPECIALIZE withLogStringFile :: FilePath -> (LogAction IO String -> IO r) -> IO r #-}
logPrint :: forall a m . (Show a, MonadIO m) => LogAction m a
logPrint :: LogAction m a
logPrint = (a -> m ()) -> LogAction m a
forall (m :: * -> *) msg. (msg -> m ()) -> LogAction m msg
LogAction ((a -> m ()) -> LogAction m a) -> (a -> m ()) -> LogAction m a
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IO ()
forall a. Show a => a -> IO ()
print
{-# INLINE logPrint #-}
{-# SPECIALIZE logPrint :: Show a => LogAction IO a #-}
logPrintStderr :: forall a m . (Show a, MonadIO m) => LogAction m a
logPrintStderr :: LogAction m a
logPrintStderr = Handle -> LogAction m a
forall a (m :: * -> *).
(Show a, MonadIO m) =>
Handle -> LogAction m a
logPrintHandle Handle
stderr
{-# INLINE logPrintStderr #-}
{-# SPECIALIZE logPrintStderr :: Show a => LogAction IO a #-}
logPrintHandle :: forall a m . (Show a, MonadIO m) => Handle -> LogAction m a
logPrintHandle :: Handle -> LogAction m a
logPrintHandle handle :: Handle
handle = (a -> m ()) -> LogAction m a
forall (m :: * -> *) msg. (msg -> m ()) -> LogAction m msg
LogAction ((a -> m ()) -> LogAction m a) -> (a -> m ()) -> LogAction m a
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> a -> IO ()
forall a. Show a => Handle -> a -> IO ()
hPrint Handle
handle
{-# INLINE logPrintHandle #-}
{-# SPECIALIZE logPrintHandle :: Show a => Handle -> LogAction IO a #-}
withLogPrintFile
:: forall a m r . (Show a, MonadIO m)
=> FilePath
-> (LogAction m a -> IO r)
-> IO r
withLogPrintFile :: String -> (LogAction m a -> IO r) -> IO r
withLogPrintFile path :: String
path action :: LogAction m a -> IO r
action = String -> IOMode -> (Handle -> IO r) -> IO r
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
path IOMode
AppendMode ((Handle -> IO r) -> IO r) -> (Handle -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ LogAction m a -> IO r
action (LogAction m a -> IO r)
-> (Handle -> LogAction m a) -> Handle -> IO r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> LogAction m a
forall a (m :: * -> *).
(Show a, MonadIO m) =>
Handle -> LogAction m a
logPrintHandle
{-# INLINE withLogPrintFile #-}
{-# SPECIALIZE withLogPrintFile :: Show a => FilePath -> (LogAction IO a -> IO r) -> IO r #-}
liftLogIO :: MonadIO m => LogAction IO msg -> LogAction m msg
liftLogIO :: LogAction IO msg -> LogAction m msg
liftLogIO (LogAction action :: msg -> IO ()
action) = (msg -> m ()) -> LogAction m msg
forall (m :: * -> *) msg. (msg -> m ()) -> LogAction m msg
LogAction (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (msg -> IO ()) -> msg -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. msg -> IO ()
action)
{-# INLINE liftLogIO #-}