Skip to content

Commit

Permalink
Redirect Qt messages to our normal logging infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
julianneswinoga committed Feb 25, 2024
1 parent 9ef5478 commit 963f6b8
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion OATFWGUI/log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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})')

0 comments on commit 963f6b8

Please sign in to comment.