2016-09-03 15:54:33 +02:00
|
|
|
#include <utils/Logger.h>
|
|
|
|
#include <utils/FileUtils.h>
|
2016-06-05 16:08:55 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
2019-08-13 20:41:01 +02:00
|
|
|
|
2020-05-12 19:51:19 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <syslog.h>
|
|
|
|
#elif _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <Shlwapi.h>
|
|
|
|
#pragma comment(lib, "Shlwapi.lib")
|
|
|
|
#endif
|
2020-07-19 15:37:47 +02:00
|
|
|
#include <QDateTime>
|
2016-06-05 16:08:55 +02:00
|
|
|
#include <QFileInfo>
|
2020-07-19 15:37:47 +02:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QThreadStorage>
|
2016-11-26 22:34:46 +01:00
|
|
|
#include <time.h>
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2022-01-07 14:47:51 +01:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
2021-11-16 18:12:56 +01:00
|
|
|
QRecursiveMutex Logger::MapLock;
|
|
|
|
#else
|
|
|
|
QMutex Logger::MapLock{ QMutex::Recursive };
|
|
|
|
#endif
|
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
QMap<QString,Logger*> Logger::LoggerMap { };
|
|
|
|
QAtomicInteger<int> Logger::GLOBAL_MIN_LOG_LEVEL { static_cast<int>(Logger::UNSET)};
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2022-01-22 17:48:03 +01:00
|
|
|
const char * LogLevelStrings[] = { "", "DEBUG", "INFO", "WARNING", "ERROR", "OFF" };
|
2020-05-12 19:51:19 +02:00
|
|
|
#ifndef _WIN32
|
2020-07-19 15:37:47 +02:00
|
|
|
const int LogLevelSysLog[] = { LOG_DEBUG, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR };
|
2020-05-12 19:51:19 +02:00
|
|
|
#endif
|
2016-06-21 21:41:26 +02:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
const size_t MAX_IDENTIFICATION_LENGTH = 22;
|
|
|
|
|
|
|
|
QAtomicInteger<unsigned int> LoggerCount = 0;
|
|
|
|
QAtomicInteger<unsigned int> LoggerId = 0;
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
const int MaxRepeatCountSize = 200;
|
|
|
|
QThreadStorage<int> RepeatCount;
|
|
|
|
QThreadStorage<Logger::T_LOG_MESSAGE> RepeatMessage;
|
|
|
|
} // namespace
|
2017-10-12 11:55:03 +02:00
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
Logger* Logger::getInstance(const QString & name, const QString & subName, Logger::LogLevel minLevel)
|
2020-07-19 15:37:47 +02:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&MapLock);
|
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
Logger* log = LoggerMap.value(name + subName, nullptr);
|
2020-07-19 15:37:47 +02:00
|
|
|
if (log == nullptr)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2022-01-22 17:48:03 +01:00
|
|
|
log = new Logger(name, subName, minLevel);
|
2020-07-19 15:37:47 +02:00
|
|
|
LoggerMap.insert(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
|
2019-08-13 20:41:01 +02:00
|
|
|
connect(log, &Logger::newLogMessage, LoggerManager::getInstance(), &LoggerManager::handleNewLogMessage);
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return log;
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
void Logger::deleteInstance(const QString & name, const QString & subName)
|
2016-06-21 21:41:26 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
QMutexLocker lock(&MapLock);
|
2017-10-12 11:55:03 +02:00
|
|
|
|
2020-06-28 23:12:22 +02:00
|
|
|
if (name.isEmpty())
|
2016-06-21 21:41:26 +02:00
|
|
|
{
|
2023-09-09 22:10:22 +02:00
|
|
|
for (auto *logger : std::as_const(LoggerMap)) {
|
2020-07-19 15:37:47 +02:00
|
|
|
delete logger;
|
2022-01-22 17:48:03 +01:00
|
|
|
}
|
2020-07-19 15:37:47 +02:00
|
|
|
|
|
|
|
LoggerMap.clear();
|
2016-06-21 21:41:26 +02:00
|
|
|
}
|
2020-07-19 15:37:47 +02:00
|
|
|
else
|
2016-06-21 21:41:26 +02:00
|
|
|
{
|
2022-01-22 17:48:03 +01:00
|
|
|
delete LoggerMap.value(name + subName, nullptr);
|
|
|
|
LoggerMap.remove(name + subName);
|
2016-06-21 21:41:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
void Logger::setLogLevel(LogLevel level, const QString & name, const QString & subName)
|
2016-06-21 21:41:26 +02:00
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
if (name.isEmpty())
|
2016-06-21 21:41:26 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
GLOBAL_MIN_LOG_LEVEL = static_cast<int>(level);
|
2016-06-21 21:41:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-22 17:48:03 +01:00
|
|
|
Logger* log = Logger::getInstance(name, subName, level);
|
2016-06-21 21:41:26 +02:00
|
|
|
log->setMinLevel(level);
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
Logger::LogLevel Logger::getLogLevel(const QString & name, const QString & subName)
|
2016-06-27 23:56:21 +02:00
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
if (name.isEmpty())
|
2016-06-27 23:56:21 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
return static_cast<Logger::LogLevel>(int(GLOBAL_MIN_LOG_LEVEL));
|
2016-06-27 23:56:21 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
const Logger* log = Logger::getInstance(name + subName);
|
2016-06-27 23:56:21 +02:00
|
|
|
return log->getMinLevel();
|
|
|
|
}
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
Logger::Logger (const QString & name, const QString & subName, LogLevel minLevel)
|
2016-11-26 22:34:46 +01:00
|
|
|
: QObject()
|
|
|
|
, _name(name)
|
2022-01-22 17:48:03 +01:00
|
|
|
, _subname(subName)
|
2016-11-26 22:34:46 +01:00
|
|
|
, _syslogEnabled(true)
|
2020-07-19 15:37:47 +02:00
|
|
|
, _loggerId(LoggerId++)
|
|
|
|
, _minLevel(static_cast<int>(minLevel))
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
qRegisterMetaType<Logger::T_LOG_MESSAGE>();
|
2017-10-12 11:55:03 +02:00
|
|
|
|
2020-07-23 16:50:37 +02:00
|
|
|
if (LoggerCount.fetchAndAddOrdered(1) == 1)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
#ifndef _WIN32
|
2020-07-23 16:50:37 +02:00
|
|
|
if (_syslogEnabled)
|
|
|
|
{
|
2022-01-22 17:48:03 +01:00
|
|
|
openlog (nullptr, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
|
2020-07-23 16:50:37 +02:00
|
|
|
}
|
2020-07-19 15:37:47 +02:00
|
|
|
#endif
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::~Logger()
|
|
|
|
{
|
2020-07-23 16:50:37 +02:00
|
|
|
if (LoggerCount.fetchAndSubOrdered(1) == 0)
|
|
|
|
{
|
2020-05-12 19:51:19 +02:00
|
|
|
#ifndef _WIN32
|
2020-07-23 16:50:37 +02:00
|
|
|
if (_syslogEnabled)
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
}
|
2020-05-12 19:51:19 +02:00
|
|
|
#endif
|
2020-07-23 16:50:37 +02:00
|
|
|
}
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 20:00:36 +02:00
|
|
|
void Logger::write(const Logger::T_LOG_MESSAGE & message)
|
2020-07-19 15:37:47 +02:00
|
|
|
{
|
|
|
|
QString location;
|
|
|
|
if (message.level == Logger::DEBUG)
|
|
|
|
{
|
|
|
|
location = QString("%1:%2:%3() | ")
|
|
|
|
.arg(message.fileName)
|
|
|
|
.arg(message.line)
|
|
|
|
.arg(message.function);
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:48:03 +01:00
|
|
|
QString name = "|" + message.loggerSubName + "| " + message.loggerName;
|
2020-07-19 15:37:47 +02:00
|
|
|
name.resize(MAX_IDENTIFICATION_LENGTH, ' ');
|
|
|
|
|
|
|
|
const QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(message.utime);
|
|
|
|
|
|
|
|
std::cout << QString("%1 %2 : <%3> %4%5")
|
|
|
|
.arg(timestamp.toString("yyyy-MM-ddThh:mm:ss.zzz"))
|
|
|
|
.arg(name)
|
|
|
|
.arg(LogLevelStrings[message.level])
|
|
|
|
.arg(location)
|
|
|
|
.arg(message.message)
|
|
|
|
.toStdString()
|
|
|
|
<< std::endl;
|
2020-07-27 20:00:36 +02:00
|
|
|
|
|
|
|
newLogMessage(message);
|
2020-07-19 15:37:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:08:55 +02:00
|
|
|
void Logger::Message(LogLevel level, const char* sourceFile, const char* func, unsigned int line, const char* fmt, ...)
|
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
Logger::LogLevel globalLevel = static_cast<Logger::LogLevel>(int(GLOBAL_MIN_LOG_LEVEL));
|
|
|
|
|
|
|
|
if ( (globalLevel == Logger::UNSET && level < _minLevel) // no global level, use level from logger
|
|
|
|
|| (globalLevel > Logger::UNSET && level < globalLevel) ) // global level set, use global level
|
2016-06-05 16:08:55 +02:00
|
|
|
return;
|
|
|
|
|
2016-07-21 20:12:51 +02:00
|
|
|
const size_t max_msg_length = 1024;
|
|
|
|
char msg[max_msg_length];
|
2016-06-05 16:08:55 +02:00
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
2016-07-21 20:12:51 +02:00
|
|
|
vsnprintf (msg, max_msg_length, fmt, args);
|
2016-06-05 16:08:55 +02:00
|
|
|
va_end (args);
|
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
const auto repeatedSummary = [&]
|
2019-08-13 20:41:01 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
Logger::T_LOG_MESSAGE repMsg = RepeatMessage.localData();
|
|
|
|
repMsg.message = "Previous line repeats " + QString::number(RepeatCount.localData()) + " times";
|
|
|
|
repMsg.utime = QDateTime::currentMSecsSinceEpoch();
|
2016-11-26 22:34:46 +01:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
write(repMsg);
|
2020-05-12 19:51:19 +02:00
|
|
|
#ifndef _WIN32
|
2019-08-13 20:41:01 +02:00
|
|
|
if ( _syslogEnabled && repMsg.level >= Logger::WARNING )
|
2020-07-19 15:37:47 +02:00
|
|
|
syslog (LogLevelSysLog[repMsg.level], "Previous line repeats %d times", RepeatCount.localData());
|
2020-05-12 19:51:19 +02:00
|
|
|
#endif
|
2016-11-26 22:34:46 +01:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
RepeatCount.setLocalData(0);
|
2019-08-13 20:41:01 +02:00
|
|
|
};
|
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
if (RepeatMessage.localData().loggerName == _name &&
|
2022-01-22 17:48:03 +01:00
|
|
|
RepeatMessage.localData().loggerSubName == _subname &&
|
2020-07-19 15:37:47 +02:00
|
|
|
RepeatMessage.localData().function == func &&
|
|
|
|
RepeatMessage.localData().message == msg &&
|
|
|
|
RepeatMessage.localData().line == line)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
if (RepeatCount.localData() >= MaxRepeatCountSize)
|
2019-08-13 20:41:01 +02:00
|
|
|
repeatedSummary();
|
|
|
|
else
|
2020-07-19 15:37:47 +02:00
|
|
|
RepeatCount.setLocalData(RepeatCount.localData() + 1);
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
2019-08-13 20:41:01 +02:00
|
|
|
else
|
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
if (RepeatCount.localData())
|
|
|
|
repeatedSummary();
|
2016-11-26 22:34:46 +01:00
|
|
|
|
2019-08-13 20:41:01 +02:00
|
|
|
Logger::T_LOG_MESSAGE logMsg;
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2019-08-13 20:41:01 +02:00
|
|
|
logMsg.loggerName = _name;
|
2022-01-22 17:48:03 +01:00
|
|
|
logMsg.loggerSubName = _subname;
|
2019-08-13 20:41:01 +02:00
|
|
|
logMsg.function = QString(func);
|
|
|
|
logMsg.line = line;
|
|
|
|
logMsg.fileName = FileUtils::getBaseName(sourceFile);
|
2020-07-19 15:37:47 +02:00
|
|
|
logMsg.utime = QDateTime::currentMSecsSinceEpoch();
|
2019-08-13 20:41:01 +02:00
|
|
|
logMsg.message = QString(msg);
|
|
|
|
logMsg.level = level;
|
|
|
|
logMsg.levelString = LogLevelStrings[level];
|
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
write(logMsg);
|
2020-05-12 19:51:19 +02:00
|
|
|
#ifndef _WIN32
|
2019-08-13 20:41:01 +02:00
|
|
|
if ( _syslogEnabled && level >= Logger::WARNING )
|
|
|
|
syslog (LogLevelSysLog[level], "%s", msg);
|
2020-05-12 19:51:19 +02:00
|
|
|
#endif
|
2020-07-19 15:37:47 +02:00
|
|
|
RepeatMessage.setLocalData(logMsg);
|
2019-08-13 20:41:01 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2016-11-26 22:46:16 +01:00
|
|
|
LoggerManager::LoggerManager()
|
2016-11-26 22:34:46 +01:00
|
|
|
: QObject()
|
2016-11-26 22:46:16 +01:00
|
|
|
, _loggerMaxMsgBufferSize(200)
|
2016-11-26 22:34:46 +01:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
_logMessageBuffer.reserve(_loggerMaxMsgBufferSize);
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
void LoggerManager::handleNewLogMessage(const Logger::T_LOG_MESSAGE & msg)
|
2016-11-26 22:34:46 +01:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
_logMessageBuffer.push_back(msg);
|
2016-11-26 22:46:16 +01:00
|
|
|
if (_logMessageBuffer.length() > _loggerMaxMsgBufferSize)
|
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
_logMessageBuffer.pop_front();
|
2016-11-26 22:46:16 +01:00
|
|
|
}
|
2016-11-26 22:34:46 +01:00
|
|
|
|
|
|
|
emit newLogMessage(msg);
|
|
|
|
}
|
|
|
|
|
2016-11-26 22:46:16 +01:00
|
|
|
LoggerManager* LoggerManager::getInstance()
|
2016-11-26 22:34:46 +01:00
|
|
|
{
|
2020-07-19 15:37:47 +02:00
|
|
|
static LoggerManager instance;
|
|
|
|
return &instance;
|
2016-11-26 22:34:46 +01:00
|
|
|
}
|