removed unnecessary selinux abort condition
[livecd.git] / imgcreate / debug.py
blob84b8d306e45a02232d0c55fdbdafaea086fcb00f
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, stream):
31 try:
32 logfile = logging.FileHandler(val,"a")
33 except IOError, e:
34 raise optparse.OptionValueError("Cannot open file '%s' : %s" %
35 (val, e.strerror))
38 logger.removeHandler(stream)
39 logger.addHandler(logfile)
41 def setup_logging(parser = None):
42 """Set up the root logger and add logging options.
44 Set up the root logger so only warning/error messages are logged to stderr
45 by default.
47 Also, optionally, add --debug, --verbose and --logfile command line options
48 to the supplied option parser, allowing the root logger configuration to be
49 modified by the user.
51 Note, to avoid possible namespace clashes, setup_logging() will only ever
52 add these three options. No new options will be added in the future.
54 parser -- an optparse.OptionParser instance, or None
56 """
57 logger = logging.getLogger()
59 logger.setLevel(logging.WARN)
61 stream = logging.StreamHandler(sys.stderr)
63 logger.addHandler(stream)
65 if parser is None:
66 return
68 group = optparse.OptionGroup(parser, "Debugging options",
69 "These options control the output of logging information during image creation")
71 group.add_option("-d", "--debug",
72 action = "callback", callback = handle_logging,
73 callback_args = (logger, logging.DEBUG),
74 help = "Output debugging information")
76 group.add_option("-v", "--verbose",
77 action = "callback", callback = handle_logging,
78 callback_args = (logger, logging.INFO),
79 help = "Output verbose progress information")
81 group.add_option("", "--logfile", type="string",
82 action = "callback", callback = handle_logfile,
83 callback_args = (logger, stream),
84 help = "Save debug information to FILE", metavar = "FILE")
86 parser.add_option_group(group)