QtWebApp
|
Decorates and writes log messages to the console, stderr. More...
#include <logger.h>
Public Member Functions | |
Logger (QObject *parent) | |
Constructor. More... | |
Logger (const QString msgFormat="{timestamp} {type} {msg}", const QString timestampFormat="dd.MM.yyyy hh:mm:ss.zzz", const QtMsgType minLevel=QtDebugMsg, const int bufferSize=0, QObject *parent=nullptr) | |
Constructor. More... | |
virtual | ~Logger () |
Destructor. | |
virtual void | log (const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0) |
Decorate and log the message, if type>=minLevel. More... | |
void | installMsgHandler () |
Installs this logger as the default message handler, so it can be used through the global static logging functions (e.g. More... | |
virtual void | clear (const bool buffer=true, const bool variables=true) |
Clear the thread-local data of the current thread. More... | |
Static Public Member Functions | |
static void | set (const QString &name, const QString &value) |
Sets a thread-local variable that may be used to decorate log messages. More... | |
Protected Member Functions | |
virtual void | write (const LogMessage *logMessage) |
Decorate and write a log message to stderr. More... | |
Protected Attributes | |
QString | msgFormat |
Format string for message decoration. | |
QString | timestampFormat |
Format string of timestamps. | |
QtMsgType | minLevel |
Minimum level of message types that are written out directly or trigger writing the buffered content. | |
int | bufferSize |
Size of backtrace buffer, number of messages per thread. More... | |
Static Protected Attributes | |
static QMutex | mutex |
Used to synchronize access of concurrent threads. | |
Decorates and writes log messages to the console, stderr.
The decorator uses a predefined msgFormat string to enrich log messages with additional information (e.g. timestamp).
The msgFormat string and also the message text may contain additional variable names in the form {name} that are filled by values taken from a static thread local dictionary.
The logger can collect a configurable number of messages in thread-local FIFO buffers. A log message with severity >= minLevel flushes the buffer, so the messages are written out. There is one exception: INFO messages are treated like DEBUG messages (level 0).
Example: If you enable the buffer and use minLevel=2, then the application waits until an error occurs. Then it writes out the error message together with all buffered lower level messages of the same thread. But as long no error occurs, nothing gets written out.
If the buffer is disabled, then only messages with severity >= minLevel are written out.
The logger can be registered to handle messages from the static global functions qDebug(), qWarning(), qCritical(), qFatal() and qInfo().
Logger::Logger | ( | QObject * | parent | ) |
Constructor.
Uses the same defaults as the other constructor.
parent | Parent object |
Definition at line 27 of file logger.cpp.
Logger::Logger | ( | const QString | msgFormat = "{timestamp} {type} {msg}" , |
const QString | timestampFormat = "dd.MM.yyyy hh:mm:ss.zzz" , |
||
const QtMsgType | minLevel = QtDebugMsg , |
||
const int | bufferSize = 0 , |
||
QObject * | parent = nullptr |
||
) |
Constructor.
Possible log levels are: 0=DEBUG, 1=WARNING, 2=CRITICAL, 3=FATAL, 4=INFO
msgFormat | Format of the decoration, e.g. "{timestamp} {type} thread={thread}: {msg}" |
timestampFormat | Format of timestamp, e.g. "dd.MM.yyyy hh:mm:ss.zzz" |
minLevel | If bufferSize=0: Messages with lower level discarded. If buffersize>0: Messages with lower level are buffered, messages with equal or higher level trigger writing the buffered content. |
bufferSize | Size of the backtrace buffer, number of messages per thread. 0=disabled. |
parent | Parent object |
Definition at line 36 of file logger.cpp.
|
virtual |
Clear the thread-local data of the current thread.
This method is thread safe.
buffer | Whether to clear the backtrace buffer |
variables | Whether to clear the log variables |
Reimplemented in stefanfrings::DualFileLogger.
Definition at line 140 of file logger.cpp.
void Logger::installMsgHandler | ( | ) |
Installs this logger as the default message handler, so it can be used through the global static logging functions (e.g.
qDebug()).
Definition at line 117 of file logger.cpp.
|
virtual |
Decorate and log the message, if type>=minLevel.
This method is thread safe.
type | Message type (level) |
message | Message text |
file | Name of the source file where the message was generated (usually filled with the macro FILE) |
function | Name of the function where the message was generated (usually filled with the macro LINE) |
line | Line Number of the source file, where the message was generated (usually filles with the macro func or FUNCTION) |
Reimplemented in stefanfrings::DualFileLogger.
Definition at line 160 of file logger.cpp.
|
static |
Sets a thread-local variable that may be used to decorate log messages.
This method is thread safe.
name | Name of the variable |
value | Value of the variable |
Definition at line 128 of file logger.cpp.
|
protectedvirtual |
Decorate and write a log message to stderr.
Override this method to provide a different output medium.
Reimplemented in stefanfrings::FileLogger.
Definition at line 110 of file logger.cpp.
|
protected |