/usr/share/pyshared/mic/utils/debug.py is in mic2 0.24.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #
# 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
import Queue
import threading
import time
import os
real_stdout = sys.stdout
real_stderr = sys.stderr
write_log_worker_thread = None
class Worker:
def __init__(self, queue, out):
self.queue = queue
self.out = out
self.stopevent = threading.Event()
self.stopevent.clear()
self.worker_thread = None
def consumer_queue(self):
while True:
try:
message = self.queue.get_nowait()
except Queue.Empty:
break
self.queue.task_done()
for out in self.out:
out.write(message)
for out in self.out:
out.flush()
def write_log_worker(self):
while True:
if self.stopevent.isSet():
self.consumer_queue()
break
self.consumer_queue()
time.sleep(1)
def start_write_log_worker(self, *args, **kwargs):
self.worker_thread = threading.Thread(target=self.write_log_worker,
args=args, kwargs=kwargs,
name="mic2-worker")
self.worker_thread.setDaemon(False)
self.worker_thread.start()
return self.worker_thread
def terminate_write_log_worker(self):
self.stopevent.set()
self.worker_thread.join()
self.consumer_queue()
for out in self.out:
if not out.isatty():
out.close()
def flush_log(self):
self.consumer_queue()
class QueueWriter():
def __init__(self, queue):
self.queue = queue
self.myfileno = 1
self.mylogworker = None
def write(self, string):
self.queue.put(string)
def close(self):
pass
def flush(self):
if self.mylogworker:
self.mylogworker.flush_log()
def isatty(self):
return False
def fileno(self):
return self.myfileno
_logworker = None
_logfile = None
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):
global _logworker, _logfile
if _logworker and _logfile:
""" Only need to initialize it once """
return
try:
val = os.path.abspath(os.path.expanduser(val))
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)
myout = QueueWriter(Queue.Queue())
myout.myfileno = logfile.stream.fileno()
sys.stdout = myout
sys.stderr = myout
_logworker = Worker(myout.queue, [logfile.stream, real_stdout])
_logworker.start_write_log_worker()
_logfile = val
myout.mylogworker = _logworker
def get_logfile():
return _logfile
def terminate_log_thread():
if _logworker:
_logworker.terminate_write_log_worker()
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)
|