@@ -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
4041import Cardano.Api qualified
4142import Control.Concurrent qualified as Concurrent
4243import Control.Concurrent.STM (atomically , modifyTVar , modifyTVar' )
44+ import Control.Lens
4345import 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
4549import Control.Monad.Trans.Except.Extra (handleIOExceptT , runExceptT )
4650import Data.Aeson (ToJSON )
4751import Data.Aeson qualified as JSON
@@ -52,10 +56,13 @@ import Data.Maybe (catMaybes)
5256import Data.String (IsString , fromString )
5357import Data.Text (Text )
5458import Data.Text qualified as Text
59+ import Debug.Trace qualified as Trace
5560import Ledger qualified
5661import Plutus.Contract.Effects (ChainIndexQuery , ChainIndexResponse )
5762import Plutus.PAB.Core.ContractInstance.STM (Activity )
5863import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString ))
64+ import Prettyprinter
65+ import Prettyprinter.Render.String qualified as Render
5966import System.Directory qualified as Directory
6067import System.Exit (ExitCode (ExitFailure , ExitSuccess ))
6168import System.Process (readProcess , readProcessWithExitCode )
@@ -152,6 +159,23 @@ printLog' :: LogLevel -> LogLevel -> String -> IO ()
152159printLog' 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+
155179callLocalCommand :: forall (a :: Type ). ShellArgs a -> IO (Either Text a )
156180callLocalCommand ShellArgs {cmdName, cmdArgs, cmdOutParser} =
157181 second cmdOutParser <$> readProcessEither (Text. unpack cmdName) (map Text. unpack cmdArgs)
0 commit comments