Skip to content

Commit

Permalink
Add spy and spy' for quick, colorful tracing
Browse files Browse the repository at this point in the history
Saw these functions in a purescript library lately and thought these
could be useful for quick inspection of values during debugging.
  • Loading branch information
ch1bo committed Jul 15, 2024
1 parent fc6f433 commit da856b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions hydra-prelude/hydra-prelude.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ library
, generic-random
, gitrev
, io-classes >=1.0.0.0
, pretty-simple
, QuickCheck
, relude
, si-timers >=1.0.0.0
Expand Down
14 changes: 14 additions & 0 deletions hydra-prelude/src/Hydra/Prelude.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- NOTE: Usage of 'trace' in 'spy'.
{-# OPTIONS_GHC -Wno-deprecations #-}

module Hydra.Prelude (
module Relude,
module Control.Monad.Class.MonadSTM,
Expand Down Expand Up @@ -35,6 +38,8 @@ module Hydra.Prelude (
decodeBase16,
(?>),
withFile,
spy,
spy',
) where

import Cardano.Binary (
Expand Down Expand Up @@ -154,6 +159,7 @@ import Test.QuickCheck (
)
import Test.QuickCheck.Gen (Gen (..))
import Test.QuickCheck.Random (mkQCGen)
import Text.Pretty.Simple (pShow)

-- | Provides a sensible way of automatically deriving generic 'Arbitrary'
-- instances for data-types. In the case where more advanced or tailored
Expand Down Expand Up @@ -249,3 +255,11 @@ withFile fp mode action =
System.IO.withFile fp mode (try . action) >>= \case
Left (e :: IOException) -> throwIO e
Right x -> pure x

-- | Like 'traceShow', but with pretty printing of the value.
spy :: Show a => a -> a
spy a = trace (toString $ pShow a) a

-- | Like 'spy' but prefixed with a label.
spy' :: Show a => String -> a -> a
spy' msg a = trace (msg <> ": " <> toString (pShow a)) a

0 comments on commit da856b1

Please sign in to comment.