0
|
1 |
"""
|
|
2 |
CLI argument handling; common stuff: logging
|
|
3 |
"""
|
|
4 |
|
|
5 |
import optparse
|
|
6 |
import logging
|
|
7 |
|
|
8 |
def parser (parser) :
|
|
9 |
"""
|
|
10 |
Return an optparse.OptionGroup.
|
|
11 |
"""
|
|
12 |
|
|
13 |
general = optparse.OptionGroup(parser, "General options")
|
|
14 |
|
|
15 |
general.add_option('-q', '--quiet', dest='loglevel', action='store_const', const=logging.ERROR, help="Less output")
|
|
16 |
general.add_option('-v', '--verbose', dest='loglevel', action='store_const', const=logging.INFO, help="More output")
|
|
17 |
general.add_option('-D', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
|
|
18 |
general.add_option('--debug-module', action='append', metavar='MODULE',
|
|
19 |
help="Enable logging for the given logger/module name")
|
|
20 |
|
|
21 |
# defaults
|
|
22 |
parser.set_defaults(
|
|
23 |
logname = parser.prog,
|
|
24 |
loglevel = logging.WARN,
|
|
25 |
debug_module = [],
|
|
26 |
)
|
|
27 |
|
|
28 |
return general
|
|
29 |
|
|
30 |
def apply (options, logname=None) :
|
|
31 |
"""
|
|
32 |
Apply the optparse options.
|
|
33 |
"""
|
|
34 |
|
|
35 |
if logname :
|
|
36 |
prefix = options.logname + ': '
|
|
37 |
else :
|
|
38 |
prefix = ''
|
|
39 |
|
|
40 |
# configure
|
|
41 |
logging.basicConfig(
|
|
42 |
# XXX: log Class.__init__ as Class, not __init__?
|
|
43 |
format = prefix + '%(name)-20s: %(levelname)5s %(funcName)s: %(message)s',
|
|
44 |
level = options.loglevel,
|
|
45 |
)
|
|
46 |
|
|
47 |
# enable debugging for specific targets
|
|
48 |
for logger in options.debug_module :
|
|
49 |
logging.getLogger(logger).setLevel(logging.DEBUG)
|
|
50 |
|