"""
CLI argument handling; common stuff: logging
"""
import optparse
import logging
def parser (parser) :
"""
Return an optparse.OptionGroup.
"""
general = optparse.OptionGroup(parser, "General options")
general.add_option('-q', '--quiet', dest='loglevel', action='store_const', const=logging.ERROR, help="Less output")
general.add_option('-v', '--verbose', dest='loglevel', action='store_const', const=logging.INFO, help="More output")
general.add_option('-D', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
general.add_option('--debug-module', action='append', metavar='MODULE',
help="Enable logging for the given logger/module name")
# defaults
parser.set_defaults(
logname = parser.prog,
loglevel = logging.WARN,
debug_module = [],
)
return general
def apply (options, logname=None) :
"""
Apply the optparse options.
"""
if logname :
prefix = options.logname + ': '
else :
prefix = ''
# configure
logging.basicConfig(
# XXX: log Class.__init__ as Class, not __init__?
format = prefix + '%(name)-20s: %(levelname)5s %(funcName)s: %(message)s',
level = options.loglevel,
)
# enable debugging for specific targets
for logger in options.debug_module :
logging.getLogger(logger).setLevel(logging.DEBUG)