Testlib: improve messageHandler without loggers

When the messageHandler is called without loggers, we used to simply try
to restore the old message handler. This is not ideal and the assertion
seems to be wrong.

The situation of not having any loggers can occur during teardown, if
the message logger is called from a worker thread, while the application
thread stops logging (compare QTBUG-129722)
In this case, we simply forward the message to the original message
handler, which should simply print the message.

Pick-to: 6.8
Change-Id: Ic8147f7ab6317f1ceb4f52c79ce298464a94de30
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Tim Blechmann 2024-10-29 12:53:53 +08:00
parent 182604ae9c
commit 56106d1bd7

View File

@ -209,9 +209,11 @@ namespace QTest {
static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(QTest::maxWarnings);
if (!QTestLog::hasLoggers()) {
// if this goes wrong, something is seriously broken.
qInstallMessageHandler(oldMessageHandler);
QTEST_ASSERT(QTestLog::hasLoggers());
// the message handler may be called from a worker thread, after the main thread stopped logging.
// Forwarding to original message handler to avoid swallowing the message
Q_ASSERT(oldMessageHandler);
oldMessageHandler(type, context, message);
return;
}
if (handleIgnoredMessage(type, message)) {