QMessagePattern: use the system message sinks for parsing errors

Instead of only printing to stderr or the Windows debug buffer. I guess
this wasn't done before because the code could recurse back and cause
stack overflows. We can do it now without recursion problems.

Change-Id: Ifa1111900d6945ea8e05fffd177e14e2f86ae482
Reviewed-by: Kai Köhne <kai.koehne@qt.io>
This commit is contained in:
Thiago Macieira 2023-08-23 10:57:54 -07:00
parent e85c64bb6d
commit 232f4f14be

View File

@ -168,7 +168,8 @@ Q_NORETURN
#endif #endif
static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message); static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message);
static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message); static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message);
static void qt_message_print(const QString &message); static void preformattedMessageHandler(QtMsgType type, const QMessageLogContext &context,
const QString &formattedMessage);
static int checked_var_value(const char *varname) static int checked_var_value(const char *varname)
{ {
@ -1329,8 +1330,14 @@ void QMessagePattern::setPattern(const QString &pattern)
else if (inIf) else if (inIf)
error += "QT_MESSAGE_PATTERN: missing %{endif}\n"_L1; error += "QT_MESSAGE_PATTERN: missing %{endif}\n"_L1;
if (!error.isEmpty()) if (!error.isEmpty()) {
qt_message_print(error); // remove the last '\n' because the sinks deal with that on their own
error.chop(1);
QMessageLogContext ctx(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE,
"QMessagePattern::setPattern", nullptr);
preformattedMessageHandler(QtWarningMsg, ctx, error);
}
literals.reset(new std::unique_ptr<const char[]>[literalsVar.size() + 1]); literals.reset(new std::unique_ptr<const char[]>[literalsVar.size() + 1]);
std::move(literalsVar.begin(), literalsVar.end(), &literals[0]); std::move(literalsVar.begin(), literalsVar.end(), &literals[0]);
@ -1927,6 +1934,18 @@ static constexpr SystemMessageSink systemMessageSink = {
#endif #endif
}; };
static void preformattedMessageHandler(QtMsgType type, const QMessageLogContext &context,
const QString &formattedMessage)
{
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Waddress") // "the address of ~~ will never be NULL
if (systemMessageSink.sink && systemMessageSink.sink(type, context, formattedMessage))
return;
QT_WARNING_POP
stderr_message_handler(type, context, formattedMessage);
}
/*! /*!
\internal \internal
*/ */
@ -1942,13 +1961,7 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con
return; return;
} }
QString formattedMessage = qFormatLogMessage(type, context, message); preformattedMessageHandler(type, context, qFormatLogMessage(type, context, message));
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Waddress") // "the address of ~~ will never be NULL
if (systemMessageSink.sink && systemMessageSink.sink(type, context, formattedMessage))
return;
QT_WARNING_POP
stderr_message_handler(type, context, formattedMessage);
} }
#if defined(Q_COMPILER_THREAD_LOCAL) #if defined(Q_COMPILER_THREAD_LOCAL)
@ -1995,22 +2008,10 @@ static void qt_message_print(QtMsgType msgType, const QMessageLogContext &contex
auto msgHandler = messageHandler.loadAcquire(); auto msgHandler = messageHandler.loadAcquire();
(msgHandler ? msgHandler : qDefaultMessageHandler)(msgType, context, message); (msgHandler ? msgHandler : qDefaultMessageHandler)(msgType, context, message);
} else { } else {
stderr_message_handler(msgType, context, message.toLocal8Bit()); stderr_message_handler(msgType, context, message);
} }
} }
static void qt_message_print(const QString &message)
{
#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
if (!shouldLogToStderr()) {
win_outputDebugString_helper(message);
return;
}
#endif
fprintf(stderr, "%s", message.toLocal8Bit().constData());
fflush(stderr);
}
template <typename String> template <typename String>
static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message) static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message)
{ {