@@ -40,14 +40,15 @@ import BotPlutusInterface.Types (
4040 ContractState (ContractState ),
4141 LogContext (BpiLog , ContractLog ),
4242 LogLevel (.. ),
43+ LogsList (LogsList ),
4344 TxBudget ,
4445 TxFile ,
4546 addBudget ,
4647 )
4748import Cardano.Api (AsType , FileError (FileIOError ), HasTextEnvelope , TextEnvelopeDescr , TextEnvelopeError )
4849import Cardano.Api qualified
4950import Control.Concurrent qualified as Concurrent
50- import Control.Concurrent.STM (atomically , modifyTVar , modifyTVar' )
51+ import Control.Concurrent.STM (TVar , atomically , modifyTVar , modifyTVar' )
5152import Control.Lens ((^.) )
5253import Control.Monad (void , when )
5354import Control.Monad.Freer (Eff , LastMember , Member , interpretM , reinterpret , send , subsume , type (~> ))
@@ -138,7 +139,12 @@ handlePABEffect contractEnv =
138139 case contractEnv. cePABConfig. pcCliLocation of
139140 Local -> Directory. createDirectoryIfMissing createParents filePath
140141 Remote ipAddr -> createDirectoryIfMissingRemote ipAddr createParents filePath
141- PrintLog logCtx logLevel txt -> printLog' contractEnv. cePABConfig. pcLogLevel logCtx logLevel txt
142+ PrintLog logCtx logLevel txt ->
143+ let logMsg = prettyLog logCtx logLevel txt
144+ in do
145+ printLog' contractEnv. cePABConfig. pcLogLevel logCtx logLevel logMsg
146+ when contractEnv. cePABConfig. pcCollectLogs $
147+ collectLog contractEnv. ceContractLogs logCtx logLevel logMsg
142148 UpdateInstanceState s -> do
143149 atomically $
144150 modifyTVar contractEnv. ceContractState $
@@ -181,7 +187,15 @@ printLog' logLevelSetting msgCtx msgLogLvl msg =
181187 where
182188 target =
183189 Render. renderString . layoutPretty defaultLayoutOptions $
184- pretty msgCtx <+> pretty msgLogLvl <+> msg
190+ prettyLog msgCtx msgLogLvl msg
191+
192+ prettyLog :: LogContext -> LogLevel -> PP. Doc () -> PP. Doc ()
193+ prettyLog msgCtx msgLogLvl msg = pretty msgCtx <+> pretty msgLogLvl <+> msg
194+
195+ collectLog :: TVar LogsList -> LogContext -> LogLevel -> PP. Doc () -> IO ()
196+ collectLog logs logCtx logLvl msg = atomically $ modifyTVar' logs appendLog
197+ where
198+ appendLog (LogsList ls) = LogsList $ (logCtx, logLvl, msg) : ls
185199
186200-- | Reinterpret contract logs to be handled by PABEffect later down the line.
187201handleContractLog :: forall w a effs . Member (PABEffect w ) effs => Pretty a => Eff (LogMsg a ': effs ) ~> Eff effs
0 commit comments