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

import GHC.Stack (withFrozenCallStack)

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

-- |The default high-level effect for simple text messages.
-- To be used with the severity constructors:
--
-- @
-- import qualified Polysemy.Log as Log
--
-- prog = do
--   Log.debug "debugging…"
--   Log.warn "warning!"
-- @
--
-- Interpreters should preprocess and relay the message to 'Polysemy.Log.DataLog'.
data Log :: Effect where
  -- |Schedule a message to be logged.
  Log :: HasCallStack => LogMessage -> Log m ()

-- |Log a message with the given severity.
-- Basic 'Sem' constructor.
log ::
  HasCallStack =>
  Member Log r =>
  Severity ->
  Text ->
  Sem r ()
log :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
severity Text
message =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Log (Sem r) () -> Sem r ()
forall (e :: Effect) (r :: EffectRow) a.
Member e r =>
e (Sem r) a -> Sem r a
send (LogMessage -> Log (Sem r) ()
forall (m :: * -> *). HasCallStack => LogMessage -> Log m ()
Log (Severity -> Text -> LogMessage
LogMessage Severity
severity Text
message))
{-# inline log #-}

-- |Log a message with the 'Trace' severity.
trace ::
  HasCallStack =>
  Member Log r =>
  Text ->
  Sem r ()
trace :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
trace =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Text -> Sem r ()) -> Text -> Sem r ())
-> (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
Trace
{-# inline trace #-}

-- |Log a message with the 'Debug' severity.
debug ::
  HasCallStack =>
  Member Log r =>
  Text ->
  Sem r ()
debug :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
debug =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Text -> Sem r ()) -> Text -> Sem r ())
-> (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
Debug
{-# inline debug #-}

-- |Log a message with the 'Info' severity.
info ::
  HasCallStack =>
  Member Log r =>
  Text ->
  Sem r ()
info :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
info =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Text -> Sem r ()) -> Text -> Sem r ())
-> (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
Info
{-# inline info #-}

-- |Log a message with the 'Warn' severity.
warn ::
  HasCallStack =>
  Member Log r =>
  Text ->
  Sem r ()
warn :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
warn =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Text -> Sem r ()) -> Text -> Sem r ())
-> (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
Warn
{-# inline warn #-}

-- |Log a message with the 'Polysemy.Log.Data.Severity.Error' severity.
error ::
  HasCallStack =>
  Member Log r =>
  Text ->
  Sem r ()
error :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
error =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Text -> Sem r ()) -> Text -> Sem r ())
-> (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
Error
{-# inline error #-}

-- |Log a message with the 'Crit' severity.
crit ::
  HasCallStack =>
  Member Log r =>
  Text ->
  Sem r ()
crit :: forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
crit =
  (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Text -> Sem r ()) -> Text -> Sem r ())
-> (HasCallStack => Text -> Sem r ()) -> Text -> Sem r ()
forall a b. (a -> b) -> a -> b
$
  Severity -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Severity -> Text -> Sem r ()
log Severity
Crit
{-# inline crit #-}