11{-# LANGUAGE GHC2021 #-}
22
3- module Benign.Katip where
3+ -- | This module is a small wrapper around
4+ -- [Katip](https://hackage.haskell.org/package/katip) to allow logging in pure
5+ -- code.
6+ --
7+ -- The wrapper is a little primitive still and you will have to handle the
8+ -- transition from IO code to pure code manually. Pull requests are, of course,
9+ -- welcome.
10+ module Benign.Katip
11+ ( withKatipContext ,
12+ withKatipNamespace ,
13+ withKatip ,
14+ logLocM ,
15+ )
16+ where
417
518import Benign qualified
619import GHC.Stack
@@ -22,19 +35,13 @@ katipNamespace = unsafePerformIO Benign.newField
2235
2336-- | See 'Katip.katipAddContext'.
2437withKatipContext :: (Katip. LogItem i ) => i -> Benign. Strat a -> a -> a
25- withKatipContext item = Benign. withAltering katipContext addContext
26- where
27- addContext (Just st) = Just $ st <> Katip. liftPayload item
28- addContext Nothing = error " todo"
38+ withKatipContext item = Benign. withAltering katipContext (<> Just (Katip. liftPayload item))
2939
3040-- | See 'Katip.katipAddNamespace'.
3141withKatipNamespace :: Katip. Namespace -> Benign. Strat a -> a -> a
32- withKatipNamespace namespace = Benign. withAltering katipNamespace addNamespace
33- where
34- addNamespace (Just st) = Just $ st <> namespace
35- addNamespace Nothing = error " todo"
42+ withKatipNamespace namespace = Benign. withAltering katipNamespace (<> Just namespace)
3643
37- -- | Within this computation, Katip is configured.
44+ -- | Within this computation, Katip is configured for pure code .
3845withKatip ::
3946 (Katip. LogItem c ) =>
4047 Katip. LogEnv ->
@@ -48,7 +55,9 @@ withKatip env ctx namespace strat =
4855 . Benign. withSettingIO' katipContext (Katip. liftPayload ctx)
4956 . Benign. withSettingIO katipNamespace namespace strat
5057
51- logLocM :: forall a . (HasCallStack ) => Katip. Severity -> Katip. LogStr -> Benign. Strat a -> a -> a
58+ -- | @'logLocM' s msg a@ logs a an event, like Katip's 'Katip.logLocM', before
59+ -- evaluating @a@.
60+ logLocM :: forall a . (HasCallStack ) => Katip. Severity -> Katip. LogStr -> a -> a
5261logLocM severity str = withFrozenCallStack spanLog
5362 where
5463 -- The whole purpose of naming `span` is to freeze the call stack. It's
@@ -59,16 +68,16 @@ logLocM severity str = withFrozenCallStack spanLog
5968 -- scratch. This would be invisible. I tried to harden this function by
6069 -- declaring type signatures everywhere. I haven't tested it yet though. It
6170 -- may be wrong.
62- spanLog :: HasCallStack => Benign. Strat a - > a -> a
63- spanLog = Benign. unsafeSpanBenign doLog (return () )
71+ spanLog :: ( HasCallStack ) = > a -> a
72+ spanLog = Benign. unsafeSpanBenign doLog (return () ) Benign. whnf
6473
65- doLog :: HasCallStack => IO ()
74+ doLog :: ( HasCallStack ) => IO ()
6675 doLog = do
6776 -- Making an intermediary `KatipContextT` is a little roundabout, but it's
6877 -- easier than reaching to Katip's internals.
6978 --
7079 -- TODO: catch errors
71- Just env <- Benign. lookupLocalState katipEnv
72- Just ctx <- Benign. lookupLocalState katipContext
73- Just namespace <- Benign. lookupLocalState katipNamespace
80+ Just env <- Benign. lookupLexicalState katipEnv
81+ Just ctx <- Benign. lookupLexicalState katipContext
82+ Just namespace <- Benign. lookupLexicalState katipNamespace
7483 Katip. runKatipContextT env ctx namespace $ Katip. logLocM severity str
0 commit comments