{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Colog.Polysemy.Formatting
(
WithLog
, WithLog'
, logDebug
, logInfo
, logWarning
, logError
, logException
, newLogEnv
, ignoreLog
, filterLogs
, setLogLevel
, addThreadAndTimeToLog
, renderThreadTimeMessage
, HasCallStack
, runLogAction
, logTextStdout
, logTextStderr
, logTextHandle
, cmap
, Severity(..)
, Msg(..)
) where
import Prelude hiding (log)
import Colog (Msg(..), Severity(..), cmap)
import Colog.Actions (logTextHandle, logTextStderr, logTextStdout)
import Colog.Polysemy (Log(..), runLogAction)
import qualified Colog.Polysemy as Colog
import Control.Category ((>>>))
import Control.Exception (Exception, displayException)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TLB
import Formatting
import GHC.Stack (HasCallStack, callStack, withFrozenCallStack)
import Polysemy
import Colog.Polysemy.Formatting.LogEnv (newLogEnv)
import Colog.Polysemy.Formatting.Render (renderThreadTimeMessage)
import Colog.Polysemy.Formatting.ThreadTimeMessage (addThreadAndTimeToLog, HasSeverity(..))
import Colog.Polysemy.Formatting.WithLog (WithLog, WithLog')
log :: WithLog r => Severity -> Format (Sem r ()) a -> a
log :: Severity -> Format (Sem r ()) a -> a
log Severity
sev Format (Sem r ()) a
m = (HasCallStack => a) -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => a) -> a) -> (HasCallStack => a) -> a
forall a b. (a -> b) -> a -> b
$
Format (Sem r ()) a -> (Builder -> Sem r ()) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format (Sem r ()) a
m
((Builder -> Sem r ()) -> a) -> (Builder -> Sem r ()) -> a
forall a b. (a -> b) -> a -> b
$ Msg Severity -> Sem r ()
forall msg (r :: [(* -> *) -> * -> *]).
Member (Log msg) r =>
msg -> Sem r ()
Colog.log
(Msg Severity -> Sem r ())
-> (Builder -> Msg Severity) -> Builder -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Severity -> CallStack -> Text -> Msg Severity
forall sev. sev -> CallStack -> Text -> Msg sev
Msg Severity
sev CallStack
HasCallStack => CallStack
callStack
(Text -> Msg Severity)
-> (Builder -> Text) -> Builder -> Msg Severity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.toStrict
(Text -> Text) -> (Builder -> Text) -> Builder -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText
logDebug :: WithLog r => Format (Sem r ()) a -> a
logDebug :: Format (Sem r ()) a -> a
logDebug = (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a)
-> (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a
-> a
forall a b. (a -> b) -> a -> b
$ Severity -> Format (Sem r ()) a -> a
forall (r :: [(* -> *) -> * -> *]) a.
WithLog r =>
Severity -> Format (Sem r ()) a -> a
log Severity
Debug
logInfo :: WithLog r => Format (Sem r ()) a -> a
logInfo :: Format (Sem r ()) a -> a
logInfo = (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a)
-> (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a
-> a
forall a b. (a -> b) -> a -> b
$ Severity -> Format (Sem r ()) a -> a
forall (r :: [(* -> *) -> * -> *]) a.
WithLog r =>
Severity -> Format (Sem r ()) a -> a
log Severity
Info
logWarning :: WithLog r => Format (Sem r ()) a -> a
logWarning :: Format (Sem r ()) a -> a
logWarning = (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a)
-> (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a
-> a
forall a b. (a -> b) -> a -> b
$ Severity -> Format (Sem r ()) a -> a
forall (r :: [(* -> *) -> * -> *]) a.
WithLog r =>
Severity -> Format (Sem r ()) a -> a
log Severity
Warning
logError :: WithLog r => Format (Sem r ()) a -> a
logError :: Format (Sem r ()) a -> a
logError = (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a -> a)
-> (HasCallStack => Format (Sem r ()) a -> a)
-> Format (Sem r ()) a
-> a
forall a b. (a -> b) -> a -> b
$ Severity -> Format (Sem r ()) a -> a
forall (r :: [(* -> *) -> * -> *]) a.
WithLog r =>
Severity -> Format (Sem r ()) a -> a
log Severity
Error
logException :: (WithLog r, Exception e) => e -> Sem r ()
logException :: e -> Sem r ()
logException = Sem r () -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (Sem r () -> Sem r ()) -> (e -> Sem r ()) -> e -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Format (Sem r ()) (String -> Sem r ()) -> String -> Sem r ()
forall (r :: [(* -> *) -> * -> *]) a.
WithLog r =>
Format (Sem r ()) a -> a
logError Format (Sem r ()) (String -> Sem r ())
forall r. Format r (String -> r)
string (String -> Sem r ()) -> (e -> String) -> e -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> String
forall e. Exception e => e -> String
displayException
ignoreLog :: Sem (Log msg ': r) a -> Sem r a
ignoreLog :: Sem (Log msg : r) a -> Sem r a
ignoreLog = (forall x (rInitial :: [(* -> *) -> * -> *]).
Log msg (Sem rInitial) x -> Sem r x)
-> Sem (Log msg : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: [(* -> *) -> * -> *]).
e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall x (rInitial :: [(* -> *) -> * -> *]).
Log msg (Sem rInitial) x -> Sem r x)
-> Sem (Log msg : r) a -> Sem r a)
-> (forall x (rInitial :: [(* -> *) -> * -> *]).
Log msg (Sem rInitial) x -> Sem r x)
-> Sem (Log msg : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
Log _ -> () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
filterLogs
:: Member (Log msg) r
=> (msg -> Bool)
-> Sem (Log msg ': r) a
-> Sem r a
filterLogs :: (msg -> Bool) -> Sem (Log msg : r) a -> Sem r a
filterLogs msg -> Bool
f = (forall x (rInitial :: [(* -> *) -> * -> *]).
Log msg (Sem rInitial) x -> Sem r x)
-> Sem (Log msg : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: [(* -> *) -> * -> *]).
e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall x (rInitial :: [(* -> *) -> * -> *]).
Log msg (Sem rInitial) x -> Sem r x)
-> Sem (Log msg : r) a -> Sem r a)
-> (forall x (rInitial :: [(* -> *) -> * -> *]).
Log msg (Sem rInitial) x -> Sem r x)
-> Sem (Log msg : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
Log msg -> if msg -> Bool
f msg
msg
then msg -> Sem r ()
forall msg (r :: [(* -> *) -> * -> *]).
Member (Log msg) r =>
msg -> Sem r ()
Colog.log msg
msg
else () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
setLogLevel
:: ( HasSeverity msg
, Member (Log msg) r
)
=> Severity
-> Sem (Log msg ': r) a
-> Sem r a
setLogLevel :: Severity -> Sem (Log msg : r) a -> Sem r a
setLogLevel Severity
level = (msg -> Bool) -> Sem (Log msg : r) a -> Sem r a
forall msg (r :: [(* -> *) -> * -> *]) a.
Member (Log msg) r =>
(msg -> Bool) -> Sem (Log msg : r) a -> Sem r a
filterLogs (msg -> Severity
forall msg. HasSeverity msg => msg -> Severity
getSeverity (msg -> Severity) -> (Severity -> Bool) -> msg -> Bool
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Severity -> Severity -> Bool
forall a. Ord a => a -> a -> Bool
(<=) Severity
level)