From fa89133f9c84c51ac40e3d5172a377e3554ebf27 Mon Sep 17 00:00:00 2001 From: emeric Date: Mon, 25 Aug 2014 15:10:16 +0200 Subject: [PATCH] WIP.Logger: Added severity level config. Added module tag --- config/ConfigReader.cpp | 1 + etc/lms.conf.sample | 1 + logger/Logger.cpp | 91 ++++++++++++++++++++++++++++++++++++ logger/Logger.hpp | 101 ++++++++++++++++++++++++++++++++++++++++ main/main.cpp | 1 + 5 files changed, 195 insertions(+) create mode 100644 logger/Logger.cpp create mode 100644 logger/Logger.hpp diff --git a/config/ConfigReader.cpp b/config/ConfigReader.cpp index ad498772..1996a6ad 100644 --- a/config/ConfigReader.cpp +++ b/config/ConfigReader.cpp @@ -10,6 +10,7 @@ ConfigReader::getLoggerConfig(Logger::Config& config) { config.enableFileLogging = _config.lookupValue("main.logger.file", config.logPath); config.enableConsoleLogging = _config.lookup("main.logger.console"); + config.minSeverity = static_cast((int)_config.lookup("main.logger.level")); } void diff --git a/etc/lms.conf.sample b/etc/lms.conf.sample index acf08036..7308214e 100644 --- a/etc/lms.conf.sample +++ b/etc/lms.conf.sample @@ -4,6 +4,7 @@ main = { logger = { file = "/var/lms/lms.log" # comment to disable file logging console = true; + level = 7; } db = "/var/lms/lms.db"; diff --git a/logger/Logger.cpp b/logger/Logger.cpp new file mode 100644 index 00000000..12b6e6dc --- /dev/null +++ b/logger/Logger.cpp @@ -0,0 +1,91 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +#include "Logger.hpp" + +Logger& +Logger::instance() +{ + static Logger instance; + return instance; +} + +Logger::Logger() +{ + // Initialiaz loggers + static const struct LoggerDef { + Module module; + std::string name; + } loggers[] = { + {MOD_MAIN, "MAIN"}, + {MOD_UI, "UI"}, + {MOD_REMOTE, "REMOTE"} + }; + + BOOST_FOREACH(const struct LoggerDef& logger, loggers) { + std::cout << "Adding attribute" << std::endl; + _loggers[logger.module].add_attribute("Module", boost::log::attributes::constant< Module >(logger.module)); + } +} + +void +Logger::init(const Config& config) +{ + boost::log::add_common_attributes(); + + boost::log::register_simple_formatter_factory< Severity, char >("Severity"); + + if (config.enableFileLogging) + { + boost::log::add_file_log + ( + boost::log::keywords::file_name = config.logPath + std::string(".%N"), + boost::log::keywords::rotation_size = 10 * 1024 * 1024, + boost::log::keywords::open_mode = std::ios_base::app, + boost::log::keywords::format = ( + boost::log::expressions::stream + << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "[%Y-%m-%d %H:%M:%S]") + << " [" << boost::log::expressions::attr< Module >("Module") << "]" + << " [" << boost::log::expressions::attr< Severity >("Severity") << "]" + << " " << boost::log::expressions::smessage + ) + ); + } + + if (config.enableConsoleLogging) + { + boost::log::add_console_log(std::cout, + boost::log::keywords::format = ( + boost::log::expressions::stream + << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "[%Y-%m-%d %H:%M:%S]") + << " [" << boost::log::expressions::attr< Module >("Module") << "]" + << " [" << boost::log::expressions::attr< Severity >("Severity") << "]" + << " " << boost::log::expressions::smessage + ) + ); + } + + boost::log::core::get()->set_filter + ( + boost::log::expressions::attr("Severity") <= config.minSeverity + ); + +} + diff --git a/logger/Logger.hpp b/logger/Logger.hpp new file mode 100644 index 00000000..4a06c2bd --- /dev/null +++ b/logger/Logger.hpp @@ -0,0 +1,101 @@ +#ifndef LOGGER_HPP__ +#define LOGGER_HPP__ + +#include + +#include +#include + +#include +#include + +#define LMS_LOG(module, level) BOOST_LOG_SEV(Logger::instance().get(module), level) + +enum Severity +{ + SEV_DEBUG = 7, + SEV_INFO = 6, + SEV_NOTICE = 5, + SEV_WARNING = 4, + SEV_ERROR = 3, + SEV_CRIT = 2, +}; + +enum Module +{ + MOD_MAIN = 0, + MOD_UI, + MOD_REMOTE, +}; + +BOOST_LOG_ATTRIBUTE_KEYWORD(module, "Module", Module) + +class Logger +{ + public: + + static Logger& instance(); + + struct Config { + bool enableFileLogging; + bool enableConsoleLogging; + std::string logPath; + Severity minSeverity; + }; + + //[ example_tutorial_file_advanced + void init(const Config& config); + + boost::log::sources::severity_logger< Severity >& + get(Module module) { return _loggers[module]; } + + private: + + Logger(); + + std::map > _loggers; +}; + +// The formatting logic for the severity level +template< typename CharT, typename TraitsT > +inline std::basic_ostream< CharT, TraitsT >& operator<< ( + std::basic_ostream< CharT, TraitsT >& strm, Severity lvl) +{ + static const char* const str[] = + { + "", + "", + "CRIT", + "ERROR", + "WARNING", + "NOTICE", + "INFO", + "DEBUG" + }; + if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str))) + strm << str[lvl]; + else + strm << static_cast< int >(lvl); + return strm; +} + +template< typename CharT, typename TraitsT > +inline std::basic_ostream< CharT, TraitsT >& operator<< ( + std::basic_ostream< CharT, TraitsT >& strm, Module val) +{ + static const char* const str[] = + { + "MAIN", + "UI", + "REMOTE", + }; + if (static_cast< std::size_t >(val) < (sizeof(str) / sizeof(*str))) + strm << str[val]; + else + strm << static_cast< int >(val); + return strm; +} + + +#endif // LOGGER_HPP__ + diff --git a/main/main.cpp b/main/main.cpp index 3bd54169..a5c606d7 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -48,6 +48,7 @@ int main(int argc, char* argv[]) Logger::instance().init(loggerConfig); } + Service::DatabaseUpdateService::Config dbUpdateConfig; configReader.getDatabaseUpdateConfig(dbUpdateConfig);