From 1d5a3a617572f1c53f84da885753164463a4ef7f Mon Sep 17 00:00:00 2001 From: knaggita Date: Tue, 9 Aug 2016 14:56:26 +0300 Subject: [PATCH] Add :debug-loglevel :debug-logfilter commands --- qutebrowser/misc/utilcmds.py | 29 +++++++++++++++++++++++++++++ qutebrowser/utils/log.py | 26 +++++++++++++++++++++++++- tests/end2end/features/misc.feature | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/qutebrowser/misc/utilcmds.py b/qutebrowser/misc/utilcmds.py index e99ad45fb..e192cecda 100644 --- a/qutebrowser/misc/utilcmds.py +++ b/qutebrowser/misc/utilcmds.py @@ -256,3 +256,32 @@ def window_only(current_win_id): for win_id, window in objreg.window_registry.items(): if win_id != current_win_id: window.close() + + +@cmdutils.register(debug=True) +@cmdutils.argument('level', choices=[level.lower() + for level in log.LOG_LEVELS]) +def debug_log_level(level: str): + """Change the log level for console logging. + + Args: + level: log level for console log. + """ + log.console_handler.setLevel(log.LOG_LEVELS[level.upper()]) + + +@cmdutils.register(debug=True) +def debug_log_filter(filter_names: str): + """Change the log filter for console logging. + + Args: + filter_names: log filters for console log. + """ + if set(filter_names.split(',')).issubset(log.LOGGER_NAMES): + log.console_handler.removeFilter(log.console_filter) + log.console_filter = log.LogFilter(filter_names.split(',')) + log.console_handler.addFilter(log.console_filter) + else: + raise cmdexc.CommandError("Invalid argument, {} choose from {}". + format( + filter_names, ','.join(log.LOGGER_NAMES))) diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index 177c263f8..6fd9c4d62 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -87,6 +87,15 @@ LOG_LEVELS = { 'CRITICAL': logging.CRITICAL, } +LOGGER_NAMES = [ + 'statusbar', 'completion', 'init', 'url', + 'destroy', 'modes', 'webview', 'misc', + 'mouse', 'procs', 'hints', 'keyboard', + 'commands', 'signals', 'downloads', + 'js', 'qt', 'rfc6266', 'ipc', 'shlexer', + 'save', 'message', 'config', 'sessions' +] + def vdebug(self, msg, *args, **kwargs): """Log with a VDEBUG level. @@ -131,6 +140,8 @@ sessions = logging.getLogger('sessions') ram_handler = None +console_handler = None +console_filter = None def stub(suffix=''): @@ -149,6 +160,7 @@ class CriticalQtWarning(Exception): def init_log(args): """Init loggers based on the argparse namespace passed.""" + global console level = args.loglevel.upper() try: numeric_level = getattr(logging, level) @@ -161,9 +173,11 @@ def init_log(args): console, ram = _init_handlers(numeric_level, args.color, args.force_color, args.json_logging, args.loglines) root = logging.getLogger() + global console_filter if console is not None: if args.logfilter is not None: - console.addFilter(LogFilter(args.logfilter.split(','))) + console_filter = LogFilter(args.logfilter.split(',')) + console.addFilter(console_filter) root.addHandler(console) if ram is not None: root.addHandler(ram) @@ -175,6 +189,10 @@ def init_log(args): _log_inited = True +def change(filters): + console.addFilter(LogFilter(filters.split(','))) + + def _init_py_warnings(): """Initialize Python warning handling.""" warnings.simplefilter('default') @@ -210,6 +228,7 @@ def _init_handlers(level, color, force_color, json_logging, ram_capacity): json_logging: Output log lines in JSON (this disables all colors). """ global ram_handler + global console_handler console_fmt, ram_fmt, html_fmt, use_colorama = _init_formatters( level, color, force_color, json_logging) @@ -236,6 +255,11 @@ def _init_handlers(level, color, force_color, json_logging, ram_capacity): return console_handler, ram_handler +def change_loglevel(level): + value = LOG_LEVELS[level.upper()] + console_handler.setLevel(value) + + def _init_formatters(level, color, force_color, json_logging): """Init log formatters. diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index f8414999e..c61924de9 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -474,7 +474,7 @@ Feature: Various utility commands. ## https://github.com/The-Compiler/qutebrowser/issues/1523 Scenario: Completing a single option argument - When I run :set-cmd-text -s :-- + When I run :set-cmd-text -s :-- Then no crash should happen ## https://github.com/The-Compiler/qutebrowser/issues/1386