From f770b26a6adc66d68a6d5d4cbfe70fefcd41d373 Mon Sep 17 00:00:00 2001 From: sphaso Date: Wed, 3 Oct 2018 06:38:10 +0200 Subject: [PATCH] [#46] Move log actions that work with String to Colog.Core.IO --- co-log-core/co-log-core.cabal | 1 + co-log-core/src/Colog/Core.hs | 3 +++ co-log-core/src/Colog/Core/IO.hs | 37 ++++++++++++++++++++++++++++++++ co-log/src/Colog/Actions.hs | 37 ++------------------------------ 4 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 co-log-core/src/Colog/Core/IO.hs diff --git a/co-log-core/co-log-core.cabal b/co-log-core/co-log-core.cabal index 9e77464..fef09d7 100644 --- a/co-log-core/co-log-core.cabal +++ b/co-log-core/co-log-core.cabal @@ -25,6 +25,7 @@ library Colog.Core.Action Colog.Core.Class Colog.Core.Severity + Colog.Core.IO build-depends: base >= 4.11 && < 5 diff --git a/co-log-core/src/Colog/Core.hs b/co-log-core/src/Colog/Core.hs index 60eb6c1..6b17611 100644 --- a/co-log-core/src/Colog/Core.hs +++ b/co-log-core/src/Colog/Core.hs @@ -1,9 +1,12 @@ module Colog.Core ( module Colog.Core.Action , module Colog.Core.Class + , module Colog.Core.IO , module Colog.Core.Severity ) where import Colog.Core.Action import Colog.Core.Class +import Colog.Core.IO import Colog.Core.Severity + diff --git a/co-log-core/src/Colog/Core/IO.hs b/co-log-core/src/Colog/Core/IO.hs new file mode 100644 index 0000000..ec92bd7 --- /dev/null +++ b/co-log-core/src/Colog/Core/IO.hs @@ -0,0 +1,37 @@ +module Colog.Core.IO + ( logStringStdout + , logStringStderr + , logStringHandle + , withLogStringFile + ) where + +import Control.Monad.IO.Class (MonadIO, liftIO) +import System.IO (Handle, IOMode( AppendMode ), hPutStrLn, stderr, stdout, withFile) +import Colog.Core.Action (LogAction (..)) + + +---------------------------------------------------------------------------- +-- String +---------------------------------------------------------------------------- + +{- | Action that prints 'String' to stdout. -} +logStringStdout :: MonadIO m => LogAction m String +logStringStdout = logStringHandle stdout + +{- | Action that prints 'String' to stderr. -} +logStringStderr :: MonadIO m => LogAction m String +logStringStderr = logStringHandle stderr + +{- | Action that prints 'String' to 'Handle'. -} +logStringHandle :: MonadIO m => Handle -> LogAction m String +logStringHandle handle = LogAction $ liftIO . hPutStrLn handle + +{- | Action that prints 'String' to file. Instead of returning 'LogAction' it's +implemented in continuation-passing style because it's more efficient to open +file only once at the start of the application and write to 'Handle' instead of +opening file each time we need to write to it. + +Opens file in 'AppendMode'. +-} +withLogStringFile :: MonadIO m => FilePath -> (LogAction m String -> IO r) -> IO r +withLogStringFile path action = withFile path AppendMode $ action . logStringHandle diff --git a/co-log/src/Colog/Actions.hs b/co-log/src/Colog/Actions.hs index dacd651..90d200c 100644 --- a/co-log/src/Colog/Actions.hs +++ b/co-log/src/Colog/Actions.hs @@ -1,12 +1,7 @@ module Colog.Actions - ( -- * 'String' actions - logStringStdout - , logStringStderr - , logStringHandle - , withLogStringFile - + ( -- * 'ByteString' actions - , logByteStringStdout + logByteStringStdout , logByteStringStderr , logByteStringHandle , withLogByteStringFile @@ -18,39 +13,11 @@ module Colog.Actions , withLogTextFile ) where -import System.IO (hPutStrLn) - import Colog.Core.Action (LogAction (..)) import qualified Data.ByteString.Char8 as BS import qualified Data.Text.IO as TIO ----------------------------------------------------------------------------- --- String ----------------------------------------------------------------------------- - -{- | Action that prints 'String' to stdout. -} -logStringStdout :: MonadIO m => LogAction m String -logStringStdout = LogAction putStrLn - -{- | Action that prints 'String' to stderr. -} -logStringStderr :: MonadIO m => LogAction m String -logStringStderr = logStringHandle stderr - -{- | Action that prints 'String' to 'Handle'. -} -logStringHandle :: MonadIO m => Handle -> LogAction m String -logStringHandle handle = LogAction $ liftIO . hPutStrLn handle - -{- | Action that prints 'String' to file. Instead of returning 'LogAction' it's -implemented in continuation-passing style because it's more efficient to open -file only once at the start of the application and write to 'Handle' instead of -opening file each time we need to write to it. - -Opens file in 'AppendMode'. --} -withLogStringFile :: MonadIO m => FilePath -> (LogAction m String -> IO r) -> IO r -withLogStringFile path action = withFile path AppendMode $ action . logStringHandle - ---------------------------------------------------------------------------- -- ByteString ----------------------------------------------------------------------------