2016-06-05 16:08:55 +02:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
// QT includes
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
2020-07-19 15:37:47 +02:00
|
|
|
#include <QMap>
|
|
|
|
#include <QAtomicInteger>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMutex>
|
2016-11-26 22:34:46 +01:00
|
|
|
|
|
|
|
// stl includes
|
2016-06-05 16:08:55 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2020-06-04 16:21:23 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <stdexcept>
|
|
|
|
#endif
|
2016-11-26 22:34:46 +01:00
|
|
|
|
2017-03-01 15:23:53 +01:00
|
|
|
#include <utils/global_defines.h>
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
#define LOG_MESSAGE(severity, logger, ...) (logger)->Message(severity, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
|
|
|
|
|
2016-06-05 16:08:55 +02:00
|
|
|
// standard log messages
|
2020-07-19 15:37:47 +02:00
|
|
|
#define Debug(logger, ...) LOG_MESSAGE(Logger::DEBUG , logger, __VA_ARGS__)
|
|
|
|
#define Info(logger, ...) LOG_MESSAGE(Logger::INFO , logger, __VA_ARGS__)
|
|
|
|
#define Warning(logger, ...) LOG_MESSAGE(Logger::WARNING, logger, __VA_ARGS__)
|
|
|
|
#define Error(logger, ...) LOG_MESSAGE(Logger::ERRORR , logger, __VA_ARGS__)
|
2016-06-05 16:08:55 +02:00
|
|
|
|
|
|
|
// conditional log messages
|
2020-07-19 15:37:47 +02:00
|
|
|
#define DebugIf(condition, logger, ...) if (condition) Debug(logger, __VA_ARGS__)
|
|
|
|
#define InfoIf(condition, logger, ...) if (condition) Info(logger, __VA_ARGS__)
|
|
|
|
#define WarningIf(condition, logger, ...) if (condition) Warning(logger, __VA_ARGS__)
|
|
|
|
#define ErrorIf(condition, logger, ...) if (condition) Error(logger, __VA_ARGS__)
|
2016-06-05 16:08:55 +02:00
|
|
|
|
|
|
|
// ================================================================
|
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
class Logger : public QObject
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2016-11-26 22:34:46 +01:00
|
|
|
Q_OBJECT
|
|
|
|
|
2016-06-05 16:08:55 +02:00
|
|
|
public:
|
2020-05-12 19:51:19 +02:00
|
|
|
enum LogLevel {
|
2020-07-19 15:37:47 +02:00
|
|
|
UNSET = 0,
|
|
|
|
DEBUG = 1,
|
|
|
|
INFO = 2,
|
|
|
|
WARNING = 3,
|
|
|
|
ERRORR = 4,
|
|
|
|
OFF = 5
|
2020-05-12 19:51:19 +02:00
|
|
|
};
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
struct T_LOG_MESSAGE
|
2016-11-26 22:34:46 +01:00
|
|
|
{
|
|
|
|
QString appName;
|
|
|
|
QString loggerName;
|
|
|
|
QString function;
|
|
|
|
unsigned int line;
|
|
|
|
QString fileName;
|
2020-07-19 15:37:47 +02:00
|
|
|
uint64_t utime;
|
2016-11-26 22:34:46 +01:00
|
|
|
QString message;
|
|
|
|
LogLevel level;
|
|
|
|
QString levelString;
|
2020-07-19 15:37:47 +02:00
|
|
|
};
|
2016-11-26 22:34:46 +01:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
static Logger* getInstance(const QString & name = "", LogLevel minLevel=Logger::INFO);
|
|
|
|
static void deleteInstance(const QString & name = "");
|
|
|
|
static void setLogLevel(LogLevel level, const QString & name = "");
|
|
|
|
static LogLevel getLogLevel(const QString & name = "");
|
2016-06-27 23:56:21 +02:00
|
|
|
|
|
|
|
void Message(LogLevel level, const char* sourceFile, const char* func, unsigned int line, const char* fmt, ...);
|
2020-07-19 15:37:47 +02:00
|
|
|
void setMinLevel(LogLevel level) { _minLevel = static_cast<int>(level); }
|
2020-08-08 23:12:43 +02:00
|
|
|
LogLevel getMinLevel() const { return static_cast<LogLevel>(int(_minLevel)); }
|
2020-07-19 15:37:47 +02:00
|
|
|
QString getName() const { return _name; }
|
|
|
|
QString getAppName() const { return _appname; }
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
signals:
|
|
|
|
void newLogMessage(Logger::T_LOG_MESSAGE);
|
|
|
|
|
2016-06-05 16:08:55 +02:00
|
|
|
protected:
|
2020-07-19 15:37:47 +02:00
|
|
|
Logger(const QString & name="", LogLevel minLevel = INFO);
|
2020-08-08 23:12:43 +02:00
|
|
|
~Logger() override;
|
2016-06-05 16:08:55 +02:00
|
|
|
|
|
|
|
private:
|
2020-07-27 20:00:36 +02:00
|
|
|
void write(const Logger::T_LOG_MESSAGE & message);
|
2016-06-05 16:08:55 +02:00
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
static QMutex MapLock;
|
|
|
|
static QMap<QString,Logger*> LoggerMap;
|
|
|
|
static QAtomicInteger<int> GLOBAL_MIN_LOG_LEVEL;
|
|
|
|
|
|
|
|
const QString _name;
|
|
|
|
const QString _appname;
|
|
|
|
const bool _syslogEnabled;
|
|
|
|
const unsigned _loggerId;
|
|
|
|
|
|
|
|
/* Only non-const member, hence the atomic */
|
|
|
|
QAtomicInteger<int> _minLevel;
|
|
|
|
};
|
2016-11-26 22:46:16 +01:00
|
|
|
|
|
|
|
class LoggerManager : public QObject
|
2016-11-26 22:34:46 +01:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-11-26 22:46:16 +01:00
|
|
|
static LoggerManager* getInstance();
|
2020-07-19 15:37:47 +02:00
|
|
|
const QList<Logger::T_LOG_MESSAGE>* getLogMessageBuffer() const { return &_logMessageBuffer; }
|
2016-11-26 22:34:46 +01:00
|
|
|
|
|
|
|
public slots:
|
2019-06-05 18:19:08 +02:00
|
|
|
void handleNewLogMessage(const Logger::T_LOG_MESSAGE&);
|
2016-11-26 22:34:46 +01:00
|
|
|
|
|
|
|
signals:
|
2020-07-27 20:00:36 +02:00
|
|
|
void newLogMessage(const Logger::T_LOG_MESSAGE&);
|
2016-11-26 22:46:16 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
LoggerManager();
|
|
|
|
|
2020-07-19 15:37:47 +02:00
|
|
|
QList<Logger::T_LOG_MESSAGE> _logMessageBuffer;
|
2016-11-26 22:46:16 +01:00
|
|
|
const int _loggerMaxMsgBufferSize;
|
2016-11-26 22:34:46 +01:00
|
|
|
};
|
2017-03-01 15:23:53 +01:00
|
|
|
|
2020-05-25 21:51:11 +02:00
|
|
|
Q_DECLARE_METATYPE(Logger::T_LOG_MESSAGE)
|