diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-04 18:39:18 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-04 18:39:18 +0100 |
commit | 8599c4d371139dc0d4444b128792cd499129d349 (patch) | |
tree | 79376b7fb2496d20265ec245d93964965b0103c4 /common/log.cpp | |
parent | afa7c80114a327d85ee6c99b9d3d811107d9e204 (diff) | |
download | sink-8599c4d371139dc0d4444b128792cd499129d349.tar.gz sink-8599c4d371139dc0d4444b128792cd499129d349.zip |
Filter debug output by program name and area
Diffstat (limited to 'common/log.cpp')
-rw-r--r-- | common/log.cpp | 56 |
1 files changed, 46 insertions, 10 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() | |||
142 | return debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); | 142 | return debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); |
143 | } | 143 | } |
144 | 144 | ||
145 | void Sink::Log::setFilter(const QByteArrayList &filter) | ||
146 | { | ||
147 | qputenv("SINKDEBUGFILTER", filter.join(',')); | ||
148 | } | ||
149 | |||
150 | void Sink::Log::setAreas(const QByteArrayList &filter) | ||
151 | { | ||
152 | qputenv("SINKDEBUGAREAS", filter.join(',')); | ||
153 | } | ||
154 | |||
155 | static QByteArray getProgramName() | ||
156 | { | ||
157 | if (QCoreApplication::instance()) { | ||
158 | return QCoreApplication::instance()->applicationName().toLocal8Bit(); | ||
159 | } else { | ||
160 | return "<unknown program name>"; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | static bool containsItemStartingWith(const QByteArray &pattern, const QByteArrayList &list) | ||
165 | { | ||
166 | for (const auto &item : list) { | ||
167 | if (pattern.startsWith(item)) { | ||
168 | return true; | ||
169 | } | ||
170 | } | ||
171 | return false; | ||
172 | } | ||
173 | |||
145 | QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) | 174 | QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) |
146 | { | 175 | { |
176 | static NullStream nullstream; | ||
147 | DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); | 177 | DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); |
148 | if (debugLevel < debugOutputLevel) { | 178 | if (debugLevel < debugOutputLevel) { |
149 | static NullStream stream; | 179 | return QDebug(&nullstream); |
150 | return QDebug(&stream); | ||
151 | } | 180 | } |
152 | 181 | ||
153 | static DebugStream stream; | 182 | auto areas = qgetenv("SINKDEBUGAREAS").split(','); |
154 | QDebug debug(&stream); | 183 | if (debugArea && !areas.isEmpty()) { |
184 | if (!containsItemStartingWith(debugArea, areas)) { | ||
185 | return QDebug(&nullstream); | ||
186 | } | ||
187 | } | ||
188 | static QByteArray programName = getProgramName(); | ||
155 | 189 | ||
156 | static QByteArray programName; | 190 | auto filter = qgetenv("SINKDEBUGFILTER").split(','); |
157 | if (programName.isEmpty()) { | 191 | if (!filter.isEmpty() && !filter.contains(programName)) { |
158 | if (QCoreApplication::instance()) | 192 | if (!containsItemStartingWith(programName, filter)) { |
159 | programName = QCoreApplication::instance()->applicationName().toLocal8Bit(); | 193 | return QDebug(&nullstream); |
160 | else | 194 | } |
161 | programName = "<unknown program name>"; | ||
162 | } | 195 | } |
163 | 196 | ||
164 | QString prefix; | 197 | QString prefix; |
@@ -221,6 +254,9 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, | |||
221 | } | 254 | } |
222 | output += ": "; | 255 | output += ": "; |
223 | 256 | ||
257 | static DebugStream stream; | ||
258 | QDebug debug(&stream); | ||
259 | |||
224 | debug.noquote().nospace() << output; | 260 | debug.noquote().nospace() << output; |
225 | 261 | ||
226 | return debug; | 262 | return debug; |