Add commandline argument for debug log buffer size

This commit is contained in:
Florian Bruhin 2014-06-19 12:06:36 +02:00
parent fdda1dd3a0
commit 3435aef78c
3 changed files with 22 additions and 8 deletions

View File

@ -127,7 +127,10 @@ class QuteHandlers:
@classmethod
def qute_log(cls):
"""Handler for qute:log. Return HTML content as bytes."""
text = cgi.escape(log.ram_handler.dump_log())
if log.ram_handler is None:
text = "Log output was disabled."
else:
text = cgi.escape(log.ram_handler.dump_log())
return _get_html('log', '<pre>{}</pre>'.format(text))
@classmethod

View File

@ -42,6 +42,10 @@ def _parse_args():
debug.add_argument('--logfilter',
help="Comma-separated list of things to be logged "
"to the debug log on stdout.")
debug.add_argument('--loglines',
help="How many lines of the debug log to keep in RAM "
"(-1: unlimited).",
default=1000, type=int)
debug.add_argument('--debug', help="Turn on debugging options.",
action='store_true')
debug.add_argument('--nocolor', help="Turn off colored logging.",

View File

@ -85,19 +85,20 @@ def init_log(args):
except AttributeError:
raise ValueError("Invalid log level: {}".format(args.loglevel))
console, ram = _init_handlers(numeric_level, args.color)
console, ram = _init_handlers(numeric_level, args.color, args.loglines)
if args.logfilter is not None and numeric_level <= logging.DEBUG:
console.addFilter(LogFilter(args.logfilter.split(',')))
root = getLogger()
if console is not None:
root.addHandler(console)
root.addHandler(ram)
if ram is not None:
root.addHandler(ram)
root.setLevel(logging.NOTSET)
logging.captureWarnings(True)
qInstallMessageHandler(qt_message_handler)
def _init_handlers(level, color):
def _init_handlers(level, color, ram_capacity):
"""Init log handlers.
Args:
@ -119,9 +120,12 @@ def _init_handlers(level, color):
console_handler.setLevel(level)
console_handler.setFormatter(console_formatter)
ram_handler = RAMHandler(capacity=500)
ram_handler.setLevel(logging.NOTSET)
ram_handler.setFormatter(ram_formatter)
if ram_capacity == 0:
ram_handler = None
else:
ram_handler = RAMHandler(capacity=ram_capacity)
ram_handler.setLevel(logging.NOTSET)
ram_handler.setFormatter(ram_formatter)
return console_handler, ram_handler
@ -264,7 +268,10 @@ class RAMHandler(logging.Handler):
def __init__(self, capacity):
super().__init__()
self.data = deque(maxlen=capacity)
if capacity != -1:
self.data = deque(maxlen=capacity)
else:
self.data = deque()
def emit(self, record):
self.data.append(record)