From 29d723d82834a30557136f7585254eaf3673ce69 Mon Sep 17 00:00:00 2001 From: Johannes Thorn Date: Sun, 28 Sep 2008 00:35:16 +0200 Subject: Logger gr?ndlich ?berarbeitet und den Networklogger um Timestamp erweitert. --- src/Common/Backends/ConsoleLogger.h | 9 +++-- src/Common/Backends/FileLogger.h | 9 +++-- src/Common/Logger.cpp | 4 ++- src/Common/Logger.h | 50 ++++---------------------- src/Common/Makefile.am | 4 +-- src/Common/Makefile.in | 9 ++--- src/Core/RequestHandlers/LogRequestHandler.cpp | 2 +- src/Daemon/Backends/NetworkLogger.h | 5 +-- src/Daemon/Requests/LogRequest.cpp | 2 +- src/Daemon/Requests/LogRequest.h | 6 ++-- src/Net/Packets/LogPacket.cpp | 4 ++- src/Net/Packets/LogPacket.h | 4 ++- 12 files changed, 45 insertions(+), 63 deletions(-) diff --git a/src/Common/Backends/ConsoleLogger.h b/src/Common/Backends/ConsoleLogger.h index 21b86ae..e1c8c43 100644 --- a/src/Common/Backends/ConsoleLogger.h +++ b/src/Common/Backends/ConsoleLogger.h @@ -21,17 +21,22 @@ #define MAD_COMMON_BACKENDS_CONSOLELOGGER_H_ #include "../Logger.h" +#include "../RemoteLogger.h" #include +#include namespace Mad { namespace Common { namespace Backends { -class ConsoleLogger : public Logger { +class ConsoleLogger : public Logger, public RemoteLogger { protected: - virtual void logMessage(MessageCategory, MessageLevel, const std::string &message) { + virtual void logMessage(MessageCategory, MessageLevel, time_t, const std::string &message) { std::cerr << message << std::endl; } + virtual void logMessage(MessageCategory, MessageLevel, time_t, const std::string &message, const std::string &messageSource) { + std::cerr << message << " from "<< messageSource << std::endl; + } public: ConsoleLogger() {} diff --git a/src/Common/Backends/FileLogger.h b/src/Common/Backends/FileLogger.h index 98538ee..59d2e16 100644 --- a/src/Common/Backends/FileLogger.h +++ b/src/Common/Backends/FileLogger.h @@ -21,20 +21,25 @@ #define MAD_COMMON_BACKENDS_FILELOGGER_H_ #include "../Logger.h" +#include "../RemoteLogger.h" #include +#include namespace Mad { namespace Common { namespace Backends { -class FileLogger : public Logger { +class FileLogger : public Logger, public RemoteLogger { private: std::ofstream file; protected: - virtual void logMessage(MessageCategory, MessageLevel, const std::string &message) { + virtual void logMessage(MessageCategory, MessageLevel, time_t, const std::string &message) { file << message << std::endl; } + virtual void logMessage(MessageCategory, MessageLevel, time_t, const std::string &message, const std::string &messageSource) { + file << message << " from "<< messageSource << std::endl; + } public: FileLogger(const std::string &filename) diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp index f87d41b..a7d3bd1 100644 --- a/src/Common/Logger.cpp +++ b/src/Common/Logger.cpp @@ -19,6 +19,7 @@ #include "Logger.h" #include +#include namespace Mad { namespace Common { @@ -55,9 +56,10 @@ 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::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { if((*logger)->getLevel() >= level && (*logger)->isCategorySet(category)) - (*logger)->logMessage(category, level, message); + (*logger)->logMessage(category, level, messageTimestamp, message); } } diff --git a/src/Common/Logger.h b/src/Common/Logger.h index 593fd7a..1fff654 100644 --- a/src/Common/Logger.h +++ b/src/Common/Logger.h @@ -20,35 +20,24 @@ #ifndef MAD_COMMON_LOGGER_H_ #define MAD_COMMON_LOGGER_H_ +#include "LoggerBase.h" #include -#include #include #include -#include +#include +#include namespace Mad { namespace Common { -class Logger { - public: - enum MessageLevel { - CRITICAL, ERROR, WARNING, DEFAULT, VERBOSE, DEBUG - }; - - enum MessageCategory { - SYSTEM, NETWORK, DAEMON, USER, DISK, PROGRAM, GENERAL - }; - +class Logger : public LoggerBase { private: static std::list loggers; - std::bitset<16> categories; - MessageLevel level; static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap); protected: - Logger() : level(DEFAULT) {setAllCategories();} - virtual void logMessage(MessageCategory category, MessageLevel level, const std::string &message) = 0; + virtual void logMessage(MessageCategory category, MessageLevel level, time_t messageTimestamp, const std::string &message) = 0; public: static void log(MessageCategory category, MessageLevel level, const std::string &message); @@ -77,34 +66,7 @@ class Logger { loggers.erase(it); } - void setCategory(MessageCategory newCategory) { - categories.set(newCategory); - } - - void unsetCategory(MessageCategory oldCategory) { - categories.reset(oldCategory); - } - - void setAllCategories() { - categories.set(); - } - - void unsetAllCategories() { - categories.reset(); - } - - bool isCategorySet(MessageCategory category) { - return categories.test(category); - } - - MessageLevel getLevel() const { - return level; - } - - void setLevel(MessageLevel newLevel) { - level = newLevel; - } - + Logger() {} virtual ~Logger() {} }; diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index d232f0b..be2f08e 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = Backends Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = ConfigManager.cpp Exception.cpp Logger.cpp RequestManager.cpp SystemBackend.cpp Util.cpp +libcommon_la_SOURCES = RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigManager.h Exception.h HostInfo.h Logger.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h +noinst_HEADERS = LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index e50a6d9..40cb554 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -49,8 +49,8 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcommon_la_DEPENDENCIES = Backends/libbackends.la \ Requests/librequests.la RequestHandlers/librequesthandlers.la -am_libcommon_la_OBJECTS = ConfigManager.lo Exception.lo Logger.lo \ - RequestManager.lo SystemBackend.lo Util.lo +am_libcommon_la_OBJECTS = RemoteLogger.lo Logger.lo ConfigManager.lo \ + Exception.lo RequestManager.lo SystemBackend.lo Util.lo libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -200,9 +200,9 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Backends Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = ConfigManager.cpp Exception.cpp Logger.cpp RequestManager.cpp SystemBackend.cpp Util.cpp +libcommon_la_SOURCES = RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigManager.h Exception.h HostInfo.h Logger.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h +noinst_HEADERS = LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h all: all-recursive .SUFFIXES: @@ -257,6 +257,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Exception.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Logger.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RemoteLogger.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackend.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Util.Plo@am__quote@ diff --git a/src/Core/RequestHandlers/LogRequestHandler.cpp b/src/Core/RequestHandlers/LogRequestHandler.cpp index f2021b2..1366710 100644 --- a/src/Core/RequestHandlers/LogRequestHandler.cpp +++ b/src/Core/RequestHandlers/LogRequestHandler.cpp @@ -39,7 +39,7 @@ void LogRequestHandler::handlePacket(Net::Connection *connection, const Net::Pac // TODO Require authentication Net::Packets::LogPacket logPacket(packet); - Common::Logger::logf(logPacket.getCategory(), logPacket.getLevel(), "Remote log: %s", logPacket.getMessage().c_str()); + Common::Logger::log(logPacket.getCategory(), logPacket.getLevel(), logPacket.getMessage().c_str()); connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId())); diff --git a/src/Daemon/Backends/NetworkLogger.h b/src/Daemon/Backends/NetworkLogger.h index b2895d2..f1989bb 100644 --- a/src/Daemon/Backends/NetworkLogger.h +++ b/src/Daemon/Backends/NetworkLogger.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace Mad { namespace Daemon { @@ -34,9 +35,9 @@ class NetworkLogger : public Common::Logger { Net::Connection *connection; protected: - virtual void logMessage(Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, const std::string &message) { + virtual void logMessage(Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, time_t messageTimestamp, const std::string &message) { Common::RequestManager::getRequestManager()->sendRequest(connection, - std::auto_ptr(new Requests::LogRequest(category, level, message, Requests::LogRequest::slot_type())) + std::auto_ptr(new Requests::LogRequest(category, level, messageTimestamp, message, Requests::LogRequest::slot_type())) ); } diff --git a/src/Daemon/Requests/LogRequest.cpp b/src/Daemon/Requests/LogRequest.cpp index 62115cc..3a04284 100644 --- a/src/Daemon/Requests/LogRequest.cpp +++ b/src/Daemon/Requests/LogRequest.cpp @@ -26,7 +26,7 @@ namespace Daemon { namespace Requests { void LogRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packets::LogPacket(Net::Packet::LOG, requestId, category, level, message)); + connection->send(Net::Packets::LogPacket(Net::Packet::LOG, requestId, category, level, messageTimestamp, message)); } void LogRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { diff --git a/src/Daemon/Requests/LogRequest.h b/src/Daemon/Requests/LogRequest.h index a0a2e44..8d3798a 100644 --- a/src/Daemon/Requests/LogRequest.h +++ b/src/Daemon/Requests/LogRequest.h @@ -22,6 +22,7 @@ #include #include +#include namespace Mad { namespace Daemon { @@ -31,6 +32,7 @@ class LogRequest : public Common::Request<> { private: Common::Logger::MessageCategory category; Common::Logger::MessageLevel level; + time_t messageTimestamp; std::string message; protected: @@ -38,8 +40,8 @@ class LogRequest : public Common::Request<> { virtual void handlePacket(Net::Connection*, const Net::Packet &packet); public: - LogRequest(Common::Logger::MessageCategory category0, Common::Logger::MessageLevel level0, const std::string &message0, slot_type slot) - : Common::Request<>(slot), category(category0), level(level0), message(message0) {} + LogRequest(Common::Logger::MessageCategory category0, Common::Logger::MessageLevel level0, time_t messageTimestamp0, const std::string &message0, slot_type slot) + : Common::Request<>(slot), category(category0), level(level0), messageTimestamp(messageTimestamp0), message(message0) {} }; } diff --git a/src/Net/Packets/LogPacket.cpp b/src/Net/Packets/LogPacket.cpp index 97994b0..fbe59cd 100644 --- a/src/Net/Packets/LogPacket.cpp +++ b/src/Net/Packets/LogPacket.cpp @@ -18,12 +18,13 @@ */ #include "LogPacket.h" +#include namespace Mad { namespace Net { namespace Packets { -LogPacket::LogPacket(Type type, uint16_t requestId, Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, const std::string &message) +LogPacket::LogPacket(Type type, uint16_t requestId, Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, time_t messageTimestamp, const std::string &message) : Packet(type, requestId) { setLength(sizeof(LogData) + message.length()); @@ -31,6 +32,7 @@ LogPacket::LogPacket(Type type, uint16_t requestId, Common::Logger::MessageCateg logData->category = htons(category); logData->level = htons(level); + logData->messageTimestamp = htons(messageTimestamp); std::memcpy(logData->message, message.c_str(), message.length()); } diff --git a/src/Net/Packets/LogPacket.h b/src/Net/Packets/LogPacket.h index 814ea28..2c541ae 100644 --- a/src/Net/Packets/LogPacket.h +++ b/src/Net/Packets/LogPacket.h @@ -24,6 +24,7 @@ #include #include +#include namespace Mad { namespace Net { @@ -34,13 +35,14 @@ class LogPacket : public Packet { struct LogData { uint16_t category; uint16_t level; + uint16_t messageTimestamp; uint8_t message[0]; }; LogData *logData; public: - LogPacket(Type type, uint16_t requestId, Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, const std::string &message); + LogPacket(Type type, uint16_t requestId, Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, time_t messageTimestamp, const std::string &message); LogPacket(const Packet &p) : Packet(p) { if(getLength() < sizeof(LogData)) -- cgit v1.2.3