2018-07-31 11:30:08 +03:00
|
|
|
{-# LANGUAGE FlexibleContexts, TypeFamilies #-}
|
2020-12-10 00:23:30 +03:00
|
|
|
{- |
|
|
|
|
|
|
|
|
Helpers for debug output and pretty-printing
|
|
|
|
(using pretty-simple, with which there may be some overlap).
|
2020-12-10 00:46:46 +03:00
|
|
|
This module also exports Debug.Trace.
|
2020-12-10 00:23:30 +03:00
|
|
|
|
|
|
|
@dbg0@-@dbg9@ will pretty-print values to stderr
|
|
|
|
if the program was run with a sufficiently high @--debug=N@ argument.
|
|
|
|
(@--debug@ with no argument means @--debug=1@; @dbg0@ always prints).
|
|
|
|
|
|
|
|
The @debugLevel@ global is set once at startup using unsafePerformIO.
|
|
|
|
In GHCI, this happens only on the first run of :main, so if you want
|
|
|
|
to change the debug level without restarting GHCI,
|
|
|
|
save a dummy change in Debug.hs and do a :reload.
|
|
|
|
(Sometimes it's more convenient to temporarily add dbg0's and :reload.)
|
|
|
|
|
2020-12-27 04:11:38 +03:00
|
|
|
In hledger, debug levels are used as follows:
|
|
|
|
|
|
|
|
Debug level: What to show:
|
|
|
|
------------ ---------------------------------------------------------
|
|
|
|
0 normal command output only (no warnings, eg)
|
|
|
|
1 (--debug) useful warnings, most common troubleshooting info, eg valuation
|
|
|
|
2 common troubleshooting info, more detail
|
|
|
|
3 report options selection
|
|
|
|
4 report generation
|
|
|
|
5 report generation, more detail
|
|
|
|
6 input file reading
|
|
|
|
7 input file reading, more detail
|
|
|
|
8 command line parsing
|
|
|
|
9 any other rarely needed / more in-depth info
|
|
|
|
|
2020-06-15 03:17:09 +03:00
|
|
|
-}
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
-- more:
|
|
|
|
-- http://hackage.haskell.org/packages/archive/TraceUtils/0.1.0.2/doc/html/Debug-TraceUtils.html
|
|
|
|
-- http://hackage.haskell.org/packages/archive/trace-call/0.1/doc/html/Debug-TraceCall.html
|
|
|
|
-- http://hackage.haskell.org/packages/archive/htrace/0.1/doc/html/Debug-HTrace.html
|
|
|
|
-- http://hackage.haskell.org/packages/archive/traced/2009.7.20/doc/html/Debug-Traced.html
|
|
|
|
|
|
|
|
module Hledger.Utils.Debug (
|
2020-12-10 00:46:46 +03:00
|
|
|
-- * Pretty printing
|
2018-07-16 17:28:58 +03:00
|
|
|
pprint
|
|
|
|
,pshow
|
2020-12-10 00:46:46 +03:00
|
|
|
-- * Tracing
|
2018-07-16 17:28:58 +03:00
|
|
|
,traceWith
|
2020-12-10 00:46:46 +03:00
|
|
|
-- * Pretty tracing
|
|
|
|
,ptrace
|
|
|
|
-- ** Debug-level-aware tracing
|
|
|
|
,debugLevel
|
2020-03-12 05:08:05 +03:00
|
|
|
,traceAt
|
|
|
|
,traceAtWith
|
2018-07-16 17:28:58 +03:00
|
|
|
,ptraceAt
|
2019-06-02 00:53:26 +03:00
|
|
|
,ptraceAtWith
|
2020-12-10 00:46:46 +03:00
|
|
|
-- ** Easiest form (recommended)
|
2018-07-16 17:28:58 +03:00
|
|
|
,dbg0
|
|
|
|
,dbg1
|
|
|
|
,dbg2
|
|
|
|
,dbg3
|
|
|
|
,dbg4
|
|
|
|
,dbg5
|
|
|
|
,dbg6
|
|
|
|
,dbg7
|
|
|
|
,dbg8
|
|
|
|
,dbg9
|
2020-12-10 00:46:46 +03:00
|
|
|
,dbgExit
|
|
|
|
-- ** More control
|
2019-06-02 00:53:26 +03:00
|
|
|
,dbg0With
|
|
|
|
,dbg1With
|
|
|
|
,dbg2With
|
|
|
|
,dbg3With
|
|
|
|
,dbg4With
|
|
|
|
,dbg5With
|
|
|
|
,dbg6With
|
|
|
|
,dbg7With
|
|
|
|
,dbg8With
|
|
|
|
,dbg9With
|
2020-12-10 00:46:46 +03:00
|
|
|
-- ** For standalone lines in IO blocks
|
2018-07-16 17:28:58 +03:00
|
|
|
,ptraceAtIO
|
|
|
|
,dbg0IO
|
|
|
|
,dbg1IO
|
|
|
|
,dbg2IO
|
|
|
|
,dbg3IO
|
|
|
|
,dbg4IO
|
|
|
|
,dbg5IO
|
|
|
|
,dbg6IO
|
|
|
|
,dbg7IO
|
|
|
|
,dbg8IO
|
|
|
|
,dbg9IO
|
2020-12-10 00:46:46 +03:00
|
|
|
-- ** Trace the state of hledger parsers
|
2018-07-16 17:28:58 +03:00
|
|
|
,traceParse
|
|
|
|
,dbgparse
|
2014-10-29 03:21:33 +03:00
|
|
|
,module Debug.Trace
|
2021-04-14 03:23:29 +03:00
|
|
|
,useColorOnStdout
|
|
|
|
,useColorOnStderr
|
|
|
|
)
|
2014-10-29 03:21:33 +03:00
|
|
|
where
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
import Control.Monad (when)
|
|
|
|
import Control.Monad.IO.Class
|
|
|
|
import Data.List hiding (uncons)
|
|
|
|
import qualified Data.Text as T
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
import qualified Data.Text.Lazy as TL
|
2016-07-29 18:57:10 +03:00
|
|
|
import Debug.Trace
|
|
|
|
import Hledger.Utils.Parse
|
|
|
|
import Safe (readDef)
|
2021-04-14 03:01:00 +03:00
|
|
|
import System.Environment (getArgs, lookupEnv)
|
2016-07-29 18:57:10 +03:00
|
|
|
import System.Exit
|
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
|
|
|
import Text.Megaparsec
|
|
|
|
import Text.Printf
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
import Text.Pretty.Simple -- (defaultOutputOptionsDarkBg, OutputOptions(..), pShowOpt, pPrintOpt)
|
2021-04-14 03:01:00 +03:00
|
|
|
import Data.Maybe (isJust)
|
|
|
|
import System.Console.ANSI (hSupportsANSIColor)
|
2021-04-14 03:23:29 +03:00
|
|
|
import System.IO (stdout, Handle, stderr)
|
2014-10-29 03:21:33 +03:00
|
|
|
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
prettyopts =
|
2021-04-14 03:06:02 +03:00
|
|
|
baseopts
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
{ outputOptionsIndentAmount=2
|
|
|
|
, outputOptionsCompact=True
|
|
|
|
}
|
2021-04-14 03:06:02 +03:00
|
|
|
where
|
|
|
|
baseopts
|
2021-04-14 03:23:29 +03:00
|
|
|
| useColorOnStderr = defaultOutputOptionsDarkBg -- defaultOutputOptionsLightBg
|
|
|
|
| otherwise = defaultOutputOptionsNoColor
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
|
|
|
|
-- | Pretty print. Generic alias for pretty-simple's pPrint.
|
2016-05-20 18:31:39 +03:00
|
|
|
pprint :: Show a => a -> IO ()
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
pprint = pPrintOpt CheckColorTty prettyopts
|
2018-07-16 14:47:37 +03:00
|
|
|
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
-- | Pretty show. Generic alias for pretty-simple's pShow.
|
2018-07-16 14:47:37 +03:00
|
|
|
pshow :: Show a => a -> String
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
pshow = TL.unpack . pShowOpt prettyopts
|
|
|
|
|
|
|
|
-- XXX some of the below can be improved with pretty-simple, https://github.com/cdepillabout/pretty-simple#readme
|
2016-05-20 18:31:39 +03:00
|
|
|
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
-- | Pretty trace. Easier alias for traceShowId + pShow.
|
2018-07-16 17:28:58 +03:00
|
|
|
ptrace :: Show a => a -> a
|
|
|
|
ptrace = traceWith pshow
|
|
|
|
|
2020-03-12 05:08:05 +03:00
|
|
|
-- | 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
|
2016-09-06 18:31:53 +03:00
|
|
|
traceWith f a = trace (f a) a
|
2014-10-29 03:21:33 +03:00
|
|
|
|
2021-04-14 03:23:29 +03:00
|
|
|
-- | Global debug level, which controls the verbosity of debug errput
|
|
|
|
-- on the console. The default is 0 meaning no debug errput. The
|
|
|
|
-- @--debug@ command line flag sets it to 1, or @--debug=N@ sets it to
|
|
|
|
-- a higher value (note: not @--debug N@ for some reason). This uses
|
|
|
|
-- unsafePerformIO and can be accessed from anywhere and before normal
|
|
|
|
-- command-line processing. When running with :main in GHCI, you must
|
|
|
|
-- touch and reload this module to see the effect of a new --debug option.
|
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
2021-08-16 07:49:40 +03:00
|
|
|
{-# NOINLINE debugLevel #-}
|
2021-04-18 05:06:53 +03:00
|
|
|
-- Avoid using dbg* in this function (infinite loop).
|
2021-04-14 03:23:29 +03:00
|
|
|
debugLevel :: Int
|
2021-08-16 07:25:18 +03:00
|
|
|
debugLevel = case dropWhile (/="--debug") args of
|
|
|
|
["--debug"] -> 1
|
2021-04-14 03:23:29 +03:00
|
|
|
"--debug":n:_ -> readDef 1 n
|
|
|
|
_ ->
|
|
|
|
case take 1 $ filter ("--debug" `isPrefixOf`) args of
|
|
|
|
['-':'-':'d':'e':'b':'u':'g':'=':v] -> readDef 1 v
|
|
|
|
_ -> 0
|
|
|
|
|
|
|
|
where
|
|
|
|
args = unsafePerformIO getArgs
|
|
|
|
|
2021-04-18 05:06:53 +03:00
|
|
|
-- Avoid using dbg*, pshow etc. in this function (infinite loop).
|
2021-04-14 03:23:29 +03:00
|
|
|
-- | Check the IO environment to see if ANSI colour codes should be used on stdout.
|
2021-04-14 03:01:00 +03:00
|
|
|
-- This is done using unsafePerformIO so it can be used anywhere, eg in
|
|
|
|
-- low-level debug utilities, which should be ok since we are just reading.
|
|
|
|
-- The logic is: use color if
|
|
|
|
-- a NO_COLOR environment variable is not defined
|
|
|
|
-- and the program was not started with --color=no|never
|
2021-04-18 05:06:53 +03:00
|
|
|
-- and (
|
|
|
|
-- the program was started with --color=yes|always
|
|
|
|
-- or stdout supports ANSI color and -o/--output-file was not used or is "-"
|
|
|
|
-- ).
|
2021-04-14 03:46:18 +03:00
|
|
|
-- Caveats:
|
|
|
|
-- When running code in GHCI, this module must be reloaded to see a change.
|
2021-04-14 03:01:00 +03:00
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
2021-04-14 03:23:29 +03:00
|
|
|
-- {-# NOINLINE useColorOnStdout #-}
|
|
|
|
useColorOnStdout :: Bool
|
2021-04-18 05:06:53 +03:00
|
|
|
useColorOnStdout = not hasOutputFile && useColorOnHandle stdout
|
2021-04-14 03:23:29 +03:00
|
|
|
|
2021-04-18 05:06:53 +03:00
|
|
|
-- Avoid using dbg*, pshow etc. in this function (infinite loop).
|
|
|
|
-- | Like useColorOnStdout, but checks for ANSI color support on stderr,
|
|
|
|
-- and is not affected by -o/--output-file.
|
2021-04-14 03:23:29 +03:00
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
|
|
|
-- {-# NOINLINE useColorOnStdout #-}
|
|
|
|
useColorOnStderr :: Bool
|
|
|
|
useColorOnStderr = useColorOnHandle stderr
|
|
|
|
|
2021-04-18 05:06:53 +03:00
|
|
|
-- Avoid using dbg*, pshow etc. in this function (infinite loop).
|
2021-04-14 03:23:29 +03:00
|
|
|
-- XXX sorry, I'm just cargo-culting these pragmas:
|
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
|
|
|
-- {-# NOINLINE useColorOnHandle #-}
|
|
|
|
useColorOnHandle :: Handle -> Bool
|
|
|
|
useColorOnHandle h = unsafePerformIO $ do
|
2021-04-14 03:01:00 +03:00
|
|
|
no_color <- isJust <$> lookupEnv "NO_COLOR"
|
2021-04-14 03:23:29 +03:00
|
|
|
supports_color <- hSupportsANSIColor h
|
2021-04-14 03:01:00 +03:00
|
|
|
let coloroption = colorOption
|
2021-08-16 08:15:45 +03:00
|
|
|
return $ not no_color
|
|
|
|
&& coloroption `notElem` ["never","no"]
|
|
|
|
&& (coloroption `elem` ["always","yes"] || supports_color)
|
2021-04-14 03:01:00 +03:00
|
|
|
|
2021-04-18 05:06:53 +03:00
|
|
|
-- Keep synced with color/colour flag definition in hledger:CliOptions.
|
|
|
|
-- Avoid using dbg*, pshow etc. in this function (infinite loop).
|
2021-04-14 03:01:00 +03:00
|
|
|
-- | Read the value of the --color or --colour command line option provided at program startup
|
|
|
|
-- using unsafePerformIO. If this option was not provided, returns the empty string.
|
|
|
|
-- (When running code in GHCI, this module must be reloaded to see a change.)
|
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
|
|
|
-- {-# NOINLINE colorOption #-}
|
|
|
|
colorOption :: String
|
|
|
|
colorOption =
|
|
|
|
-- similar to debugLevel
|
|
|
|
let args = unsafePerformIO getArgs in
|
2021-08-16 07:25:18 +03:00
|
|
|
case dropWhile (/="--color") args of
|
2021-04-18 05:06:20 +03:00
|
|
|
-- --color ARG
|
2021-04-14 03:01:00 +03:00
|
|
|
"--color":v:_ -> v
|
|
|
|
_ ->
|
|
|
|
case take 1 $ filter ("--color=" `isPrefixOf`) args of
|
2021-04-18 05:06:20 +03:00
|
|
|
-- --color=ARG
|
2021-04-14 03:01:00 +03:00
|
|
|
['-':'-':'c':'o':'l':'o':'r':'=':v] -> v
|
|
|
|
_ ->
|
2021-08-16 07:25:18 +03:00
|
|
|
case dropWhile (/="--colour") args of
|
2021-04-18 05:06:20 +03:00
|
|
|
-- --colour ARG
|
2021-04-14 03:01:00 +03:00
|
|
|
"--colour":v:_ -> v
|
|
|
|
_ ->
|
|
|
|
case take 1 $ filter ("--colour=" `isPrefixOf`) args of
|
2021-04-18 05:06:20 +03:00
|
|
|
-- --colour=ARG
|
2021-04-14 03:01:00 +03:00
|
|
|
['-':'-':'c':'o':'l':'o':'u':'r':'=':v] -> v
|
|
|
|
_ -> ""
|
|
|
|
|
2021-04-18 05:06:53 +03:00
|
|
|
-- Avoid using dbg*, pshow etc. in this function (infinite loop).
|
|
|
|
-- | Check whether the -o/--output-file option has been used at program startup
|
|
|
|
-- with an argument other than "-", using unsafePerformIO.
|
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
|
|
|
-- {-# NOINLINE hasOutputFile #-}
|
|
|
|
hasOutputFile :: Bool
|
2021-08-16 07:09:17 +03:00
|
|
|
hasOutputFile = outputFileOption `notElem` [Nothing, Just "-"]
|
2021-04-18 05:06:53 +03:00
|
|
|
|
|
|
|
-- Keep synced with output-file flag definition in hledger:CliOptions.
|
|
|
|
-- Avoid using dbg*, pshow etc. in this function (infinite loop).
|
|
|
|
-- | Read the value of the -o/--output-file command line option provided at program startup,
|
|
|
|
-- if any, using unsafePerformIO.
|
|
|
|
-- (When running code in GHCI, this module must be reloaded to see a change.)
|
|
|
|
-- {-# OPTIONS_GHC -fno-cse #-}
|
|
|
|
-- {-# NOINLINE outputFileOption #-}
|
|
|
|
outputFileOption :: Maybe String
|
|
|
|
outputFileOption =
|
|
|
|
let args = unsafePerformIO getArgs in
|
2021-08-16 07:25:18 +03:00
|
|
|
case dropWhile (not . ("-o" `isPrefixOf`)) args of
|
2021-04-18 05:06:53 +03:00
|
|
|
-- -oARG
|
|
|
|
('-':'o':v@(_:_)):_ -> Just v
|
|
|
|
-- -o ARG
|
|
|
|
"-o":v:_ -> Just v
|
|
|
|
_ ->
|
2021-08-16 07:25:18 +03:00
|
|
|
case dropWhile (/="--output-file") args of
|
2021-04-18 05:06:53 +03:00
|
|
|
-- --output-file ARG
|
|
|
|
"--output-file":v:_ -> Just v
|
|
|
|
_ ->
|
|
|
|
case take 1 $ filter ("--output-file=" `isPrefixOf`) args of
|
|
|
|
-- --output=file=ARG
|
|
|
|
['-':'-':'o':'u':'t':'p':'u':'t':'-':'f':'i':'l':'e':'=':v] -> Just v
|
|
|
|
_ -> Nothing
|
|
|
|
|
2020-03-12 05:08:05 +03:00
|
|
|
-- | 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
|
2021-08-16 08:25:49 +03:00
|
|
|
| level > 0 && debugLevel < level = const id
|
2020-03-12 05:08:05 +03:00
|
|
|
| otherwise = trace
|
|
|
|
|
2021-01-07 19:57:25 +03:00
|
|
|
-- | Trace (print to stderr) a showable value using a custom show function,
|
|
|
|
-- if the global debug level is at or above the specified level.
|
|
|
|
-- At level 0, always prints. Otherwise, uses unsafePerformIO.
|
|
|
|
traceAtWith :: Int -> (a -> String) -> a -> a
|
|
|
|
traceAtWith level f a = traceAt level (f a) a
|
2020-03-12 05:08:05 +03:00
|
|
|
|
2018-07-16 17:28:58 +03:00
|
|
|
-- | 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.
|
|
|
|
ptraceAt :: Show a => Int -> String -> a -> a
|
|
|
|
ptraceAt level
|
2021-08-16 08:25:49 +03:00
|
|
|
| level > 0 && debugLevel < level = const id
|
lib: replace pretty-show with pretty-simple
pretty-simple, already used in .ghci, will hopefully give nicer debug
output, including for values which don't have Read-able Show output.
This should mean that we can start removing custom string-like Show
instances that were a workaround for pretty-show.
We are using the latest version (4.0.0.0) to get compact output.
Here's some old pretty-show output:
CsvRules
{ rdirectives = [ ( "skip" , "1" ) ]
, rcsvfieldindexes = [ ( "date" , 1 ) , ( "amount" , 2 ) ]
, rassignments = [ ( "amount" , "%2" ) , ( "date" , "%1" ) ]
, rconditionalblocks = []
}
And the new pretty-simple output:
CsvRules
{ rdirectives=
[ ( "skip", "1" ) ]
, rcsvfieldindexes=
[ ( "date", 1 ), ( "amount", 2 ) ]
, rassignments=
[ ( "amount", "%2" ), ( "date", "%1" ) ]
, rconditionalblocks= []
}
Non-compact pretty-simple output would be:
CsvRules
{ rdirectives=
[
( "skip"
, "1B"
)
]
, rcsvfieldindexes=
[
( "date"
, 1
)
,
( "amount"
, 2
)
]
, rassignments=
[
( "amount"
, "%2"
)
,
( "date"
, "%1"
)
]
, rconditionalblocks=[]
}
Also:
- Account's Show instance no longer converts : to _ in account names
- drop unused pretty-show dependency from hledger, hledger-ui packages
- regenerate hledger-lib with the older hpack that's shipped in stack
2020-11-10 18:08:29 +03:00
|
|
|
| otherwise = \s a -> let p = pshow a
|
2018-07-16 17:28:58 +03:00
|
|
|
ls = lines p
|
|
|
|
nlorspace | length ls > 1 = "\n"
|
2021-08-16 07:25:18 +03:00
|
|
|
| otherwise = replicate (11 - length s) ' '
|
|
|
|
ls' | length ls > 1 = map (' ':) ls
|
2018-07-16 17:28:58 +03:00
|
|
|
| otherwise = ls
|
|
|
|
in trace (s++":"++nlorspace++intercalate "\n" ls') a
|
2016-10-27 17:19:26 +03:00
|
|
|
|
2019-06-02 00:53:26 +03:00
|
|
|
-- | Like ptraceAt, but takes a custom show function instead of a label.
|
|
|
|
ptraceAtWith :: Show a => Int -> (a -> String) -> a -> a
|
|
|
|
ptraceAtWith level f
|
|
|
|
| level > 0 && debugLevel < level = id
|
|
|
|
| otherwise = \a -> let p = f a
|
|
|
|
-- ls = lines p
|
|
|
|
-- nlorspace | length ls > 1 = "\n"
|
|
|
|
-- | otherwise = " " ++ take (10 - length s) (repeat ' ')
|
|
|
|
-- ls' | length ls > 1 = map (" "++) ls
|
|
|
|
-- | otherwise = ls
|
|
|
|
-- in trace (s++":"++nlorspace++intercalate "\n" ls') a
|
|
|
|
in trace p a
|
|
|
|
|
|
|
|
-- "dbg" would clash with megaparsec.
|
|
|
|
-- | Pretty-print a label and the showable value to the console, then return it.
|
2016-10-27 17:19:26 +03:00
|
|
|
dbg0 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg0 = ptraceAt 0
|
2014-10-29 03:21:33 +03:00
|
|
|
|
2019-06-02 00:53:26 +03:00
|
|
|
-- | Pretty-print a label and the showable value to the console when the global debug level is >= 1, then return it.
|
2018-07-16 17:28:58 +03:00
|
|
|
-- Uses unsafePerformIO.
|
2014-10-29 03:21:33 +03:00
|
|
|
dbg1 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg1 = ptraceAt 1
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg2 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg2 = ptraceAt 2
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg3 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg3 = ptraceAt 3
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg4 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg4 = ptraceAt 4
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg5 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg5 = ptraceAt 5
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg6 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg6 = ptraceAt 6
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg7 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg7 = ptraceAt 7
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg8 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg8 = ptraceAt 8
|
2014-10-29 03:21:33 +03:00
|
|
|
|
|
|
|
dbg9 :: Show a => String -> a -> a
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg9 = ptraceAt 9
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2020-12-10 00:46:46 +03:00
|
|
|
-- | Like dbg0, but also exit the program. Uses unsafePerformIO.
|
|
|
|
dbgExit :: Show a => String -> a -> a
|
|
|
|
dbgExit msg = const (unsafePerformIO exitFailure) . dbg0 msg
|
|
|
|
|
2019-06-02 00:53:26 +03:00
|
|
|
-- | Like dbg0, but takes a custom show function instead of a label.
|
|
|
|
dbg0With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg0With = ptraceAtWith 0
|
|
|
|
|
|
|
|
dbg1With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg1With = ptraceAtWith 1
|
|
|
|
|
|
|
|
dbg2With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg2With = ptraceAtWith 2
|
|
|
|
|
|
|
|
dbg3With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg3With = ptraceAtWith 3
|
|
|
|
|
|
|
|
dbg4With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg4With = ptraceAtWith 4
|
|
|
|
|
|
|
|
dbg5With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg5With = ptraceAtWith 5
|
|
|
|
|
|
|
|
dbg6With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg6With = ptraceAtWith 6
|
|
|
|
|
|
|
|
dbg7With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg7With = ptraceAtWith 7
|
|
|
|
|
|
|
|
dbg8With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg8With = ptraceAtWith 8
|
|
|
|
|
|
|
|
dbg9With :: Show a => (a -> String) -> a -> a
|
|
|
|
dbg9With = ptraceAtWith 9
|
|
|
|
|
2020-02-29 20:42:04 +03:00
|
|
|
-- | Like ptraceAt, but convenient to insert in an IO monad and
|
|
|
|
-- enforces monadic sequencing (plus convenience aliases).
|
2018-07-16 17:28:58 +03:00
|
|
|
-- XXX These have a bug; they should use
|
|
|
|
-- traceIO, not trace, otherwise GHC can occasionally over-optimise
|
2016-12-07 18:52:10 +03:00
|
|
|
-- (cf lpaste a few days ago where it killed/blocked a child thread).
|
2018-07-16 17:28:58 +03:00
|
|
|
ptraceAtIO :: (MonadIO m, Show a) => Int -> String -> a -> m ()
|
|
|
|
ptraceAtIO lvl lbl x = liftIO $ ptraceAt lvl lbl x `seq` return ()
|
|
|
|
|
|
|
|
-- XXX Could not deduce (a ~ ())
|
|
|
|
-- ptraceAtM :: (Monad m, Show a) => Int -> String -> a -> m a
|
|
|
|
-- ptraceAtM lvl lbl x = ptraceAt lvl lbl x `seq` return x
|
|
|
|
|
2016-11-24 21:59:03 +03:00
|
|
|
dbg0IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg0IO = ptraceAtIO 0
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg1IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg1IO = ptraceAtIO 1
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg2IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg2IO = ptraceAtIO 2
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg3IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg3IO = ptraceAtIO 3
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg4IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg4IO = ptraceAtIO 4
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg5IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg5IO = ptraceAtIO 5
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg6IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg6IO = ptraceAtIO 6
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg7IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg7IO = ptraceAtIO 7
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg8IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg8IO = ptraceAtIO 8
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2016-05-20 17:51:51 +03:00
|
|
|
dbg9IO :: (MonadIO m, Show a) => String -> a -> m ()
|
2018-07-16 17:28:58 +03:00
|
|
|
dbg9IO = ptraceAtIO 9
|
2015-05-14 22:49:17 +03:00
|
|
|
|
2018-07-16 17:28:58 +03:00
|
|
|
-- | Print the provided label (if non-null) and current parser state
|
2020-12-10 00:46:46 +03:00
|
|
|
-- (position and next input) to the console. See also megaparsec's dbg.
|
2018-07-16 17:28:58 +03:00
|
|
|
traceParse :: String -> TextParser m ()
|
|
|
|
traceParse msg = do
|
2018-09-30 04:32:08 +03:00
|
|
|
pos <- getSourcePos
|
2018-07-16 17:28:58 +03:00
|
|
|
next <- (T.take peeklength) `fmap` getInput
|
|
|
|
let (l,c) = (sourceLine pos, sourceColumn pos)
|
|
|
|
s = printf "at line %2d col %2d: %s" (unPos l) (unPos c) (show next) :: String
|
|
|
|
s' = printf ("%-"++show (peeklength+30)++"s") s ++ " " ++ msg
|
|
|
|
trace s' $ return ()
|
|
|
|
where
|
|
|
|
peeklength = 30
|
2014-10-29 03:21:33 +03:00
|
|
|
|
2018-07-16 17:28:58 +03:00
|
|
|
-- | Print the provided label (if non-null) and current parser state
|
|
|
|
-- (position and next input) to the console if the global debug level
|
|
|
|
-- is at or above the specified level. Uses unsafePerformIO.
|
|
|
|
-- (See also megaparsec's dbg.)
|
|
|
|
traceParseAt :: Int -> String -> TextParser m ()
|
|
|
|
traceParseAt level msg = when (level <= debugLevel) $ traceParse msg
|
|
|
|
|
|
|
|
-- | Convenience alias for traceParseAt
|
|
|
|
dbgparse :: Int -> String -> TextParser m ()
|
2021-08-16 08:32:12 +03:00
|
|
|
dbgparse = traceParseAt
|
2014-10-29 03:21:33 +03:00
|
|
|
|