Skip to content

Commit

Permalink
cardano-tracer: align formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaro committed May 30, 2023
1 parent 15f1665 commit 2c18d78
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 47 deletions.
42 changes: 5 additions & 37 deletions cardano-tracer/src/Cardano/Tracer/Handlers/Logs/File.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.Tracer.Handlers.Logs.File
Expand All @@ -9,15 +8,10 @@ module Cardano.Tracer.Handlers.Logs.File
import Control.Concurrent.Extra (Lock, withLock)
import Control.Monad (unless)
import Control.Monad.Extra (ifM)
import Data.Aeson (Value, decodeStrict', pairs, (.=))
import Data.Aeson.Encoding (encodingToLazyByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.Char (isDigit)
import Data.Maybe (mapMaybe)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Time.Format (defaultTimeLocale, formatTime)
import System.Directory (createDirectoryIfMissing, doesDirectoryExist, makeAbsolute)
import System.Directory.Extra (listFiles)
import System.FilePath ((</>))
Expand Down Expand Up @@ -90,38 +84,12 @@ getPathToCurrentlog nodeName rootDirAbs format =
createEmptyLog subDirForLogs format

traceObjectToText :: TraceObject -> Maybe T.Text
traceObjectToText TraceObject{toHuman, toHostname, toNamespace, toSeverity, toThreadId, toTimestamp} =
traceObjectToText TraceObject{toHuman} =
case toHuman of
Nothing -> Nothing
Just msgForHuman -> Just $
"[" <> host <> ":" <> name <> ":" <> sev <> ":" <> thId <> "] [" <> time <> "] "
<> msgForHuman <> nl
where
host = T.pack toHostname
name = mkName toNamespace
sev = T.pack $ show toSeverity
thId = T.filter isDigit toThreadId
time = T.pack $ formatTime defaultTimeLocale "%F %T%2Q %Z" toTimestamp

mkName :: [T.Text] -> T.Text
mkName [] = "noname"
mkName l = T.intercalate "." l
Just msgForHuman -> Just $ msgForHuman <> nl

traceObjectToJSON :: TraceObject -> Maybe T.Text
traceObjectToJSON TraceObject{toMachine, toTimestamp, toNamespace, toHostname, toSeverity, toThreadId} =
case toMachine of
Nothing -> Nothing
Just msg -> Just $ toAsJSON msg <> nl
where
toAsJSON msgForMachine =
TE.decodeUtf8
. LBS.toStrict
. encodingToLazyByteString
. pairs $ "at" .= formatTime defaultTimeLocale "%F %H:%M:%S%4QZ" toTimestamp
<> "ns" .= mkName toNamespace
<> "data" .= case decodeStrict' $ TE.encodeUtf8 msgForMachine of
Just (v :: Value) -> v
Nothing -> ""
<> "sev" .= T.pack (show toSeverity)
<> "thread" .= T.filter isDigit toThreadId
<> "host" .= T.pack toHostname
traceObjectToJSON TraceObject{toMachine} =
Just $ toMachine <> nl

6 changes: 2 additions & 4 deletions cardano-tracer/src/Cardano/Tracer/Handlers/Logs/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ writeTraceObjectsToJournal nodeName = mapM_ (sendJournalFields . mkJournalFields
where
mkJournalFields trOb@TraceObject{toHuman, toMachine} =
case (toHuman, toMachine) of
(Just msgForHuman, Nothing) -> mkJournalFields' trOb msgForHuman
(Nothing, Just msgForMachine) -> mkJournalFields' trOb msgForMachine
(Just _, Just msgForMachine) -> mkJournalFields' trOb msgForMachine
(Nothing, Nothing) -> HM.empty
(Nothing, msgForMachine) -> mkJournalFields' trOb msgForMachine
(Just _, msgForMachine) -> mkJournalFields' trOb msgForMachine

mkJournalFields' TraceObject{toSeverity, toNamespace, toThreadId, toTimestamp} msg =
syslogIdentifier nodeName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ data Event = Event
-- | The queue for events we should notify about.
type EventsQueue = TBQueue Event

data EventGroup
data EventGroup
-- Common problems
= EventWarnings
| EventErrors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ saveTraceObjects savedTraceObjects nodeId traceObjects =
getTOValue TraceObject{toNamespace, toHuman, toMachine, toSeverity, toTimestamp} =
case (toNamespace, toHuman, toMachine) of
([], _, _) -> Nothing
(ns, Just msg, Nothing) -> Just (mkName ns, (msg, toSeverity, toTimestamp))
(ns, Nothing, Just msg) -> Just (mkName ns, (msg, toSeverity, toTimestamp))
(ns, Just msg, Just _) -> Just (mkName ns, (msg, toSeverity, toTimestamp))
_ -> Nothing
(ns, _, msg) -> Just (mkName ns, (msg, toSeverity, toTimestamp))

mkName = intercalate "."

Expand All @@ -80,7 +77,7 @@ getTraceObjects savedTraceObjects nodeId = atomically $ do
qForThisNode <- M.lookup nodeId <$> readTVar savedTraceObjects
maybe (return []) flushTQueue qForThisNode

-- | Counters for displayed logs item in "live view window".
-- | Counters for displayed logs item in "live view window".
type LogsLiveViewCounters = TVar (Map NodeId Int)

initLogsLiveViewCounters :: IO LogsLiveViewCounters
Expand Down

0 comments on commit 2c18d78

Please sign in to comment.