diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 8f684270f..39943a061 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -27,6 +27,8 @@ Added - New `:jump-mark` command to jump to a mark, bound to `'` - New `:set-mark` command to set a mark, bound to ```(backtick) - The `'` mark gets set when moving away (hinting link with anchor, searching, etc.) so you can move back with `''` +- New `--force-color` argument to force colored logging even if stdout is not a + terminal Changed ~~~~~~~ diff --git a/doc/qutebrowser.1.asciidoc b/doc/qutebrowser.1.asciidoc index 17d184e0b..b4c0dd094 100644 --- a/doc/qutebrowser.1.asciidoc +++ b/doc/qutebrowser.1.asciidoc @@ -81,6 +81,9 @@ show it. *--nocolor*:: Turn off colored logging. +*--force-color*:: + Force colored logging + *--harfbuzz* '{old,new,system,auto}':: HarfBuzz engine version to use. Default: auto. diff --git a/qutebrowser/qutebrowser.py b/qutebrowser/qutebrowser.py index 6c0b7699f..cd0283c53 100644 --- a/qutebrowser/qutebrowser.py +++ b/qutebrowser/qutebrowser.py @@ -88,6 +88,8 @@ def get_argparser(): action='store_true') debug.add_argument('--nocolor', help="Turn off colored logging.", action='store_false', dest='color') + debug.add_argument('--force-color', help="Force colored logging", + action='store_true') debug.add_argument('--harfbuzz', choices=['old', 'new', 'system', 'auto'], default='auto', help="HarfBuzz engine version to use. " "Default: auto.") diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index aa6a6171a..7fa202221 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -150,7 +150,8 @@ def init_log(args): if numeric_level > logging.DEBUG and args.debug: numeric_level = logging.DEBUG - console, ram = _init_handlers(numeric_level, args.color, args.loglines) + console, ram = _init_handlers(numeric_level, args.color, args.force_color, + args.loglines) root = logging.getLogger() if console is not None: if args.logfilter is not None: @@ -174,22 +175,24 @@ def disable_qt_msghandler(): QtCore.qInstallMessageHandler(old_handler) -def _init_handlers(level, color, ram_capacity): +def _init_handlers(level, color, force_color, ram_capacity): """Init log handlers. Args: level: The numeric logging level. color: Whether to use color if available. + force_color: Force colored output. """ global ram_handler console_fmt, ram_fmt, html_fmt, use_colorama = _init_formatters( - level, color) + level, color, force_color) if sys.stderr is None: console_handler = None else: + strip = False if force_color else None if use_colorama: - stream = colorama.AnsiToWin32(sys.stderr) + stream = colorama.AnsiToWin32(sys.stderr, strip=strip) else: stream = sys.stderr console_handler = logging.StreamHandler(stream) @@ -207,12 +210,13 @@ def _init_handlers(level, color, ram_capacity): return console_handler, ram_handler -def _init_formatters(level, color): +def _init_formatters(level, color, force_color): """Init log formatters. Args: level: The numeric logging level. color: Whether to use color if available. + force_color: Force colored output. Return: A (console_formatter, ram_formatter, use_colorama) tuple. @@ -232,7 +236,7 @@ def _init_formatters(level, color): return None, ram_formatter, html_formatter, False use_colorama = False if (colorlog is not None and (os.name == 'posix' or colorama) and - sys.stderr.isatty() and color): + (sys.stderr.isatty() or force_color) and color): console_formatter = colorlog.ColoredFormatter( console_fmt_colored, DATEFMT, log_colors=LOG_COLORS) if colorama and os.name != 'posix':