{-# LANGUAGE UndecidableInstances #-}

-- | simple logging to a mutable cell for testing purposes
module Control.Carrier.Logging.Ref where

import Control.Algebra (Algebra (alg), (:+:) (L, R))
import Control.Carrier.Reader (ReaderC (ReaderC))
import Control.Effect.Logging (LogMsg (MkLogMsg), Logging (LoggerLog))
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Logger (ToLogStr (toLogStr))
import Data.IORef (IORef, atomicModifyIORef')
import Data.Kind (Type)
import Data.Sequence (Seq ((:|>)))
import UnliftIO (MonadUnliftIO)

type RefLoggingC :: (Type -> Type) -> Type -> Type
newtype RefLoggingC m a = RefLoggingC {forall (m :: * -> *) a.
RefLoggingC m a -> IORef (Seq LogMsg) -> m a
runRefLoggingC :: IORef (Seq LogMsg) -> m a}
  deriving
    ((forall a b. (a -> b) -> RefLoggingC m a -> RefLoggingC m b)
-> (forall a b. a -> RefLoggingC m b -> RefLoggingC m a)
-> Functor (RefLoggingC m)
forall a b. a -> RefLoggingC m b -> RefLoggingC m a
forall a b. (a -> b) -> RefLoggingC m a -> RefLoggingC m b
forall (m :: * -> *) a b.
Functor m =>
a -> RefLoggingC m b -> RefLoggingC m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> RefLoggingC m a -> RefLoggingC m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> RefLoggingC m a -> RefLoggingC m b
fmap :: forall a b. (a -> b) -> RefLoggingC m a -> RefLoggingC m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> RefLoggingC m b -> RefLoggingC m a
<$ :: forall a b. a -> RefLoggingC m b -> RefLoggingC m a
Functor, Functor (RefLoggingC m)
Functor (RefLoggingC m)
-> (forall a. a -> RefLoggingC m a)
-> (forall a b.
    RefLoggingC m (a -> b) -> RefLoggingC m a -> RefLoggingC m b)
-> (forall a b c.
    (a -> b -> c)
    -> RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m c)
-> (forall a b.
    RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b)
-> (forall a b.
    RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m a)
-> Applicative (RefLoggingC m)
forall a. a -> RefLoggingC m a
forall a b. RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m a
forall a b. RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
forall a b.
RefLoggingC m (a -> b) -> RefLoggingC m a -> RefLoggingC m b
forall a b c.
(a -> b -> c)
-> RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall {m :: * -> *}. Applicative m => Functor (RefLoggingC m)
forall (m :: * -> *) a. Applicative m => a -> RefLoggingC m a
forall (m :: * -> *) a b.
Applicative m =>
RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m a
forall (m :: * -> *) a b.
Applicative m =>
RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
forall (m :: * -> *) a b.
Applicative m =>
RefLoggingC m (a -> b) -> RefLoggingC m a -> RefLoggingC m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m c
$cpure :: forall (m :: * -> *) a. Applicative m => a -> RefLoggingC m a
pure :: forall a. a -> RefLoggingC m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
RefLoggingC m (a -> b) -> RefLoggingC m a -> RefLoggingC m b
<*> :: forall a b.
RefLoggingC m (a -> b) -> RefLoggingC m a -> RefLoggingC m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m c
liftA2 :: forall a b c.
(a -> b -> c)
-> RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m c
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
*> :: forall a b. RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m a
<* :: forall a b. RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m a
Applicative, Applicative (RefLoggingC m)
Applicative (RefLoggingC m)
-> (forall a b.
    RefLoggingC m a -> (a -> RefLoggingC m b) -> RefLoggingC m b)
-> (forall a b.
    RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b)
-> (forall a. a -> RefLoggingC m a)
-> Monad (RefLoggingC m)
forall a. a -> RefLoggingC m a
forall a b. RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
forall a b.
RefLoggingC m a -> (a -> RefLoggingC m b) -> RefLoggingC m b
forall {m :: * -> *}. Monad m => Applicative (RefLoggingC m)
forall (m :: * -> *) a. Monad m => a -> RefLoggingC m a
forall (m :: * -> *) a b.
Monad m =>
RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
forall (m :: * -> *) a b.
Monad m =>
RefLoggingC m a -> (a -> RefLoggingC m b) -> RefLoggingC m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
RefLoggingC m a -> (a -> RefLoggingC m b) -> RefLoggingC m b
>>= :: forall a b.
RefLoggingC m a -> (a -> RefLoggingC m b) -> RefLoggingC m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
>> :: forall a b. RefLoggingC m a -> RefLoggingC m b -> RefLoggingC m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> RefLoggingC m a
return :: forall a. a -> RefLoggingC m a
Monad, Monad (RefLoggingC m)
Monad (RefLoggingC m)
-> (forall a. IO a -> RefLoggingC m a) -> MonadIO (RefLoggingC m)
forall a. IO a -> RefLoggingC m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall {m :: * -> *}. MonadIO m => Monad (RefLoggingC m)
forall (m :: * -> *) a. MonadIO m => IO a -> RefLoggingC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> RefLoggingC m a
liftIO :: forall a. IO a -> RefLoggingC m a
MonadIO, Monad (RefLoggingC m)
Monad (RefLoggingC m)
-> (forall a. String -> RefLoggingC m a)
-> MonadFail (RefLoggingC m)
forall a. String -> RefLoggingC m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
forall {m :: * -> *}. MonadFail m => Monad (RefLoggingC m)
forall (m :: * -> *) a. MonadFail m => String -> RefLoggingC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> RefLoggingC m a
fail :: forall a. String -> RefLoggingC m a
MonadFail, MonadIO (RefLoggingC m)
MonadIO (RefLoggingC m)
-> (forall b.
    ((forall a. RefLoggingC m a -> IO a) -> IO b) -> RefLoggingC m b)
-> MonadUnliftIO (RefLoggingC m)
forall b.
((forall a. RefLoggingC m a -> IO a) -> IO b) -> RefLoggingC m b
forall (m :: * -> *).
MonadIO m
-> (forall b. ((forall a. m a -> IO a) -> IO b) -> m b)
-> MonadUnliftIO m
forall {m :: * -> *}. MonadUnliftIO m => MonadIO (RefLoggingC m)
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. RefLoggingC m a -> IO a) -> IO b) -> RefLoggingC m b
$cwithRunInIO :: forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. RefLoggingC m a -> IO a) -> IO b) -> RefLoggingC m b
withRunInIO :: forall b.
((forall a. RefLoggingC m a -> IO a) -> IO b) -> RefLoggingC m b
MonadUnliftIO)
    via ReaderC (IORef (Seq LogMsg)) m

