Coverage for qutebrowser/utils/log.py : 68%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org> # # This file is part of qutebrowser. # # qutebrowser 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, either version 3 of the License, or # (at your option) any later version. # # qutebrowser 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
# Optional imports except ImportError: colorama = None
for i, color in enumerate(COLORS, start=30)}
# Log formats to use. '{message}') '{log_color}{levelname:8}{reset} ' '{cyan}{name:10} {module}:{funcName}:{lineno}{reset} ' '{log_color}{message}{reset}') '<tr>' '<td><pre>%(green)s%(asctime)-8s%(reset)s</pre></td>' '<td><pre>%(log_color)s%(levelname)-8s%(reset)s</pre></td>' '<td></pre>%(cyan)s%(name)-10s</pre></td>' '<td><pre>%(cyan)s%(module)s:%(funcName)s:%(lineno)s%(reset)s</pre></td>' '<td><pre>%(log_color)s%(message)s%(reset)s</pre></td>' '</tr>' ) 'VDEBUG': 'white', 'DEBUG': 'white', 'INFO': 'green', 'WARNING': 'yellow', 'ERROR': 'red', 'CRITICAL': 'red', }
# We first monkey-patch logging to support our VDEBUG level before getting the # loggers. Based on http://stackoverflow.com/a/13638084
'VDEBUG': logging.VDEBUG, 'DEBUG': logging.DEBUG, 'INFO': logging.INFO, 'WARNING': logging.WARNING, 'ERROR': logging.ERROR, 'CRITICAL': logging.CRITICAL, }
'statusbar', 'completion', 'init', 'url', 'destroy', 'modes', 'webview', 'misc', 'mouse', 'procs', 'hints', 'keyboard', 'commands', 'signals', 'downloads', 'js', 'qt', 'rfc6266', 'ipc', 'shlexer', 'save', 'message', 'config', 'sessions', 'webelem', 'prompt', 'network', 'sql', 'greasemonkey' ]
"""Log with a VDEBUG level.
VDEBUG is used when a debug message is rather verbose, and probably of little use to the end user or for post-mortem debugging, i.e. the content probably won't change unless the code changes. """ # pylint: disable=protected-access # pylint: enable=protected-access
# The different loggers used.
"""Show a STUB: message for the calling function.""" except IndexError: # pragma: no cover misc.exception("Failed to get stack") function = '<unknown>'
"""Init loggers based on the argparse namespace passed.""" except AttributeError: raise ValueError("Invalid log level: {}".format(args.loglevel))
numeric_level = logging.DEBUG
args.json_logging, args.loglines) global console_filter console_filter = LogFilter(None) if args.logfilter is not None: console_filter.names = args.logfilter.split(',') console.addFilter(console_filter) root.addHandler(console) global _log_inited, _args
"""Initialize Python warning handling."""
def disable_qt_msghandler(): """Contextmanager which temporarily disables the Qt message handler.""" finally:
def ignore_py_warnings(**kwargs): """Contextmanager to temporarily disable certain Python warnings."""
"""Init log handlers.
Args: level: The numeric logging level. color: Whether to use color if available. force_color: Force colored output. json_logging: Output log lines in JSON (this disables all colors). """ global ram_handler global console_handler level, color, force_color, json_logging)
else: strip = False if force_color else None if use_colorama: stream = colorama.AnsiToWin32(sys.stderr, strip=strip) else: stream = sys.stderr console_handler = logging.StreamHandler(stream) console_handler.setLevel(level) console_handler.setFormatter(console_fmt)
ram_handler = None else:
"""Get the log format the console logger should use.
Args: level: The numeric logging level.
Return: Format of the requested level. """
"""Init log formatters.
Args: level: The numeric logging level. color: Whether to use color if available. force_color: Force colored output. json_logging: Format lines as JSON (disables all color).
Return: A (console_formatter, ram_formatter, use_colorama) tuple. console_formatter/ram_formatter: logging.Formatter instances. use_colorama: Whether to use colorama. """ use_colors=False) log_colors=LOG_COLORS)
if json_logging: console_formatter = JSONFormatter() return console_formatter, ram_formatter, html_formatter, False
use_colorama = False color_supported = os.name == 'posix' or colorama
if color_supported and (sys.stderr.isatty() or force_color) and color: use_colors = True if colorama and os.name != 'posix': use_colorama = True else: use_colors = False
console_formatter = ColoredFormatter(console_fmt, DATEFMT, '{', use_colors=use_colors) return console_formatter, ram_formatter, html_formatter, use_colorama
"""Change console formatter based on level.
Args: level: The numeric logging level """ if not isinstance(console_handler.formatter, ColoredFormatter): # JSON Formatter being used for end2end tests pass
use_colors = console_handler.formatter.use_colors console_fmt = get_console_format(level) console_formatter = ColoredFormatter(console_fmt, DATEFMT, '{', use_colors=use_colors) console_handler.setFormatter(console_formatter)
"""Qt message handler to redirect qWarning etc. to the logging system.
Args: QtMsgType msg_type: The level of the message. QMessageLogContext context: The source code location of the message. msg: The message text. """ # Mapping from Qt logging levels to the matching logging module levels. # Note we map critical to ERROR as it's actually "just" an error, and fatal # to critical. QtCore.QtDebugMsg: logging.DEBUG, QtCore.QtWarningMsg: logging.WARNING, QtCore.QtCriticalMsg: logging.ERROR, QtCore.QtFatalMsg: logging.CRITICAL, } except AttributeError: # While we don't support Qt < 5.5 anymore, logging still needs to work pass
# Change levels of some well-known messages to debug so they don't get # shown to the user. # # If a message starts with any text in suppressed_msgs, it's not logged as # error. # PNGs in Qt with broken color profile # https://bugreports.qt.io/browse/QTBUG-39788 ('libpng warning: iCCP: Not recognizing known sRGB profile that has ' 'been edited'), 'libpng warning: iCCP: known incorrect sRGB profile', # Hopefully harmless warning 'OpenType support missing for script ', # Error if a QNetworkReply gets two different errors set. Harmless Qt # bug on some pages. # https://bugreports.qt.io/browse/QTBUG-30298 ('QNetworkReplyImplPrivate::error: Internal problem, this method must ' 'only be called once.'), # Sometimes indicates missing text, but most of the time harmless 'load glyph failed ', # Harmless, see https://bugreports.qt.io/browse/QTBUG-42479 ('content-type missing in HTTP POST, defaulting to ' 'application/x-www-form-urlencoded. ' 'Use QNetworkRequest::setHeader() to fix this problem.'), # https://bugreports.qt.io/browse/QTBUG-43118 'Using blocking call!', # Hopefully harmless ('"Method "GetAll" with signature "s" on interface ' '"org.freedesktop.DBus.Properties" doesn\'t exist'), ('"Method \\"GetAll\\" with signature \\"s\\" on interface ' '\\"org.freedesktop.DBus.Properties\\" doesn\'t exist\\n"'), 'WOFF support requires QtWebKit to be built with zlib support.', # Weird Enlightment/GTK X extensions 'QXcbWindow: Unhandled client message: "_E_', 'QXcbWindow: Unhandled client message: "_ECORE_', 'QXcbWindow: Unhandled client message: "_GTK_', # Happens on AppVeyor CI 'SetProcessDpiAwareness failed:', # https://bugreports.qt.io/browse/QTBUG-49174 ('QObject::connect: Cannot connect (null)::stateChanged(' 'QNetworkSession::State) to ' 'QNetworkReplyHttpImpl::_q_networkSessionStateChanged(' 'QNetworkSession::State)'), # https://bugreports.qt.io/browse/QTBUG-53989 ("Image of format '' blocked because it is not considered safe. If " "you are sure it is safe to do so, you can white-list the format by " "setting the environment variable QTWEBKIT_IMAGEFORMAT_WHITELIST="), # Installing Qt from the installer may cause it looking for SSL3 or # OpenSSL 1.0 which may not be available on the system "QSslSocket: cannot resolve ", "QSslSocket: cannot call unresolved function ", # When enabling debugging with QtWebEngine ("Remote debugging server started successfully. Try pointing a " "Chromium-based browser to "), # https://github.com/qutebrowser/qutebrowser/issues/1287 "QXcbClipboard: SelectionRequest too old", # https://github.com/qutebrowser/qutebrowser/issues/2071 'QXcbWindow: Unhandled client message: ""', # https://codereview.qt-project.org/176831 "QObject::disconnect: Unexpected null parameter", ] # not using utils.is_mac here, because we can't be sure we can successfully # import the utils module here. 'libpng warning: iCCP: known incorrect sRGB profile', # https://bugreports.qt.io/browse/QTBUG-47154 ('virtual void QSslSocketBackendPrivate::transmit() SSLRead ' 'failed with: -9805'), ]
level = logging.DEBUG else:
elif ':' in context.function: func = '"{}"'.format(context.function) else: func = context.function
else: name = 'qt-' + context.category 'could not find or load the Qt platform plugin ' '"xcb".'): # Handle this message specially. msg += ("\n\nOn Archlinux, this should fix the problem:\n" " pacman -S libxkbcommon-x11") faulthandler.disable()
else: stack = None None, func, sinfo=stack)
"""Hide Qt warnings matching the given regex.""" finally:
"""Filter to filter Qt warnings.
Attributes: _pattern: The start of the message. """
"""Determine if the specified record is to be logged."""
"""Filter to filter log records based on the commandline argument.
The default Filter only supports one name to show - we support a comma-separated list instead.
Attributes: _names: A list of names that should be logged. """
"""Determine if the specified record is to be logged.""" # More important than DEBUG, so we won't filter at all
"""Logging handler which keeps the messages in a deque in RAM.
Loosely based on logging.BufferingHandler which is unsuitable because it uses a simple list rather than a deque.
Attributes: _data: A deque containing the logging records. """
else: self._data = collections.deque()
# We don't log VDEBUG to RAM.
"""Dump the complete formatted log data as string.
FIXME: We should do all the HTML formatter via jinja2. (probably obsolete when moving to a widget for logging, https://github.com/qutebrowser/qutebrowser/issues/34 """ finally:
self._data = collections.deque(self._data, maxlen=capacity)
"""Logging formatter to output colored logs.
Attributes: use_colors: Whether to do colored logging or not. """
if self.use_colors: color_dict = dict(COLOR_ESCAPES) color_dict['reset'] = RESET_ESCAPE log_color = LOG_COLORS[record.levelname] color_dict['log_color'] = COLOR_ESCAPES[log_color] else: color_dict = {color: '' for color in COLOR_ESCAPES} color_dict['reset'] = '' color_dict['log_color'] = '' record.__dict__.update(color_dict) return super().format(record)
"""Formatter for HTML-colored log messages.
Attributes: _log_colors: The colors to use for logging levels. _colordict: The colordict passed to the logger. """
"""Constructor.
Args: fmt: The format string to use. datefmt: The date format to use. log_colors: The colors to use for logging levels. """ # We could solve this nicer by using CSS, but for this simple case this # works.
record.__dict__.update(self._colordict) if record.levelname in self._log_colors: color = self._log_colors[record.levelname] record.log_color = self._colordict[color] else: record.log_color = '' for field in ['msg', 'filename', 'funcName', 'levelname', 'module', 'name', 'pathname', 'processName', 'threadName']: data = str(getattr(record, field)) setattr(record, field, pyhtml.escape(data)) msg = super().format(record) if not msg.endswith(self._colordict['reset']): msg += self._colordict['reset'] return msg
out = super().formatTime(record, datefmt) return pyhtml.escape(out)
"""Formatter for JSON-encoded log messages."""
obj = {} for field in ['created', 'msecs', 'levelname', 'name', 'module', 'funcName', 'lineno', 'levelno']: obj[field] = getattr(record, field) obj['message'] = record.getMessage() if record.exc_info is not None: obj['traceback'] = super().formatException(record.exc_info) return json.dumps(obj) |