From 8599c4d371139dc0d4444b128792cd499129d349 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 4 Feb 2016 18:39:18 +0100 Subject: Filter debug output by program name and area --- common/log.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++---------- common/log.h | 18 +++++++++++++++++- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/common/log.cpp b/common/log.cpp index 85a55c0..0f011b1 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -142,23 +142,56 @@ Sink::Log::DebugLevel Sink::Log::debugOutputLevel() return debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); } +void Sink::Log::setFilter(const QByteArrayList &filter) +{ + qputenv("SINKDEBUGFILTER", filter.join(',')); +} + +void Sink::Log::setAreas(const QByteArrayList &filter) +{ + qputenv("SINKDEBUGAREAS", filter.join(',')); +} + +static QByteArray getProgramName() +{ + if (QCoreApplication::instance()) { + return QCoreApplication::instance()->applicationName().toLocal8Bit(); + } else { + return ""; + } +} + +static bool containsItemStartingWith(const QByteArray &pattern, const QByteArrayList &list) +{ + for (const auto &item : list) { + if (pattern.startsWith(item)) { + return true; + } + } + return false; +} + QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) { + static NullStream nullstream; DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); if (debugLevel < debugOutputLevel) { - static NullStream stream; - return QDebug(&stream); + return QDebug(&nullstream); } - static DebugStream stream; - QDebug debug(&stream); + auto areas = qgetenv("SINKDEBUGAREAS").split(','); + if (debugArea && !areas.isEmpty()) { + if (!containsItemStartingWith(debugArea, areas)) { + return QDebug(&nullstream); + } + } + static QByteArray programName = getProgramName(); - static QByteArray programName; - if (programName.isEmpty()) { - if (QCoreApplication::instance()) - programName = QCoreApplication::instance()->applicationName().toLocal8Bit(); - else - programName = ""; + auto filter = qgetenv("SINKDEBUGFILTER").split(','); + if (!filter.isEmpty() && !filter.contains(programName)) { + if (!containsItemStartingWith(programName, filter)) { + return QDebug(&nullstream); + } } QString prefix; @@ -221,6 +254,9 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, } output += ": "; + static DebugStream stream; + QDebug debug(&stream); + debug.noquote().nospace() << output; return debug; diff --git a/common/log.h b/common/log.h index 564c574..c643f84 100644 --- a/common/log.h +++ b/common/log.h @@ -16,15 +16,31 @@ enum DebugLevel { QByteArray SINKCOMMON_EXPORT debugLevelName(DebugLevel debugLevel); DebugLevel SINKCOMMON_EXPORT debugLevelFromName(const QByteArray &name); +/** + * Sets the debug output level. + * + * Everything below is ignored. + */ void SINKCOMMON_EXPORT setDebugOutputLevel(DebugLevel); DebugLevel SINKCOMMON_EXPORT debugOutputLevel(); +/// Set debug areas that should be logged +void SINKCOMMON_EXPORT setAreas(const QByteArrayList &areas); + +/** + * Set an application name filter. + * + * Note: In case of resources the identifier is the application name. + */ +void SINKCOMMON_EXPORT setFilter(const QByteArrayList &filter); + + QDebug SINKCOMMON_EXPORT debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); } } -#define DEBUG_AREA 0 +#define DEBUG_AREA nullptr #define Trace_() Sink::Log::debugStream(Sink::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) #define Log_() Sink::Log::debugStream(Sink::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) -- cgit v1.2.3