Skip to content

Commit

Permalink
Add tracing from an handle
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 23, 2024
1 parent ba2bde1 commit f803c6b
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 41 deletions.
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ unit-tests-cabal-match match:
LOCAL_CLUSTER_CONFIGS=../../lib/local-cluster/test/data/cluster-configs \
cabal test cardano-wallet-unit:unit cardano-wallet-read:test -O0 -v0 \
--test-options '--match="{{match}}"'
cabal test cardano-wallet-launcher:unit -O0 -v0 \
--test-options '--match="{{match}}"'

# run unit tests
unit-tests-cabal:
Expand Down
81 changes: 40 additions & 41 deletions lib/launcher/cardano-wallet-launcher.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@ synopsis: Utilities for a building commands launcher
homepage: https://github.com/cardano-foundation/cardano-wallet
author: Cardano Foundation (High Assurance Lab)
maintainer: hal@cardanofoundation.org
copyright: 2018-2020 IOHK
copyright: 2018-2023 IOHK, 2024- Cardano Foundation
license: Apache-2.0
category: Web
build-type: Simple
cabal-version: >=1.10

flag release
description: Enable optimization and `-Werror`
default: False
manual: True
description: Enable optimization and `-Werror`
default: False
manual: True

library
default-language:
Haskell2010
default-language: Haskell2010
default-extensions:
NoImplicitPrelude
OverloadedStrings
ghc-options:
-Wall
-Wcompat
-fwarn-redundant-constraints
if (flag(release))
ghc-options:
-Werror
NoImplicitPrelude
OverloadedStrings

ghc-options: -Wall -Wcompat -fwarn-redundant-constraints

if flag(release)
ghc-options: -Werror

build-depends:
base
, bytestring
Expand All @@ -43,33 +41,35 @@ library
, text-class
, unliftio
, unliftio-core
hs-source-dirs:
src

hs-source-dirs: src
exposed-modules:
Cardano.Launcher
, Cardano.Launcher.Node
Cardano.Launcher.Wallet
, Cardano.Startup
Cardano.Launcher
Cardano.Launcher.Logging
Cardano.Launcher.Node
Cardano.Launcher.Wallet
Cardano.Startup

if os(windows)
build-depends: Win32
other-modules: Cardano.Startup.Windows
cpp-options: -DWINDOWS

else
build-depends: unix
other-modules: Cardano.Startup.POSIX

test-suite unit
default-language:
Haskell2010
default-language: Haskell2010
default-extensions:
NoImplicitPrelude
OverloadedStrings
ghc-options:
-threaded
-rtsopts
-Wall
if (flag(release))
NoImplicitPrelude
OverloadedStrings

ghc-options: -threaded -rtsopts -Wall

if flag(release)
ghc-options: -O2 -Werror

build-depends:
base
, bytestring
Expand All @@ -86,16 +86,15 @@ test-suite unit
, text-class
, time
, unliftio
build-tool-depends:
hspec-discover:hspec-discover
type:
exitcode-stdio-1.0
hs-source-dirs:
test/unit
main-is:
launcher-unit-test.hs

build-tool-depends: hspec-discover:hspec-discover
type: exitcode-stdio-1.0
hs-source-dirs: test/unit
main-is: launcher-unit-test.hs
other-modules:
Cardano.LauncherSpec
, Cardano.StartupSpec
Cardano.Launcher.LoggingSpec
Cardano.LauncherSpec
Cardano.StartupSpec

if os(windows)
cpp-options: -DWINDOWS
cpp-options: -DWINDOWS
29 changes: 29 additions & 0 deletions lib/launcher/src/Cardano/Launcher/Logging.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Cardano.Launcher.Logging (traceHandle) where

import Control.Monad.IO.Class
( MonadIO (..)
)
import Control.Tracer
( Tracer
, traceWith
)
import Data.Text
( Text
)
import Prelude
import System.IO
( Handle
, hIsEOF
)

import qualified Data.Text.IO as T

traceHandle :: MonadIO m => Tracer m Text -> Handle -> m ()
traceHandle tr h = do
eof <- liftIO $ hIsEOF h
if eof
then pure ()
else do
line <- liftIO $ T.hGetLine h
traceWith tr line
traceHandle tr h
85 changes: 85 additions & 0 deletions lib/launcher/test/unit/Cardano/Launcher/LoggingSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Launcher.LoggingSpec (spec) where

import Prelude

import Cardano.Launcher
( Command (Command)
, ProcessRun (ProcessRun)
, StdStream (CreatePipe, NoStream)
, withBackendProcess
)
import Cardano.Launcher.Logging
( traceHandle
)
import Control.Monad
( forM_
)
import Control.Tracer
( Tracer (..)
, nullTracer
)
import Data.Functor.Contravariant
( (>$<)
)
import Data.IORef
( modifyIORef
, newIORef
, readIORef
)
import Data.Text
( Text
)
import System.IO
( SeekMode (..)
, hSeek
)
import Test.Hspec
( Spec
, describe
, it
, shouldBe
, shouldReturn
)
import UnliftIO
( withSystemTempFile
)

import qualified Data.Text.IO as T

holdTrace :: IO (Tracer IO Text, IO [Text])
holdTrace = do
ref <- newIORef []
let tracer = Tracer $ \line -> modifyIORef ref (line :)
pure (tracer, reverse <$> readIORef ref)

prepend :: Text -> Text -> Text
prepend prefix = ((prefix <> ": ") <>)

spec :: Spec
spec = do
describe "traceHandle" $ do
it "traces lines from a handle" $ do
let ls = ["line 1", "line 2", "line 3"]
withSystemTempFile "traceHandle" $ \_ handle -> do
forM_ ls $ T.hPutStrLn handle
hSeek handle AbsoluteSeek 0
(tracer, readTrace) <- holdTrace
traceHandle (("traced:" <>) >$< tracer) handle
readTrace `shouldReturn` (("traced:" <>) <$> ls)
it "traces output of a process" $ do
let c = Command
"echo"
["cwd"]
(pure ())
NoStream
CreatePipe
run _ (Just handle) _ _ = do
(tracer, readTrace) <- holdTrace
traceHandle (prepend "stdout" >$< tracer) handle
readTrace `shouldReturn` prepend "stdout" <$> ["cwd"]
run _ _ _ _ = fail "no stdout"
r <- withBackendProcess nullTracer c $ ProcessRun run
r `shouldBe` Right ()
pure ()

0 comments on commit f803c6b

Please sign in to comment.