-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simplified thread safe tracer for
ToText
instances
- Loading branch information
Showing
2 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module Cardano.BM.ToTextTracer | ||
( ToTextTracer (..) | ||
, newToTextTracer | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
import Cardano.BM.Data.Tracer | ||
( HasSeverityAnnotation (..) | ||
, Tracer (Tracer) | ||
) | ||
import Cardano.BM.Tracing | ||
( Severity | ||
) | ||
import Control.Monad | ||
( forever | ||
, unless | ||
, (>=>) | ||
) | ||
import Control.Monad.STM | ||
( retry | ||
) | ||
import Control.Monad.Trans.Cont | ||
( ContT (..) | ||
) | ||
import Data.Text.Class | ||
( ToText (..) | ||
) | ||
import Data.Time | ||
( getCurrentTime | ||
) | ||
import Data.Time.Format.ISO8601 | ||
( iso8601Show | ||
) | ||
import UnliftIO | ||
( BufferMode (NoBuffering) | ||
, IOMode (WriteMode) | ||
, MonadIO (liftIO) | ||
, async | ||
, atomically | ||
, hSetBuffering | ||
, isEmptyTChan | ||
, link | ||
, newTChanIO | ||
, readTChan | ||
, stdout | ||
, withFile | ||
, writeTChan | ||
) | ||
|
||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
|
||
-- | A thread-safe tracer that logs messages to a file or stdout for anything | ||
-- that has ToText instance | ||
newtype ToTextTracer | ||
= ToTextTracer | ||
(forall a. (HasSeverityAnnotation a, ToText a) => Tracer IO a) | ||
|
||
-- | Create a new `ToTextTracer` | ||
newToTextTracer | ||
:: Maybe FilePath | ||
-- ^ If provided, logs will be written to this file, otherwise to stdout | ||
-> Maybe Severity | ||
-- ^ Minimum severity level to log | ||
-> (ToTextTracer -> IO r) | ||
-- ^ Action to run with the new tracer | ||
-> IO r | ||
newToTextTracer clusterLogs minSeverity = runContT $ do | ||
ch <- newTChanIO | ||
h <- case clusterLogs of | ||
Nothing -> pure stdout | ||
Just logFile -> do | ||
ContT $ withFile logFile WriteMode | ||
liftIO $ hSetBuffering h NoBuffering | ||
liftIO $ async >=> link $ forever $ do | ||
(x, s, t) <- atomically $ readTChan ch | ||
T.hPutStrLn h | ||
$ T.pack (iso8601Show t) | ||
<> " [" | ||
<> T.pack (show s) | ||
<> "] " | ||
<> x | ||
ContT $ \k -> do | ||
r <- k $ ToTextTracer $ Tracer $ \msg -> do | ||
let severity = getSeverityAnnotation msg | ||
unless (Just severity < minSeverity) $ do | ||
t <- getCurrentTime | ||
atomically $ writeTChan ch (toText msg, severity, t) | ||
-- wait until the channel is empty | ||
atomically $ do | ||
empty <- isEmptyTChan ch | ||
unless empty retry | ||
pure r |