Handle QtInfoMsg (Qt 5.5) in qt_message_handler.

This commit is contained in:
Florian Bruhin 2015-05-15 20:15:09 +02:00
parent 1a1a8ba26f
commit 12940eb542

View File

@ -29,8 +29,7 @@ import faulthandler
import traceback import traceback
import warnings import warnings
from PyQt5.QtCore import (QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, from PyQt5 import QtCore
qInstallMessageHandler)
# Optional imports # Optional imports
try: try:
import colorama import colorama
@ -153,15 +152,15 @@ def init_log(args):
root.setLevel(logging.NOTSET) root.setLevel(logging.NOTSET)
logging.captureWarnings(True) logging.captureWarnings(True)
warnings.simplefilter('default') warnings.simplefilter('default')
qInstallMessageHandler(qt_message_handler) QtCore.qInstallMessageHandler(qt_message_handler)
@contextlib.contextmanager @contextlib.contextmanager
def disable_qt_msghandler(): def disable_qt_msghandler():
"""Contextmanager which temporarily disables the Qt message handler.""" """Contextmanager which temporarily disables the Qt message handler."""
old_handler = qInstallMessageHandler(None) old_handler = QtCore.qInstallMessageHandler(None)
yield yield
qInstallMessageHandler(old_handler) QtCore.qInstallMessageHandler(old_handler)
def _init_handlers(level, color, ram_capacity): def _init_handlers(level, color, ram_capacity):
@ -244,11 +243,16 @@ def qt_message_handler(msg_type, context, msg):
# Note we map critical to ERROR as it's actually "just" an error, and fatal # Note we map critical to ERROR as it's actually "just" an error, and fatal
# to critical. # to critical.
qt_to_logging = { qt_to_logging = {
QtDebugMsg: logging.DEBUG, QtCore.QtDebugMsg: logging.DEBUG,
QtWarningMsg: logging.WARNING, QtCore.QtWarningMsg: logging.WARNING,
QtCriticalMsg: logging.ERROR, QtCore.QtCriticalMsg: logging.ERROR,
QtFatalMsg: logging.CRITICAL, QtCore.QtFatalMsg: logging.CRITICAL,
} }
try:
qt_to_logging[QtCore.QtInfoMsg] = logging.INFO
except AttributeError:
# Qt < 5.5
pass
# Change levels of some well-known messages to debug so they don't get # Change levels of some well-known messages to debug so they don't get
# shown to the user. # shown to the user.
# suppressed_msgs is a list of regexes matching the message texts to hide. # suppressed_msgs is a list of regexes matching the message texts to hide.