From e0c3275d74f641c690bb39ca215a14591ddcff6e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 11 Mar 2020 19:08:05 -0700 Subject: [PATCH] lib: debug helpers traceAt, traceAtWith --- hledger-lib/Hledger/Utils/Debug.hs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Utils/Debug.hs b/hledger-lib/Hledger/Utils/Debug.hs index e2938ce42..fad152fe2 100644 --- a/hledger-lib/Hledger/Utils/Debug.hs +++ b/hledger-lib/Hledger/Utils/Debug.hs @@ -12,6 +12,8 @@ module Hledger.Utils.Debug ( ,pshow ,ptrace ,traceWith + ,traceAt + ,traceAtWith ,debugLevel ,ptraceAt ,ptraceAtWith @@ -81,8 +83,9 @@ pshow = ppShow ptrace :: Show a => a -> a ptrace = traceWith pshow --- | Trace (print to stderr) a showable value using a custom show function. -traceWith :: (a -> String) -> a -> a +-- | Like traceShowId, but uses a custom show function to render the value. +-- traceShowIdWith was too much of a mouthful. +traceWith :: Show a => (a -> String) -> a -> a traceWith f a = trace (f a) a -- | Global debug level, which controls the verbosity of debug output @@ -108,6 +111,18 @@ debugLevel = case snd $ break (=="--debug") args of where args = unsafePerformIO getArgs +-- | Trace (print to stderr) a string if the global debug level is at +-- or above the specified level. At level 0, always prints. Otherwise, +-- uses unsafePerformIO. +traceAt :: Int -> String -> a -> a +traceAt level + | level > 0 && debugLevel < level = flip const + | otherwise = trace + +-- | Trace (print to stderr) a showable value using a custom show function. +traceAtWith :: (a -> String) -> a -> a +traceAtWith f a = trace (f a) a + -- | Pretty-print a label and a showable value to the console -- if the global debug level is at or above the specified level. -- At level 0, always prints. Otherwise, uses unsafePerformIO.