simple-log 0.1.3 → 0.1.4
raw patch · 3 files changed
+54/−8 lines, 3 files
Files
- simple-log.cabal +1/−1
- src/System/Log/Base.hs +1/−1
- src/System/Log/Monad.hs +52/−6
simple-log.cabal view
@@ -1,5 +1,5 @@ Name: simple-log -Version: 0.1.3 +Version: 0.1.4 Synopsis: Simple log for Haskell Description: Log library for Haskell with removing unnecessary traces License: BSD3
src/System/Log/Base.hs view
@@ -220,7 +220,7 @@ scoperLog :: Show a => Log -> Text -> IO a -> IO a scoperLog l s act = do r <- scopeLog l s act - writeLog l Trace $ T.concat ["Scope ", s, " leaves with result: ", fromString $ show r] + writeLog l Trace $ T.concat ["Scope ", s, " leaves with result: ", fromString . show $ r] return r -- | Log entry, scope or message
src/System/Log/Monad.hs view
@@ -1,7 +1,18 @@-{-# LANGUAGE FlexibleInstances, UndecidableInstances #-} +{-# LANGUAGE OverloadedStrings, FlexibleInstances, UndecidableInstances #-} module System.Log.Monad ( - withNoLog, withLog, log, scope_, scope, scoper, MonadLog(..) + withNoLog, + withLog, + log, + scope_, + scope, + scopeM, + scoper, + scoperM, + ignoreError, + ignoreErrorM, + trace, + MonadLog(..) ) where import Prelude hiding (log, catch) @@ -9,7 +20,9 @@ import Control.Exception (SomeException) import Control.Monad.IO.Class import Control.Monad.Reader +import Control.Monad.Error import Control.Monad.CatchIO +import Data.String import Data.Text (Text) import qualified Data.Text as T import Data.Time @@ -41,15 +54,48 @@ -- | Scope with log all exceptions scope :: (MonadLog m) => Text -> m a -> m a -scope s act = scope_ s (catch act onError) where +scope s act = scope_ s $ catch act onError where onError :: (MonadLog m) => SomeException -> m a onError e = do - log Error $ T.pack $ "Scope leaves with exception: " ++ show e + log Error $ T.concat ["Scope leaves with exception: ", fromString . show $ e] throw e +-- | Scope with log exceptions from MonadError +scopeM :: (Error e, Show e, MonadLog m, MonadError e m) => Text -> m a -> m a +scopeM s act = scope s $ catchError act onError where + onError :: (Error e, Show e, MonadLog m, MonadError e m) => e -> m a + onError e = do + log Error $ T.concat ["Scope leaves with exception: ", fromString . show $ e] + throwError e + -- | Scope with tracing result -scoper :: (MonadLog m, Show a) => Text -> m a -> m a +scoper :: (Show a, MonadLog m) => Text -> m a -> m a scoper s act = do r <- scope s act - log Trace $ T.concat [T.pack "Scope ", s, T.pack " leaves with result: ", T.pack $ show r] + log Trace $ T.concat ["Scope ", s, " leaves with result: ", fromString . show $ r] return r + +scoperM :: (Error e, Show e, Show a, MonadLog m, MonadError e m) => Text -> m a -> m a +scoperM s act = do + r <- scopeM s act + log Trace $ T.concat ["Scope", s, " leaves with resul: ", fromString . show $ r] + return r + +-- | Ignore error +ignoreError :: (MonadLog m) => m () -> m () +ignoreError act = catch act onError where + onError :: (MonadLog m) => SomeException -> m () + onError _ = return () + +-- | Ignore MonadError error +ignoreErrorM :: (Error e, MonadLog m, MonadError e m) => m () -> m () +ignoreErrorM act = catchError act onError where + onError :: (Error e, MonadLog m, MonadError e m) => e -> m () + onError _ = return () + +-- | Trace value +trace :: (Show a, MonadLog m) => Text -> m a -> m a +trace name act = do + v <- act + log Trace $ T.concat [name, " = ", fromString . show $ v] + return v