libdrmconf  0.11.3
A library to program DMR radios.
logger.hh
1 
3 #ifndef LOGGER_HH
4 #define LOGGER_HH
5 
6 #include <QFile>
7 #include <QTextStream>
8 #include <QList>
9 
11 #define logDebug() LogMessage(LogMessage::DEBUG, __FILE__, __LINE__)
13 #define logInfo() LogMessage(LogMessage::INFO, __FILE__, __LINE__)
15 #define logWarn() LogMessage(LogMessage::WARNING, __FILE__, __LINE__)
17 #define logError() LogMessage(LogMessage::ERROR, __FILE__, __LINE__)
19 #ifdef __cpp_lib_stacktrace
20 #include <stacktrace>
21 #define logFatal() LogMessage(LogMessage::FATAL, __FILE__, __LINE__) << \
22  QString::fromStdString(std::to_string(std::stacktrace::current()))
23 #else
24 #define logFatal() LogMessage(LogMessage::FATAL, __FILE__, __LINE__)
25 #endif
26 
31 class LogMessage: public QTextStream
32 {
33 public:
35  typedef enum {
37  INFO,
40  FATAL
41  } Level;
42 
43 public:
49  LogMessage(Level level, const QString &file, int line, const QString &message="");
51  LogMessage(const LogMessage &other);
53  virtual ~LogMessage();
54 
56  Level level() const;
58  const QString &file() const;
60  int line() const;
62  const QString &message() const;
63 
64 protected:
68  QString _file;
70  int _line;
72  QString _message;
73 };
74 
75 
78 class LogHandler: public QObject
79 {
80  Q_OBJECT
81 
82 public:
84  explicit LogHandler(QObject *parent=nullptr);
86  virtual ~LogHandler();
88  virtual void handle(const LogMessage &message) = 0;
89 };
90 
91 
94 class Logger: public QObject
95 {
96  Q_OBJECT
97 
98 protected:
100  Logger();
101 
102 public:
104  virtual ~Logger();
105 
107  void log(const LogMessage &msg);
109  void addHandler(LogHandler *handler);
111  void remHandler(LogHandler *handler);
112 
113 protected slots:
115  void onHandlerDeleted(QObject *obj);
116 
117 public:
119  static Logger &get();
120 
121 protected:
123  static Logger *_instance;
125  QList<LogHandler *> _handler;
126 };
127 
128 
132 {
133  Q_OBJECT
134 
135 public:
141  StreamLogHandler(QTextStream &stream, LogMessage::Level minLevel=LogMessage::DEBUG, bool color=false, QObject *parent=nullptr);
142 
144  LogMessage::Level minLevel() const;
147 
148  void handle(const LogMessage &message);
149 
150 protected:
152  QTextStream &_stream;
156  bool _color;
157 };
158 
159 
163 {
164  Q_OBJECT
165 
166 public:
171  FileLogHandler(const QString &file, LogMessage::Level minLevel=LogMessage::DEBUG, QObject *parent=nullptr);
172 
174  virtual ~FileLogHandler();
175 
177  LogMessage::Level minLevel() const;
180 
181  void handle(const LogMessage &message);
182 
183 protected:
185  QFile _file;
187  QTextStream _stream;
190 };
191 
192 #endif // LOGGER_HH
A log-handler that dumps log-messages into files.
Definition: logger.hh:163
void setMinLevel(LogMessage::Level minLevel)
Resets the minimum log level.
Definition: logger.cc:218
LogMessage::Level minLevel() const
Returns the minimum log level.
Definition: logger.cc:213
QFile _file
The file to log into.
Definition: logger.hh:185
void handle(const LogMessage &message)
Callback to handle log messages.
Definition: logger.cc:223
QTextStream _stream
A reference to the text stream to log into.
Definition: logger.hh:187
FileLogHandler(const QString &file, LogMessage::Level minLevel=LogMessage::DEBUG, QObject *parent=nullptr)
Constructor.
Definition: logger.cc:181
LogMessage::Level _minLevel
The minimum log level.
Definition: logger.hh:189
virtual ~FileLogHandler()
Destructor, closes log file.
Definition: logger.cc:205
Interface for all log message handler.
Definition: logger.hh:79
virtual void handle(const LogMessage &message)=0
Callback to handle log messages.
LogHandler(QObject *parent=nullptr)
Constructor.
Definition: logger.cc:52
virtual ~LogHandler()
Destructor.
Definition: logger.cc:58
Implements a log-message.
Definition: logger.hh:32
Level _level
The log level.
Definition: logger.hh:66
QString _file
The source file.
Definition: logger.hh:68
int _line
The source line.
Definition: logger.hh:70
Level
Possible log-levels.
Definition: logger.hh:35
@ DEBUG
Level for debug messages. Will not be shown to the user unless requested.
Definition: logger.hh:36
@ FATAL
Level for fatal error messages.
Definition: logger.hh:40
@ INFO
Level for informative messages. Will not be shown to the user unless requested.
Definition: logger.hh:37
@ ERROR
Level for error messages.
Definition: logger.hh:39
@ WARNING
Level for warning messages.
Definition: logger.hh:38
virtual ~LogMessage()
Destructor.
Definition: logger.cc:24
QString _message
The log message content.
Definition: logger.hh:72
const QString & message() const
Returns the log message content.
Definition: logger.cc:44
Level level() const
Returns the level of the log message.
Definition: logger.cc:29
int line() const
Returns the source line.
Definition: logger.cc:39
const QString & file() const
Returns the source file.
Definition: logger.cc:34
LogMessage(Level level, const QString &file, int line, const QString &message="")
Constructor.
Definition: logger.cc:10
Singleton class to process log messages.
Definition: logger.hh:95
QList< LogHandler * > _handler
The list of registered log-handler.
Definition: logger.hh:125
static Logger * _instance
The singleton instance.
Definition: logger.hh:123
void onHandlerDeleted(QObject *obj)
Internal callback to handle deleted handler objects.
Definition: logger.cc:106
void addHandler(LogHandler *handler)
Adds a log-handler to the logger.
Definition: logger.cc:86
static Logger & get()
Factory method to get the singleton instance.
Definition: logger.cc:111
Logger()
Hidden constructor.
Definition: logger.cc:68
void remHandler(LogHandler *handler)
Removes a log-handler from the logger.
Definition: logger.cc:97
virtual ~Logger()
Destructor.
Definition: logger.cc:74
void log(const LogMessage &msg)
Logs a message.
Definition: logger.cc:79
A log-handler that dumps log-messages into a QTextStream.
Definition: logger.hh:132
LogMessage::Level minLevel() const
Returns the minimum log level.
Definition: logger.cc:129
void handle(const LogMessage &message)
Callback to handle log messages.
Definition: logger.cc:139
LogMessage::Level _minLevel
The minimum log level.
Definition: logger.hh:154
QTextStream & _stream
A reference to the text stream to log into.
Definition: logger.hh:152
void setMinLevel(LogMessage::Level minLevel)
Resets the minimum log level.
Definition: logger.cc:134
StreamLogHandler(QTextStream &stream, LogMessage::Level minLevel=LogMessage::DEBUG, bool color=false, QObject *parent=nullptr)
Constructor.
Definition: logger.cc:122
bool _color
If true, write messages using console colors.
Definition: logger.hh:156