diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/log.cpp | 56 | ||||
-rw-r--r-- | 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() | |||
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; |
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 { | |||
16 | QByteArray SINKCOMMON_EXPORT debugLevelName(DebugLevel debugLevel); | 16 | QByteArray SINKCOMMON_EXPORT debugLevelName(DebugLevel debugLevel); |
17 | DebugLevel SINKCOMMON_EXPORT debugLevelFromName(const QByteArray &name); | 17 | DebugLevel SINKCOMMON_EXPORT debugLevelFromName(const QByteArray &name); |
18 | 18 | ||
19 | /** | ||
20 | * Sets the debug output level. | ||
21 | * | ||
22 | * Everything below is ignored. | ||
23 | */ | ||
19 | void SINKCOMMON_EXPORT setDebugOutputLevel(DebugLevel); | 24 | void SINKCOMMON_EXPORT setDebugOutputLevel(DebugLevel); |
20 | DebugLevel SINKCOMMON_EXPORT debugOutputLevel(); | 25 | DebugLevel SINKCOMMON_EXPORT debugOutputLevel(); |
21 | 26 | ||
27 | /// Set debug areas that should be logged | ||
28 | void SINKCOMMON_EXPORT setAreas(const QByteArrayList &areas); | ||
29 | |||
30 | /** | ||
31 | * Set an application name filter. | ||
32 | * | ||
33 | * Note: In case of resources the identifier is the application name. | ||
34 | */ | ||
35 | void SINKCOMMON_EXPORT setFilter(const QByteArrayList &filter); | ||
36 | |||
37 | |||
22 | QDebug SINKCOMMON_EXPORT debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); | 38 | QDebug SINKCOMMON_EXPORT debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); |
23 | 39 | ||
24 | } | 40 | } |
25 | } | 41 | } |
26 | 42 | ||
27 | #define DEBUG_AREA 0 | 43 | #define DEBUG_AREA nullptr |
28 | 44 | ||
29 | #define Trace_() Sink::Log::debugStream(Sink::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) | 45 | #define Trace_() Sink::Log::debugStream(Sink::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) |
30 | #define Log_() Sink::Log::debugStream(Sink::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) | 46 | #define Log_() Sink::Log::debugStream(Sink::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) |