File: debug.py

package info (click to toggle)
mic2 0.19-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 932 kB
  • ctags: 864
  • sloc: python: 9,292; sh: 1,347; makefile: 62
file content (86 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#
# debug.py: Helper routines for debugging
#
# Copyright 2008, Red Hat  Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import logging
import logging.handlers
import optparse
import sys


def handle_logging(option, opt, val, parser, logger, level):
    if level < logger.level:
        logger.setLevel(level)

def handle_logfile(option, opt, val, parser, logger, stream):
    try:
        logfile = logging.FileHandler(val,"a")
    except IOError, (err, msg):
        raise optparse.OptionValueError("Cannot open file '%s' : %s" %
                                        (val, msg))


    logger.removeHandler(stream)
    logger.addHandler(logfile)

def setup_logging(parser = None):
    """Set up the root logger and add logging options.

    Set up the root logger so only warning/error messages are logged to stderr
    by default.

    Also, optionally, add --debug, --verbose and --logfile command line options
    to the supplied option parser, allowing the root logger configuration to be
    modified by the user.

    Note, to avoid possible namespace clashes, setup_logging() will only ever
    add these three options. No new options will be added in the future.

    parser -- an optparse.OptionParser instance, or None

    """
    logger = logging.getLogger()

    logger.setLevel(logging.WARN)

    stream = logging.StreamHandler(sys.stderr)

    logger.addHandler(stream)

    if parser is None:
        return

    group = optparse.OptionGroup(parser, "Debugging options",
                                 "These options control the output of logging information during image creation")

    group.add_option("-d", "--debug",
                     action = "callback", callback = handle_logging,
                     callback_args = (logger, logging.DEBUG),
                     help = "Output debugging information")

    group.add_option("-v", "--verbose",
                     action = "callback", callback = handle_logging,
                     callback_args = (logger, logging.INFO),
                     help = "Output verbose progress information")

    group.add_option("", "--logfile", type="string",
                     action = "callback", callback = handle_logfile,
                     callback_args = (logger, stream),
                     help = "Save debug information to FILE", metavar = "FILE")

    parser.add_option_group(group)