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 144767e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 37 deletions.
7 changes: 7 additions & 0 deletions co-log-core/CHANGELOG.md
Expand Up @@ -4,6 +4,13 @@ Change log
`co-log-core` uses [PVP Versioning][1].
The change log is available [on GitHub][2].

0.1.1
=====

* [#46](https://github.com/kowainik/co-log/issues/46):
Moves `logStringStdout`, `logStringStderr`, `logStringHandle`,
`withLogStringFile` from `Colog.Actions` to `Colog.Core.IO`

0.1.0
=====

Expand Down
3 changes: 2 additions & 1 deletion co-log-core/co-log-core.cabal
@@ -1,5 +1,5 @@
name: co-log-core
version: 0.1.0
version: 0.1.1
description: Logging library
synopsis: Logging library
homepage: https://github.com/kowainik/co-log
Expand All @@ -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
2 changes: 2 additions & 0 deletions co-log-core/src/Colog/Core.hs
@@ -1,9 +1,11 @@
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
33 changes: 33 additions & 0 deletions co-log-core/src/Colog/Core/IO.hs
@@ -0,0 +1,33 @@
module Colog.Core.IO
( logStringStdout
, logStringStderr
, logStringHandle
, withLogStringFile
) where

import Control.Monad.IO.Class (MonadIO, liftIO)
import System.IO (Handle, IOMode( AppendMode ), hPutStrLn, stderr, withFile)
import Colog.Core.Action (LogAction (..))


{- | Action that prints 'String' to stdout. -}
logStringStdout :: MonadIO m => LogAction m String
logStringStdout = LogAction (liftIO . 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
7 changes: 7 additions & 0 deletions co-log/CHANGELOG.md
Expand Up @@ -4,6 +4,13 @@ Change log
`co-log uses` [PVP Versioning][1].
The change log is available [on GitHub][2].

0.2.0
=====

* [#46](https://github.com/kowainik/co-log/issues/46):
Moves `logStringStdout`, `logStringStderr`, `logStringHandle`,
`withLogStringFile` from `Colog.Actions` to `Colog.Core.IO`

0.1.0
=====

Expand Down
2 changes: 1 addition & 1 deletion co-log/co-log.cabal
@@ -1,6 +1,6 @@
cabal-version: 2.0
name: co-log
version: 0.1.0
version: 0.2.0
description: Logging library
synopsis: Logging library
homepage: https://github.com/kowainik/co-log
Expand Down
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 144767e

Please sign in to comment.