Skip to content

Commit 09f046d

Browse files
Delegate contract logging to PABEffect
1 parent f61f949 commit 09f046d

File tree

4 files changed

+131
-38
lines changed

4 files changed

+131
-38
lines changed

flake.lock

Lines changed: 114 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description = "bot-plutus-interface";
33

44
inputs = {
5-
haskell-nix.url = "github:L-as/haskell.nix";
5+
haskell-nix.url = "github:mlabs-haskell/haskell.nix";
66

77
nixpkgs.follows = "haskell-nix/nixpkgs-unstable";
88

src/BotPlutusInterface/Contract.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import BotPlutusInterface.Effects (
1313
callCommand,
1414
createDirectoryIfMissing,
1515
estimateBudget,
16-
handleLogTrace',
16+
handleContractLog,
1717
handlePABEffect,
1818
logToContract,
1919
printLog,
@@ -72,7 +72,7 @@ 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
75+
import Prettyprinter (Pretty (pretty), (<+>))
7676
import Prettyprinter qualified as PP
7777
import Wallet.Emulator.Error (WalletAPIError (..))
7878
import Prelude
@@ -97,7 +97,7 @@ handleContract contractEnv =
9797
. handleResumable contractEnv
9898
. handleCheckpointIgnore
9999
. handleWriter
100-
. handleLogTrace' contractEnv.cePABConfig.pcLogLevel
100+
. handleContractLog @w
101101
. runError
102102
. raiseEnd
103103

src/BotPlutusInterface/Effects.hs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module BotPlutusInterface.Effects (
1414
uploadDir,
1515
updateInstanceState,
1616
printLog,
17-
handleLogTrace',
17+
handleContractLog,
1818
logToContract,
1919
readFileTextEnvelope,
2020
writeFileJSON,
@@ -41,9 +41,9 @@ import Cardano.Api (AsType, FileError (FileIOError), HasTextEnvelope, TextEnvelo
4141
import Cardano.Api qualified
4242
import Control.Concurrent qualified as Concurrent
4343
import Control.Concurrent.STM (atomically, modifyTVar, modifyTVar')
44-
import Control.Lens
44+
import Control.Lens ((^.))
4545
import Control.Monad (void, when)
46-
import Control.Monad.Freer (Eff, LastMember, Member, interpret, interpretM, send, type (~>))
46+
import Control.Monad.Freer (Eff, LastMember, Member, interpretM, reinterpret, send, subsume, type (~>))
4747
import Control.Monad.Freer.Extras (LogMsg (LMessage))
4848
import Control.Monad.Freer.Extras qualified as Freer
4949
import Control.Monad.Trans.Except.Extra (handleIOExceptT, runExceptT)
@@ -56,12 +56,11 @@ import Data.Maybe (catMaybes)
5656
import Data.String (IsString, fromString)
5757
import Data.Text (Text)
5858
import Data.Text qualified as Text
59-
import Debug.Trace qualified as Trace
6059
import Ledger qualified
6160
import Plutus.Contract.Effects (ChainIndexQuery, ChainIndexResponse)
6261
import Plutus.PAB.Core.ContractInstance.STM (Activity)
6362
import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString))
64-
import Prettyprinter
63+
import Prettyprinter (Pretty (pretty), defaultLayoutOptions, layoutPretty)
6564
import Prettyprinter.Render.String qualified as Render
6665
import System.Directory qualified as Directory
6766
import System.Exit (ExitCode (ExitFailure, ExitSuccess))
@@ -159,13 +158,16 @@ printLog' :: LogLevel -> LogLevel -> String -> IO ()
159158
printLog' logLevelSetting msgLogLvl msg =
160159
when (logLevelSetting >= msgLogLvl) $ putStrLn msg
161160

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
161+
-- | Reinterpret contract logs to be handled by PABEffect later down the line.
162+
handleContractLog :: forall w a effs. Member (PABEffect w) effs => Pretty a => Eff (LogMsg a ': effs) ~> Eff effs
163+
handleContractLog x = subsume $ handleContractLogInternal @w x
164+
165+
handleContractLogInternal :: forall w a effs. Pretty a => Eff (LogMsg a ': effs) ~> Eff (PABEffect w ': effs)
166+
handleContractLogInternal = reinterpret $ \case
165167
LMessage msg ->
166-
if logLevelSetting >= toNativeLogLevel (msg ^. Freer.logLevel)
167-
then Trace.trace (Render.renderString . layoutPretty defaultLayoutOptions . pretty $ msg) $ pure ()
168-
else pure ()
168+
let msgLogLevel = toNativeLogLevel (msg ^. Freer.logLevel)
169+
msgPretty = Render.renderString . layoutPretty defaultLayoutOptions . pretty $ msg
170+
in printLog @w msgLogLevel msgPretty
169171
where
170172
toNativeLogLevel Freer.Debug = Debug
171173
toNativeLogLevel Freer.Info = Info

0 commit comments

Comments
 (0)