Skip to content

Commit db8ebbc

Browse files
committed
Parse multiple logs, add colors and formatting
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent a8bc20d commit db8ebbc

File tree

1 file changed

+57
-26
lines changed
  • hydra-node/exe/visualize-logs

1 file changed

+57
-26
lines changed

hydra-node/exe/visualize-logs/Main.hs

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,38 @@ import Hydra.Prelude qualified as P
99

1010
import Conduit
1111
import Control.Lens ((^?))
12+
import Control.Monad (foldM)
1213
import Data.Aeson (eitherDecode')
1314
import Data.Aeson.Lens (key, _String)
1415
import Data.ByteString.Lazy.Char8 qualified as C8
15-
import Data.Text qualified as T
1616
import Data.Text.Encoding (encodeUtf8)
17-
import GHC.Show (show)
1817
import Hydra.Chain (ChainEvent (..))
19-
import Hydra.HeadLogic (Effect (..), Input (..), Outcome (..))
18+
import Hydra.HeadLogic (Effect (..), Input (..), Outcome (..), StateChanged (..))
2019
import Hydra.Logging (Envelope (..))
2120
import Hydra.Logging.Messages (HydraLog (..))
2221
import Hydra.Node (HydraNodeLog (..))
2322

23+
data InfoLine = InfoLine {header :: Text, line :: Text, color :: Text} deriving (Eq, Show)
24+
2425
data Decoded tx
25-
= DecodedHydraLog Text
26+
= DecodedHydraLog {t :: UTCTime, n :: Text, info :: InfoLine}
2627
| DropLog
27-
deriving (Eq)
28+
deriving (Eq, Show)
2829

29-
instance Show (Decoded tx) where
30-
show (DecodedHydraLog txt) = T.unpack txt
31-
show DropLog = ""
30+
instance Ord (Decoded tx) where
31+
compare DropLog DropLog = EQ
32+
compare DropLog DecodedHydraLog{} = LT
33+
compare DecodedHydraLog{} DropLog = GT
34+
compare (DecodedHydraLog t1 _ _) (DecodedHydraLog t2 _ _) = compare t1 t2
3235

3336
main :: IO ()
34-
main = do
37+
main = visualize ["../devnet/alice-logs.txt", "../devnet/bob-logs.txt"]
38+
39+
visualize :: [FilePath] -> IO ()
40+
visualize paths = do
3541
decodedLines <-
3642
runConduitRes $
37-
sourceFileBS "../devnet/alice-logs.txt"
43+
mapM_ sourceFileBS paths
3844
.| linesUnboundedAsciiC
3945
.| mapMC
4046
( \l ->
@@ -43,50 +49,75 @@ main = do
4349
Just line ->
4450
let envelope = fromStrict $ encodeUtf8 line
4551
in case decodeAs envelope (undefined :: Envelope (HydraLog Tx)) of
46-
Left e -> P.error $ P.show e <> line
52+
Left e -> P.error $ show e <> line
4753
Right decoded ->
4854
case decoded.message of
49-
NodeOptions opt -> pure $ DecodedHydraLog $ "NODE STARTING: \n" <> P.show opt
55+
NodeOptions opt -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "NODE OPTIONS" (show opt) green)
5056
Node msg ->
5157
case msg of
5258
BeginInput{input} ->
5359
case input of
5460
ClientInput{clientInput} ->
55-
pure $ DecodedHydraLog $ "NODE LOG: \n" <> P.show clientInput
61+
pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "CLIENT SENT" (show clientInput) green)
5662
NetworkInput{} -> pure DropLog
5763
ChainInput{chainEvent} ->
5864
case chainEvent of
59-
Observation{observedTx} -> pure $ DecodedHydraLog $ "OBESERVATION: " <> P.show observedTx
65+
Observation{observedTx} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "OBESERVATION" (show observedTx) blue)
6066
Rollback{} -> pure DropLog
6167
Tick{} -> pure DropLog
6268
PostTxError{postTxError} ->
63-
pure $ DecodedHydraLog $ "ERROR: " <> P.show postTxError
69+
pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "ERROR" (show postTxError) red)
6470
EndInput{} -> pure DropLog
6571
BeginEffect{effect} ->
6672
case effect of
6773
ClientEffect{} -> pure DropLog
68-
NetworkEffect{message} -> pure $ DecodedHydraLog $ P.show message
69-
OnChainEffect{postChainTx} -> pure $ DecodedHydraLog $ "POSTING: " <> P.show postChainTx
74+
NetworkEffect{message} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "NETWORK EFFECT" (show message) green)
75+
OnChainEffect{postChainTx} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "POSTING" (show postChainTx) blue)
7076
EndEffect{} -> pure DropLog
7177
LogicOutcome{outcome} ->
7278
case outcome of
73-
Continue{} -> pure DropLog
79+
Continue{stateChanges} ->
80+
foldM
81+
( \b a -> case a of
82+
HeadOpened{} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "HeadOpened" "" green)
83+
_ -> pure b
84+
)
85+
DropLog
86+
stateChanges
7487
Wait{} -> pure DropLog
75-
Error{error = err} -> pure $ DecodedHydraLog $ "LOGIC ERROR: " <> P.show err
88+
Error{error = err} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "LOGIC ERROR" (show err) red)
7689
DroppedFromQueue{} -> pure DropLog
77-
LoadingState -> pure $ DecodedHydraLog "Loading state..."
78-
LoadedState{} -> pure $ DecodedHydraLog "Loaded."
79-
ReplayingState -> pure $ DecodedHydraLog "Replaying state..."
80-
Misconfiguration{} -> pure $ DecodedHydraLog "MISCONFIG!"
90+
LoadingState -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "Loading state..." "" green)
91+
LoadedState{} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "Loaded." "" green)
92+
ReplayingState -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "Replaying state..." "" green)
93+
Misconfiguration{} -> pure $ DecodedHydraLog decoded.timestamp decoded.namespace (InfoLine "MISCONFIG!" "" red)
8194
_ -> pure DropLog
8295
)
83-
.| filterC (\a -> P.show a /= ("" :: Text))
96+
.| filterC (/= DropLog)
8497
.| sinkList
85-
forM_ decodedLines $ \l ->
86-
putTextLn (P.show l)
98+
forM_ (sort decodedLines) $ \l ->
99+
render l
87100

88101
decodeAs :: forall a. FromJSON a => C8.ByteString -> a -> Either String a
89102
decodeAs l _ =
90103
case eitherDecode' l :: Either String a of
91104
Left e -> Left e
92105
Right decoded -> pure decoded
106+
107+
render :: Decoded tx -> IO ()
108+
render = \case
109+
DecodedHydraLog{t, n, info = InfoLine{header, line, color}} -> putTextLn $ color <> unlines ["[" <> show t <> "]", "NAMESPACE:" <> show n, header, line]
110+
DropLog -> putTextLn ""
111+
112+
-- ANSI escape codes for colors
113+
red :: Text
114+
red = "\ESC[31m"
115+
116+
green :: Text
117+
green = "\ESC[32m"
118+
119+
blue :: Text
120+
blue = "\ESC[34m"
121+
122+
reset :: Text
123+
reset = "\ESC[0m"

0 commit comments

Comments
 (0)