Skip to content

Commit

Permalink
[co-log#46] Move log actions that work with String to Colog.Core.IO
Browse files Browse the repository at this point in the history
  • Loading branch information
sphaso committed Oct 4, 2018
1 parent 695a27f commit dc613e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
1 change: 1 addition & 0 deletions co-log-core/co-log-core.cabal
Expand Up @@ -25,6 +25,7 @@ library
Colog.Core.Action
Colog.Core.Class
Colog.Core.Severity
Colog.Core.IO

build-depends: base >= 4.11 && < 5

Expand Down
3 changes: 3 additions & 0 deletions 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

37 changes: 37 additions & 0 deletions 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
37 changes: 2 additions & 35 deletions 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
Expand All @@ -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
----------------------------------------------------------------------------
Expand Down

0 comments on commit dc613e9

Please sign in to comment.