on master help sign in
Added NoLoggingT
commitfe9012d41e76a10bacb395ffbd99bd90fc71f212 authorSimon Hafner <hafnersimon@gmail.com> dateWed 28 Feb 2018, 21:44 (9 years ago) parent627bf2b
4 files changed+64
katip/katip.cabal+1 katip/src/Katip/Core.hs+5 katip/src/Katip/Monadic.hs+57 stack.yaml+1
katip/katip.cabal+1
@@ -78,6 +78,7 @@ library
7878 , microlens >= 0.2.0.0 && < 0.5
7979 , microlens-th >= 0.1.0.0 && < 0.5
8080 , semigroups
81+ , unliftio-core >= 0.1 && < 0.2
8182 , stm >= 2.4
8283
8384 hs-source-dirs: src
katip/src/Katip/Core.hs+5
@@ -33,6 +33,7 @@ import Control.Exception.Safe
3333import Control.Monad (unless, void)
3434import Control.Monad.Base
3535import Control.Monad.IO.Class
36+import Control.Monad.IO.Unlift
3637import Control.Monad.Trans.Class
3738import Control.Monad.Trans.Control
3839#if !MIN_VERSION_either(4, 5, 0)
@@ -833,6 +834,10 @@ instance (MonadBaseControl b m) => MonadBaseControl b (KatipT m) where
833834 liftBaseWith = defaultLiftBaseWith
834835 restoreM = defaultRestoreM
835836
837+instance MonadUnliftIO m => MonadUnliftIO (KatipT m) where
838+ askUnliftIO = KatipT $
839+ withUnliftIO $ \u ->
840+ pure (UnliftIO (unliftIO u . unKatipT))
836841
837842-------------------------------------------------------------------------------
838843-- | Execute 'KatipT' on a log env.
katip/src/Katip/Monadic.hs+57
@@ -34,6 +34,7 @@ module Katip.Monadic
3434 , katipAddNamespace
3535 , katipAddContext
3636 , KatipContextTState(..)
37+ , NoLoggingT
3738 ) where
3839
3940
@@ -43,6 +44,7 @@ import Control.Exception.Safe
4344import Control.Monad.Base
4445import Control.Monad.Error.Class
4546import Control.Monad.IO.Class
47+import Control.Monad.IO.Unlift
4648import Control.Monad.Reader
4749import Control.Monad.State
4850import Control.Monad.Trans.Control
@@ -389,6 +391,11 @@ instance (MonadIO m) => KatipContext (KatipContextT m) where
389391 getKatipNamespace = KatipContextT $ ReaderT $ \lts -> return (ltsNamespace lts)
390392 localKatipNamespace f (KatipContextT m) = KatipContextT $ local (\s -> s { ltsNamespace = f (ltsNamespace s)}) m
391393
394+instance MonadUnliftIO m => MonadUnliftIO (KatipContextT m) where
395+ askUnliftIO = KatipContextT $
396+ withUnliftIO $ \u ->
397+ pure (UnliftIO (unliftIO u . unKatipContextT))
398+
392399
393400-------------------------------------------------------------------------------
394401runKatipContextT :: (LogItem c) => LogEnv -> c -> Namespace -> KatipContextT m a -> m a
@@ -429,3 +436,53 @@ katipAddContext
429436 -> m a
430437 -> m a
431438katipAddContext i = localKatipContext (<> (liftPayload i))
439+
440+newtype NoLoggingT m a = NoLoggingT {
441+ runNoLoggingT :: m a
442+ } deriving ( Functor
443+ , Applicative
444+ , Monad
445+ , MonadIO
446+ , MonadThrow
447+ , MonadCatch
448+ , MonadMask
449+ , MonadBase b
450+ , MonadState s
451+ , MonadWriter w
452+ , MonadError e
453+ , MonadPlus
454+ , Alternative
455+ , MonadFix
456+ )
457+
458+instance MonadTrans NoLoggingT where
459+ lift = NoLoggingT
460+
461+instance MonadTransControl NoLoggingT where
462+ type StT NoLoggingT a = a
463+ liftWith f = NoLoggingT $ f runNoLoggingT
464+ restoreT = NoLoggingT
465+ {-# INLINE liftWith #-}
466+ {-# INLINE restoreT #-}
467+
468+instance MonadBaseControl b m => MonadBaseControl b (NoLoggingT m) where
469+ type StM (NoLoggingT m) a = StM m a
470+ liftBaseWith f = NoLoggingT $
471+ liftBaseWith $ \runInBase ->
472+ f $ runInBase . runNoLoggingT
473+ restoreM = NoLoggingT . restoreM
474+
475+instance MonadUnliftIO m => MonadUnliftIO (NoLoggingT m) where
476+ askUnliftIO = NoLoggingT $
477+ withUnliftIO $ \u ->
478+ pure (UnliftIO (unliftIO u . runNoLoggingT))
479+
480+instance MonadIO m => Katip (NoLoggingT m) where
481+ getLogEnv = liftIO (initLogEnv "NoLoggingT" "no-logging")
482+ localLogEnv = const id
483+
484+instance MonadIO m => KatipContext (NoLoggingT m) where
485+ getKatipContext = pure mempty
486+ localKatipContext = const id
487+ getKatipNamespace = pure mempty
488+ localKatipNamespace = const id
stack.yaml+1
@@ -6,6 +6,7 @@ extra-deps:
66 # These can be dropped probably as soon as they hit an LTS
77 - bloodhound-0.15.0.2
88 - http-types-0.12
9+ - unliftio-core-0.1.1.0
910flags:
1011 katip:
1112 lib-Werror: true