-- |Description: Internal
module Polysemy.Log.Level where

import qualified Polysemy.Log.Effect.DataLog as DataLog
import Polysemy.Log.Effect.DataLog (DataLog (DataLog, Local))
import qualified Polysemy.Log.Data.LogEntry as LogEntry
import Polysemy.Log.Data.LogEntry (LogEntry)
import qualified Polysemy.Log.Data.LogMessage as LogMessage
import Polysemy.Log.Data.LogMessage (LogMessage)
import Polysemy.Log.Data.Severity (Severity)

-- |Set the minimum severity for messages to be handled, with 'Nothing' meaning no messages are logged.
-- This can be used with arbitrary message types, using the @ex@ argument to extract the severity from the message.
setLogLevelWith ::
   msg r a .
  Member (DataLog msg) r =>
  (msg -> Severity) ->
  Maybe Severity ->
  Sem r a ->
  Sem r a
setLogLevelWith :: forall msg (r :: EffectRow) a.
Member (DataLog msg) r =>
(msg -> Severity) -> Maybe Severity -> Sem r a -> Sem r a
setLogLevelWith msg -> Severity
ex Maybe Severity
level =
  forall (e :: Effect) (r :: EffectRow) a.
Member e r =>
(forall x (rInitial :: EffectRow).
 e (Sem rInitial) x -> Tactical e (Sem rInitial) r x)
-> Sem r a -> Sem r a
interceptH @(DataLog msg) \case
    DataLog msg
msg -> do
      Bool
-> Sem (WithTactics (DataLog msg) f (Sem rInitial) r) ()
-> Sem (WithTactics (DataLog msg) f (Sem rInitial) r) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> (Severity -> Bool) -> Maybe Severity -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (msg -> Severity
ex msg
msg Severity -> Severity -> Bool
forall a. Ord a => a -> a -> Bool
>=) Maybe Severity
level) do
        msg -> Sem (WithTactics (DataLog msg) f (Sem rInitial) r) ()
forall a (r :: EffectRow). Member (DataLog a) r => a -> Sem r ()
DataLog.dataLog msg
msg
      () -> Sem (WithTactics (DataLog msg) f (Sem rInitial) r) (f ())
forall (f :: * -> *) a (e :: Effect) (m :: * -> *)
       (r :: EffectRow).
Functor f =>
a -> Sem (WithTactics e f m r) (f a)
pureT ()
    Local msg -> msg
f Sem rInitial x
mb ->
      (msg -> msg)
-> Sem (WithTactics (DataLog msg) f (Sem rInitial) r) (f x)
-> Sem (WithTactics (DataLog msg) f (Sem rInitial) r) (f x)
forall a (r :: EffectRow) b.
Member (DataLog a) r =>
(a -> a) -> Sem r b -> Sem r b
DataLog.local msg -> msg
f (Sem rInitial x -> Tactical (DataLog msg) (Sem rInitial) r x
forall (m :: * -> *) a (e :: Effect) (r :: EffectRow).
m a -> Tactical e m r a
runTSimple Sem rInitial x
mb)
{-# inline setLogLevelWith #-}

-- |Set the minimum severity for messages to be handled, with 'Nothing' meaning no messages are logged.
setLogLevel ::
  Member (DataLog (LogEntry LogMessage)) r =>
  Maybe Severity ->
  Sem r a ->
  Sem r a
setLogLevel :: forall (r :: EffectRow) a.
Member (DataLog (LogEntry LogMessage)) r =>
Maybe Severity -> Sem r a -> Sem r a
setLogLevel =
  (LogEntry LogMessage -> Severity)
-> Maybe Severity -> Sem r a -> Sem r a
forall msg (r :: EffectRow) a.
Member (DataLog msg) r =>
(msg -> Severity) -> Maybe Severity -> Sem r a -> Sem r a
setLogLevelWith (LogMessage -> Severity
LogMessage.severity (LogMessage -> Severity)
-> (LogEntry LogMessage -> LogMessage)
-> LogEntry LogMessage
-> Severity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogEntry LogMessage -> LogMessage
forall a. LogEntry a -> a
LogEntry.message)
{-# inline setLogLevel #-}