{-# LANGUAGE UndecidableInstances #-}

-- | generic, thread safe logging
module Control.Carrier.Logging.Generic (GenericLoggingC (..), runGenericLogging, runStdoutLogging, runColourLogging) where

import Colourista.Pure (cyan, formatWith, green, red, white, yellow)
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 (LogLevel (..), ToLogStr (toLogStr), defaultLogStr, defaultOutput, fromLogStr)
import Data.Kind (Type)
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.IO qualified as TIO
import System.IO (BufferMode (LineBuffering), stdout)
import UnliftIO (MonadUnliftIO, bracket, cancel, hSetBuffering, withAsync)
import UnliftIO.STM (TChan, atomically, newTChanIO, readTChan, tryReadTChan, writeTChan)

type GenericLoggingC :: (Type -> Type) -> Type -> Type
newtype GenericLoggingC m a = MkGenericLoggingC {forall (m :: * -> *) a. GenericLoggingC m a -> TChan LogMsg -> m a
runGenericLoggingC :: TChan LogMsg -> m a}
  deriving
    ((forall a b.
 (a -> b) -> GenericLoggingC m a -> GenericLoggingC m b)
-> (forall a b. a -> GenericLoggingC m b -> GenericLoggingC m a)
-> Functor (GenericLoggingC m)
forall a b. a -> GenericLoggingC m b -> GenericLoggingC m a
forall a b. (a -> b) -> GenericLoggingC m a -> GenericLoggingC m b
forall (m :: * -> *) a b.
Functor m =>
a -> GenericLoggingC m b -> GenericLoggingC m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> GenericLoggingC m a -> GenericLoggingC 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) -> GenericLoggingC m a -> GenericLoggingC m b
fmap :: forall a b. (a -> b) -> GenericLoggingC m a -> GenericLoggingC m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> GenericLoggingC m b -> GenericLoggingC m a
<$ :: forall a b. a -> GenericLoggingC m b -> GenericLoggingC m a
Functor, Functor (GenericLoggingC m)
Functor (GenericLoggingC m)
-> (forall a. a -> GenericLoggingC m a)
-> (forall a b.
    GenericLoggingC m (a -> b)
    -> GenericLoggingC m a -> GenericLoggingC m b)
-> (forall a b c.
    (a -> b -> c)
    -> GenericLoggingC m a
    -> GenericLoggingC m b
    -> GenericLoggingC m c)
-> (forall a b.
    GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b)
-> (forall a b.
    GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m a)
-> Applicative (GenericLoggingC m)
forall a. a -> GenericLoggingC m a
forall a b.
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m a
forall a b.
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
forall a b.
GenericLoggingC m (a -> b)
-> GenericLoggingC m a -> GenericLoggingC m b
forall a b c.
(a -> b -> c)
-> GenericLoggingC m a
-> GenericLoggingC m b
-> GenericLoggingC 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 (GenericLoggingC m)
forall (m :: * -> *) a. Applicative m => a -> GenericLoggingC m a
forall (m :: * -> *) a b.
Applicative m =>
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m a
forall (m :: * -> *) a b.
Applicative m =>
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
forall (m :: * -> *) a b.
Applicative m =>
GenericLoggingC m (a -> b)
-> GenericLoggingC m a -> GenericLoggingC m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> GenericLoggingC m a
-> GenericLoggingC m b
-> GenericLoggingC m c
$cpure :: forall (m :: * -> *) a. Applicative m => a -> GenericLoggingC m a
pure :: forall a. a -> GenericLoggingC m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
GenericLoggingC m (a -> b)
-> GenericLoggingC m a -> GenericLoggingC m b
<*> :: forall a b.
GenericLoggingC m (a -> b)
-> GenericLoggingC m a -> GenericLoggingC m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> GenericLoggingC m a
-> GenericLoggingC m b
-> GenericLoggingC m c
liftA2 :: forall a b c.
(a -> b -> c)
-> GenericLoggingC m a
-> GenericLoggingC m b
-> GenericLoggingC m c
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
*> :: forall a b.
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m a
<* :: forall a b.
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m a
Applicative, Applicative (GenericLoggingC m)
Applicative (GenericLoggingC m)
-> (forall a b.
    GenericLoggingC m a
    -> (a -> GenericLoggingC m b) -> GenericLoggingC m b)
-> (forall a b.
    GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b)
-> (forall a. a -> GenericLoggingC m a)
-> Monad (GenericLoggingC m)
forall a. a -> GenericLoggingC m a
forall a b.
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
forall a b.
GenericLoggingC m a
-> (a -> GenericLoggingC m b) -> GenericLoggingC m b
forall {m :: * -> *}. Monad m => Applicative (GenericLoggingC m)
forall (m :: * -> *) a. Monad m => a -> GenericLoggingC m a
forall (m :: * -> *) a b.
Monad m =>
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
forall (m :: * -> *) a b.
Monad m =>
GenericLoggingC m a
-> (a -> GenericLoggingC m b) -> GenericLoggingC 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 =>
GenericLoggingC m a
-> (a -> GenericLoggingC m b) -> GenericLoggingC m b
>>= :: forall a b.
GenericLoggingC m a
-> (a -> GenericLoggingC m b) -> GenericLoggingC m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
>> :: forall a b.
GenericLoggingC m a -> GenericLoggingC m b -> GenericLoggingC m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> GenericLoggingC m a
return :: forall a. a -> GenericLoggingC m a
Monad, Monad (GenericLoggingC m)
Monad (GenericLoggingC m)
-> (forall a. IO a -> GenericLoggingC m a)
-> MonadIO (GenericLoggingC m)
forall a. IO a -> GenericLoggingC m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall {m :: * -> *}. MonadIO m => Monad (GenericLoggingC m)
forall (m :: * -> *) a. MonadIO m => IO a -> GenericLoggingC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> GenericLoggingC m a
liftIO :: forall a. IO a -> GenericLoggingC m a
MonadIO, Monad (GenericLoggingC m)
Monad (GenericLoggingC m)
-> (forall a. String -> GenericLoggingC m a)
-> MonadFail (GenericLoggingC m)
forall a. String -> GenericLoggingC m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
forall {m :: * -> *}. MonadFail m => Monad (GenericLoggingC m)
forall (m :: * -> *) a.
MonadFail m =>
String -> GenericLoggingC m a
$cfail :: forall (m :: * -> *) a.
MonadFail m =>
String -> GenericLoggingC m a
fail :: forall a. String -> GenericLoggingC m a
MonadFail, MonadIO (GenericLoggingC m)
MonadIO (GenericLoggingC m)
-> (forall b.
    ((forall a. GenericLoggingC m a -> IO a) -> IO b)
    -> GenericLoggingC m b)
-> MonadUnliftIO (GenericLoggingC m)
forall b.
((forall a. GenericLoggingC m a -> IO a) -> IO b)
-> GenericLoggingC 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 (GenericLoggingC m)
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. GenericLoggingC m a -> IO a) -> IO b)
-> GenericLoggingC m b
$cwithRunInIO :: forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. GenericLoggingC m a -> IO a) -> IO b)
-> GenericLoggingC m b
withRunInIO :: forall b.
((forall a. GenericLoggingC m a -> IO a) -> IO b)
-> GenericLoggingC m b
MonadUnliftIO)
    via ReaderC (TChan LogMsg) m

