-- |Description: Stderr Interpreters, Internal
module Polysemy.Log.Stderr where

import Polysemy.Conc (Race)
import Polysemy.Time (GhcTime, interpretTimeGhc)
import System.IO (stderr)

import Polysemy.Log.Effect.DataLog (DataLog)
import Polysemy.Log.Effect.Log (Log)
import Polysemy.Log.Data.LogEntry (LogEntry)
import Polysemy.Log.Data.LogMessage (LogMessage)
import Polysemy.Log.Data.Severity (Severity)
import Polysemy.Log.Format (formatLogEntry)
import Polysemy.Log.Handle (interpretDataLogHandleWith)
import Polysemy.Log.Level (setLogLevel)
import Polysemy.Log.Log (interpretLogDataLog, interpretLogDataLogConc)

-- |Interpret 'DataLog' by printing to stderr, converting messages to 'Text' with the supplied function.
interpretDataLogStderrWith ::
  Member (Embed IO) r =>
  (a -> Text) ->
  InterpreterFor (DataLog a) r
interpretDataLogStderrWith :: forall (r :: EffectRow) a.
Member (Embed IO) r =>
(a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogStderrWith =
  Handle -> (a -> Text) -> InterpreterFor (DataLog a) r
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> (a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogHandleWith Handle
stderr
{-# inline interpretDataLogStderrWith #-}

-- |Interpret 'DataLog' by printing to stderr, converting messages to 'Text' by using 'Show'.
interpretDataLogStderr ::
  Show a =>
  Member (Embed IO) r =>
  InterpreterFor (DataLog a) r
interpretDataLogStderr :: forall a (r :: EffectRow).
(Show a, Member (Embed IO) r) =>
InterpreterFor (DataLog a) r
interpretDataLogStderr =
  (a -> Text) -> InterpreterFor (DataLog a) r
forall (r :: EffectRow) a.
Member (Embed IO) r =>
(a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogStderrWith a -> Text
forall b a. (Show a, IsString b) => a -> b
show
{-# inline interpretDataLogStderr #-}

-- |Interpret 'Log' by printing to stderr, converting messages to 'Text' with the supplied function.
interpretLogStderrWith ::
  Members [Embed IO, GhcTime] r =>
  (LogEntry LogMessage -> Text) ->
  InterpreterFor Log r
interpretLogStderrWith :: forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
(LogEntry LogMessage -> Text) -> InterpreterFor Log r
interpretLogStderrWith LogEntry LogMessage -> Text
fmt =
  (LogEntry LogMessage -> Text)
-> InterpreterFor (DataLog (LogEntry LogMessage)) r
forall (r :: EffectRow) a.
Member (Embed IO) r =>
(a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogStderrWith LogEntry LogMessage -> Text
fmt (Sem (DataLog (LogEntry LogMessage) : r) a -> Sem r a)
-> (Sem (Log : r) a -> Sem (DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
-> Sem (DataLog (LogEntry LogMessage) : r) a
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor GhcTime r
interpretTimeGhc (Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
 -> Sem (DataLog (LogEntry LogMessage) : r) a)
-> (Sem (Log : r) a
    -> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem (DataLog (LogEntry LogMessage) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a
-> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
forall (r :: EffectRow).
Members '[DataLog (LogEntry LogMessage), GhcTime] r =>
InterpreterFor Log r
interpretLogDataLog (Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a
 -> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a)
-> (Sem (Log : r) a
    -> Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : r) a
-> Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a
forall (e2 :: Effect) (e3 :: Effect) (e1 :: Effect)
       (r :: EffectRow) a.
Sem (e1 : r) a -> Sem (e1 : e2 : e3 : r) a
raiseUnder2
{-# inline interpretLogStderrWith #-}

-- |Like 'interpretLogStderrWith', but setting a log level.
-- 'Nothing' causes no messages to be logged.
interpretLogStderrLevelWith ::
  Members [Embed IO, GhcTime] r =>
  (LogEntry LogMessage -> Text) ->
  Maybe Severity ->
  InterpreterFor Log r
interpretLogStderrLevelWith :: forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
(LogEntry LogMessage -> Text)
-> Maybe Severity -> InterpreterFor Log r
interpretLogStderrLevelWith LogEntry LogMessage -> Text
fmt Maybe Severity
level =
  (LogEntry LogMessage -> Text)
-> InterpreterFor (DataLog (LogEntry LogMessage)) r
forall (r :: EffectRow) a.
Member (Embed IO) r =>
(a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogStderrWith LogEntry LogMessage -> Text
fmt (Sem (DataLog (LogEntry LogMessage) : r) a -> Sem r a)
-> (Sem (Log : r) a -> Sem (DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Maybe Severity
-> Sem (DataLog (LogEntry LogMessage) : r) a
-> Sem (DataLog (LogEntry LogMessage) : r) a
forall (r :: EffectRow) a.
Member (DataLog (LogEntry LogMessage)) r =>
Maybe Severity -> Sem r a -> Sem r a
setLogLevel Maybe Severity
level (Sem (DataLog (LogEntry LogMessage) : r) a
 -> Sem (DataLog (LogEntry LogMessage) : r) a)
-> (Sem (Log : r) a -> Sem (DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem (DataLog (LogEntry LogMessage) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
-> Sem (DataLog (LogEntry LogMessage) : r) a
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor GhcTime r
interpretTimeGhc (Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
 -> Sem (DataLog (LogEntry LogMessage) : r) a)
-> (Sem (Log : r) a
    -> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem (DataLog (LogEntry LogMessage) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a
-> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
forall (r :: EffectRow).
Members '[DataLog (LogEntry LogMessage), GhcTime] r =>
InterpreterFor Log r
interpretLogDataLog (Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a
 -> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a)
-> (Sem (Log : r) a
    -> Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a)
-> Sem (Log : r) a
-> Sem (GhcTime : DataLog (LogEntry LogMessage) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : r) a
-> Sem (Log : GhcTime : DataLog (LogEntry LogMessage) : r) a
forall (e2 :: Effect) (e3 :: Effect) (e1 :: Effect)
       (r :: EffectRow) a.
Sem (e1 : r) a -> Sem (e1 : e2 : e3 : r) a
raiseUnder2
{-# inline interpretLogStderrLevelWith #-}

-- |Interpret 'Log' by printing to stderr, using the default formatter.
--
-- Since this adds a timestamp, it has a dependency on 'GhcTime'.
-- Use 'interpretLogStderr'' for a variant that interprets 'GhcTime' in-place.
interpretLogStderr ::
  Members [Embed IO, GhcTime] r =>
  InterpreterFor Log r
interpretLogStderr :: forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
InterpreterFor Log r
interpretLogStderr =
  (LogEntry LogMessage -> Text) -> InterpreterFor Log r
forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
(LogEntry LogMessage -> Text) -> InterpreterFor Log r
interpretLogStderrWith LogEntry LogMessage -> Text
formatLogEntry
{-# inline interpretLogStderr #-}

-- |Like 'interpretLogStderr', but setting a log level.
-- 'Nothing' causes no messages to be logged.
interpretLogStderrLevel ::
  Members [Embed IO, GhcTime] r =>
  Maybe Severity ->
  InterpreterFor Log r
interpretLogStderrLevel :: forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
Maybe Severity -> InterpreterFor Log r
interpretLogStderrLevel =
  (LogEntry LogMessage -> Text)
-> Maybe Severity -> InterpreterFor Log r
forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
(LogEntry LogMessage -> Text)
-> Maybe Severity -> InterpreterFor Log r
interpretLogStderrLevelWith LogEntry LogMessage -> Text
formatLogEntry
{-# inline interpretLogStderrLevel #-}

-- |Interpret 'Log' by printing to stderr, using the default formatter, then interpreting 'GhcTime'.
interpretLogStderr' ::
  Member (Embed IO) r =>
  InterpreterFor Log r
interpretLogStderr' :: forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor Log r
interpretLogStderr' =
  Sem (GhcTime : r) a -> Sem r a
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor GhcTime r
interpretTimeGhc (Sem (GhcTime : r) a -> Sem r a)
-> (Sem (Log : r) a -> Sem (GhcTime : r) a)
-> Sem (Log : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : GhcTime : r) a -> Sem (GhcTime : r) a
forall (r :: EffectRow).
Members '[Embed IO, GhcTime] r =>
InterpreterFor Log r
interpretLogStderr (Sem (Log : GhcTime : r) a -> Sem (GhcTime : r) a)
-> (Sem (Log : r) a -> Sem (Log : GhcTime : r) a)
-> Sem (Log : r) a
-> Sem (GhcTime : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : r) a -> Sem (Log : GhcTime : r) a
forall (e2 :: Effect) (e1 :: Effect) (r :: EffectRow) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder
{-# inline interpretLogStderr' #-}

-- |Like 'interpretLogStderr', but process messages concurrently.
interpretLogStderrConc ::
  Members [Resource, Async, Race, Embed IO] r =>
  InterpreterFor Log r
interpretLogStderrConc :: forall (r :: EffectRow).
Members '[Resource, Async, Race, Embed IO] r =>
InterpreterFor Log r
interpretLogStderrConc =
  Sem (GhcTime : r) a -> Sem r a
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor GhcTime r
interpretTimeGhc (Sem (GhcTime : r) a -> Sem r a)
-> (Sem (Log : r) a -> Sem (GhcTime : r) a)
-> Sem (Log : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  (LogEntry LogMessage -> Text)
-> InterpreterFor (DataLog (LogEntry LogMessage)) (GhcTime : r)
forall (r :: EffectRow) a.
Member (Embed IO) r =>
(a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogStderrWith LogEntry LogMessage -> Text
formatLogEntry (Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
 -> Sem (GhcTime : r) a)
-> (Sem (Log : r) a
    -> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> Sem (Log : r) a
-> Sem (GhcTime : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Int
-> InterpreterFor Log (DataLog (LogEntry LogMessage) : GhcTime : r)
forall (r :: EffectRow).
Members
  '[DataLog (LogEntry LogMessage), Resource, Async, Race, Embed IO]
  r =>
Int -> InterpreterFor Log r
interpretLogDataLogConc Int
64 (Sem (Log : DataLog (LogEntry LogMessage) : GhcTime : r) a
 -> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> (Sem (Log : r) a
    -> Sem (Log : DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> Sem (Log : r) a
-> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : r) a
-> Sem (Log : DataLog (LogEntry LogMessage) : GhcTime : r) a
forall (e2 :: Effect) (e3 :: Effect) (e1 :: Effect)
       (r :: EffectRow) a.
Sem (e1 : r) a -> Sem (e1 : e2 : e3 : r) a
raiseUnder2
{-# inline interpretLogStderrConc #-}

-- |Like 'interpretLogStderr', but process messages concurrently.
interpretLogStderrLevelConc ::
  Members [Resource, Async, Race, Embed IO] r =>
  Maybe Severity ->
  InterpreterFor Log r
interpretLogStderrLevelConc :: forall (r :: EffectRow).
Members '[Resource, Async, Race, Embed IO] r =>
Maybe Severity -> InterpreterFor Log r
interpretLogStderrLevelConc Maybe Severity
level =
  Sem (GhcTime : r) a -> Sem r a
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor GhcTime r
interpretTimeGhc (Sem (GhcTime : r) a -> Sem r a)
-> (Sem (Log : r) a -> Sem (GhcTime : r) a)
-> Sem (Log : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  (LogEntry LogMessage -> Text)
-> InterpreterFor (DataLog (LogEntry LogMessage)) (GhcTime : r)
forall (r :: EffectRow) a.
Member (Embed IO) r =>
(a -> Text) -> InterpreterFor (DataLog a) r
interpretDataLogStderrWith LogEntry LogMessage -> Text
formatLogEntry (Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
 -> Sem (GhcTime : r) a)
-> (Sem (Log : r) a
    -> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> Sem (Log : r) a
-> Sem (GhcTime : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Maybe Severity
-> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
-> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
forall (r :: EffectRow) a.
Member (DataLog (LogEntry LogMessage)) r =>
Maybe Severity -> Sem r a -> Sem r a
setLogLevel Maybe Severity
level (Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
 -> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> (Sem (Log : r) a
    -> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> Sem (Log : r) a
-> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Int
-> InterpreterFor Log (DataLog (LogEntry LogMessage) : GhcTime : r)
forall (r :: EffectRow).
Members
  '[DataLog (LogEntry LogMessage), Resource, Async, Race, Embed IO]
  r =>
Int -> InterpreterFor Log r
interpretLogDataLogConc Int
64 (Sem (Log : DataLog (LogEntry LogMessage) : GhcTime : r) a
 -> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> (Sem (Log : r) a
    -> Sem (Log : DataLog (LogEntry LogMessage) : GhcTime : r) a)
-> Sem (Log : r) a
-> Sem (DataLog (LogEntry LogMessage) : GhcTime : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Log : r) a
-> Sem (Log : DataLog (LogEntry LogMessage) : GhcTime : r) a
forall (e2 :: Effect) (e3 :: Effect) (e1 :: Effect)
       (r :: EffectRow) a.
Sem (e1 : r) a -> Sem (e1 : e2 : e3 : r) a
raiseUnder2
{-# inline interpretLogStderrLevelConc #-}