switch to using rd.live.image instead of liveimg
[livecd.git] / imgcreate / debug.py
bloba3c5cb93bf43ac9ae297704bf0a9b83b1b92da6c
2 # debug.py: Helper routines for debugging
4 # Copyright 2008, Red Hat Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Library General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 import logging
21 import logging.handlers
22 import optparse
23 import sys
26 def handle_logging(option, opt, val, parser, logger, level):
27 if level < logger.level:
28 logger.setLevel(level)
30 def handle_logfile(option, opt, val, parser, logger):
32 try:
33 logfile = logging.FileHandler(val,"a")
34 except IOError, e:
35 raise optparse.OptionValueError("Cannot open file '%s' : %s" %
36 (val, e.strerror))
37 logger.addHandler(logfile)
39 def handle_quiet(option, opt, val, parser, logger, stream):
40 logger.removeHandler(stream)
42 def setup_logging(parser = None):
43 """Set up the root logger and add logging options.
45 Set up the root logger so only warning/error messages are logged to stderr
46 by default.
48 Also, optionally, add --debug, --verbose and --logfile command line options
49 to the supplied option parser, allowing the root logger configuration to be
50 modified by the user.
52 Note, to avoid possible namespace clashes, setup_logging() will only ever
53 add these three options. No new options will be added in the future.
55 parser -- an optparse.OptionParser instance, or None
57 """
58 logger = logging.getLogger()
60 logger.setLevel(logging.WARN)
62 stream = logging.StreamHandler(sys.stderr)
64 logger.addHandler(stream)
66 if parser is None:
67 return
69 group = optparse.OptionGroup(parser, "Debugging options",
70 "These options control the output of logging information during image creation")
72 group.add_option("-d", "--debug",
73 action = "callback", callback = handle_logging,
74 callback_args = (logger, logging.DEBUG),
75 help = "Output debugging information")
77 group.add_option("-v", "--verbose",
78 action = "callback", callback = handle_logging,
79 callback_args = (logger, logging.INFO),
80 help = "Output verbose progress information")
82 group.add_option("-q", "--quiet",
83 action = "callback", callback = handle_quiet,
84 callback_args = (logger, stream),
85 help = "Supress stdout")
87 group.add_option("", "--logfile", type="string",
88 action = "callback", callback = handle_logfile,
89 callback_args = (logger,),
90 help = "Save debug information to FILE", metavar = "FILE")
92 parser.add_option_group(group)