From 13641eab666ff931a4c5984d4e792d8c198cdf25 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 6 Dec 2013 13:51:19 -0800 Subject: [PATCH] debug level sets amount/journal show verbosity Amounts and journal values are often rendered too verbosely in debug output, obscuring what's important. Here we try adjusting the level of detail in the Show instance based on the global debug level. Needs more work. --- hledger-lib/Hledger/Data/Amount.hs | 25 ++++++++++++++++++------- hledger-lib/Hledger/Data/Journal.hs | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index af3947693..e866c582d 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -115,7 +115,14 @@ amountstyle = AmountStyle L False 0 '.' ',' [] ------------------------------------------------------------------------------- -- Amount -instance Show Amount where show = showAmountDebug +instance Show Amount where + show _a@Amount{..} + -- debugLevel < 3 = showAmountWithoutPrice a + -- debugLevel < 6 = showAmount a + | debugLevel < 9 = + printf "Amount {acommodity=%s, aquantity=%s, ..}" (show acommodity) (show aquantity) + | otherwise = --showAmountDebug a + printf "Amount {acommodity=%s, aquantity=%s, aprice=%s, astyle=%s}" (show acommodity) (show aquantity) (showPriceDebug aprice) (show astyle) instance Num Amount where abs a@Amount{aquantity=q} = a{aquantity=abs q} @@ -229,11 +236,11 @@ setAmountPrecision p a@Amount{astyle=s} = a{astyle=s{asprecision=p}} withPrecision :: Amount -> Int -> Amount withPrecision = flip setAmountPrecision --- | Get the unambiguous string representation of an amount, for debugging. +-- | Get a string representation of an amount for debugging, +-- appropriate to the current debug level. 9 shows maximum detail. showAmountDebug :: Amount -> String showAmountDebug Amount{acommodity="AUTO"} = "(missing)" -showAmountDebug Amount{..} = printf "Amount {acommodity=%s, aquantity=%s, aprice=%s, astyle=%s}" - (show acommodity) (show aquantity) (showPriceDebug aprice) (show astyle) +showAmountDebug Amount{..} = printf "Amount {acommodity=%s, aquantity=%s, aprice=%s, astyle=%s}" (show acommodity) (show aquantity) (showPriceDebug aprice) (show astyle) -- | Get the string representation of an amount, without any \@ price. showAmountWithoutPrice :: Amount -> String @@ -323,7 +330,11 @@ canonicaliseAmount styles a@Amount{acommodity=c, astyle=s} = a{astyle=s'} ------------------------------------------------------------------------------- -- MixedAmount -instance Show MixedAmount where show = showMixedAmountDebug +instance Show MixedAmount where + show + -- debugLevel < 3 = intercalate "\\n" . lines . showMixedAmountWithoutPrice + -- debugLevel < 6 = intercalate "\\n" . lines . showMixedAmount + | otherwise = showMixedAmountDebug instance Num MixedAmount where fromInteger i = Mixed [fromInteger i] @@ -450,9 +461,9 @@ isReallyZeroMixedAmountCost = isReallyZeroMixedAmount . costOfMixedAmount showMixedAmount :: MixedAmount -> String showMixedAmount m = vConcatRightAligned $ map showAmount $ amounts $ normaliseMixedAmountPreservingFirstPrice m --- | Compact labelled trace of a mixed amount. +-- | Compact labelled trace of a mixed amount, for debugging. ltraceamount :: String -> MixedAmount -> MixedAmount -ltraceamount s = tracewith (((s ++ ": ") ++).showMixedAmount) +ltraceamount s = traceWith (((s ++ ": ") ++).showMixedAmount) -- | Set the display precision in the amount's commodities. setMixedAmountPrecision :: Int -> MixedAmount -> MixedAmount diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index be5569da8..7fa4813dc 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -76,7 +76,21 @@ import Hledger.Query instance Show Journal where - show j = printf "Journal %s with %d transactions, %d accounts: %s, commodity styles: %s" + show j + | debugLevel < 3 = printf "Journal %s with %d transactions, %d accounts" + (journalFilePath j) + (length (jtxns j) + + length (jmodifiertxns j) + + length (jperiodictxns j)) + (length accounts) + | debugLevel < 6 = printf "Journal %s with %d transactions, %d accounts: %s" + (journalFilePath j) + (length (jtxns j) + + length (jmodifiertxns j) + + length (jperiodictxns j)) + (length accounts) + (show accounts) + | otherwise = printf "Journal %s with %d transactions, %d accounts: %s, commodity styles: %s" (journalFilePath j) (length (jtxns j) + length (jmodifiertxns j) +