diff options
author | Bernd Weimer <bernd.weimer@qt.io> | 2021-03-19 16:10:59 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-01-19 03:23:00 +0000 |
commit | 9712219a038531d43f31fa7d1a8ccc3d6c32787d (patch) | |
tree | bd17546dec60fc621cbde38767bdec195d2d0b3c | |
parent | 94c08e5f34811d89623c1041960e52127350c84b (diff) | |
download | qtapplicationmanager-9712219a038531d43f31fa7d1a8ccc3d6c32787d.tar.gz |
Fix deferred logging crash
If more than one configuration file is used, parsing is done
concurrently and hence debug output might be generated in parallel.
Appending output to the deferred messages vector was however not
thread safe. A mutex has been added to remedy this.
Cherry-picked from 5.15: d45eef3
Change-Id: If55bf0d8d1618e943f0543dafaed0f4f08c94242
Reviewed-by: Robert Griebl <robert.griebl@qt.io>
(cherry picked from commit e7aae3561f7b8c060dff2027a1a049e86ae78ad2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/common-lib/logging.cpp | 9 | ||||
-rw-r--r-- | src/common-lib/logging.h | 2 |
2 files changed, 9 insertions, 2 deletions
diff --git a/src/common-lib/logging.cpp b/src/common-lib/logging.cpp index 91cc09f3..92b7b2ff 100644 --- a/src/common-lib/logging.cpp +++ b/src/common-lib/logging.cpp @@ -40,6 +40,7 @@ #include <QThreadStorage> #include <QAtomicInteger> #include <QCoreApplication> +#include <QMutexLocker> #include "global.h" #include "logging.h" @@ -191,6 +192,7 @@ QStringList Logging::s_rules; QtMessageHandler Logging::s_defaultQtHandler = nullptr; QByteArray Logging::s_applicationId = QByteArray(); QVariant Logging::s_useAMConsoleLoggerConfig = QVariant(); +QMutex Logging::s_deferredMessagesMutex; static std::vector<DeferredMessage> s_deferredMessages; @@ -379,6 +381,7 @@ void Logging::messageHandler(QtMsgType msgType, const QMessageLogContext &contex void Logging::deferredMessageHandler(QtMsgType msgType, const QMessageLogContext &context, const QString &message) { + QMutexLocker lock(&s_deferredMessagesMutex); s_deferredMessages.emplace_back(msgType, context, message); } @@ -449,6 +452,9 @@ void Logging::useAMConsoleLogger(const QVariant &config) void Logging::completeSetup() { + qInstallMessageHandler(messageHandler); + + QMutexLocker lock(&s_deferredMessagesMutex); for (const DeferredMessage &msg : s_deferredMessages) { QLoggingCategory cat(msg.category); if (cat.isEnabled(msg.msgType)) { @@ -456,9 +462,7 @@ void Logging::completeSetup() messageHandler(msg.msgType, context, msg.message); } } - std::vector<DeferredMessage>().swap(s_deferredMessages); - qInstallMessageHandler(messageHandler); } QByteArray Logging::applicationId() @@ -473,6 +477,7 @@ void Logging::setApplicationId(const QByteArray &appId) bool Logging::deferredMessages() { + QMutexLocker lock(&s_deferredMessagesMutex); return !s_deferredMessages.empty(); } diff --git a/src/common-lib/logging.h b/src/common-lib/logging.h index 094d388a..3090875d 100644 --- a/src/common-lib/logging.h +++ b/src/common-lib/logging.h @@ -33,6 +33,7 @@ #include <QtAppManCommon/global.h> #include <QLoggingCategory> +#include <QMutex> QT_BEGIN_NAMESPACE_AM @@ -85,6 +86,7 @@ private: static QtMessageHandler s_defaultQtHandler; static QByteArray s_applicationId; static QVariant s_useAMConsoleLoggerConfig; + static QMutex s_deferredMessagesMutex; }; void am_trace(QDebug); |