From 963f6b8d940a025a08aece241971f282b0e31414 Mon Sep 17 00:00:00 2001 From: Julianne Swinoga Date: Sun, 25 Feb 2024 08:50:13 -0500 Subject: [PATCH] Redirect Qt messages to our normal logging infrastructure --- OATFWGUI/log_utils.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/OATFWGUI/log_utils.py b/OATFWGUI/log_utils.py index 4517c86..a58bd46 100644 --- a/OATFWGUI/log_utils.py +++ b/OATFWGUI/log_utils.py @@ -8,7 +8,8 @@ from datetime import datetime from typing import Tuple, List, Optional -from PySide6.QtCore import Slot, Signal, QObject, QFileSystemWatcher, QFile, QMetaMethod +from PySide6.QtCore import Slot, Signal, QObject, QFileSystemWatcher, QFile, QMetaMethod, QtMsgType, QMessageLogContext, \ + qInstallMessageHandler from external_processes import get_install_dir from platform_check import get_platform, PlatformEnum @@ -133,6 +134,26 @@ def stop(self): self.log.warning(f'Could not remove temp file {self.tempfile.name}') +def qt_message_handler(msg_type: QtMsgType, msg_ctx: QMessageLogContext, msg: str): + qt_log_level_map = { + QtMsgType.QtDebugMsg: logging.DEBUG, + QtMsgType.QtInfoMsg: logging.INFO, + QtMsgType.QtWarningMsg: logging.WARNING, + QtMsgType.QtCriticalMsg: logging.CRITICAL, + QtMsgType.QtFatalMsg: logging.FATAL, + } + py_log_level = qt_log_level_map.get(msg_type, logging.INFO) + + if any([msg_ctx.file, msg_ctx.function, msg_ctx.line]): + # I don't think the context is usually filled but here if needed + context_str = f' {msg_ctx.file}:{msg_ctx.function}:{msg_ctx.line}' + else: + context_str = '' + # Don't want to global the `log` variable here (idk if things will break) + log = logging.getLogger('') + log.log(py_log_level, f'Qt:{msg}{context_str}') + + def setup_logging(logger, qt_log_obj: LogObject): logger.setLevel(logging.DEBUG) # file handler @@ -155,4 +176,7 @@ def setup_logging(logger, qt_log_obj: LogObject): gh.setFormatter(CustomFormatter(colour_type=LogColourTypes.html)) logger.addHandler(gh) + # Qt logging + qInstallMessageHandler(qt_message_handler) + logger.debug(f'Logging initialized (logfile={log_file})')