From 45e33954ef0dea9944911bcbbd0ead0f999da664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 18 Nov 2022 16:55:28 +0100 Subject: [PATCH] QErrorMessage: Forward log messages to original handler The convenience of having a QErrorMessage pop up a dialog when a log message is emitted should not prevent the existing message handler (typically the default handler, logging to console e.g.) from getting a stab at the messages. Change-Id: I45ba9128f509e85cd60f4e678cf8ec50bb0f93f1 Reviewed-by: Richard Moe Gustavsen --- src/widgets/dialogs/qerrormessage.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/widgets/dialogs/qerrormessage.cpp b/src/widgets/dialogs/qerrormessage.cpp index 7d52d9207c..ddadf8b336 100644 --- a/src/widgets/dialogs/qerrormessage.cpp +++ b/src/widgets/dialogs/qerrormessage.cpp @@ -149,8 +149,15 @@ static QString msgType2i18nString(QtMsgType t) return QCoreApplication::translate("QErrorMessage", messages[t]); } +static QtMessageHandler originalMessageHandler = nullptr; + static void jump(QtMsgType t, const QMessageLogContext &context, const QString &m) { + const auto forwardToOriginalHandler = qScopeGuard([&] { + if (originalMessageHandler) + originalMessageHandler(t, context, m); + }); + if (!qtMessageHandler) return; @@ -224,10 +231,12 @@ QErrorMessage::~QErrorMessage() { if (this == qtMessageHandler) { qtMessageHandler = nullptr; - QtMessageHandler tmp = qInstallMessageHandler(nullptr); - // in case someone else has later stuck in another... - if (tmp != jump) - qInstallMessageHandler(tmp); + QtMessageHandler currentMessagHandler = qInstallMessageHandler(nullptr); + if (currentMessagHandler != jump) + qInstallMessageHandler(currentMessagHandler); + else + qInstallMessageHandler(originalMessageHandler); + originalMessageHandler = nullptr; } } @@ -261,6 +270,10 @@ void QErrorMessage::done(int a) isn't one already. The object will only output log messages of QLoggingCategory::defaultCategory(). + + The object will forward all messages to the original message handler. + + \sa qInstallMessageHandler */ QErrorMessage * QErrorMessage::qtHandler() @@ -269,7 +282,7 @@ QErrorMessage * QErrorMessage::qtHandler() qtMessageHandler = new QErrorMessage(nullptr); qAddPostRoutine(deleteStaticcQErrorMessage); // clean up qtMessageHandler->setWindowTitle(QCoreApplication::applicationName()); - qInstallMessageHandler(jump); + originalMessageHandler = qInstallMessageHandler(jump); } return qtMessageHandler; }