diff options
-rw-r--r-- | src/Common/ConfigManager.cpp | 20 | ||||
-rw-r--r-- | src/Common/ConfigManager.h | 3 | ||||
-rw-r--r-- | src/Common/Logger.cpp | 53 | ||||
-rw-r--r-- | src/Common/Logger.h | 38 | ||||
-rw-r--r-- | src/Common/LoggerBase.h | 6 | ||||
-rw-r--r-- | src/mad-core.cpp | 5 | ||||
-rw-r--r-- | src/mad.cpp | 12 | ||||
-rw-r--r-- | src/madc.cpp | 8 |
8 files changed, 94 insertions, 51 deletions
diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp index c119cf7..6727cc3 100644 --- a/src/Common/ConfigManager.cpp +++ b/src/Common/ConfigManager.cpp @@ -52,7 +52,10 @@ void ConfigManager::handleConfigEntry(const ConfigEntry &entry) { Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str()); } -bool ConfigManager::loadFile(const std::string &filename, bool finish) { +bool ConfigManager::loadFile(const std::string &filename) { + if(finished) + return false; + std::ifstream file(filename.c_str()); ConfigEntry entry; std::string line, input; @@ -112,13 +115,18 @@ bool ConfigManager::loadFile(const std::string &filename, bool finish) { // TODO Depth check - if(finish) { - for(std::set<Configurable*>::iterator c = configurables.begin(); c != configurables.end(); ++c) - (*c)->configFinished(); - } - return true; } +void ConfigManager::finish() { + if(finished) + return; + + for(std::set<Configurable*>::iterator c = configurables.begin(); c != configurables.end(); ++c) + (*c)->configFinished(); + + finished = true; +} + } } diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h index 45a78ec..bf34f6f 100644 --- a/src/Common/ConfigManager.h +++ b/src/Common/ConfigManager.h @@ -46,7 +46,8 @@ class ConfigManager { void handleConfigEntry(const ConfigEntry &entry); public: - bool loadFile(const std::string &filename, bool finish = true); + bool loadFile(const std::string &filename); + void finish(); void registerConfigurable(Configurable *c) { configurables.insert(c); diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp index 27c98ba..51a320f 100644 --- a/src/Common/Logger.cpp +++ b/src/Common/Logger.cpp @@ -20,6 +20,8 @@ #include "Logger.h" #include "ConfigEntry.h" #include "ConfigManager.h" +#include "Backends/ConsoleLogger.h" +#include "Backends/FileLogger.h" #include <cstdlib> @@ -27,21 +29,49 @@ namespace Mad { namespace Common { std::auto_ptr<Logger::ConfigHelper> Logger::configHelper; -std::list<Logger*> Logger::loggers; - +std::set<Logger*> Logger::loggers; +std::auto_ptr<std::queue<Logger::Message> > Logger::messageQueue(new std::queue<Message>); bool Logger::ConfigHelper::handleConfigEntry(const ConfigEntry &entry, bool handled) { if(handled) return false; - if(entry[0].getKey().matches("Logger") && entry[1].empty()) { - logf(WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); - return true; + if(entry[0].getKey().matches("Logger")) { + if(entry[0][0].matches("Console")) { + if(entry[1].empty()) { + loggers.insert(new Backends::ConsoleLogger()); + return true; + } + } + else if(entry[1].empty()) { + logf(WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); + return true; + } } return false; } +void Logger::ConfigHelper::configFinished() { + std::auto_ptr<std::queue<Message> > queue = messageQueue; + + if(loggers.empty()) + loggers.insert(new Backends::ConsoleLogger()); + + while(!queue->empty()) { + const Message &message = queue->front(); + log(message.category, message.level, message.timestamp, message.message); + queue->pop(); + } +} + +Logger::ConfigHelper::~ConfigHelper() { + // TODO delete all those loggers... + + //while(!loggers.empty()) + // delete (*loggers.begin()); +} + void Logger::logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap) { int size = 100; char *buf = (char*)std::malloc(size); @@ -70,11 +100,16 @@ void Logger::logfv(MessageCategory category, MessageLevel level, const char *for } -void Logger::log(MessageCategory category, MessageLevel level, const std::string &message) { - time_t messageTimestamp = std::time(NULL); - for(std::list<Logger*>::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { +void Logger::log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) { + if(messageQueue.get()) { // Queue message if loggers aren't configured yet + Message m = {category, level, timestamp, message}; + messageQueue->push(m); + return; + } + + for(std::set<Logger*>::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { if((*logger)->getLevel() >= level && (*logger)->isCategorySet(category)) - (*logger)->logMessage(category, level, messageTimestamp, message); + (*logger)->logMessage(category, level, timestamp, message); } } diff --git a/src/Common/Logger.h b/src/Common/Logger.h index 9499792..cf7e7d5 100644 --- a/src/Common/Logger.h +++ b/src/Common/Logger.h @@ -26,8 +26,9 @@ #include <algorithm> #include <cstdarg> #include <ctime> -#include <list> #include <memory> +#include <queue> +#include <set> #include <string> namespace Mad { @@ -38,22 +39,38 @@ class Logger : public LoggerBase { class ConfigHelper : private Configurable { protected: virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); + virtual void configFinished(); + + public: + ~ConfigHelper(); + }; + + struct Message { + MessageCategory category; + MessageLevel level; + time_t timestamp; + std::string message; }; static std::auto_ptr<ConfigHelper> configHelper; - static std::list<Logger*> loggers; + static std::set<Logger*> loggers; + static std::auto_ptr<std::queue<Message> > messageQueue; static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap); + static void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message); + protected: - virtual void logMessage(MessageCategory category, MessageLevel level, time_t messageTimestamp, const std::string &message) = 0; + virtual void logMessage(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) = 0; public: static void initConfigHelper() { configHelper.reset(new ConfigHelper()); } - static void log(MessageCategory category, MessageLevel level, const std::string &message); + static void log(MessageCategory category, MessageLevel level, const std::string &message) { + log(category, level, std::time(0), message); + } static void log(MessageCategory category, const std::string &message) { log(category, DEFAULT, message); } @@ -68,19 +85,6 @@ class Logger : public LoggerBase { static void logf(MessageCategory category, const char *format, ...); static void logf(MessageLevel level, const char *format, ...); static void logf(const char *format, ...); - - static void registerLogger(Logger *logger) { - loggers.push_back(logger); - } - - static void unregisterLogger(Logger *logger) { - std::list<Logger*>::iterator it = std::find(loggers.begin(), loggers.end(), logger); - if(it != loggers.end()) - loggers.erase(it); - } - - Logger() {} - virtual ~Logger() {} }; } diff --git a/src/Common/LoggerBase.h b/src/Common/LoggerBase.h index 0513594..b06f03a 100644 --- a/src/Common/LoggerBase.h +++ b/src/Common/LoggerBase.h @@ -40,6 +40,9 @@ class LoggerBase { std::bitset<16> categories; MessageLevel level; + LoggerBase() : level(DEFAULT) {setAllCategories();} + virtual ~LoggerBase() {} + public: void setCategory(MessageCategory newCategory) { categories.set(newCategory); @@ -68,9 +71,6 @@ class LoggerBase { void setLevel(MessageLevel newLevel) { level = newLevel; } - - LoggerBase() : level(DEFAULT) {setAllCategories();} - virtual ~LoggerBase() {} }; } diff --git a/src/mad-core.cpp b/src/mad-core.cpp index 02be19c..19c8b05 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -19,7 +19,6 @@ #include "Common/ConfigManager.h" #include "Common/Logger.h" -#include "Common/Backends/ConsoleLogger.h" #include "Common/Backends/SystemBackendPosix.h" #include "Common/Backends/SystemBackendProc.h" #include "Net/Connection.h" @@ -37,14 +36,12 @@ int main() { sigaddset(&signals, SIGPIPE); sigprocmask(SIG_BLOCK, &signals, 0); - Common::Backends::ConsoleLogger logger; - Common::Logger::registerLogger(&logger); - Common::Logger::initConfigHelper(); Core::ConnectionManager::init(); Common::ConfigManager::getConfigManager()->loadFile("mad-core.conf"); + Common::ConfigManager::getConfigManager()->finish(); Common::Backends::SystemBackendPosix::registerBackend(); Common::Backends::SystemBackendProc::registerBackend(); diff --git a/src/mad.cpp b/src/mad.cpp index 6ca18ae..36432e1 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -22,8 +22,8 @@ #include "Net/IPAddress.h" #include "Common/Backends/SystemBackendPosix.h" #include "Common/Backends/SystemBackendProc.h" +#include "Common/ConfigManager.h" #include "Common/Logger.h" -#include "Common/Backends/ConsoleLogger.h" #include "Common/Request.h" #include "Common/RequestManager.h" #include "Common/RequestHandlers/FSInfoRequestHandler.h" @@ -42,11 +42,11 @@ static void requestFinished(const Common::Request<>&) { } int main() { - Common::Backends::ConsoleLogger consoleLogger; - Common::Logger::registerLogger(&consoleLogger); - Net::Connection::init(); + Common::Logger::initConfigHelper(); + Common::ConfigManager::getConfigManager()->finish(); + Common::RequestManager::init(false); Common::RequestManager::getRequestManager()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>(Net::Packet::FS_INFO); Common::RequestManager::getRequestManager()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>(Net::Packet::STATUS); @@ -67,7 +67,7 @@ int main() { Common::RequestManager::getRequestManager()->registerConnection(connection); Daemon::Backends::NetworkLogger networkLogger(connection); - Common::Logger::registerLogger(&networkLogger); + //TODO Common::Logger::registerLogger(&networkLogger); //char hostname[256]; @@ -78,8 +78,6 @@ int main() { while(connection->isConnected()) Net::FdManager::getFdManager()->run(); - Common::Logger::unregisterLogger(&networkLogger); - Common::RequestManager::getRequestManager()->unregisterConnection(connection); } catch(Mad::Common::Exception &e) { diff --git a/src/madc.cpp b/src/madc.cpp index 7f022b0..0dc1e81 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -20,8 +20,8 @@ #include "Net/ClientConnection.h" #include "Net/FdManager.h" #include "Net/IPAddress.h" +#include "Common/ConfigManager.h" #include "Common/Logger.h" -#include "Common/Backends/ConsoleLogger.h" #include "Common/RequestManager.h" #include "Client/CommandParser.h" #include "Client/InformationManager.h" @@ -79,11 +79,11 @@ int main(int argc, char *argv[]) { std::exit(1); } - Common::Backends::ConsoleLogger logger; - Common::Logger::registerLogger(&logger); - Net::Connection::init(); + Common::Logger::initConfigHelper(); + Common::ConfigManager::getConfigManager()->finish(); + Common::RequestManager::init(false); Net::ClientConnection *connection = new Net::ClientConnection; |