QtWebApp
logger.h
Go to the documentation of this file.
1 
6 #ifndef LOGGER_H
7 #define LOGGER_H
8 
9 #include <QtGlobal>
10 #include <QThreadStorage>
11 #include <QHash>
12 #include <QStringList>
13 #include <QMutex>
14 #include <QObject>
15 #include "logglobal.h"
16 #include "logmessage.h"
17 
18 namespace stefanfrings {
19 
52 class DECLSPEC Logger : public QObject {
53  Q_OBJECT
54  Q_DISABLE_COPY(Logger)
55 public:
56 
62  Logger(QObject* parent);
63 
64 
76  Logger(const QString msgFormat="{timestamp} {type} {msg}",
77  const QString timestampFormat="dd.MM.yyyy hh:mm:ss.zzz",
78  const QtMsgType minLevel=QtDebugMsg, const int bufferSize=0,
79  QObject* parent = nullptr);
80 
82  virtual ~Logger();
83 
94  virtual void log(const QtMsgType type, const QString& message, const QString &file="",
95  const QString &function="", const int line=0);
96 
101  void installMsgHandler();
102 
109  static void set(const QString& name, const QString& value);
110 
117  virtual void clear(const bool buffer=true, const bool variables=true);
118 
119 protected:
120 
122  QString msgFormat;
123 
126 
128  QtMsgType minLevel;
129 
132 
134  static QMutex mutex;
135 
140  virtual void write(const LogMessage* logMessage);
141 
142 private:
143 
145  static Logger* defaultLogger;
146 
160  static void msgHandler(const QtMsgType type, const QString &message, const QString &file="",
161  const QString &function="", const int line=0);
162 
163 
164 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
165 
173  static void msgHandler5(const QtMsgType type, const QMessageLogContext& context, const QString &message);
174 
175 #else
176 
183  static void msgHandler4(const QtMsgType type, const char * message);
184 
185 #endif
186 
188  static QThreadStorage<QHash<QString,QString>*> logVars;
189 
191  QThreadStorage<QList<LogMessage*>*> buffers;
192 };
193 
194 } // end of namespace
195 
196 #endif // LOGGER_H
Represents a single log message together with some data that are used to decorate the log message.
Definition: logmessage.h:37
Decorates and writes log messages to the console, stderr.
Definition: logger.h:52
QtMsgType minLevel
Minimum level of message types that are written out directly or trigger writing the buffered content.
Definition: logger.h:128
QString timestampFormat
Format string of timestamps.
Definition: logger.h:125
static QMutex mutex
Used to synchronize access of concurrent threads.
Definition: logger.h:134
int bufferSize
Size of backtrace buffer, number of messages per thread.
Definition: logger.h:131
QString msgFormat
Format string for message decoration.
Definition: logger.h:122