From dd2d4263459c12b9ca65a23711f5f77fe34fef1b Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 28 Aug 2017 21:19:51 -0600 Subject: use Q_GLOBAL_STATIC In an attempt to resolve T6890. --- common/log.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'common/log.cpp') diff --git a/common/log.cpp b/common/log.cpp index 5dfb872..3edc89a 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -267,7 +266,7 @@ public: QSet mDebugAreas; }; -static auto sDebugAreaCollector = std::unique_ptr(new DebugAreaCollector); +Q_GLOBAL_STATIC(DebugAreaCollector, sDebugAreaCollector); QSet Sink::Log::debugAreas() { -- cgit v1.2.3 From d05f3be54f619575769eaf7f00edff9f99feb6f6 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 28 Aug 2017 21:39:07 -0600 Subject: Avoid use after destruction --- common/log.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'common/log.cpp') diff --git a/common/log.cpp b/common/log.cpp index 3edc89a..ac87c84 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -270,12 +270,17 @@ Q_GLOBAL_STATIC(DebugAreaCollector, sDebugAreaCollector); QSet Sink::Log::debugAreas() { - return sDebugAreaCollector->debugAreas(); + if (!sDebugAreaCollector.isDestroyed()) { + return sDebugAreaCollector->debugAreas(); + } + return {}; } static void collectDebugArea(const QString &debugArea) { - sDebugAreaCollector->add(debugArea); + if (!sDebugAreaCollector.isDestroyed()) { + sDebugAreaCollector->add(debugArea); + } } static bool containsItemStartingWith(const QByteArray &pattern, const QByteArrayList &list) -- cgit v1.2.3 From 6bfcc22e08aebbabeac3e2ccb61556439d9f4b56 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 31 Aug 2017 14:06:33 -0600 Subject: Use Q_GLOBAL_STATIC for threadsafety. This resolves the following warning on shutdown it seems: "QObject::connect: No such signal QObject::aboutToClose() in ../../include/QtCore/5.9.1/QtCore/private/../../../../../src/corelib/io/qtextstream_p.h:75" --- common/log.cpp | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'common/log.cpp') diff --git a/common/log.cpp b/common/log.cpp index ac87c84..bfc9d5e 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -26,10 +26,13 @@ static QSettings &config() return *sSettings.localData(); } -static QByteArray sPrimaryComponent; +Q_GLOBAL_STATIC(QByteArray, sPrimaryComponent); + void Sink::Log::setPrimaryComponent(const QString &component) { - sPrimaryComponent = component.toUtf8(); + if (!sPrimaryComponent.isDestroyed()) { + *sPrimaryComponent = component.toUtf8(); + } } class DebugStream : public QIODevice @@ -322,13 +325,17 @@ static QByteArray getFileName(const char *file) static QString assembleDebugArea(const char *debugArea, const char *debugComponent, const char *file) { - if (sPrimaryComponent.isEmpty()) { - sPrimaryComponent = getProgramName(); + if (!sPrimaryComponent.isDestroyed() && sPrimaryComponent->isEmpty()) { + *sPrimaryComponent = getProgramName(); + } + if (!sPrimaryComponent.isDestroyed()) { + //Using stringbuilder for fewer allocations + return QLatin1String{*sPrimaryComponent} % QLatin1String{"."} % + (debugComponent ? (QLatin1String{debugComponent} + QLatin1String{"."}) : QLatin1String{""}) % + (debugArea ? QLatin1String{debugArea} : QLatin1String{getFileName(file)}); + } else { + return {}; } - //Using stringbuilder for fewer allocations - return QLatin1String{sPrimaryComponent} % QLatin1String{"."} % - (debugComponent ? (QLatin1String{debugComponent} + QLatin1String{"."}) : QLatin1String{""}) % - (debugArea ? QLatin1String{debugArea} : QLatin1String{getFileName(file)}); } static bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea) @@ -350,14 +357,19 @@ bool Sink::Log::isFiltered(DebugLevel debugLevel, const char *debugArea, const c return isFiltered(debugLevel, assembleDebugArea(debugArea, debugComponent, file).toLatin1()); } +Q_GLOBAL_STATIC(NullStream, sNullStream); +Q_GLOBAL_STATIC(DebugStream, sDebugStream); + QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, const char *function, const char *debugArea, const char *debugComponent) { const auto fullDebugArea = assembleDebugArea(debugArea, debugComponent, file); collectDebugArea(fullDebugArea); - static NullStream nullstream; if (isFiltered(debugLevel, fullDebugArea.toLatin1())) { - return QDebug(&nullstream); + if (!sNullStream.isDestroyed()) { + return QDebug(sNullStream); + } + return QDebug{QtDebugMsg}; } QString prefix; @@ -422,8 +434,10 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, } output += ":"; - static DebugStream stream; - QDebug debug(&stream); + if (sDebugStream.isDestroyed()) { + return QDebug{QtDebugMsg}; + } + QDebug debug(sDebugStream); debug.noquote().nospace() << output; -- cgit v1.2.3