diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-08-13 22:01:31 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-08-13 22:01:31 +0200 |
commit | 5807b5dc09e5532532753ba2bb48f014d20ad5a0 (patch) | |
tree | 3f60a89f5a131c0bb328241fd83b0df474c254a9 | |
parent | b6d5d206de4d02149c6530236154283bf834087a (diff) | |
download | sink-5807b5dc09e5532532753ba2bb48f014d20ad5a0.tar.gz sink-5807b5dc09e5532532753ba2bb48f014d20ad5a0.zip |
A way to set the debuglevel.
Unittests can set the level themselves (so i.e. benchmarks don't print
a shitload of messages), while in normal operation we can set it from
the environment. There's no override currently, but first things first.
-rw-r--r-- | common/log.cpp | 43 | ||||
-rw-r--r-- | common/log.h | 16 | ||||
-rw-r--r-- | common/resourceaccess.cpp | 4 |
3 files changed, 55 insertions, 8 deletions
diff --git a/common/log.cpp b/common/log.cpp index 95a9c01..1fe09be 100644 --- a/common/log.cpp +++ b/common/log.cpp | |||
@@ -6,6 +6,8 @@ | |||
6 | #include <iostream> | 6 | #include <iostream> |
7 | #include <unistd.h> | 7 | #include <unistd.h> |
8 | 8 | ||
9 | using namespace Akonadi2::Log; | ||
10 | |||
9 | class DebugStream: public QIODevice | 11 | class DebugStream: public QIODevice |
10 | { | 12 | { |
11 | public: | 13 | public: |
@@ -94,9 +96,46 @@ static QString colorCommand(QList<int> colorCodes) | |||
94 | return string; | 96 | return string; |
95 | } | 97 | } |
96 | 98 | ||
97 | QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) | 99 | QByteArray debugLevelName(DebugLevel debugLevel) |
100 | { | ||
101 | switch (debugLevel) { | ||
102 | case DebugLevel::Trace: | ||
103 | return "Trace"; | ||
104 | case DebugLevel::Log: | ||
105 | return "Log"; | ||
106 | case DebugLevel::Warning: | ||
107 | return "Warning"; | ||
108 | case DebugLevel::Error: | ||
109 | return "Error"; | ||
110 | default: | ||
111 | break; | ||
112 | }; | ||
113 | Q_ASSERT(false); | ||
114 | return QByteArray(); | ||
115 | } | ||
116 | |||
117 | DebugLevel debugLevelFromName(const QByteArray &name) | ||
118 | { | ||
119 | if (name.toLower() == "trace") | ||
120 | return DebugLevel::Trace; | ||
121 | if (name.toLower() == "log") | ||
122 | return DebugLevel::Log; | ||
123 | if (name.toLower() == "warning") | ||
124 | return DebugLevel::Warning; | ||
125 | if (name.toLower() == "error") | ||
126 | return DebugLevel::Error; | ||
127 | return DebugLevel::Log; | ||
128 | } | ||
129 | |||
130 | void Akonadi2::Log::setDebugOutputLevel(DebugLevel debugLevel) | ||
131 | { | ||
132 | qputenv("AKONADI2DEBUGLEVEL", debugLevelName(debugLevel)); | ||
133 | } | ||
134 | |||
135 | QDebug Akonadi2::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) | ||
98 | { | 136 | { |
99 | if (debugLevel <= DebugLevel::Trace) { | 137 | DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("AKONADI2DEBUGLEVEL")); |
138 | if (debugLevel < debugOutputLevel) { | ||
100 | static NullStream stream; | 139 | static NullStream stream; |
101 | return QDebug(&stream); | 140 | return QDebug(&stream); |
102 | } | 141 | } |
diff --git a/common/log.h b/common/log.h index ee92f46..9db9e8e 100644 --- a/common/log.h +++ b/common/log.h | |||
@@ -2,6 +2,9 @@ | |||
2 | 2 | ||
3 | #include <QDebug> | 3 | #include <QDebug> |
4 | 4 | ||
5 | namespace Akonadi2 { | ||
6 | namespace Log { | ||
7 | |||
5 | enum DebugLevel { | 8 | enum DebugLevel { |
6 | Trace, | 9 | Trace, |
7 | Log, | 10 | Log, |
@@ -9,10 +12,15 @@ enum DebugLevel { | |||
9 | Error | 12 | Error |
10 | }; | 13 | }; |
11 | 14 | ||
15 | void setDebugOutputLevel(DebugLevel); | ||
16 | |||
12 | QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); | 17 | QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); |
13 | 18 | ||
14 | #define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) | 19 | } |
15 | #define Log() debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) | 20 | } |
16 | #define Warning() debugStream(DebugLevel::Warning, __LINE__, __FILE__, Q_FUNC_INFO) | 21 | |
22 | #define Trace() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) | ||
23 | #define Log() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) | ||
24 | #define Warning() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Warning, __LINE__, __FILE__, Q_FUNC_INFO) | ||
17 | //FIXME Error clashes with Storage::Error and MessageQueue::Error | 25 | //FIXME Error clashes with Storage::Error and MessageQueue::Error |
18 | #define ErrorMsg() debugStream(DebugLevel::Error, __LINE__, __FILE__, Q_FUNC_INFO) | 26 | #define ErrorMsg() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Error, __LINE__, __FILE__, Q_FUNC_INFO) |
diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index ef4e64c..84bc4ea 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp | |||
@@ -38,9 +38,9 @@ | |||
38 | #include <QProcess> | 38 | #include <QProcess> |
39 | 39 | ||
40 | #undef Trace | 40 | #undef Trace |
41 | #define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess") | 41 | #define Trace() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess") |
42 | #undef Log | 42 | #undef Log |
43 | #define Log(IDENTIFIER) debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess("+IDENTIFIER+")") | 43 | #define Log(IDENTIFIER) Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess("+IDENTIFIER+")") |
44 | 44 | ||
45 | namespace Akonadi2 | 45 | namespace Akonadi2 |
46 | { | 46 | { |