module Control.Effect.Logging
(
LogMsg (..)
, Logging (..)
, loggerLog
, logTH
, logDebug
, logInfo
, logWarn
, logError
)
where
import Control.Algebra (Has, send)
import Control.Monad.Logger
( Loc
, LogLevel (LevelDebug, LevelError, LevelInfo, LevelWarn)
, ToLogStr
, liftLoc
)
import Data.Kind (Type)
import Data.Text (Text)
import Language.Haskell.TH (Exp, Q)
import Language.Haskell.TH.Syntax qualified as TH
data LogMsg where
MkLogMsg
:: ToLogStr msg
=> { LogMsg -> Loc
loc :: !Loc
, LogMsg -> LogLevel
lvl :: !LogLevel
, ()
msg :: !msg
}
-> LogMsg
type Logging :: (Type -> Type) -> Type -> Type
data Logging m r where
LoggerLog :: !LogMsg -> Logging m ()
loggerLog :: forall msg sig m. (Has Logging sig m, ToLogStr msg) => Loc -> LogLevel -> msg -> m ()
loggerLog :: forall msg (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Has Logging sig m, ToLogStr msg) =>
Loc -> LogLevel -> msg -> m ()
loggerLog Loc
loc LogLevel
lvl msg
msg = Logging m () -> m ()
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (LogMsg -> Logging m ()
forall (m :: * -> *). LogMsg -> Logging m ()
LoggerLog (Loc -> LogLevel -> msg -> LogMsg
forall msg. ToLogStr msg => Loc -> LogLevel -> msg -> LogMsg
MkLogMsg Loc
loc LogLevel
lvl msg
msg))
{-# INLINE loggerLog #-}
logTH :: LogLevel -> Q Exp
logTH :: LogLevel -> Q Exp
logTH LogLevel
level = [|loggerLog @Text $(Q Loc
forall (m :: * -> *). Quasi m => m Loc
TH.qLocation Q Loc -> (Loc -> Q Exp) -> Q Exp
forall a b. Q a -> (a -> Q b) -> Q b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Loc -> Q Exp
liftLoc) $(LogLevel -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => LogLevel -> m Exp
TH.lift LogLevel
level)|]
logDebug :: Q Exp
logDebug :: Q Exp
logDebug = LogLevel -> Q Exp
logTH LogLevel
LevelDebug
logInfo :: Q Exp
logInfo :: Q Exp
logInfo = LogLevel -> Q Exp
logTH LogLevel
LevelInfo
logWarn :: Q Exp
logWarn :: Q Exp
logWarn = LogLevel -> Q Exp
logTH LogLevel
LevelWarn
logError :: Q Exp
logError :: Q Exp
logError = LogLevel -> Q Exp
logTH LogLevel
LevelError