{-# options_haddock prune #-}

-- |Description: Internal
module Polysemy.Log.Data.DataLog where

import GHC.Stack (withFrozenCallStack)
import Polysemy.Time (GhcTime)

import Polysemy.Log.Data.LogEntry (LogEntry, annotate)
import Polysemy.Log.Data.LogMessage (LogMessage (LogMessage))
import Polysemy.Log.Data.Severity (Severity (Crit, Debug, Error, Info, Trace, Warn))

-- |Structural logs, used as a backend for the simpler 'Text' log effect, 'Polysemy.Log.Log'.
--
-- Can also be used on its own, or reinterpreted into an effect like those from /co-log/ or /di/.
data DataLog a :: Effect where
  -- |Schedule an arbitrary value for logging.
  DataLog :: a -> DataLog a m ()
  -- |Stores the provided function in the interpreter and applies it to all log messages emitted within the higher-order
  -- thunk that's the second argument.
  Local :: (a -> a) -> m b -> DataLog a m b

makeSem ''DataLog

-- |Alias for the logger with the default message type used by 'Polysemy.Log.Log'.
type Logger =
  DataLog (LogEntry LogMessage)

-- |Log a text message with the given severity.
-- Basic 'Sem' constructor.
log ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Severity ->
  Text ->
  Sem r ()
log :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
severity Text
message =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack do
    DataLog (LogEntry LogMessage) (Sem r) () -> Sem r ()
forall (e :: Effect) (r :: EffectRow) a.
Member e r =>
e (Sem r) a -> Sem r a
send (DataLog (LogEntry LogMessage) (Sem r) () -> Sem r ())
-> (LogEntry LogMessage
    -> DataLog (LogEntry LogMessage) (Sem r) ())
-> LogEntry LogMessage
-> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogEntry LogMessage -> DataLog (LogEntry LogMessage) (Sem r) ()
forall a (m :: * -> *). a -> DataLog a m ()
DataLog (LogEntry LogMessage -> Sem r ())
-> Sem r (LogEntry LogMessage) -> Sem r ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LogMessage -> Sem r (LogEntry LogMessage)
forall (r :: EffectRow) a.
(HasCallStack, Member GhcTime r) =>
a -> Sem r (LogEntry a)
annotate (Severity -> Text -> LogMessage
LogMessage Severity
severity Text
message)
{-# inline log #-}

-- |Log a text message with the 'Trace' severity.
trace ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Text ->
  Sem r ()
trace :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Text -> Sem r ()
trace =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
Trace)
{-# inline trace #-}

-- |Log a text message with the 'Debug' severity.
debug ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Text ->
  Sem r ()
debug :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Text -> Sem r ()
debug =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
Debug)
{-# inline debug #-}

-- |Log a text message with the 'Info' severity.
info ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Text ->
  Sem r ()
info :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Text -> Sem r ()
info =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
Info)
{-# inline info #-}

-- |Log a text message with the 'Warn' severity.
warn ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Text ->
  Sem r ()
warn :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Text -> Sem r ()
warn =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
Warn)
{-# inline warn #-}

-- |Log a text message with the 'Polysemy.Log.Data.Severity.Error' severity.
error ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Text ->
  Sem r ()
error :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Text -> Sem r ()
error =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
Error)
{-# inline error #-}

-- |Log a text message with the 'Crit' severity.
crit ::
  HasCallStack =>
  Members [Logger, GhcTime] r =>
  Text ->
  Sem r ()
crit :: forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Text -> Sem r ()
crit =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Members '[Logger, GhcTime] r) =>
Severity -> Text -> Sem r ()
log Severity
Crit)
{-# inline crit #-}