mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
extend logger (#34)
This commit is contained in:
parent
eccd4e6637
commit
f48af9daff
@ -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<std::string,Logger*> *LoggerMap;
|
||||
static LogLevel GLOBAL_MIN_LOG_LEVEL;
|
||||
|
||||
std::string _name;
|
||||
std::string _appname;
|
||||
|
@ -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<std::string,Logger*> *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<std::string,Logger*>;
|
||||
LoggerMap = new std::map<std::string,Logger*>;
|
||||
}
|
||||
|
||||
if ( LoggerMap->find(name) == LoggerMap->end() )
|
||||
{
|
||||
Logger* log = new Logger(name,minLevel);
|
||||
Logger::LoggerMap->insert(std::pair<std::string,Logger*>(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<std::string,Logger*>(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<std::string,Logger*>::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 ):
|
||||
@ -55,11 +91,6 @@ Logger::Logger ( std::string name, LogLevel minLevel ):
|
||||
|
||||
loggerCount++;
|
||||
|
||||
// if (pLoggerMap == NULL)
|
||||
// pLoggerMap = new std::map<unsigned int,Logger*>;
|
||||
//
|
||||
//
|
||||
|
||||
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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user