summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-05-12 17:01:25 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-05-12 17:18:18 +0200
commit58b9fa88198eecc224597e52d8bbd7f833fca63b (patch)
tree4d7b65992d699320398f947bfb134c54a69ecf8c /common
parent6a3bf46334fc4136da480287898d3f19c88261ee (diff)
downloadsink-58b9fa88198eecc224597e52d8bbd7f833fca63b.tar.gz
sink-58b9fa88198eecc224597e52d8bbd7f833fca63b.zip
Completely shortcut the stream evaluation if we're not going to use the
output Serializing the data is the expensive part, so we want to completely avoid that for the noisier part if we're not going to use it. Additionally we're now using a stringbuilder for the debugarea to try to further improve the situation with temporary memory allocations.
Diffstat (limited to 'common')
-rw-r--r--common/log.cpp37
-rw-r--r--common/log.h5
2 files changed, 28 insertions, 14 deletions
diff --git a/common/log.cpp b/common/log.cpp
index d1a3a37..5dfb872 100644
--- a/common/log.cpp
+++ b/common/log.cpp
@@ -14,6 +14,7 @@
14#include <atomic> 14#include <atomic>
15#include <definitions.h> 15#include <definitions.h>
16#include <QThreadStorage> 16#include <QThreadStorage>
17#include <QStringBuilder>
17 18
18using namespace Sink::Log; 19using namespace Sink::Log;
19 20
@@ -315,13 +316,24 @@ static QByteArray getFileName(const char *file)
315 return filename.split('.').first(); 316 return filename.split('.').first();
316} 317}
317 318
318bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea) 319static QString assembleDebugArea(const char *debugArea, const char *debugComponent, const char *file)
320{
321 if (sPrimaryComponent.isEmpty()) {
322 sPrimaryComponent = getProgramName();
323 }
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}
329
330static bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea)
319{ 331{
320 if (debugLevel < debugOutputLevel()) { 332 if (debugLevel < debugOutputLevel()) {
321 return true; 333 return true;
322 } 334 }
323 auto areas = debugOutputFilter(Sink::Log::Area); 335 const auto areas = debugOutputFilter(Sink::Log::Area);
324 if (debugLevel <= Sink::Log::Trace && !areas.isEmpty()) { 336 if ((debugLevel <= Sink::Log::Trace) && !areas.isEmpty()) {
325 if (!containsItemStartingWith(fullDebugArea, areas)) { 337 if (!containsItemStartingWith(fullDebugArea, areas)) {
326 return true; 338 return true;
327 } 339 }
@@ -329,19 +341,18 @@ bool isFiltered(DebugLevel debugLevel, const QByteArray &fullDebugArea)
329 return false; 341 return false;
330} 342}
331 343
332QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, const char *function, const char *debugArea, const char *debugComponent) 344bool Sink::Log::isFiltered(DebugLevel debugLevel, const char *debugArea, const char *debugComponent, const char *file)
333{ 345{
334 if (sPrimaryComponent.isEmpty()) { 346 return isFiltered(debugLevel, assembleDebugArea(debugArea, debugComponent, file).toLatin1());
335 sPrimaryComponent = getProgramName(); 347}
336 }
337 const QByteArray fullDebugArea = sPrimaryComponent + "." +
338 (debugComponent ? (QByteArray{debugComponent} + ".") : "") +
339 (debugArea ? QByteArray{debugArea} : getFileName(file));
340 348
349QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, const char *function, const char *debugArea, const char *debugComponent)
350{
351 const auto fullDebugArea = assembleDebugArea(debugArea, debugComponent, file);
341 collectDebugArea(fullDebugArea); 352 collectDebugArea(fullDebugArea);
342 353
343 static NullStream nullstream; 354 static NullStream nullstream;
344 if (isFiltered(debugLevel, fullDebugArea)) { 355 if (isFiltered(debugLevel, fullDebugArea.toLatin1())) {
345 return QDebug(&nullstream); 356 return QDebug(&nullstream);
346 } 357 }
347 358
@@ -391,12 +402,12 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file,
391 } 402 }
392 static std::atomic<int> maxDebugAreaSize{25}; 403 static std::atomic<int> maxDebugAreaSize{25};
393 maxDebugAreaSize = qMax(fullDebugArea.size(), maxDebugAreaSize.load()); 404 maxDebugAreaSize = qMax(fullDebugArea.size(), maxDebugAreaSize.load());
394 output += QString(" %1 ").arg(QString{fullDebugArea}.leftJustified(maxDebugAreaSize, ' ', false)); 405 output += QString(" %1 ").arg(fullDebugArea.leftJustified(maxDebugAreaSize, ' ', false));
395 if (useColor) { 406 if (useColor) {
396 output += resetColor; 407 output += resetColor;
397 } 408 }
398 if (showFunction) { 409 if (showFunction) {
399 output += QString(" %3").arg(QString{fullDebugArea}.leftJustified(25, ' ', true)); 410 output += QString(" %3").arg(fullDebugArea.leftJustified(25, ' ', true));
400 } 411 }
401 if (showLocation) { 412 if (showLocation) {
402 const auto filename = QString::fromLatin1(file).split('/').last(); 413 const auto filename = QString::fromLatin1(file).split('/').last();
diff --git a/common/log.h b/common/log.h
index be5a508..8266fdb 100644
--- a/common/log.h
+++ b/common/log.h
@@ -85,12 +85,15 @@ SINK_EXPORT inline QDebug operator<<(QDebug d, const TraceTime &time)
85 d << time.time << "[ms]"; 85 d << time.time << "[ms]";
86 return d; 86 return d;
87} 87}
88
89SINK_EXPORT bool isFiltered(DebugLevel debugLevel, const char *debugArea, const char *debugComponent, const char *file);
90
88} 91}
89} 92}
90 93
91static const char *getComponentName() { return nullptr; } 94static const char *getComponentName() { return nullptr; }
92 95
93#define SINK_DEBUG_STREAM_IMPL(LEVEL, AREA, COMPONENT) Sink::Log::debugStream(LEVEL, __LINE__, __FILE__, Q_FUNC_INFO, AREA, COMPONENT) 96#define SINK_DEBUG_STREAM_IMPL(LEVEL, AREA, COMPONENT) if (!Sink::Log::isFiltered(LEVEL, AREA, COMPONENT, __FILE__)) Sink::Log::debugStream(LEVEL, __LINE__, __FILE__, Q_FUNC_INFO, AREA, COMPONENT)
94 97
95#define Trace_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Trace, AREA, nullptr) 98#define Trace_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Trace, AREA, nullptr)
96#define Log_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Log, AREA, nullptr) 99#define Log_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Log, AREA, nullptr)