@@ -14,6 +14,7 @@ module BotPlutusInterface.Effects (
1414 uploadDir ,
1515 updateInstanceState ,
1616 printLog ,
17+ printBpiLog ,
1718 handleContractLog ,
1819 logToContract ,
1920 readFileTextEnvelope ,
@@ -32,6 +33,7 @@ import BotPlutusInterface.Types (
3233 CLILocation (.. ),
3334 ContractEnvironment ,
3435 ContractState (ContractState ),
36+ LogContext (BpiLog , ContractLog ),
3537 LogLevel (.. ),
3638 TxBudget ,
3739 TxFile ,
@@ -60,7 +62,8 @@ import Ledger qualified
6062import Plutus.Contract.Effects (ChainIndexQuery , ChainIndexResponse )
6163import Plutus.PAB.Core.ContractInstance.STM (Activity )
6264import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString ))
63- import Prettyprinter (Pretty (pretty ), defaultLayoutOptions , layoutPretty )
65+ import Prettyprinter (Pretty (pretty ), defaultLayoutOptions , layoutPretty , (<+>) )
66+ import Prettyprinter qualified as PP
6467import Prettyprinter.Render.String qualified as Render
6568import System.Directory qualified as Directory
6669import System.Exit (ExitCode (ExitFailure , ExitSuccess ))
@@ -81,7 +84,7 @@ data PABEffect (w :: Type) (r :: Type) where
8184 CreateDirectoryIfMissing :: Bool -> FilePath -> PABEffect w ()
8285 -- Same as above but creates folder on the CLI machine, be that local or remote.
8386 CreateDirectoryIfMissingCLI :: Bool -> FilePath -> PABEffect w ()
84- PrintLog :: LogLevel -> String -> PABEffect w ()
87+ PrintLog :: LogContext -> LogLevel -> PP. Doc () -> PABEffect w ()
8588 UpdateInstanceState :: Activity -> PABEffect w ()
8689 LogToContract :: (ToJSON w , Monoid w ) => w -> PABEffect w ()
8790 ThreadDelay :: Int -> PABEffect w ()
@@ -123,7 +126,7 @@ handlePABEffect contractEnv =
123126 case contractEnv. cePABConfig. pcCliLocation of
124127 Local -> Directory. createDirectoryIfMissing createParents filePath
125128 Remote ipAddr -> createDirectoryIfMissingRemote ipAddr createParents filePath
126- PrintLog logLevel txt -> printLog' contractEnv. cePABConfig. pcLogLevel logLevel txt
129+ PrintLog logCtx logLevel txt -> printLog' contractEnv. cePABConfig. pcLogLevel logCtx logLevel txt
127130 UpdateInstanceState s -> do
128131 atomically $
129132 modifyTVar contractEnv. ceContractState $
@@ -154,20 +157,25 @@ handlePABEffect contractEnv =
154157 SaveBudget txId exBudget -> saveBudgetImpl contractEnv txId exBudget
155158 )
156159
157- printLog' :: LogLevel -> LogLevel -> String -> IO ()
158- printLog' logLevelSetting msgLogLvl msg =
159- when (logLevelSetting >= msgLogLvl) $ putStrLn msg
160+ printLog' :: LogLevel -> LogContext -> LogLevel -> PP. Doc () -> IO ()
161+ printLog' logLevelSetting msgCtx msgLogLvl msg =
162+ when (logLevelSetting >= msgLogLvl) $ putStrLn target
163+ where
164+ target =
165+ Render. renderString . layoutPretty defaultLayoutOptions $
166+ pretty msgCtx <+> pretty msgLogLvl <+> msg
160167
161168-- | Reinterpret contract logs to be handled by PABEffect later down the line.
162169handleContractLog :: forall w a effs . Member (PABEffect w ) effs => Pretty a => Eff (LogMsg a ': effs ) ~> Eff effs
163170handleContractLog x = subsume $ handleContractLogInternal @ w x
164171
165172handleContractLogInternal :: forall w a effs . Pretty a => Eff (LogMsg a ': effs ) ~> Eff (PABEffect w ': effs )
166173handleContractLogInternal = reinterpret $ \ case
167- LMessage msg ->
168- let msgLogLevel = toNativeLogLevel (msg ^. Freer. logLevel)
169- msgPretty = Render. renderString . layoutPretty defaultLayoutOptions . pretty $ msg
170- in printLog @ w msgLogLevel msgPretty
174+ LMessage logMsg ->
175+ let msgContent = logMsg ^. Freer. logMessageContent
176+ msgLogLevel = toNativeLogLevel (logMsg ^. Freer. logLevel)
177+ msgPretty = pretty msgContent
178+ in printLog @ w ContractLog msgLogLevel msgPretty
171179 where
172180 toNativeLogLevel Freer. Debug = Debug
173181 toNativeLogLevel Freer. Info = Info
@@ -247,12 +255,21 @@ createDirectoryIfMissingCLI ::
247255createDirectoryIfMissingCLI createParents path = send @ (PABEffect w ) $ CreateDirectoryIfMissingCLI createParents path
248256
249257printLog ::
258+ forall (w :: Type ) (effs :: [Type -> Type ]).
259+ Member (PABEffect w ) effs =>
260+ LogContext ->
261+ LogLevel ->
262+ PP. Doc () ->
263+ Eff effs ()
264+ printLog logCtx logLevel msg = send @ (PABEffect w ) $ PrintLog logCtx logLevel msg
265+
266+ printBpiLog ::
250267 forall (w :: Type ) (effs :: [Type -> Type ]).
251268 Member (PABEffect w ) effs =>
252269 LogLevel ->
253- String ->
270+ PP. Doc () ->
254271 Eff effs ()
255- printLog logLevel msg = send @ ( PABEffect w ) $ PrintLog logLevel msg
272+ printBpiLog = printLog @ w BpiLog
256273
257274updateInstanceState ::
258275 forall (w :: Type ) (effs :: [Type -> Type ]). Member (PABEffect w ) effs => Activity -> Eff effs ()
0 commit comments