Feed Qt logs into Python's logging system.

main
Ben Niemann 1 year ago
parent 14adb0777f
commit ab9511e2f1

@ -22,9 +22,12 @@ import sys
import logging.handlers
from logging import * # pylint: disable=unused-wildcard-import,wildcard-import
import queue
import traceback
from typing import Any, Dict, Optional, List, Tuple
from typing_extensions import Literal
from PySide2 import QtCore
from . import logsink
@ -118,6 +121,9 @@ class LogManager:
self.queue_handler = logging.handlers.QueueHandler(self.queue)
self.root_logger.addHandler(self.queue_handler)
# Feed Qt logs into logging system
QtCore.qInstallMessageHandler(self.__qtMessageHandler)
# Make loggers of 3rd party modules less noisy.
for other in ('qasync', 'vext'):
getLogger(other).setLevel(WARNING)
@ -174,3 +180,35 @@ class LogManager:
'%(asctime)s\t%(levelname)s\t%(process)s\t%(thread)08x\t%(name)s\t%(message)s',
'%Y%m%d-%H%M%S'))
return handler
__qtLevelMap = {
QtCore.QtDebugMsg: logging.DEBUG,
QtCore.QtInfoMsg: logging.INFO,
QtCore.QtWarningMsg: logging.WARNING,
QtCore.QtCriticalMsg: logging.ERROR,
QtCore.QtFatalMsg: logging.CRITICAL,
}
def __qtMessageHandler(
self,
msg_type: QtCore.QtMsgType,
context: QtCore.QMessageLogContext,
msg: str,
) -> None:
try:
if context.category is None or context.category == 'default':
loggerName = 'qt'
else:
loggerName = context.category
logger = logging.getLogger(loggerName)
record = logger.makeRecord(
name=loggerName,
level=self.__qtLevelMap[msg_type],
fn=context.file,
lno=context.line,
msg=msg, args=(),
exc_info=None,
func=context.function)
logger.handle(record)
except:
traceback.print_exc()

@ -103,6 +103,8 @@ class MainApp(App):
logger = logging.getLogger('noisicaa.ui.main')
logger.info("Starting noisicaa UI...")
os.environ['QT_LOGGING_RULES'] = 'qt.scenegraph.general=true'
self.__qtApp = QtGui.QGuiApplication(argv)
self.__qtApp.setQuitOnLastWindowClosed(False)
self.__qtApp.setOrganizationName("odahoda")

Loading…
Cancel
Save