Skip to content

MessageHandler

⋔ Inheritance diagram

graph TD
  1473367668336["debugging.MessageHandler"]
  140713234304496["builtins.object"]
  140713234304496 --> 1473367668336

🛈 DocStrings

Source code in prettyqt\debugging\messagehandler.py
class MessageHandler:
    def __init__(self, logger: logging.Logger):
        self._logger = logger
        self._previous_handler = None

    def install(self):
        self._previous_handler = QtCore.qInstallMessageHandler(self)

    def uninstall(self):
        if self._previous_handler is None:
            QtCore.qInstallMessageHandler(self._previous_handler)

    def __enter__(self):
        self.install()
        return self

    def __exit__(self, *args):
        self.uninstall()

    def __call__(
        self,
        msgtype: QtCore.QtMsgType,
        context: QtCore.QMessageLogContext,
        message: str,
    ):
        ctx = dict.fromkeys(["category", "file", "function", "line"])
        with contextlib.suppress(UnicodeDecodeError):
            ctx["category"] = context.category
        with contextlib.suppress(UnicodeDecodeError):
            ctx["file"] = context.file
        with contextlib.suppress(UnicodeDecodeError):
            ctx["function"] = context.function
        with contextlib.suppress(UnicodeDecodeError):
            ctx["line"] = context.line

        level = LOG_MAP[msgtype]
        self._logger.log(level, message, extra=ctx)