git.iko.soy/typeable/pandoc
on master help sign in
Add timing info for filters in `--verbose` mode.

+ Add RunningFilter, FilterCompleted constructors to LogMessage + When verbose mode is specified (verbosity == INFO), print a notice when running a filter and when a filter completes (including timing). Closes #6112.

commitfd9c03febe92411bfdfc63e8f19a037cf04f3a0e authorJohn MacFarlane <jgm@berkeley.edu> dateTue 4 Feb 2020, 09:38 (7 years ago) parent30962e2
2 files changed+30−5
src/Text/Pandoc/Filter.hs+18−4 src/Text/Pandoc/Logging.hs+12−1
src/Text/Pandoc/Filter.hs+18−4
@@ -20,11 +20,13 @@ module Text.Pandoc.Filter
2020 ) where
2121
2222import Prelude
23+import System.CPUTime (getCPUTime)
2324import Data.Aeson.TH (deriveJSON, defaultOptions)
2425import GHC.Generics (Generic)
25−import Text.Pandoc.Class (PandocIO)
26+import Text.Pandoc.Class (PandocIO, report, getVerbosity)
2627import Text.Pandoc.Definition (Pandoc)
2728import Text.Pandoc.Options (ReaderOptions)
29+import Text.Pandoc.Logging
2830import qualified Text.Pandoc.Filter.JSON as JSONFilter
2931import qualified Text.Pandoc.Filter.Lua as LuaFilter
3032import qualified Text.Pandoc.Filter.Path as Path
@@ -32,7 +34,8 @@ import Data.YAML
3234import qualified Data.Text as T
3335import System.FilePath (takeExtension)
3436import Control.Applicative ((<|>))
35−import Control.Monad (foldM)
37+import Control.Monad.Trans (MonadIO (liftIO))
38+import Control.Monad (foldM, when)
3639
3740-- | Type of filter and path to filter file.
3841data Filter = LuaFilter FilePath
@@ -65,8 +68,19 @@ applyFilters ropts filters args d = do
6568 expandedFilters <- mapM expandFilterPath filters
6669 foldM applyFilter d expandedFilters
6770 where
68− applyFilter doc (JSONFilter f) = JSONFilter.apply ropts args f doc
69− applyFilter doc (LuaFilter f) = LuaFilter.apply ropts args f doc
71+ applyFilter doc (JSONFilter f) =
72+ withMessages f $ JSONFilter.apply ropts args f doc
73+ applyFilter doc (LuaFilter f) =
74+ withMessages f $ LuaFilter.apply ropts args f doc
75+ withMessages f action = do
76+ verbosity <- getVerbosity
77+ when (verbosity == INFO) $ report $ RunningFilter f
78+ starttime <- toMilliseconds <$> liftIO getCPUTime
79+ res <- action
80+ endtime <- toMilliseconds <$> liftIO getCPUTime
81+ when (verbosity == INFO) $ report $ FilterCompleted f (endtime - starttime)
82+ return res
83+ toMilliseconds picoseconds = picoseconds `div` 1000000000
7084
7185-- | Expand paths of filters, searching the data directory.
7286expandFilterPath :: Filter -> PandocIO Filter
src/Text/Pandoc/Logging.hs+12−1
@@ -98,6 +98,8 @@ data LogMessage =
9898 | UnexpectedXmlElement Text.Text Text.Text
9999 | UnknownOrgExportOption Text.Text
100100 | CouldNotDeduceFormat [Text.Text] Text.Text
101+ | RunningFilter FilePath
102+ | FilterCompleted FilePath Integer
101103 deriving (Show, Eq, Data, Ord, Typeable, Generic)
102104
103105instance ToJSON LogMessage where
@@ -222,7 +224,11 @@ instance ToJSON LogMessage where
222224 CouldNotDeduceFormat exts format ->
223225 ["extensions" .= exts
224226 ,"format" .= format]
225−
227+ RunningFilter fp ->
228+ ["path" .= Text.pack fp ]
229+ FilterCompleted fp ms ->
230+ ["path" .= Text.pack fp
231+ ,"milliseconds" .= Text.pack (show ms) ]
226232
227233showPos :: SourcePos -> Text.Text
228234showPos pos = Text.pack $ sn ++ "line " ++
@@ -331,6 +337,9 @@ showLogMessage msg =
331337 "Could not deduce format from file extension " <>
332338 Text.intercalate " or " exts <> "\n" <>
333339 "Defaulting to " <> format
340+ RunningFilter fp -> "Running filter " <> Text.pack fp
341+ FilterCompleted fp ms -> "Completed filter " <> Text.pack fp <>
342+ " in " <> Text.pack (show ms) <> " ms"
334343
335344messageVerbosity :: LogMessage -> Verbosity
336345messageVerbosity msg =
@@ -374,3 +383,5 @@ messageVerbosity msg =
374383 UnexpectedXmlElement {} -> WARNING
375384 UnknownOrgExportOption {} -> WARNING
376385 CouldNotDeduceFormat{} -> WARNING
386+ RunningFilter{} -> INFO
387+ FilterCompleted{} -> INFO