mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
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:
@@ -1,54 +1,64 @@
|
||||
# Define the current source locations
|
||||
|
||||
SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/utils)
|
||||
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/utils)
|
||||
|
||||
SET(Utils_QT_HEADERS
|
||||
${CURRENT_HEADER_DIR}/Logger.h
|
||||
)
|
||||
|
||||
SET(Utils_HEADERS
|
||||
${CURRENT_HEADER_DIR}/ColorBgr.h
|
||||
${CURRENT_HEADER_DIR}/ColorRgb.h
|
||||
${CURRENT_HEADER_DIR}/ColorRgba.h
|
||||
${CURRENT_HEADER_DIR}/ColorRgbw.h
|
||||
${CURRENT_HEADER_DIR}/Image.h
|
||||
${CURRENT_HEADER_DIR}/Sleep.h
|
||||
${CURRENT_HEADER_DIR}/FileUtils.h
|
||||
${CURRENT_HEADER_DIR}/Process.h
|
||||
${CURRENT_HEADER_DIR}/PixelFormat.h
|
||||
${CURRENT_HEADER_DIR}/VideoMode.h
|
||||
${CURRENT_HEADER_DIR}/ImageResampler.h
|
||||
${CURRENT_HEADER_DIR}/HsvTransform.h
|
||||
${CURRENT_HEADER_DIR}/HslTransform.h
|
||||
${CURRENT_HEADER_DIR}/RgbChannelTransform.h
|
||||
${CURRENT_HEADER_DIR}/RgbChannelAdjustment.h
|
||||
${CURRENT_HEADER_DIR}/RgbToRgbw.h
|
||||
${CURRENT_HEADER_DIR}/jsonschema/QJsonFactory.h
|
||||
${CURRENT_HEADER_DIR}/jsonschema/QJsonSchemaChecker.h
|
||||
)
|
||||
|
||||
SET(Utils_SOURCES
|
||||
${CURRENT_SOURCE_DIR}/ColorArgb.cpp
|
||||
${CURRENT_SOURCE_DIR}/ColorBgr.cpp
|
||||
${CURRENT_SOURCE_DIR}/ColorRgb.cpp
|
||||
${CURRENT_SOURCE_DIR}/ColorRgba.cpp
|
||||
${CURRENT_SOURCE_DIR}/ColorRgbw.cpp
|
||||
${CURRENT_SOURCE_DIR}/FileUtils.cpp
|
||||
${CURRENT_SOURCE_DIR}/Process.cpp
|
||||
${CURRENT_SOURCE_DIR}/Logger.cpp
|
||||
${CURRENT_SOURCE_DIR}/ImageResampler.cpp
|
||||
${CURRENT_SOURCE_DIR}/HsvTransform.cpp
|
||||
${CURRENT_SOURCE_DIR}/HslTransform.cpp
|
||||
${CURRENT_SOURCE_DIR}/RgbChannelTransform.cpp
|
||||
${CURRENT_SOURCE_DIR}/RgbChannelAdjustment.cpp
|
||||
${CURRENT_SOURCE_DIR}/RgbToRgbw.cpp
|
||||
${CURRENT_SOURCE_DIR}/jsonschema/QJsonSchemaChecker.cpp
|
||||
)
|
||||
|
||||
if ( ENABLE_PROFILER )
|
||||
SET ( PROFILER_SOURCE ${CURRENT_HEADER_DIR}/Profiler.h ${CURRENT_SOURCE_DIR}/Profiler.cpp )
|
||||
endif()
|
||||
|
||||
qt5_wrap_cpp(Utils_HEADERS_MOC ${Utils_QT_HEADERS})
|
||||
|
||||
|
||||
add_library(hyperion-utils
|
||||
${CURRENT_HEADER_DIR}/ColorArgb.h
|
||||
${CURRENT_SOURCE_DIR}/ColorArgb.cpp
|
||||
${CURRENT_HEADER_DIR}/ColorBgr.h
|
||||
${CURRENT_SOURCE_DIR}/ColorBgr.cpp
|
||||
${CURRENT_HEADER_DIR}/ColorRgb.h
|
||||
${CURRENT_SOURCE_DIR}/ColorRgb.cpp
|
||||
${CURRENT_HEADER_DIR}/ColorRgba.h
|
||||
${CURRENT_SOURCE_DIR}/ColorRgba.cpp
|
||||
${CURRENT_HEADER_DIR}/ColorRgbw.h
|
||||
${CURRENT_SOURCE_DIR}/ColorRgbw.cpp
|
||||
${CURRENT_HEADER_DIR}/Image.h
|
||||
${CURRENT_HEADER_DIR}/Sleep.h
|
||||
${CURRENT_HEADER_DIR}/FileUtils.h
|
||||
${CURRENT_SOURCE_DIR}/FileUtils.cpp
|
||||
${CURRENT_HEADER_DIR}/Process.h
|
||||
${CURRENT_SOURCE_DIR}/Process.cpp
|
||||
|
||||
${CURRENT_HEADER_DIR}/Logger.h
|
||||
${CURRENT_SOURCE_DIR}/Logger.cpp
|
||||
${Utils_QT_HEADERS}
|
||||
${Utils_HEADERS_MOC}
|
||||
${Utils_HEADERS}
|
||||
${Utils_SOURCES}
|
||||
${PROFILER_SOURCE}
|
||||
|
||||
${CURRENT_HEADER_DIR}/PixelFormat.h
|
||||
${CURRENT_HEADER_DIR}/VideoMode.h
|
||||
|
||||
${CURRENT_HEADER_DIR}/ImageResampler.h
|
||||
${CURRENT_SOURCE_DIR}/ImageResampler.cpp
|
||||
|
||||
${CURRENT_HEADER_DIR}/HsvTransform.h
|
||||
${CURRENT_SOURCE_DIR}/HsvTransform.cpp
|
||||
${CURRENT_HEADER_DIR}/HslTransform.h
|
||||
${CURRENT_SOURCE_DIR}/HslTransform.cpp
|
||||
${CURRENT_HEADER_DIR}/RgbChannelTransform.h
|
||||
${CURRENT_SOURCE_DIR}/RgbChannelTransform.cpp
|
||||
${CURRENT_HEADER_DIR}/RgbChannelAdjustment.h
|
||||
${CURRENT_SOURCE_DIR}/RgbChannelAdjustment.cpp
|
||||
|
||||
${CURRENT_HEADER_DIR}/RgbToRgbw.h
|
||||
${CURRENT_SOURCE_DIR}/RgbToRgbw.cpp
|
||||
|
||||
${CURRENT_HEADER_DIR}/jsonschema/QJsonFactory.h
|
||||
${CURRENT_HEADER_DIR}/jsonschema/QJsonSchemaChecker.h
|
||||
${CURRENT_SOURCE_DIR}/jsonschema/QJsonSchemaChecker.cpp
|
||||
)
|
||||
|
||||
qt5_use_modules(hyperion-utils Core)
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -21,12 +21,12 @@ double getClockDelta(clock_t start)
|
||||
return ((double)(clock() - start) / CLOCKS_PER_SEC) ;
|
||||
}
|
||||
|
||||
Profiler::Profiler(const char* sourceFile, const char* func, unsigned int line) :
|
||||
_file(sourceFile),
|
||||
_func(func),
|
||||
_line(line),
|
||||
_blockId(blockCounter++),
|
||||
_startTime(clock())
|
||||
Profiler::Profiler(const char* sourceFile, const char* func, unsigned int line)
|
||||
: _file(sourceFile)
|
||||
, _func(func)
|
||||
, _line(line)
|
||||
, _blockId(blockCounter++)
|
||||
, _startTime(clock())
|
||||
{
|
||||
Profiler::initLogger();
|
||||
_logger->Message(Logger::DEBUG,_file,_func,_line,">>> enter block %d", _blockId);
|
||||
|
Reference in New Issue
Block a user