From 85c914d0087458009ec74bbfdd4f7d8345aa1d2c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 31 Aug 2009 23:10:27 +0200 Subject: =?UTF-8?q?Logger:=20Unterst=C3=BCtzung=20f=C3=BCr=20RemoteLogger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/Configurable.h | 2 -- src/Core/LogManager.cpp | 8 +++--- src/Core/Logger.h | 51 +++++++++++++++++++++++++++++++++++++-- src/modules/FileLogger/Module.cpp | 5 +++- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/Core/Configurable.h b/src/Core/Configurable.h index f385e10..c526379 100644 --- a/src/Core/Configurable.h +++ b/src/Core/Configurable.h @@ -20,8 +20,6 @@ #ifndef MAD_CORE_CONFIGURABLE_H_ #define MAD_CORE_CONFIGURABLE_H_ -#include "export.h" - namespace Mad { namespace Core { diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp index 8fe7428..af3e0ba 100644 --- a/src/Core/LogManager.cpp +++ b/src/Core/LogManager.cpp @@ -84,7 +84,10 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { else if(entry[1].getKey().matches("Level")) { if(entry[2].empty()) { try { - consoleLogger->setLevel(parseLevel(entry[1][0])); + if(boost::algorithm::to_lower_copy(static_cast(entry[1][0])) == "remote") + consoleLogger->setRemoteLevel(parseLevel(entry[1][1])); + else + consoleLogger->setLevel(parseLevel(entry[1][0])); } catch(Core::Exception e) { application->logf(Logger::LOG_WARNING, "Unknown log level '%s'.", entry[1][0].c_str()); @@ -142,8 +145,7 @@ void LogManager::loggerThread() { { boost::lock_guard loggerLock(loggerMutex); for(std::set >::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { - if((*logger)->getLevel() >= message.level && (*logger)->isCategorySet(message.category)) - (*logger)->logMessage(message.category, message.level, message.timestamp, message.message, message.source); + (*logger)->log(message.category, message.level, message.timestamp, message.message, message.source); } } diff --git a/src/Core/Logger.h b/src/Core/Logger.h index 24582bc..a43b340 100644 --- a/src/Core/Logger.h +++ b/src/Core/Logger.h @@ -34,7 +34,7 @@ class Logger { friend class LogManager; enum MessageLevel { - LOG_CRITICAL, LOG_ERROR, LOG_WARNING, LOG_DEFAULT, LOG_VERBOSE, LOG_DEBUG + LOG_CRITICAL = 1, LOG_ERROR, LOG_WARNING, LOG_DEFAULT, LOG_VERBOSE, LOG_DEBUG }; enum MessageCategory { @@ -45,10 +45,21 @@ class Logger { std::bitset<16> categories; MessageLevel level; + std::bitset<16> remoteCategories; + MessageLevel remoteLevel; + + bool remote; + protected: - Logger() : level(LOG_DEFAULT) {setAllCategories();} + Logger() : level(LOG_DEFAULT), remoteLevel(LOG_DEFAULT) {setAllCategories(); setAllRemoteCategories(); remote = false;} virtual ~Logger() {} + void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) { + if((source.empty() && getLevel() >= level && isCategorySet(category)) + || (!source.empty() && remote && getRemoteLevel() >= level && isRemoteCategorySet(category))) + logMessage(category, level, timestamp, message, source); + } + virtual void logMessage(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source) = 0; public: @@ -72,6 +83,32 @@ class Logger { return categories.test(category); } + + void setRemoteCategory(MessageCategory newCategory) { + remote = true; + remoteCategories.set(newCategory); + } + + void unsetRemoteCategory(MessageCategory oldCategory) { + remote = true; + remoteCategories.reset(oldCategory); + } + + void setAllRemoteCategories() { + remote = true; + remoteCategories.set(); + } + + void unsetAllRemoteCategories() { + remote = true; + remoteCategories.reset(); + } + + bool isRemoteCategorySet(MessageCategory category) const { + return remoteCategories.test(category); + } + + MessageLevel getLevel() const { return level; } @@ -79,6 +116,16 @@ class Logger { void setLevel(MessageLevel newLevel) { level = newLevel; } + + + MessageLevel getRemoteLevel() const { + return remoteLevel; + } + + void setRemoteLevel(MessageLevel newLevel) { + remote = true; + remoteLevel = newLevel; + } }; } diff --git a/src/modules/FileLogger/Module.cpp b/src/modules/FileLogger/Module.cpp index 88d475c..d000c8b 100644 --- a/src/modules/FileLogger/Module.cpp +++ b/src/modules/FileLogger/Module.cpp @@ -49,7 +49,10 @@ bool Module::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { else if(entry[1].getKey().matches("Level")) { if(entry[2].empty()) { try { - lastLogger->setLevel(Core::LogManager::parseLevel(entry[1][0])); + if(boost::algorithm::to_lower_copy(static_cast(entry[1][0])) == "remote") + lastLogger->setRemoteLevel(Core::LogManager::parseLevel(entry[1][1])); + else + lastLogger->setLevel(Core::LogManager::parseLevel(entry[1][0])); } catch(Core::Exception e) { application->logf(Core::Logger::LOG_WARNING, "Unknown log level '%s'.", entry[1][0].c_str()); -- cgit v1.2.3