{-# INLINE runGenericLogging #-}
runGenericLogging
  :: MonadUnliftIO m
  => (LogMsg -> m ())
  -- ^ what to do with a log message
  -> GenericLoggingC m a
  -- ^ the action
  -> m a
runGenericLogging :: forall (m :: * -> *) a.
MonadUnliftIO m =>
(LogMsg -> m ()) -> GenericLoggingC m a -> m a
runGenericLogging LogMsg -> m ()
fn GenericLoggingC m a
act =
  m (TChan LogMsg)
-> (TChan LogMsg -> m ()) -> (TChan LogMsg -> m a) -> m a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket
    m (TChan LogMsg)
forall (m :: * -> *) a. MonadIO m => m (TChan a)
newTChanIO
    TChan LogMsg -> m ()
flushChan
    \TChan LogMsg
chan -> m Any -> (Async Any -> m a) -> m a
forall (m :: * -> *) a b.
MonadUnliftIO m =>
m a -> (Async a -> m b) -> m b
withAsync (TChan LogMsg -> m Any
readFromChan TChan LogMsg
chan) \Async Any
async -> do
      a
res <- GenericLoggingC m a -> TChan LogMsg -> m a
forall (m :: * -> *) a. GenericLoggingC m a -> TChan LogMsg -> m a
runGenericLoggingC GenericLoggingC m a
act TChan LogMsg
chan
      Async Any -> m ()
forall (m :: * -> *) a. MonadIO m => Async a -> m ()
cancel Async Any
async
      a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
res
 where
  readFromChan :: TChan LogMsg -> m Any
readFromChan TChan LogMsg
chan = m Any
go where go :: m Any
go = do STM LogMsg -> m LogMsg
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (TChan LogMsg -> STM LogMsg
forall a. TChan a -> STM a
readTChan TChan LogMsg
chan) m LogMsg -> (LogMsg -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LogMsg -> m ()
fn; m Any
go
  flushChan :: TChan LogMsg -> m ()
flushChan TChan LogMsg
chan = m ()
go where go :: m ()
go = STM (Maybe LogMsg) -> m (Maybe LogMsg)
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (TChan LogMsg -> STM (Maybe LogMsg)
forall a. TChan a -> STM (Maybe a)
tryReadTChan TChan LogMsg
chan) m (Maybe LogMsg) -> (Maybe LogMsg -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m () -> (LogMsg -> m ()) -> Maybe LogMsg -> m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) \LogMsg
el -> do LogMsg -> m ()
fn LogMsg
el; m ()
go

instance (Algebra sig m, MonadIO m) => Algebra (Logging :+: sig) (GenericLoggingC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (GenericLoggingC m)
-> (:+:) Logging sig n a -> ctx () -> GenericLoggingC m (ctx a)
alg Handler ctx n (GenericLoggingC m)
hdl (:+:) Logging sig n a
sig ctx ()
ctx = case (:+:) Logging sig n a
sig of
    L (LoggerLog LogMsg
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) -> GenericLoggingC m a -> GenericLoggingC m (ctx a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TChan LogMsg -> m a) -> GenericLoggingC m a
forall (m :: * -> *) a.
(TChan LogMsg -> m a) -> GenericLoggingC m a
MkGenericLoggingC (STM a -> m a
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM a -> m a) -> (TChan LogMsg -> STM a) -> TChan LogMsg -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TChan LogMsg -> LogMsg -> STM a)
-> LogMsg -> TChan LogMsg -> STM a
forall a b c. (a -> b -> c) -> b -> a -> c
flip TChan LogMsg -> LogMsg -> STM a
TChan LogMsg -> LogMsg -> STM ()
forall a. TChan a -> a -> STM ()
writeTChan LogMsg
msg)
    R sig n a
other -> (TChan LogMsg -> m (ctx a)) -> GenericLoggingC m (ctx a)
forall (m :: * -> *) a.
(TChan LogMsg -> m a) -> GenericLoggingC m a
MkGenericLoggingC \TChan LogMsg
chan -> 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 ((GenericLoggingC m (ctx x) -> TChan LogMsg -> m (ctx x))
-> TChan LogMsg -> GenericLoggingC m (ctx x) -> m (ctx x)
forall a b c. (a -> b -> c) -> b -> a -> c
flip GenericLoggingC m (ctx x) -> TChan LogMsg -> m (ctx x)
forall (m :: * -> *) a. GenericLoggingC m a -> TChan LogMsg -> m a
runGenericLoggingC TChan LogMsg
chan (GenericLoggingC m (ctx x) -> m (ctx x))
-> (ctx (n x) -> GenericLoggingC m (ctx x))
-> ctx (n x)
-> m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> GenericLoggingC m (ctx x)
Handler ctx n (GenericLoggingC m)
hdl) sig n a
other ctx ()
ctx

-- | log to stdout, supports concurrency
runStdoutLogging :: MonadUnliftIO m => GenericLoggingC m a -> m a
runStdoutLogging :: forall (m :: * -> *) a.
MonadUnliftIO m =>
GenericLoggingC m a -> m a
runStdoutLogging GenericLoggingC m a
act = do
  Handle -> BufferMode -> m ()
forall (m :: * -> *). MonadIO m => Handle -> BufferMode -> m ()
hSetBuffering Handle
stdout BufferMode
LineBuffering
  (LogMsg -> m ()) -> GenericLoggingC m a -> m a
forall (m :: * -> *) a.
MonadUnliftIO m =>
(LogMsg -> m ()) -> GenericLoggingC m a -> m a
runGenericLogging
    LogMsg -> m ()
forall {m :: * -> *}. MonadIO m => LogMsg -> m ()
logStdout
    GenericLoggingC m a
act
 where
  {-# INLINE logStdout #-}
  logStdout :: LogMsg -> m ()
logStdout (MkLogMsg Loc
loc LogLevel
lvl msg
msg) = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
    Handle -> Loc -> LogSource -> LogLevel -> LogStr -> IO ()
defaultOutput Handle
stdout Loc
loc (String -> LogSource
T.pack String
"") LogLevel
lvl (msg -> LogStr
forall msg. ToLogStr msg => msg -> LogStr
toLogStr msg
msg)
{-# INLINE runStdoutLogging #-}

-- | logs to stdout, supports concurrency, logs are printed in colours according to the severity
runColourLogging :: MonadUnliftIO m => GenericLoggingC m a -> m a
runColourLogging :: forall (m :: * -> *) a.
MonadUnliftIO m =>
GenericLoggingC m a -> m a
runColourLogging GenericLoggingC m a
act = do
  Handle -> BufferMode -> m ()
forall (m :: * -> *). MonadIO m => Handle -> BufferMode -> m ()
hSetBuffering Handle
stdout BufferMode
LineBuffering
  (LogMsg -> m ()) -> GenericLoggingC m a -> m a
forall (m :: * -> *) a.
MonadUnliftIO m =>
(LogMsg -> m ()) -> GenericLoggingC m a -> m a
runGenericLogging
    LogMsg -> m ()
forall {m :: * -> *}. MonadIO m => LogMsg -> m ()
logColourful
    GenericLoggingC m a
act
 where
  {-# INLINE logColourful #-}
  logColourful :: LogMsg -> m ()
logColourful (MkLogMsg Loc
loc LogLevel
lvl msg
msg) = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO do
    let tm :: LogSource
tm = ByteString -> LogSource
T.decodeUtf8 (ByteString -> LogSource) -> ByteString -> LogSource
forall a b. (a -> b) -> a -> b
$ LogStr -> ByteString
fromLogStr (LogStr -> ByteString) -> LogStr -> ByteString
forall a b. (a -> b) -> a -> b
$ Loc -> LogSource -> LogLevel -> LogStr -> LogStr
defaultLogStr Loc
loc (String -> LogSource
T.pack String
"") LogLevel
lvl (msg -> LogStr
forall msg. ToLogStr msg => msg -> LogStr
toLogStr msg
msg)
        fmt :: LogSource
fmt = case LogLevel
lvl of
          LogLevel
LevelDebug -> LogSource
forall str. IsString str => str
cyan
          LogLevel
LevelWarn -> LogSource
forall str. IsString str => str
yellow
          LogLevel
LevelInfo -> LogSource
forall str. IsString str => str
green
          LogLevel
LevelError -> LogSource
forall str. IsString str => str
red
          LevelOther LogSource
_ -> LogSource
forall str. IsString str => str
white
    LogSource -> IO ()
TIO.putStr (LogSource -> IO ()) -> LogSource -> IO ()
forall a b. (a -> b) -> a -> b
$ [LogSource] -> LogSource -> LogSource
forall str. (IsString str, Semigroup str) => [str] -> str -> str
formatWith [LogSource
fmt] LogSource
tm
{-# INLINE runColourLogging #-}