From addd2e74ce2315079858f9dba7aede91e498e842 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sun, 3 Jun 2018 22:50:53 +1200 Subject: [PATCH] Allow negating categories in --logfilter Sometimes I want to see all the logs _except_ for the sql stuff and "marked cookies as dirty". with this you should be able to pass `--logfilter \!sql,save`. --- doc/contributing.asciidoc | 4 ++-- qutebrowser/qutebrowser.py | 2 +- qutebrowser/utils/log.py | 25 +++++++++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/doc/contributing.asciidoc b/doc/contributing.asciidoc index a8f17651a..54f3bc16e 100644 --- a/doc/contributing.asciidoc +++ b/doc/contributing.asciidoc @@ -188,8 +188,8 @@ There are some useful functions for debugging in the `qutebrowser.utils.debug` module. When starting qutebrowser with the `--debug` flag, you also get useful debug -logs. You can add +--logfilter _category[,category,...]_+ to restrict logging -to the given categories. +logs. You can add +--logfilter _[!]category[,category,...]_+ to restrict +logging to the given categories. With `--debug` there are also some additional +debug-_*_+ commands available, for example `:debug-all-objects` and `:debug-all-widgets` which print a list of diff --git a/qutebrowser/qutebrowser.py b/qutebrowser/qutebrowser.py index 292c80c6a..271c7981a 100644 --- a/qutebrowser/qutebrowser.py +++ b/qutebrowser/qutebrowser.py @@ -146,7 +146,7 @@ def logfilter_error(logfilter): Args: logfilter: A comma separated list of logger names. """ - if set(logfilter.split(',')).issubset(log.LOGGER_NAMES): + if set(logfilter.lstrip('!').split(',')).issubset(log.LOGGER_NAMES): return logfilter else: raise argparse.ArgumentTypeError( diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index d6cae1312..6f56f1876 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -182,9 +182,16 @@ def init_log(args): root = logging.getLogger() global console_filter if console is not None: - console_filter = LogFilter(None) - if args.logfilter is not None: - console_filter.names = args.logfilter.split(',') + if not args.logfilter: + negate = False + names = None + elif args.logfilter.startswith('!'): + negate = True + names = args.logfilter[1:].split(',') + else: + negate = False + names = args.logfilter.split(',') + console_filter = LogFilter(names, negate) console.addFilter(console_filter) root.addHandler(console) if ram is not None: @@ -498,12 +505,14 @@ class LogFilter(logging.Filter): comma-separated list instead. Attributes: - _names: A list of names that should be logged. + names: A list of record names to filter. + negated: Wether names is a list of records to log or to suppress. """ - def __init__(self, names): + def __init__(self, names, negate=False): super().__init__() self.names = names + self.negated = negate def filter(self, record): """Determine if the specified record is to be logged.""" @@ -514,12 +523,12 @@ class LogFilter(logging.Filter): return True for name in self.names: if record.name == name: - return True + return not self.negated elif not record.name.startswith(name): continue elif record.name[len(name)] == '.': - return True - return False + return not self.negated + return self.negated class RAMHandler(logging.Handler):