From 5807b5dc09e5532532753ba2bb48f014d20ad5a0 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 13 Aug 2015 22:01:31 +0200 Subject: 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. --- common/log.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'common/log.cpp') 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 @@ #include #include +using namespace Akonadi2::Log; + class DebugStream: public QIODevice { public: @@ -94,9 +96,46 @@ static QString colorCommand(QList colorCodes) return string; } -QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) +QByteArray debugLevelName(DebugLevel debugLevel) +{ + switch (debugLevel) { + case DebugLevel::Trace: + return "Trace"; + case DebugLevel::Log: + return "Log"; + case DebugLevel::Warning: + return "Warning"; + case DebugLevel::Error: + return "Error"; + default: + break; + }; + Q_ASSERT(false); + return QByteArray(); +} + +DebugLevel debugLevelFromName(const QByteArray &name) +{ + if (name.toLower() == "trace") + return DebugLevel::Trace; + if (name.toLower() == "log") + return DebugLevel::Log; + if (name.toLower() == "warning") + return DebugLevel::Warning; + if (name.toLower() == "error") + return DebugLevel::Error; + return DebugLevel::Log; +} + +void Akonadi2::Log::setDebugOutputLevel(DebugLevel debugLevel) +{ + qputenv("AKONADI2DEBUGLEVEL", debugLevelName(debugLevel)); +} + +QDebug Akonadi2::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) { - if (debugLevel <= DebugLevel::Trace) { + DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("AKONADI2DEBUGLEVEL")); + if (debugLevel < debugOutputLevel) { static NullStream stream; return QDebug(&stream); } -- cgit v1.2.3