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