{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Imm.Logger.Simple (module Imm.Logger.Simple, module Reexport) where
import Imm.Logger as Reexport
import Imm.Prelude
import Imm.Pretty
import Control.Concurrent.MVar.Lifted
import Control.Monad.Trans.Control
import Data.Text.Prettyprint.Doc.Render.Terminal
import System.Log.FastLogger as Reexport
data LoggerSettings = LoggerSettings
{ _loggerSet :: LoggerSet
, _errorLoggerSet :: LoggerSet
, _logLevel :: LogLevel
, _colorizeLogs :: Bool
}
defaultLogger :: IO (MVar LoggerSettings)
defaultLogger = newMVar =<< LoggerSettings
<$> newStdoutLoggerSet defaultBufSize
<*> newStderrLoggerSet defaultBufSize
<*> pure Info
<*> pure True
mkHandle :: MonadBaseControl IO m => MVar LoggerSettings -> Handle m
mkHandle settings = Handle
{ log = \l t -> do
s <- readMVar settings
let loggerSet = (if l == Error then _errorLoggerSet else _loggerSet) s
handleColor = (\c -> if c then id else unAnnotate) $ _colorizeLogs s
refLevel = _logLevel s
when (l >= refLevel) $ liftBase $ pushLogStrLn loggerSet $ toLogStr $ renderLazy $ layoutPretty defaultLayoutOptions $ handleColor t
, getLogLevel = _logLevel <$> readMVar settings
, setLogLevel = \level -> modifyMVar_ settings $ \s -> return (s { _logLevel = level })
, setColorizeLogs = \value -> modifyMVar_ settings $ \s -> return (s { _colorizeLogs = value })
, flushLogs = do
s <- readMVar settings
liftBase $ flushLogStr $ _loggerSet s
liftBase $ flushLogStr $ _errorLoggerSet s
}