From 1d5f3a44d5d9e2599b016edcb57b5b2ba1e5dcdd Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 30 Jun 2017 19:14:57 +0100 Subject: [PATCH] lib: debug: add a log0 that writes to debug.log in the current directory Seems to work somewhat, might have some laziness issues. --- hledger-lib/Hledger/Utils/Debug.hs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Utils/Debug.hs b/hledger-lib/Hledger/Utils/Debug.hs index 6c65135b3..24b11b0ba 100644 --- a/hledger-lib/Hledger/Utils/Debug.hs +++ b/hledger-lib/Hledger/Utils/Debug.hs @@ -151,7 +151,7 @@ dbg9IO :: (MonadIO m, Show a) => String -> a -> m () dbg9IO = tracePrettyAtIO 9 -- | Pretty-print a message and a showable value to the console if the debug level is at or above the specified level. --- dbtAt 0 always prints. Otherwise, uses unsafePerformIO. +-- At level 0, always prints. Otherwise, uses unsafePerformIO. tracePrettyAt :: Show a => Int -> String -> a -> a tracePrettyAt lvl = dbgppshow lvl @@ -171,6 +171,25 @@ tracePrettyAt lvl = dbgppshow lvl tracePrettyAtIO :: (MonadIO m, Show a) => Int -> String -> a -> m () tracePrettyAtIO lvl lbl x = liftIO $ tracePrettyAt lvl lbl x `seq` return () +log0 :: Show a => String -> a -> a +log0 = logPrettyAt 0 + +-- | Log a message and a pretty-printed showable value to ./debug.log, +-- if the debug level is at or above the specified level. +-- At level 0, always logs. Otherwise, uses unsafePerformIO. +logPrettyAt :: Show a => Int -> String -> a -> a +logPrettyAt lvl + | lvl > 0 && debugLevel < lvl = flip const + | otherwise = \s a -> + let p = ppShow a + ls = lines p + nlorspace | length ls > 1 = "\n" + | otherwise = " " ++ take (10 - length s) (repeat ' ') + ls' | length ls > 1 = map (" "++) ls + | otherwise = ls + output = s++":"++nlorspace++intercalate "\n" ls' + in unsafePerformIO $ appendFile "debug.log" output >> return a + -- | print this string to the console before evaluating the expression, -- if the global debug level is at or above the specified level. Uses unsafePerformIO. -- dbgtrace :: Int -> String -> a -> a