global logbuffer (#297)

* - implement a global logbuffer
- providerrs232 reduce log spam

* logger add signal and start json push

* implement logger notifier ... need some cleanup
This commit is contained in:
redPanther
2016-11-26 22:34:46 +01:00
committed by GitHub
parent 9360183f2e
commit 73406c982b
7 changed files with 247 additions and 70 deletions

View File

@@ -4,22 +4,25 @@
#include <iostream>
#include <algorithm>
#include <syslog.h>
#include <map>
#include <QFileInfo>
#include <QString>
#include <time.h>
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;
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;
static const int loggerMaxMsgBufferSize = 50;
std::map<std::string,Logger*> *Logger::LoggerMap = nullptr;
Logger::LogLevel Logger::GLOBAL_MIN_LOG_LEVEL = Logger::UNSET;
QVector<Logger::T_LOG_MESSAGE> *Logger::GlobalLogMessageBuffer = nullptr;
LoggerNotifier* LoggerNotifier::instance = nullptr;
Logger* Logger::getInstance(std::string name, Logger::LogLevel minLevel)
{
Logger* log = nullptr;
if (LoggerMap == nullptr)
{
LoggerMap = new std::map<std::string,Logger*>;
@@ -27,13 +30,22 @@ Logger* Logger::getInstance(std::string name, Logger::LogLevel minLevel)
if ( LoggerMap->find(name) == LoggerMap->end() )
{
Logger* log = new Logger(name,minLevel);
log = new Logger(name,minLevel);
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 LoggerMap->at(name);
connect(log, SIGNAL(newLogMessage(Logger::T_LOG_MESSAGE)), LoggerNotifier::getInstance(), SLOT(handleNewLogMessage(Logger::T_LOG_MESSAGE)));
}
else
{
log = LoggerMap->at(name);
}
if (GlobalLogMessageBuffer == nullptr)
{
GlobalLogMessageBuffer = new QVector<Logger::T_LOG_MESSAGE>;
}
return log;
}
void Logger::deleteInstance(std::string name)
@@ -82,11 +94,12 @@ Logger::LogLevel Logger::getLogLevel(std::string name)
return log->getMinLevel();
}
Logger::Logger ( std::string name, LogLevel minLevel ):
_name(name),
_minLevel(minLevel),
_syslogEnabled(true),
_loggerId(loggerId++)
Logger::Logger ( std::string name, LogLevel minLevel )
: QObject()
, _name(name)
, _minLevel(minLevel)
, _syslogEnabled(true)
, _loggerId(loggerId++)
{
#ifdef __GLIBC__
const char* _appname_char = program_invocation_short_name;
@@ -126,13 +139,32 @@ void Logger::Message(LogLevel level, const char* sourceFile, const char* func, u
vsnprintf (msg, max_msg_length, fmt, args);
va_end (args);
Logger::T_LOG_MESSAGE logMsg;
logMsg.appName = QString::fromStdString(_appname);
logMsg.loggerName = QString::fromStdString(_name);
logMsg.function = QString(func);
logMsg.line = line;
logMsg.fileName = FileUtils::getBaseName(sourceFile);
time(&(logMsg.utime));
logMsg.message = QString(msg);
logMsg.level = level;
logMsg.levelString = QString::fromStdString(LogLevelStrings[level]);
emit newLogMessage(logMsg);
QString location;
QString function(func);
if ( level == Logger::DEBUG )
{
location = "<" + FileUtils::getBaseName(sourceFile) + ":" + QString::number(line)+":"+ function + "()> ";
location = "<" + logMsg.fileName + ":" + QString::number(line)+":"+ logMsg.function + "()> ";
}
GlobalLogMessageBuffer->append(logMsg);
if (GlobalLogMessageBuffer->length() > loggerMaxMsgBufferSize)
{
GlobalLogMessageBuffer->erase(GlobalLogMessageBuffer->begin());
}
std::cout
<< "[" << _appname << " " << _name << "] <"
<< LogLevelStrings[level] << "> " << location.toStdString() << msg
@@ -143,3 +175,25 @@ void Logger::Message(LogLevel level, const char* sourceFile, const char* func, u
}
LoggerNotifier::LoggerNotifier()
: QObject()
{
}
LoggerNotifier::~LoggerNotifier()
{
}
void LoggerNotifier::handleNewLogMessage(Logger::T_LOG_MESSAGE msg)
{
//std::cout << "<" << msg.loggerName.toStdString() << "> " << msg.message.toStdString() << std::endl;
emit newLogMessage(msg);
}
LoggerNotifier* LoggerNotifier::getInstance()
{
if ( instance == nullptr )
instance = new LoggerNotifier();
return instance;
}