Skip to content

Commit f61f949

Browse files
Log levels
1 parent 4097715 commit f61f949

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/BotPlutusInterface/Contract.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import BotPlutusInterface.Effects (
1313
callCommand,
1414
createDirectoryIfMissing,
1515
estimateBudget,
16+
handleLogTrace',
1617
handlePABEffect,
1718
logToContract,
1819
printLog,
@@ -35,21 +36,20 @@ import Control.Lens (preview, (^.))
3536
import Control.Monad (join, void, when)
3637
import Control.Monad.Freer (Eff, Member, interpret, reinterpret, runM, subsume, type (~>))
3738
import Control.Monad.Freer.Error (runError)
38-
import Control.Monad.Freer.Extras.Log (handleLogTrace)
3939
import Control.Monad.Freer.Extras.Modify (raiseEnd)
4040
import Control.Monad.Freer.Writer (Writer (Tell))
4141
import Control.Monad.Trans.Class (lift)
4242
import Control.Monad.Trans.Either (EitherT, eitherT, firstEitherT, newEitherT)
43-
import Data.Aeson (ToJSON, Value (String, Number, Bool, Null, Array, Object))
43+
import Data.Aeson (ToJSON, Value (Array, Bool, Null, Number, Object, String))
4444
import Data.Aeson.Extras (encodeByteString)
4545
import Data.Either (fromRight)
46+
import Data.HashMap.Strict qualified as HM
4647
import Data.Kind (Type)
4748
import Data.Map qualified as Map
4849
import Data.Row (Row)
4950
import Data.Text (Text)
5051
import Data.Text qualified as Text
51-
import qualified Data.Vector as V
52-
import qualified Data.HashMap.Strict as HM
52+
import Data.Vector qualified as V
5353
import Ledger (POSIXTime)
5454
import Ledger qualified
5555
import Ledger.Address (PaymentPubKeyHash (PaymentPubKeyHash))
@@ -72,10 +72,10 @@ import Plutus.Contract.Effects (
7272
import Plutus.Contract.Resumable (Resumable (..))
7373
import Plutus.Contract.Types (Contract (..), ContractEffs)
7474
import PlutusTx.Builtins (fromBuiltin)
75+
import Prettyprinter
76+
import Prettyprinter qualified as PP
7577
import Wallet.Emulator.Error (WalletAPIError (..))
7678
import Prelude
77-
import Prettyprinter
78-
import qualified Prettyprinter as PP
7979

8080
runContract ::
8181
forall (w :: Type) (s :: Row Type) (e :: Type) (a :: Type).
@@ -97,7 +97,7 @@ handleContract contractEnv =
9797
. handleResumable contractEnv
9898
. handleCheckpointIgnore
9999
. handleWriter
100-
. handleLogTrace
100+
. handleLogTrace' contractEnv.cePABConfig.pcLogLevel
101101
. runError
102102
. raiseEnd
103103

@@ -106,16 +106,17 @@ instance Pretty Value where
106106
pretty (Number n) = pretty $ show n
107107
pretty (Bool b) = pretty b
108108
pretty (Array arr) = PP.list $ pretty <$> V.toList arr
109-
pretty (Object obj) = PP.group
110-
. PP.encloseSep (PP.flatAlt "{ " "{") (PP.flatAlt " }" "}") ", "
111-
. map
112-
( \(k, v) ->
113-
PP.hang 2 $
114-
PP.sep
115-
[ pretty k <+> ": "
116-
, pretty v
117-
]
118-
)
109+
pretty (Object obj) =
110+
PP.group
111+
. PP.encloseSep (PP.flatAlt "{ " "{") (PP.flatAlt " }" "}") ", "
112+
. map
113+
( \(k, v) ->
114+
PP.hang 2 $
115+
PP.sep
116+
[ pretty k <+> ": "
117+
, pretty v
118+
]
119+
)
119120
$ HM.toList obj
120121
pretty Null = "null"
121122

src/BotPlutusInterface/Effects.hs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module BotPlutusInterface.Effects (
1414
uploadDir,
1515
updateInstanceState,
1616
printLog,
17+
handleLogTrace',
1718
logToContract,
1819
readFileTextEnvelope,
1920
writeFileJSON,
@@ -40,8 +41,11 @@ import Cardano.Api (AsType, FileError (FileIOError), HasTextEnvelope, TextEnvelo
4041
import Cardano.Api qualified
4142
import Control.Concurrent qualified as Concurrent
4243
import Control.Concurrent.STM (atomically, modifyTVar, modifyTVar')
44+
import Control.Lens
4345
import Control.Monad (void, when)
44-
import Control.Monad.Freer (Eff, LastMember, Member, interpretM, send, type (~>))
46+
import Control.Monad.Freer (Eff, LastMember, Member, interpret, interpretM, send, type (~>))
47+
import Control.Monad.Freer.Extras (LogMsg (LMessage))
48+
import Control.Monad.Freer.Extras qualified as Freer
4549
import Control.Monad.Trans.Except.Extra (handleIOExceptT, runExceptT)
4650
import Data.Aeson (ToJSON)
4751
import Data.Aeson qualified as JSON
@@ -52,10 +56,13 @@ import Data.Maybe (catMaybes)
5256
import Data.String (IsString, fromString)
5357
import Data.Text (Text)
5458
import Data.Text qualified as Text
59+
import Debug.Trace qualified as Trace
5560
import Ledger qualified
5661
import Plutus.Contract.Effects (ChainIndexQuery, ChainIndexResponse)
5762
import Plutus.PAB.Core.ContractInstance.STM (Activity)
5863
import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString))
64+
import Prettyprinter
65+
import Prettyprinter.Render.String qualified as Render
5966
import System.Directory qualified as Directory
6067
import System.Exit (ExitCode (ExitFailure, ExitSuccess))
6168
import System.Process (readProcess, readProcessWithExitCode)
@@ -152,6 +159,23 @@ printLog' :: LogLevel -> LogLevel -> String -> IO ()
152159
printLog' logLevelSetting msgLogLvl msg =
153160
when (logLevelSetting >= msgLogLvl) $ putStrLn msg
154161

162+
-- | Version of "Control.Monad.Freer.Extras.handleLogTrace" that takes into account the log level setting.
163+
handleLogTrace' :: Pretty a => LogLevel -> Eff (LogMsg a ': effs) ~> Eff effs
164+
handleLogTrace' logLevelSetting = interpret $ \case
165+
LMessage msg ->
166+
if logLevelSetting >= toNativeLogLevel (msg ^. Freer.logLevel)
167+
then Trace.trace (Render.renderString . layoutPretty defaultLayoutOptions . pretty $ msg) $ pure ()
168+
else pure ()
169+
where
170+
toNativeLogLevel Freer.Debug = Debug
171+
toNativeLogLevel Freer.Info = Info
172+
toNativeLogLevel Freer.Notice = Notice
173+
toNativeLogLevel Freer.Warning = Warn
174+
toNativeLogLevel Freer.Error = Error
175+
toNativeLogLevel Freer.Critical = Error
176+
toNativeLogLevel Freer.Alert = Error
177+
toNativeLogLevel Freer.Emergency = Error
178+
155179
callLocalCommand :: forall (a :: Type). ShellArgs a -> IO (Either Text a)
156180
callLocalCommand ShellArgs {cmdName, cmdArgs, cmdOutParser} =
157181
second cmdOutParser <$> readProcessEither (Text.unpack cmdName) (map Text.unpack cmdArgs)

0 commit comments

Comments
 (0)