diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-08-25 02:35:34 +0000 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-08-25 02:35:34 +0000 |
commit | 6f80d2d579d0be8773829ee277086af087e85862 (patch) | |
tree | 93a52fed4d72f6076f800e7aeea1a3ca663c1e06 | |
parent | 6db2d77345b1d3da432a73d9eaf0be34165567c3 (diff) |
Improves formatting of logs
-rw-r--r-- | hsm-cam/Hsm/Cam.hs | 4 | ||||
-rw-r--r-- | hsm-log/Hsm/Log.hs | 44 | ||||
-rw-r--r-- | hsm-log/hsm-log.cabal | 1 | ||||
-rw-r--r-- | hsm-repl/Hsm/Repl.hs | 4 |
4 files changed, 36 insertions, 17 deletions
diff --git a/hsm-cam/Hsm/Cam.hs b/hsm-cam/Hsm/Cam.hs index 8300ae7..f894bc3 100644 --- a/hsm-cam/Hsm/Cam.hs +++ b/hsm-cam/Hsm/Cam.hs @@ -43,7 +43,7 @@ import Hsm.Cam.FFI , stopCamera , stopCameraManager ) -import Hsm.Log (Log, Severity(Info, Trace), getLoggerIO, logMsg) +import Hsm.Log (Log, Severity(Info, Trace), logMsg, makeLoggerIO) import MMAP (mapShared, mkMmapFlags, mmap, munmap, protRead) data Cam (a :: * -> *) (b :: *) @@ -84,7 +84,7 @@ runCam action = do where loggerAlloc = do logMsg Info "Registering FFI logger" - loggerIO <- getLoggerIO + loggerIO <- makeLoggerIO loggerFFI <- liftIO . makeLogger $ \severity message -> peekCString message >>= loggerIO (toEnum severity) liftIO $ registerLogger loggerFFI return loggerFFI diff --git a/hsm-log/Hsm/Log.hs b/hsm-log/Hsm/Log.hs index 3c25501..a1ffdb4 100644 --- a/hsm-log/Hsm/Log.hs +++ b/hsm-log/Hsm/Log.hs @@ -5,27 +5,29 @@ module Hsm.Log ( Severity(Attention, Info, Trace) , Log - , getLoggerIO + , makeLoggerIO , logMsg + , logBlock , runLog ) where import Control.Monad (when) import Data.Function (applyWhen) +import Data.List (intercalate) import Data.Proxy (Proxy(Proxy)) import Data.Time.Clock (getCurrentTime) import Data.Time.ISO8601 (formatISO8601Millis) import Effectful (Dispatch(Static), DispatchOf, Eff, Effect, IOE, (:>)) import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalStaticRep, getStaticRep, unsafeEff_) +import GHC.Conc.Sync (fromThreadId, myThreadId) import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) -import GHC.TypeLits.Printf (printf) -import String.ANSI (red) +import String.ANSI (blue, green, red, white) data Severity = Attention | Info | Trace - deriving (Enum, Eq, Ord, Show) + deriving (Enum, Eq, Ord) data Log (d :: Symbol) (a :: * -> *) (b :: *) @@ -34,22 +36,40 @@ type instance DispatchOf (Log d) = Static WithSideEffects newtype instance StaticRep (Log d) = Log Severity -getLoggerIO :: +makeLoggerIO :: forall d es. (KnownSymbol d, Log d :> es) => Eff es (Severity -> String -> IO ()) -getLoggerIO = do - Log level <- getStaticRep - return $ \severity message -> - when (severity <= level) $ do - time <- formatISO8601Millis <$> getCurrentTime - putStrLn . applyWhen (severity == Attention) red $ printf "%s %s [%s] %s" time (symbolVal $ Proxy @d) (show severity) message +makeLoggerIO = getStaticRep >>= \(Log level) -> return $ loggerIO level + where + loggerIO level severity message = + when (severity <= level) $ do + time <- formatISO8601Millis <$> getCurrentTime + threadId <- show . fromThreadId <$> myThreadId + putStrLn $ unwords [time, fmtDomainAndThreadId threadId, fmtSeverity, fmtMessage] + where + fmtDomainAndThreadId threadId = white $ symbolVal (Proxy @d) <> ":" <> threadId + fmtSeverity = + case severity of + Attention -> red "ATTENTION" + Info -> green "INFO" + Trace -> blue "TRACE" + fmtMessage = applyWhen (severity == Attention) red message logMsg :: forall d es. (KnownSymbol d, Log d :> es) => Severity -> String -> Eff es () -logMsg severity message = getLoggerIO >>= \loggerIO -> unsafeEff_ $ loggerIO severity message +logMsg severity message = makeLoggerIO >>= \loggerIO -> unsafeEff_ $ loggerIO severity message + +logBlock :: + forall d es. (KnownSymbol d, Log d :> es) + => Severity + -> String + -> Eff es () +logBlock severity message = logMsg severity $ separator <> intercalate separator (lines message) + where + separator = "\n ... " runLog :: forall d es a. IOE :> es diff --git a/hsm-log/hsm-log.cabal b/hsm-log/hsm-log.cabal index 7aab0de..e8c3972 100644 --- a/hsm-log/hsm-log.cabal +++ b/hsm-log/hsm-log.cabal @@ -11,7 +11,6 @@ library , iso8601-time , text-ansi , time - , typelits-printf default-language: GHC2024 exposed-modules: Hsm.Log diff --git a/hsm-repl/Hsm/Repl.hs b/hsm-repl/Hsm/Repl.hs index dacc76a..46261d6 100644 --- a/hsm-repl/Hsm/Repl.hs +++ b/hsm-repl/Hsm/Repl.hs @@ -13,7 +13,7 @@ import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalS import Effectful.Exception (bracket) import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) import Generic.Data.Function.Common.Generic.Meta (KnownSymbols, symbolVals) -import Hsm.Log (Log, Severity(Attention, Info, Trace), logMsg) +import Hsm.Log (Log, Severity(Attention, Info, Trace), logBlock, logMsg) import Language.Haskell.Interpreter (GhcError(errMsg), InterpreterError(WontCompile), as, interpret, runInterpreter, setImports) import System.Console.Haskeline (defaultSettings, getInputLine, handleInterrupt, withInterrupt) import System.Console.Haskeline.IO (InputState, cancelInput, initializeInput, queryInput) @@ -45,7 +45,7 @@ repl = query >>= maybe (return Nothing) parse logMsg Trace $ "Parsed value: " <> show value return $ Just value Left (WontCompile errors) -> do - forM_ errors $ logMsg Attention . errMsg + forM_ errors $ logBlock Attention . errMsg repl Left err -> do logMsg Attention $ show err |