2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2014-05-23 16:11:55 +02:00
|
|
|
# Copyright 2014 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/>.
|
|
|
|
|
|
|
|
"""Loggers and utilities related to logging."""
|
|
|
|
|
2014-06-02 22:43:32 +02:00
|
|
|
import re
|
2014-05-25 20:08:07 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2014-06-25 10:00:27 +02:00
|
|
|
import cgi
|
2014-05-23 16:57:08 +02:00
|
|
|
import logging
|
2014-05-23 16:11:55 +02:00
|
|
|
from logging import getLogger
|
2014-05-25 18:46:15 +02:00
|
|
|
from collections import deque
|
2014-05-23 16:11:55 +02:00
|
|
|
|
2014-06-02 22:43:32 +02:00
|
|
|
from PyQt5.QtCore import (QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg,
|
|
|
|
qInstallMessageHandler)
|
2014-06-06 12:13:32 +02:00
|
|
|
# Optional imports
|
2014-05-25 20:00:48 +02:00
|
|
|
try:
|
|
|
|
# pylint: disable=import-error
|
|
|
|
from colorlog import ColoredFormatter
|
|
|
|
except ImportError:
|
|
|
|
ColoredFormatter = None
|
2014-06-06 12:13:32 +02:00
|
|
|
try:
|
|
|
|
# pylint: disable=import-error
|
|
|
|
import colorama
|
|
|
|
except ImportError:
|
|
|
|
colorama = None
|
2014-05-25 20:00:48 +02:00
|
|
|
|
2014-06-06 12:29:33 +02:00
|
|
|
# Log formats to use.
|
|
|
|
SIMPLE_FMT = '{levelname}: {message}'
|
|
|
|
EXTENDED_FMT = ('{asctime:8} {levelname:8} {name:10} {module}:{funcName}:'
|
|
|
|
'{lineno} {message}')
|
|
|
|
SIMPLE_FMT_COLORED = '%(log_color)s%(levelname)s%(reset)s: %(message)s'
|
2014-06-25 10:00:27 +02:00
|
|
|
EXTENDED_FMT_COLORED = ('%(green)s%(asctime)-8s%(reset)s %(log_color)s'
|
|
|
|
'%(levelname)-8s%(reset)s %(yellow)s%(name)-10s '
|
2014-06-06 12:29:33 +02:00
|
|
|
'%(module)s:%(funcName)s:%(lineno)s%(reset)s '
|
|
|
|
'%(message)s')
|
2014-06-25 10:00:27 +02:00
|
|
|
EXTENDED_FMT_HTML = ('<tr><td><pre>%(green)s%(asctime)-8s%(reset)s</pre></td>'
|
|
|
|
'<td><pre>%(log_color)s%(levelname)-8s%(reset)s</pre>'
|
|
|
|
'</td><td></pre>%(yellow)s%(name)-10s</pre></td>'
|
|
|
|
'<td><pre>%(module)s:%(funcName)s:%(lineno)s%(reset)s'
|
|
|
|
'</pre></td><td><pre>%(message)s</pre></td></tr>')
|
2014-06-06 12:29:33 +02:00
|
|
|
DATEFMT = '%H:%M:%S'
|
2014-06-25 10:00:27 +02:00
|
|
|
LOG_COLORS = {
|
|
|
|
'DEBUG': 'cyan',
|
|
|
|
'INFO': 'green',
|
|
|
|
'WARNING': 'yellow',
|
|
|
|
'ERROR': 'red',
|
|
|
|
'CRITICAL': 'red',
|
|
|
|
}
|
2014-06-06 12:29:33 +02:00
|
|
|
|
|
|
|
|
2014-05-23 16:11:55 +02:00
|
|
|
# The different loggers used.
|
|
|
|
|
|
|
|
statusbar = getLogger('statusbar')
|
|
|
|
completion = getLogger('completion')
|
|
|
|
destroy = getLogger('destroy')
|
|
|
|
modes = getLogger('modes')
|
|
|
|
webview = getLogger('webview')
|
|
|
|
mouse = getLogger('mouse')
|
|
|
|
misc = getLogger('misc')
|
|
|
|
url = getLogger('url')
|
|
|
|
procs = getLogger('procs')
|
|
|
|
commands = getLogger('commands')
|
|
|
|
init = getLogger('init')
|
|
|
|
signals = getLogger('signals')
|
|
|
|
hints = getLogger('hints')
|
|
|
|
keyboard = getLogger('keyboard')
|
2014-06-10 22:11:17 +02:00
|
|
|
downloads = getLogger('downloads')
|
2014-06-23 07:00:21 +02:00
|
|
|
js = getLogger('js') # Javascript console messages
|
|
|
|
qt = getLogger('qt') # Warnings produced by Qt
|
2014-06-13 07:38:10 +02:00
|
|
|
style = getLogger('style')
|
2014-06-19 20:52:44 +02:00
|
|
|
rfc6266 = getLogger('rfc6266')
|
2014-05-23 16:57:08 +02:00
|
|
|
|
|
|
|
|
2014-05-25 18:46:15 +02:00
|
|
|
ram_handler = None
|
|
|
|
|
|
|
|
|
2014-05-23 16:57:08 +02:00
|
|
|
def init_log(args):
|
|
|
|
"""Init loggers based on the argparse namespace passed."""
|
2014-05-23 18:22:22 +02:00
|
|
|
level = 'DEBUG' if args.debug else args.loglevel.upper()
|
|
|
|
try:
|
2014-05-26 09:00:45 +02:00
|
|
|
numeric_level = getattr(logging, level)
|
|
|
|
except AttributeError:
|
2014-05-23 18:22:22 +02:00
|
|
|
raise ValueError("Invalid log level: {}".format(args.loglevel))
|
2014-06-06 12:29:33 +02:00
|
|
|
|
2014-06-19 12:06:36 +02:00
|
|
|
console, ram = _init_handlers(numeric_level, args.color, args.loglines)
|
2014-06-06 12:29:33 +02:00
|
|
|
root = getLogger()
|
2014-06-19 10:19:00 +02:00
|
|
|
if console is not None:
|
2014-06-19 20:52:44 +02:00
|
|
|
if args.logfilter is not None and numeric_level <= logging.DEBUG:
|
|
|
|
console.addFilter(LogFilter(args.logfilter.split(',')))
|
|
|
|
console.addFilter(LeplFilter())
|
2014-06-19 10:19:00 +02:00
|
|
|
root.addHandler(console)
|
2014-06-19 12:06:36 +02:00
|
|
|
if ram is not None:
|
|
|
|
root.addHandler(ram)
|
2014-06-20 08:50:18 +02:00
|
|
|
ram.addFilter(LeplFilter())
|
2014-06-06 12:29:33 +02:00
|
|
|
root.setLevel(logging.NOTSET)
|
|
|
|
logging.captureWarnings(True)
|
|
|
|
qInstallMessageHandler(qt_message_handler)
|
|
|
|
|
|
|
|
|
2014-06-19 20:52:44 +02:00
|
|
|
def fix_rfc2622():
|
|
|
|
"""Fix the rfc6266 logger.
|
|
|
|
|
|
|
|
In rfc2622 <= v0.04, a NullHandler class is added as handler, instead of an
|
|
|
|
object, which causes an exception later.
|
|
|
|
|
|
|
|
This was fixed in [1], but since v0.05 is not out yet, we work around the
|
|
|
|
issue by deleting the wrong handler.
|
|
|
|
|
|
|
|
This should be executed after init_log is done and rfc6266 is imported, but
|
|
|
|
before using it.
|
|
|
|
|
|
|
|
[1]: https://github.com/g2p/rfc6266/commit/cad58963ed13f5e1068fcc9e4326123b6b2bdcf8
|
|
|
|
"""
|
|
|
|
rfc6266.removeHandler(logging.NullHandler)
|
|
|
|
|
|
|
|
|
2014-06-19 12:06:36 +02:00
|
|
|
def _init_handlers(level, color, ram_capacity):
|
2014-06-06 12:29:33 +02:00
|
|
|
"""Init log handlers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
level: The numeric logging level.
|
|
|
|
color: Whether to use color if available.
|
|
|
|
"""
|
|
|
|
global ram_handler
|
2014-06-25 10:00:27 +02:00
|
|
|
console_fmt, ram_fmt, html_fmt, use_colorama = _init_formatters(
|
2014-06-06 12:29:33 +02:00
|
|
|
level, color)
|
|
|
|
|
2014-06-19 10:19:00 +02:00
|
|
|
if sys.stderr is None:
|
|
|
|
console_handler = None
|
2014-06-06 12:29:33 +02:00
|
|
|
else:
|
2014-06-19 10:19:00 +02:00
|
|
|
if use_colorama:
|
|
|
|
stream = colorama.AnsiToWin32(sys.stderr)
|
|
|
|
else:
|
|
|
|
stream = sys.stderr
|
|
|
|
console_handler = logging.StreamHandler(stream)
|
|
|
|
console_handler.setLevel(level)
|
2014-06-25 10:00:27 +02:00
|
|
|
console_handler.setFormatter(console_fmt)
|
2014-06-06 12:29:33 +02:00
|
|
|
|
2014-06-19 12:06:36 +02:00
|
|
|
if ram_capacity == 0:
|
|
|
|
ram_handler = None
|
|
|
|
else:
|
|
|
|
ram_handler = RAMHandler(capacity=ram_capacity)
|
|
|
|
ram_handler.setLevel(logging.NOTSET)
|
2014-06-25 10:00:27 +02:00
|
|
|
ram_handler.setFormatter(ram_fmt)
|
|
|
|
ram_handler.html_formatter = html_fmt
|
2014-06-06 12:29:33 +02:00
|
|
|
|
|
|
|
return console_handler, ram_handler
|
|
|
|
|
|
|
|
|
|
|
|
def _init_formatters(level, color):
|
|
|
|
"""Init log formatters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
level: The numeric logging level.
|
|
|
|
color: Whether to use color if available.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A (console_formatter, ram_formatter, use_colorama) tuple.
|
|
|
|
console_formatter/ram_formatter: logging.Formatter instances.
|
|
|
|
use_colorama: Whether to use colorama.
|
|
|
|
"""
|
|
|
|
if level <= logging.DEBUG:
|
|
|
|
console_fmt = EXTENDED_FMT
|
|
|
|
console_fmt_colored = EXTENDED_FMT_COLORED
|
2014-05-25 20:00:48 +02:00
|
|
|
else:
|
2014-06-06 12:29:33 +02:00
|
|
|
console_fmt = SIMPLE_FMT
|
|
|
|
console_fmt_colored = SIMPLE_FMT_COLORED
|
2014-06-19 10:19:00 +02:00
|
|
|
ram_formatter = logging.Formatter(EXTENDED_FMT, DATEFMT, '{')
|
2014-06-25 10:00:27 +02:00
|
|
|
html_formatter = HTMLFormatter(EXTENDED_FMT_HTML, DATEFMT,
|
|
|
|
log_colors=LOG_COLORS)
|
2014-06-19 10:19:00 +02:00
|
|
|
if sys.stderr is None:
|
2014-06-25 10:00:27 +02:00
|
|
|
return None, ram_formatter, html_formatter, False
|
2014-06-06 12:29:33 +02:00
|
|
|
use_colorama = False
|
2014-06-06 12:13:32 +02:00
|
|
|
if (ColoredFormatter is not None and (os.name == 'posix' or colorama) and
|
2014-06-06 12:29:33 +02:00
|
|
|
sys.stderr.isatty() and color):
|
2014-06-25 10:00:27 +02:00
|
|
|
console_formatter = ColoredFormatter(console_fmt_colored, DATEFMT,
|
|
|
|
log_colors=LOG_COLORS)
|
2014-06-06 12:13:32 +02:00
|
|
|
if colorama:
|
|
|
|
colorama.init()
|
2014-06-06 12:29:33 +02:00
|
|
|
use_colorama = True
|
2014-05-25 20:08:07 +02:00
|
|
|
else:
|
2014-06-06 12:29:33 +02:00
|
|
|
console_formatter = logging.Formatter(console_fmt, DATEFMT, '{')
|
2014-06-25 10:00:27 +02:00
|
|
|
return console_formatter, ram_formatter, html_formatter, use_colorama
|
2014-06-02 22:43:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def qt_message_handler(msg_type, context, msg):
|
|
|
|
"""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.
|
2014-06-03 14:34:38 +02:00
|
|
|
qt_to_logging = {
|
2014-06-02 22:43:32 +02:00
|
|
|
QtDebugMsg: logging.DEBUG,
|
|
|
|
QtWarningMsg: logging.WARNING,
|
|
|
|
QtCriticalMsg: logging.ERROR,
|
|
|
|
QtFatalMsg: logging.CRITICAL,
|
|
|
|
}
|
2014-06-03 17:18:20 +02:00
|
|
|
# Change levels of some well-known messages to debug so they don't get
|
|
|
|
# shown to the user.
|
2014-06-03 17:23:51 +02:00
|
|
|
# suppressed_msgs is a list of regexes matching the message texts to hide.
|
2014-06-06 17:12:54 +02:00
|
|
|
suppressed_msgs = ("libpng warning: iCCP: Not recognizing known sRGB "
|
2014-06-03 17:04:22 +02:00
|
|
|
"profile that has been edited",
|
2014-06-06 17:12:54 +02:00
|
|
|
"OpenType support missing for script [0-9]*")
|
2014-06-03 17:23:51 +02:00
|
|
|
if any(re.match(pattern, msg.strip()) for pattern in suppressed_msgs):
|
2014-06-03 17:18:20 +02:00
|
|
|
level = logging.DEBUG
|
|
|
|
else:
|
|
|
|
level = qt_to_logging[msg_type]
|
2014-06-02 22:43:32 +02:00
|
|
|
# We get something like "void qt_png_warning(png_structp, png_const_charp)"
|
|
|
|
# from Qt, but only want "qt_png_warning".
|
2014-06-03 17:24:12 +02:00
|
|
|
match = re.match(r'.*( |::)(\w*)\(.*\)', context.function)
|
2014-06-03 06:54:55 +02:00
|
|
|
if match is not None:
|
2014-06-03 17:24:12 +02:00
|
|
|
func = match.group(2)
|
2014-06-03 06:54:55 +02:00
|
|
|
else:
|
|
|
|
func = context.function
|
2014-06-02 23:16:09 +02:00
|
|
|
name = 'qt' if context.category == 'default' else 'qt-' + context.category
|
2014-06-10 23:11:17 +02:00
|
|
|
if msg.splitlines()[0] == ('This application failed to start because it '
|
|
|
|
'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")
|
|
|
|
try:
|
|
|
|
import faulthandler
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
faulthandler.disable()
|
2014-06-03 17:18:20 +02:00
|
|
|
record = qt.makeRecord(name, level, context.file, context.line, msg, None,
|
|
|
|
None, func)
|
2014-06-02 22:43:32 +02:00
|
|
|
qt.handle(record)
|
2014-06-02 22:19:35 +02:00
|
|
|
|
2014-05-23 16:57:08 +02:00
|
|
|
|
|
|
|
class LogFilter(logging.Filter):
|
|
|
|
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, names):
|
|
|
|
super().__init__()
|
|
|
|
self.names = names
|
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
"""Determine if the specified record is to be logged."""
|
|
|
|
if self.names is None:
|
|
|
|
return True
|
2014-06-06 16:51:24 +02:00
|
|
|
if record.levelno > logging.DEBUG:
|
|
|
|
# More important than DEBUG, so we won't filter at all
|
2014-06-06 15:48:42 +02:00
|
|
|
return True
|
2014-05-23 16:57:08 +02:00
|
|
|
for name in self.names:
|
|
|
|
if record.name == name:
|
|
|
|
return True
|
|
|
|
elif not record.name.startswith(name):
|
|
|
|
continue
|
|
|
|
elif record.name[len(name)] == '.':
|
|
|
|
return True
|
|
|
|
return False
|
2014-05-25 18:46:15 +02:00
|
|
|
|
|
|
|
|
2014-06-19 20:52:44 +02:00
|
|
|
class LeplFilter(logging.Filter):
|
|
|
|
|
|
|
|
"""Filter to filter debug log records by the lepl library."""
|
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
"""Determine if the specified record is to be logged."""
|
|
|
|
if (record.levelno == logging.INFO and
|
|
|
|
record.name == 'lepl.lexer.rewriters.AddLexer'):
|
|
|
|
# Special useless info message triggered by rfc6266
|
|
|
|
return False
|
|
|
|
if record.levelno > logging.DEBUG:
|
|
|
|
# More important than DEBUG, so we won't filter at all
|
|
|
|
return True
|
|
|
|
return not record.name.startswith('lepl.')
|
|
|
|
|
|
|
|
|
2014-05-25 18:46:15 +02:00
|
|
|
class RAMHandler(logging.Handler):
|
|
|
|
|
|
|
|
"""Logging handler which keeps the messages in a deque in RAM.
|
|
|
|
|
|
|
|
Loosly 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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, capacity):
|
|
|
|
super().__init__()
|
2014-06-25 10:00:27 +02:00
|
|
|
self.html_formatter = None
|
2014-06-19 12:06:36 +02:00
|
|
|
if capacity != -1:
|
|
|
|
self.data = deque(maxlen=capacity)
|
|
|
|
else:
|
|
|
|
self.data = deque()
|
2014-05-25 18:46:15 +02:00
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
self.data.append(record)
|
|
|
|
|
2014-06-25 10:00:27 +02:00
|
|
|
def dump_log(self, html=False):
|
2014-05-25 18:46:15 +02:00
|
|
|
"""Dump the complete formatted log data as as string."""
|
2014-06-25 10:00:27 +02:00
|
|
|
if html:
|
|
|
|
fmt = self.html_formatter.format
|
|
|
|
lines = ['<table>']
|
|
|
|
else:
|
|
|
|
fmt = self.format
|
|
|
|
lines = []
|
2014-05-25 18:46:15 +02:00
|
|
|
for record in self.data:
|
2014-06-25 10:00:27 +02:00
|
|
|
lines.append(fmt(record))
|
|
|
|
if html:
|
|
|
|
lines.append('</table>')
|
2014-05-25 18:46:15 +02:00
|
|
|
return '\n'.join(lines)
|
2014-06-25 10:00:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HTMLFormatter(logging.Formatter):
|
|
|
|
|
|
|
|
"""Formatter for HTML-colored log messages, similiar to colorlog."""
|
|
|
|
|
|
|
|
def __init__(self, fmt, datefmt, log_colors):
|
|
|
|
"""Constructor.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fmt: The format string to use.
|
|
|
|
datefmt: The date format to use.
|
|
|
|
log_colors: The colors to use for logging levels.
|
|
|
|
"""
|
|
|
|
super().__init__(fmt, datefmt)
|
|
|
|
self._log_colors = log_colors
|
|
|
|
self._colordict = {}
|
|
|
|
# We could solve this nicer by using CSS, but for this simple case this
|
|
|
|
# works.
|
|
|
|
for color in ['black', 'red', 'green', 'yellow', 'blue', 'purple',
|
|
|
|
'cyan', 'white']:
|
|
|
|
self._colordict[color] = '<font color="{}">'.format(color)
|
|
|
|
self._colordict['reset'] = '</font>'
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
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 ['asctime', 'filename', 'funcName', 'levelname',
|
|
|
|
'module', 'message', 'name', 'pathname', 'processName',
|
|
|
|
'threadName']:
|
|
|
|
setattr(record, field, cgi.escape(getattr(record, field)))
|
|
|
|
message = super().format(record)
|
|
|
|
if not message.endswith(self._colordict['reset']):
|
|
|
|
message += self._colordict['reset']
|
|
|
|
return message
|