summaryrefslogtreecommitdiffstats
path: root/common/log.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-02-05 12:35:39 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-02-05 12:35:39 +0100
commitc8fbb5710737087b0cb5679d6211252dcba4ccce (patch)
tree433329a59a505596500520491dc4ab3f7bc9b9b6 /common/log.cpp
parente1716e503d96d4c8331fb1dc2e701f2b6df240ec (diff)
downloadsink-c8fbb5710737087b0cb5679d6211252dcba4ccce.tar.gz
sink-c8fbb5710737087b0cb5679d6211252dcba4ccce.zip
Use a config file instead of environment variables for log settings.
qputenv only modifies the env of the current process, so akonadish didn't work at all to change the settings. We might have to do some performance optimizations at some point, but for the time being this works.
Diffstat (limited to 'common/log.cpp')
-rw-r--r--common/log.cpp42
1 files changed, 32 insertions, 10 deletions
diff --git a/common/log.cpp b/common/log.cpp
index d2e5f47..4606e67 100644
--- a/common/log.cpp
+++ b/common/log.cpp
@@ -3,11 +3,19 @@
3#include <QString> 3#include <QString>
4#include <QIODevice> 4#include <QIODevice>
5#include <QCoreApplication> 5#include <QCoreApplication>
6#include <QSettings>
7#include <QStandardPaths>
8#include <QSharedPointer>
6#include <iostream> 9#include <iostream>
7#include <unistd.h> 10#include <unistd.h>
8 11
9using namespace Sink::Log; 12using namespace Sink::Log;
10 13
14static QSharedPointer<QSettings> config()
15{
16 return QSharedPointer<QSettings>::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink/log.ini", QSettings::IniFormat);
17}
18
11class DebugStream: public QIODevice 19class DebugStream: public QIODevice
12{ 20{
13public: 21public:
@@ -134,29 +142,44 @@ DebugLevel Sink::Log::debugLevelFromName(const QByteArray &name)
134 142
135void Sink::Log::setDebugOutputLevel(DebugLevel debugLevel) 143void Sink::Log::setDebugOutputLevel(DebugLevel debugLevel)
136{ 144{
137 qputenv("SINKDEBUGLEVEL", debugLevelName(debugLevel)); 145 config()->setValue("level", debugLevel);
138} 146}
139 147
140Sink::Log::DebugLevel Sink::Log::debugOutputLevel() 148Sink::Log::DebugLevel Sink::Log::debugOutputLevel()
141{ 149{
142 return debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); 150 return static_cast<Sink::Log::DebugLevel>(config()->value("level", Sink::Log::Log).toInt());
143} 151}
144 152
145void Sink::Log::setDebugOutputFilter(FilterType type, const QByteArrayList &filter) 153void Sink::Log::setDebugOutputFilter(FilterType type, const QByteArrayList &filter)
146{ 154{
147 switch (type) { 155 switch (type) {
148 case ApplicationName: 156 case ApplicationName:
149 qputenv("SINKDEBUGFILTER", filter.join(',')); 157 config()->setValue("applicationfilter", QVariant::fromValue(filter));
150 break; 158 break;
151 case Area: 159 case Area:
152 qputenv("SINKDEBUGAREAS", filter.join(',')); 160 config()->setValue("areafilter", QVariant::fromValue(filter));
153 break; 161 break;
154 } 162 }
155} 163}
156 164
165QByteArrayList Sink::Log::debugOutputFilter(FilterType type)
166{
167 switch (type) {
168 case ApplicationName:
169 return config()->value("applicationfilter").value<QByteArrayList>();
170 case Area:
171 return config()->value("areafilter").value<QByteArrayList>();
172 }
173}
174
157void Sink::Log::setDebugOutputFields(const QByteArrayList &output) 175void Sink::Log::setDebugOutputFields(const QByteArrayList &output)
158{ 176{
159 qputenv("SINKDEBUGOUTPUT", output.join(',')); 177 config()->setValue("outputfields", QVariant::fromValue(output));
178}
179
180QByteArrayList Sink::Log::debugOutputFields()
181{
182 return config()->value("outputfields").value<QByteArrayList>();
160} 183}
161 184
162static QByteArray getProgramName() 185static QByteArray getProgramName()
@@ -191,12 +214,11 @@ static bool caseInsensitiveContains(const QByteArray &pattern, const QByteArrayL
191QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) 214QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea)
192{ 215{
193 static NullStream nullstream; 216 static NullStream nullstream;
194 DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("SINKDEBUGLEVEL")); 217 if (debugLevel < debugOutputLevel()) {
195 if (debugLevel < debugOutputLevel) {
196 return QDebug(&nullstream); 218 return QDebug(&nullstream);
197 } 219 }
198 220
199 auto areas = qgetenv("SINKDEBUGAREAS").split(','); 221 auto areas = debugOutputFilter(Sink::Log::Area);
200 if (debugArea && !areas.isEmpty()) { 222 if (debugArea && !areas.isEmpty()) {
201 if (!containsItemStartingWith(debugArea, areas)) { 223 if (!containsItemStartingWith(debugArea, areas)) {
202 return QDebug(&nullstream); 224 return QDebug(&nullstream);
@@ -204,7 +226,7 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file,
204 } 226 }
205 static QByteArray programName = getProgramName(); 227 static QByteArray programName = getProgramName();
206 228
207 auto filter = qgetenv("SINKDEBUGFILTER").split(','); 229 auto filter = debugOutputFilter(Sink::Log::ApplicationName);
208 if (!filter.isEmpty() && !filter.contains(programName)) { 230 if (!filter.isEmpty() && !filter.contains(programName)) {
209 if (!containsItemStartingWith(programName, filter)) { 231 if (!containsItemStartingWith(programName, filter)) {
210 return QDebug(&nullstream); 232 return QDebug(&nullstream);
@@ -231,7 +253,7 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file,
231 break; 253 break;
232 }; 254 };
233 255
234 auto debugOutput = qgetenv("SINKDEBUGOUTPUT").split(','); 256 auto debugOutput = debugOutputFields();
235 257
236 bool showLocation = debugOutput.isEmpty() ? false : caseInsensitiveContains("location", debugOutput); 258 bool showLocation = debugOutput.isEmpty() ? false : caseInsensitiveContains("location", debugOutput);
237 bool showFunction = debugOutput.isEmpty() ? false : caseInsensitiveContains("function", debugOutput); 259 bool showFunction = debugOutput.isEmpty() ? false : caseInsensitiveContains("function", debugOutput);