summaryrefslogtreecommitdiffstats
path: root/common/log.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-08-13 22:01:31 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-08-13 22:01:31 +0200
commit5807b5dc09e5532532753ba2bb48f014d20ad5a0 (patch)
tree3f60a89f5a131c0bb328241fd83b0df474c254a9 /common/log.cpp
parentb6d5d206de4d02149c6530236154283bf834087a (diff)
downloadsink-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.
Diffstat (limited to 'common/log.cpp')
-rw-r--r--common/log.cpp43
1 files changed, 41 insertions, 2 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
9using namespace Akonadi2::Log;
10
9class DebugStream: public QIODevice 11class DebugStream: public QIODevice
10{ 12{
11public: 13public:
@@ -94,9 +96,46 @@ static QString colorCommand(QList<int> colorCodes)
94 return string; 96 return string;
95} 97}
96 98
97QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) 99QByteArray 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
117DebugLevel 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
130void Akonadi2::Log::setDebugOutputLevel(DebugLevel debugLevel)
131{
132 qputenv("AKONADI2DEBUGLEVEL", debugLevelName(debugLevel));
133}
134
135QDebug 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 }