summaryrefslogtreecommitdiffstats
path: root/common/log.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-09-13 12:42:31 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-09-13 12:42:31 +0200
commitc12a9a09da59b9e418316dba02e6215cb55e47ee (patch)
tree05498d9a42e399bcca787f40c1fc473fb09e680e /common/log.cpp
parent55fe06979ceebe67553135b43aa47e70d931304b (diff)
parentebdb89b8bb482bbb5ecd544c3d38bef35fc7d820 (diff)
downloadsink-0.4.0.tar.gz
sink-0.4.0.zip
Merge commit 'ebdb89b8bb482bbb5ecd544c3d38bef35fc7d820'v0.4.0
Diffstat (limited to 'common/log.cpp')
-rw-r--r--common/log.cpp50
1 files changed, 34 insertions, 16 deletions
diff --git a/common/log.cpp b/common/log.cpp
index 5dfb872..bfc9d5e 100644
--- a/common/log.cpp
+++ b/common/log.cpp
@@ -10,7 +10,6 @@
10#include <QMutexLocker> 10#include <QMutexLocker>
11#include <iostream> 11#include <iostream>
12#include <unistd.h> 12#include <unistd.h>
13#include <memory>
14#include <atomic> 13#include <atomic>
15#include <definitions.h> 14#include <definitions.h>
16#include <QThreadStorage> 15#include <QThreadStorage>
@@ -27,10 +26,13 @@ static QSettings &config()
27 return *sSettings.localData(); 26 return *sSettings.localData();
28} 27}
29 28
30static QByteArray sPrimaryComponent; 29Q_GLOBAL_STATIC(QByteArray, sPrimaryComponent);
30
31void Sink::Log::setPrimaryComponent(const QString &component) 31void Sink::Log::setPrimaryComponent(const QString &component)
32{ 32{
33 sPrimaryComponent = component.toUtf8(); 33 if (!sPrimaryComponent.isDestroyed()) {
34 *sPrimaryComponent = component.toUtf8();
35 }
34} 36}
35 37
36class DebugStream : public QIODevice 38class DebugStream : public QIODevice
@@ -267,16 +269,21 @@ public:
267 QSet<QString> mDebugAreas; 269 QSet<QString> mDebugAreas;
268}; 270};
269 271
270static auto sDebugAreaCollector = std::unique_ptr<DebugAreaCollector>(new DebugAreaCollector); 272Q_GLOBAL_STATIC(DebugAreaCollector, sDebugAreaCollector);
271 273
272QSet<QString> Sink::Log::debugAreas() 274QSet<QString> Sink::Log::debugAreas()
273{ 275{
274 return sDebugAreaCollector->debugAreas(); 276 if (!sDebugAreaCollector.isDestroyed()) {
277 return sDebugAreaCollector->debugAreas();
278 }
279 return {};
275} 280}
276 281
277static void collectDebugArea(const QString &debugArea) 282static void collectDebugArea(const QString &debugArea)
278{ 283{
279 sDebugAreaCollector->add(debugArea); 284 if (!sDebugAreaCollector.isDestroyed()) {
285 sDebugAreaCollector->add(debugArea);
286 }
280} 287}
281 288
282static bool containsItemStartingWith(const QByteArray &pattern, const QByteArrayList &list) 289static bool containsItemStartingWith(const QByteArray &pattern, const QByteArrayList &list)
@@ -318,13 +325,17 @@ static QByteArray getFileName(const char *file)
318 325
319static QString assembleDebugArea(const char *debugArea, const char *debugComponent, const char *file) 326static QString assembleDebugArea(const char *debugArea, const char *debugComponent, const char *file)
320{ 327{
321 if (sPrimaryComponent.isEmpty()) { 328 if (!sPrimaryComponent.isDestroyed() && sPrimaryComponent->isEmpty()) {
322 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 {};
323 } 338 }
324 //Using stringbuilder for fewer allocations
325 return QLatin1String{sPrimaryComponent} % QLatin1String{"."} %
326 (debugComponent ? (QLatin1String{debugComponent} + QLatin1String{"."}) : QLatin1String{""}) %
327 (debugArea ? QLatin1String{debugArea} : QLatin1String{getFileName(file)});
328} 339}
329 340
330static bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea) 341static bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea)
@@ -346,14 +357,19 @@ bool Sink::Log::isFiltered(DebugLevel debugLevel, const char *debugArea, const c
346 return isFiltered(debugLevel, assembleDebugArea(debugArea, debugComponent, file).toLatin1()); 357 return isFiltered(debugLevel, assembleDebugArea(debugArea, debugComponent, file).toLatin1());
347} 358}
348 359
360Q_GLOBAL_STATIC(NullStream, sNullStream);
361Q_GLOBAL_STATIC(DebugStream, sDebugStream);
362
349QDebug 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)
350{ 364{
351 const auto fullDebugArea = assembleDebugArea(debugArea, debugComponent, file); 365 const auto fullDebugArea = assembleDebugArea(debugArea, debugComponent, file);
352 collectDebugArea(fullDebugArea); 366 collectDebugArea(fullDebugArea);
353 367
354 static NullStream nullstream;
355 if (isFiltered(debugLevel, fullDebugArea.toLatin1())) { 368 if (isFiltered(debugLevel, fullDebugArea.toLatin1())) {
356 return QDebug(&nullstream); 369 if (!sNullStream.isDestroyed()) {
370 return QDebug(sNullStream);
371 }
372 return QDebug{QtDebugMsg};
357 } 373 }
358 374
359 QString prefix; 375 QString prefix;
@@ -418,8 +434,10 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file,
418 } 434 }
419 output += ":"; 435 output += ":";
420 436
421 static DebugStream stream; 437 if (sDebugStream.isDestroyed()) {
422 QDebug debug(&stream); 438 return QDebug{QtDebugMsg};
439 }
440 QDebug debug(sDebugStream);
423 441
424 debug.noquote().nospace() << output; 442 debug.noquote().nospace() << output;
425 443