1 """Control of and utilities for debugging."""
6 # When debugging, it can be helpful to force some options, especially when
7 # debugging the configuration mechanisms you usually use to control debugging!
8 # This is a list of forced debugging options.
12 class DebugControl(object):
13 """Control and output for debugging."""
15 def __init__(self
, options
, output
):
16 """Configure the options and output file for debugging."""
17 self
.options
= options
20 def should(self
, option
):
21 """Decide whether to output debug information in category `option`."""
22 return (option
in self
.options
or option
in FORCED_DEBUG
)
25 """Write a line of debug output."""
26 if self
.should('pid'):
27 msg
= "pid %5d: %s" % (os
.getpid(), msg
)
28 self
.output
.write(msg
+"\n")
31 def write_formatted_info(self
, info
):
32 """Write a sequence of (label,data) pairs nicely."""
33 for line
in info_formatter(info
):
34 self
.write(" %s" % line
)
37 def info_formatter(info
):
38 """Produce a sequence of formatted lines from info.
40 `info` is a sequence of pairs (label, data). The produced lines are
41 nicely formatted, ready to print.
44 label_len
= max([len(l
) for l
, _d
in info
])
45 for label
, data
in info
:
48 if isinstance(data
, (list, tuple)):
49 prefix
= "%*s:" % (label_len
, label
)
51 yield "%*s %s" % (label_len
+1, prefix
, e
)
54 yield "%*s: %s" % (label_len
, label
, data
)