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 +++++++++++++++++++++++++++++++++++++++++-- common/log.h | 16 ++++++++++++---- 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 @@ #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); } 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 @@ #include +namespace Akonadi2 { +namespace Log { + enum DebugLevel { Trace, Log, @@ -9,10 +12,15 @@ enum DebugLevel { Error }; +void setDebugOutputLevel(DebugLevel); + QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); -#define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) -#define Log() debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) -#define Warning() debugStream(DebugLevel::Warning, __LINE__, __FILE__, Q_FUNC_INFO) +} +} + +#define Trace() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) +#define Log() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) +#define Warning() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Warning, __LINE__, __FILE__, Q_FUNC_INFO) //FIXME Error clashes with Storage::Error and MessageQueue::Error -#define ErrorMsg() debugStream(DebugLevel::Error, __LINE__, __FILE__, Q_FUNC_INFO) +#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 @@ #include #undef Trace -#define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess") +#define Trace() Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess") #undef Log -#define Log(IDENTIFIER) debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess("+IDENTIFIER+")") +#define Log(IDENTIFIER) Akonadi2::Log::debugStream(Akonadi2::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess("+IDENTIFIER+")") namespace Akonadi2 { -- cgit v1.2.3