-- | pass a cell that is supposed to be logged to
runRefLogging :: IORef (Seq LogMsg) -> RefLoggingC m a -> m a
runRefLogging :: forall (m :: * -> *) a.
IORef (Seq LogMsg) -> RefLoggingC m a -> m a
runRefLogging = (RefLoggingC m a -> IORef (Seq LogMsg) -> m a)
-> IORef (Seq LogMsg) -> RefLoggingC m a -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip RefLoggingC m a -> IORef (Seq LogMsg) -> m a
forall (m :: * -> *) a.
RefLoggingC m a -> IORef (Seq LogMsg) -> m a
runRefLoggingC
{-# INLINE runRefLogging #-}

instance (Algebra sig m, MonadIO m) => Algebra (Logging :+: sig) (RefLoggingC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (RefLoggingC m)
-> (:+:) Logging sig n a -> ctx () -> RefLoggingC m (ctx a)
alg Handler ctx n (RefLoggingC m)
hdl (:+:) Logging sig n a
sig ctx ()
ctx = case (:+:) Logging sig n a
sig of
    L (LoggerLog (MkLogMsg Loc
loc LogLevel
lvl msg
msg)) ->
      (a -> ctx () -> ctx a
forall a b. a -> ctx b -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx) (a -> ctx a) -> RefLoggingC m a -> RefLoggingC m (ctx a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (IORef (Seq LogMsg) -> m a) -> RefLoggingC m a
forall (m :: * -> *) a.
(IORef (Seq LogMsg) -> m a) -> RefLoggingC m a
RefLoggingC \IORef (Seq LogMsg)
ref -> do
        IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
          IORef (Seq LogMsg) -> (Seq LogMsg -> (Seq LogMsg, a)) -> IO a
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef (Seq LogMsg)
ref (\Seq LogMsg
t -> (Seq LogMsg
t Seq LogMsg -> LogMsg -> Seq LogMsg
forall a. Seq a -> a -> Seq a
:|> Loc -> LogLevel -> LogStr -> LogMsg
forall msg. ToLogStr msg => Loc -> LogLevel -> msg -> LogMsg
MkLogMsg Loc
loc LogLevel
lvl (msg -> LogStr
forall msg. ToLogStr msg => msg -> LogStr
toLogStr msg
msg), ()))
    R sig n a
other -> (IORef (Seq LogMsg) -> m (ctx a)) -> RefLoggingC m (ctx a)
forall (m :: * -> *) a.
(IORef (Seq LogMsg) -> m a) -> RefLoggingC m a
RefLoggingC \IORef (Seq LogMsg)
ref -> do
      Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
       (n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (IORef (Seq LogMsg) -> RefLoggingC m (ctx x) -> m (ctx x)
forall (m :: * -> *) a.
IORef (Seq LogMsg) -> RefLoggingC m a -> m a
runRefLogging IORef (Seq LogMsg)
ref (RefLoggingC m (ctx x) -> m (ctx x))
-> (ctx (n x) -> RefLoggingC m (ctx x)) -> ctx (n x) -> m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> RefLoggingC m (ctx x)
Handler ctx n (RefLoggingC m)
hdl) sig n a
other ctx ()
ctx