From f48af9daff8546a6893d94382be338a2714f00d1 Mon Sep 17 00:00:00 2001 From: redPanther Date: Tue, 21 Jun 2016 21:41:26 +0200 Subject: [PATCH] extend logger (#34) --- include/utils/Logger.h | 5 +++- libsrc/utils/Logger.cpp | 63 +++++++++++++++++++++++++++++++---------- src/hyperiond/main.cpp | 6 +++- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/include/utils/Logger.h b/include/utils/Logger.h index 77413150..1282ebf4 100644 --- a/include/utils/Logger.h +++ b/include/utils/Logger.h @@ -25,9 +25,11 @@ class Logger { public: - enum LogLevel { DEBUG=0, INFO=1,WARNING=2,ERROR=3 }; + enum LogLevel { UNSET=0,DEBUG=1, INFO=2,WARNING=3,ERROR=4 }; static Logger* getInstance(std::string name="", LogLevel minLevel=Logger::INFO); + static void deleteInstance(std::string name=""); + static void setLogLevel(LogLevel level,std::string name=""); void Message(LogLevel level, const char* sourceFile, const char* func, unsigned int line, const char* fmt, ...); void setMinLevel(LogLevel level) { _minLevel = level; }; @@ -38,6 +40,7 @@ protected: private: static std::map *LoggerMap; + static LogLevel GLOBAL_MIN_LOG_LEVEL; std::string _name; std::string _appname; diff --git a/libsrc/utils/Logger.cpp b/libsrc/utils/Logger.cpp index 4046d2a1..b9f59114 100644 --- a/libsrc/utils/Logger.cpp +++ b/libsrc/utils/Logger.cpp @@ -13,31 +13,67 @@ std::string getBaseName( std::string sourceFile) return fi.fileName().toStdString(); } -static const char * LogLevelStrings[] = { "DEBUG", "INFO", "WARNING", "ERROR" }; -static const int LogLevelSysLog[] = { LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR }; +static const char * LogLevelStrings[] = { "", "DEBUG", "INFO", "WARNING", "ERROR" }; +static const int LogLevelSysLog[] = { LOG_DEBUG, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR }; static unsigned int loggerCount = 0; static unsigned int loggerId = 0; + std::map *Logger::LoggerMap = nullptr; +Logger::LogLevel Logger::GLOBAL_MIN_LOG_LEVEL = Logger::UNSET; Logger* Logger::getInstance(std::string name, Logger::LogLevel minLevel) { - if (Logger::LoggerMap == nullptr) + if (LoggerMap == nullptr) { - Logger::LoggerMap = new std::map; + LoggerMap = new std::map; } if ( LoggerMap->find(name) == LoggerMap->end() ) { Logger* log = new Logger(name,minLevel); - Logger::LoggerMap->insert(std::pair(name,log)); // compat version, replace it with following line if we have 100% c++11 - //Logger::LoggerMap->emplace(name,log); // not compat with older linux distro's e.g. wheezy + LoggerMap->insert(std::pair(name,log)); // compat version, replace it with following line if we have 100% c++11 + //LoggerMap->emplace(name,log); // not compat with older linux distro's e.g. wheezy return log; } - return Logger::LoggerMap->at(name); + return LoggerMap->at(name); } +void Logger::deleteInstance(std::string name) +{ + if (LoggerMap == nullptr) + return; + + if ( name.empty() ) + { + std::map::iterator it; + for ( it=LoggerMap->begin(); it != LoggerMap->end(); it++) + { + delete it->second; + } + LoggerMap->clear(); + } + else if (LoggerMap->find(name) != LoggerMap->end()) + { + delete LoggerMap->at(name); + LoggerMap->erase(name); + } + +} + +void Logger::setLogLevel(LogLevel level,std::string name) +{ + if ( name.empty() ) + { + GLOBAL_MIN_LOG_LEVEL = level; + } + else + { + Logger* log = Logger::getInstance(name,level); + log->setMinLevel(level); + } +} Logger::Logger ( std::string name, LogLevel minLevel ): @@ -54,12 +90,7 @@ Logger::Logger ( std::string name, LogLevel minLevel ): std::transform(_appname.begin(), _appname.end(),_appname.begin(), ::toupper); loggerCount++; - -// if (pLoggerMap == NULL) -// pLoggerMap = new std::map; -// -// - + if (_syslogEnabled && loggerCount == 1 ) { openlog (program_invocation_short_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); @@ -68,7 +99,7 @@ Logger::Logger ( std::string name, LogLevel minLevel ): Logger::~Logger() { - //LoggerMap.erase(_loggerId); + Debug(this, "logger '%s' destroyed", _name.c_str() ); loggerCount--; if ( loggerCount == 0 ) closelog(); @@ -77,9 +108,11 @@ Logger::~Logger() void Logger::Message(LogLevel level, const char* sourceFile, const char* func, unsigned int line, const char* fmt, ...) { - if ( level < _minLevel ) + if ( (GLOBAL_MIN_LOG_LEVEL == Logger::UNSET && level < _minLevel) // no global level, use level from logger + || (GLOBAL_MIN_LOG_LEVEL > Logger::UNSET && level < GLOBAL_MIN_LOG_LEVEL) ) // global level set, use global level return; + char msg[512]; va_list args; va_start (args, fmt); diff --git a/src/hyperiond/main.cpp b/src/hyperiond/main.cpp index 6a72c5e1..d6e40984 100644 --- a/src/hyperiond/main.cpp +++ b/src/hyperiond/main.cpp @@ -39,7 +39,9 @@ void startNewHyperion(int parentPid, std::string hyperionFile, std::string confi int main(int argc, char** argv) { - Logger* log = Logger::getInstance("MAIN", Logger::INFO); + // initialize main logger and set global log level + Logger* log = Logger::getInstance("MAIN"); + Logger::setLogLevel(Logger::INFO); // Initialising QCoreApplication QCoreApplication app(argc, argv); @@ -125,8 +127,10 @@ int main(int argc, char** argv) int rc = app.exec(); Info(log, "INFO: Application closed with code %d", rc); + // delete components delete webConfig; delete hyperiond; + Logger::deleteInstance(); return rc; }