summaryrefslogtreecommitdiffstats
path: root/common/log.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-08-31 14:06:33 -0600
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-08-31 14:06:33 -0600
commit6bfcc22e08aebbabeac3e2ccb61556439d9f4b56 (patch)
tree7b69ab55af2eb69dc70ec7ef906d1cd035ddaccd /common/log.cpp
parent4a1bcc63cd2919f74f29b41c7b000f80da7449f4 (diff)
downloadsink-6bfcc22e08aebbabeac3e2ccb61556439d9f4b56.tar.gz
sink-6bfcc22e08aebbabeac3e2ccb61556439d9f4b56.zip
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"
Diffstat (limited to 'common/log.cpp')
-rw-r--r--common/log.cpp38
1 files changed, 26 insertions, 12 deletions
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()
26 return *sSettings.localData(); 26 return *sSettings.localData();
27} 27}
28 28
29static QByteArray sPrimaryComponent; 29Q_GLOBAL_STATIC(QByteArray, sPrimaryComponent);
30
30void Sink::Log::setPrimaryComponent(const QString &component) 31void Sink::Log::setPrimaryComponent(const QString &component)
31{ 32{
32 sPrimaryComponent = component.toUtf8(); 33 if (!sPrimaryComponent.isDestroyed()) {
34 *sPrimaryComponent = component.toUtf8();
35 }
33} 36}
34 37
35class DebugStream : public QIODevice 38class DebugStream : public QIODevice
@@ -322,13 +325,17 @@ static QByteArray getFileName(const char *file)
322 325
323static QString assembleDebugArea(const char *debugArea, const char *debugComponent, const char *file) 326static QString assembleDebugArea(const char *debugArea, const char *debugComponent, const char *file)
324{ 327{
325 if (sPrimaryComponent.isEmpty()) { 328 if (!sPrimaryComponent.isDestroyed() && sPrimaryComponent->isEmpty()) {
326 sPrimaryComponent = getProgramName(); 329 *sPrimaryComponent = getProgramName();
330 }
331 if (!sPrimaryComponent.isDestroyed()) {
332 //Using stringbuilder for fewer allocations
333 return QLatin1String{*sPrimaryComponent} % QLatin1String{"."} %
334 (debugComponent ? (QLatin1String{debugComponent} + QLatin1String{"."}) : QLatin1String{""}) %
335 (debugArea ? QLatin1String{debugArea} : QLatin1String{getFileName(file)});
336 } else {
337 return {};
327 } 338 }
328 //Using stringbuilder for fewer allocations
329 return QLatin1String{sPrimaryComponent} % QLatin1String{"."} %
330 (debugComponent ? (QLatin1String{debugComponent} + QLatin1String{"."}) : QLatin1String{""}) %
331 (debugArea ? QLatin1String{debugArea} : QLatin1String{getFileName(file)});
332} 339}
333 340
334static bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea) 341static bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea)
@@ -350,14 +357,19 @@ bool Sink::Log::isFiltered(DebugLevel debugLevel, const char *debugArea, const c
350 return isFiltered(debugLevel, assembleDebugArea(debugArea, debugComponent, file).toLatin1()); 357 return isFiltered(debugLevel, assembleDebugArea(debugArea, debugComponent, file).toLatin1());
351} 358}
352 359
360Q_GLOBAL_STATIC(NullStream, sNullStream);
361Q_GLOBAL_STATIC(DebugStream, sDebugStream);
362
353QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, const char *function, const char *debugArea, const char *debugComponent) 363QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, const char *function, const char *debugArea, const char *debugComponent)
354{ 364{
355 const auto fullDebugArea = assembleDebugArea(debugArea, debugComponent, file); 365 const auto fullDebugArea = assembleDebugArea(debugArea, debugComponent, file);
356 collectDebugArea(fullDebugArea); 366 collectDebugArea(fullDebugArea);
357 367
358 static NullStream nullstream;
359 if (isFiltered(debugLevel, fullDebugArea.toLatin1())) { 368 if (isFiltered(debugLevel, fullDebugArea.toLatin1())) {
360 return QDebug(&nullstream); 369 if (!sNullStream.isDestroyed()) {
370 return QDebug(sNullStream);
371 }
372 return QDebug{QtDebugMsg};
361 } 373 }
362 374
363 QString prefix; 375 QString prefix;
@@ -422,8 +434,10 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file,
422 } 434 }
423 output += ":"; 435 output += ":";
424 436
425 static DebugStream stream; 437 if (sDebugStream.isDestroyed()) {
426 QDebug debug(&stream); 438 return QDebug{QtDebugMsg};
439 }
440 QDebug debug(sDebugStream);
427 441
428 debug.noquote().nospace() << output; 442 debug.noquote().nospace() << output;
429 443