diff options
| author | Albert Krewinkel <[email protected]> | 2025-07-31 11:15:12 +0200 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2025-08-01 10:04:14 -0700 |
| commit | 57e7d895db367eb09cb4cb31678ac3b5f6dc9ef7 (patch) | |
| tree | 44a41692992ef7bfc3507ed33ba749540e7bbd5e | |
| parent | 8799ad87b797e67577bc5368580685c3fa8e97fb (diff) | |
PandocMonad: add function `runSilently` [API Change]
The function runs an action in the PandocMonad, but returns all log
messages reported by that action instead of adding them to the main log.
| -rw-r--r-- | src/Text/Pandoc/Class/PandocMonad.hs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Class/PandocMonad.hs b/src/Text/Pandoc/Class/PandocMonad.hs index 7fc89bdab..e106c23e1 100644 --- a/src/Text/Pandoc/Class/PandocMonad.hs +++ b/src/Text/Pandoc/Class/PandocMonad.hs @@ -28,6 +28,7 @@ module Text.Pandoc.Class.PandocMonad , getZonedTime , readFileFromDirs , report + , runSilently , setRequestHeader , setNoCheckCertificate , getLog @@ -192,6 +193,23 @@ report msg = do when (level <= verbosity) $ logOutput msg modifyCommonState $ \st -> st{ stLog = msg : stLog st } +-- | Run an action, but suppress the output of any log messages; +-- instead, all messages reported by @action@ are returned separately +-- and not added to the main log. +runSilently :: PandocMonad m => m a -> m (a, [LogMessage]) +runSilently action = do + -- get current settings + origLog <- getsCommonState stLog + origVerbosity <- getVerbosity + -- reset log level and set verbosity to the minimum + modifyCommonState (\st -> st { stVerbosity = ERROR, stLog = []}) + result <- action + -- get log messages reported while running `action` + newLog <- getsCommonState stLog + modifyCommonState (\st -> st { stVerbosity = origVerbosity, stLog = origLog}) + + return (result, newLog) + -- | Set request header to use in HTTP requests. setRequestHeader :: PandocMonad m => T.Text -- ^ Header name |
