|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE ImplicitParams #-} |
| 3 | +module RIO.Logger |
| 4 | + ( LogLevel (..) |
| 5 | + , LogSource |
| 6 | + , LogStr |
| 7 | + , HasLogFunc (..) |
| 8 | + , logGeneric |
| 9 | + , logDebug |
| 10 | + , logInfo |
| 11 | + , logWarn |
| 12 | + , logError |
| 13 | + , logOther |
| 14 | + , logSticky |
| 15 | + , logStickyDone |
| 16 | + , runNoLogging |
| 17 | + , NoLogging (..) |
| 18 | + ) where |
| 19 | + |
| 20 | +import Data.Text (Text) |
| 21 | +import Control.Monad.IO.Class (MonadIO, liftIO) |
| 22 | +import Control.Monad.Reader (MonadReader, ReaderT, runReaderT) |
| 23 | +import Lens.Micro (Getting, to) |
| 24 | +import Lens.Micro.Mtl (view) |
| 25 | +import GHC.Stack (HasCallStack, CallStack) |
| 26 | + |
| 27 | +data LogLevel = LevelDebug | LevelInfo | LevelWarn | LevelError | LevelOther !Text |
| 28 | + deriving (Eq, Show, Read, Ord) |
| 29 | + |
| 30 | +type LogSource = Text |
| 31 | +type LogStr = Text |
| 32 | +class HasLogFunc env where |
| 33 | + logFuncL :: Getting r env (CallStack -> LogSource -> LogLevel -> LogStr -> IO ()) |
| 34 | + |
| 35 | +logGeneric |
| 36 | + :: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) |
| 37 | + => LogSource |
| 38 | + -> LogLevel |
| 39 | + -> LogStr |
| 40 | + -> m () |
| 41 | +logGeneric src level str = do |
| 42 | + logFunc <- view logFuncL |
| 43 | + liftIO $ logFunc ?callStack src level str |
| 44 | + |
| 45 | +logDebug |
| 46 | + :: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) |
| 47 | + => LogStr |
| 48 | + -> m () |
| 49 | +logDebug = logGeneric "" LevelDebug |
| 50 | + |
| 51 | +logInfo |
| 52 | + :: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) |
| 53 | + => LogStr |
| 54 | + -> m () |
| 55 | +logInfo = logGeneric "" LevelInfo |
| 56 | + |
| 57 | +logWarn |
| 58 | + :: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) |
| 59 | + => LogStr |
| 60 | + -> m () |
| 61 | +logWarn = logGeneric "" LevelWarn |
| 62 | + |
| 63 | +logError |
| 64 | + :: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) |
| 65 | + => LogStr |
| 66 | + -> m () |
| 67 | +logError = logGeneric "" LevelError |
| 68 | + |
| 69 | +logOther |
| 70 | + :: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) |
| 71 | + => Text -- ^ level |
| 72 | + -> LogStr |
| 73 | + -> m () |
| 74 | +logOther = logGeneric "" . LevelOther |
| 75 | + |
| 76 | +runNoLogging :: MonadIO m => ReaderT NoLogging m a -> m a |
| 77 | +runNoLogging = flip runReaderT NoLogging |
| 78 | + |
| 79 | +data NoLogging = NoLogging |
| 80 | +instance HasLogFunc NoLogging where |
| 81 | + logFuncL = to (\_ _ _ _ _ -> return ()) |
| 82 | + |
| 83 | +-- | Write a "sticky" line to the terminal. Any subsequent lines will |
| 84 | +-- overwrite this one, and that same line will be repeated below |
| 85 | +-- again. In other words, the line sticks at the bottom of the output |
| 86 | +-- forever. Running this function again will replace the sticky line |
| 87 | +-- with a new sticky line. When you want to get rid of the sticky |
| 88 | +-- line, run 'logStickyDone'. |
| 89 | +-- |
| 90 | +logSticky :: (MonadIO m, HasCallStack, MonadReader env m, HasLogFunc env) => Text -> m () |
| 91 | +logSticky = logOther "sticky" |
| 92 | + |
| 93 | +-- | This will print out the given message with a newline and disable |
| 94 | +-- any further stickiness of the line until a new call to 'logSticky' |
| 95 | +-- happens. |
| 96 | +-- |
| 97 | +-- It might be better at some point to have a 'runSticky' function |
| 98 | +-- that encompasses the logSticky->logStickyDone pairing. |
| 99 | +logStickyDone :: (MonadIO m, HasCallStack, MonadReader env m, HasLogFunc env) => Text -> m () |
| 100 | +logStickyDone = logOther "sticky-done" |
0 commit comments