QtWebApp
stefanfrings::Logger Class Reference

Decorates and writes log messages to the console, stderr. More...

#include <logger.h>

Inheritance diagram for stefanfrings::Logger:
Collaboration diagram for stefanfrings::Logger:

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.
 

Detailed Description

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().

See also
set() describes how to set logger variables
LogMessage for a description of the message decoration.
Warning
You should prefer a derived class, for example FileLogger, because logging to the console is less useful.

Definition at line 52 of file logger.h.

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( QObject *  parent)

Constructor.

Uses the same defaults as the other constructor.

Parameters
parentParent object

Definition at line 27 of file logger.cpp.

◆ Logger() [2/2]

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

Parameters
msgFormatFormat of the decoration, e.g. "{timestamp} {type} thread={thread}: {msg}"
timestampFormatFormat of timestamp, e.g. "dd.MM.yyyy hh:mm:ss.zzz"
minLevelIf 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.
bufferSizeSize of the backtrace buffer, number of messages per thread. 0=disabled.
parentParent object
See also
LogMessage for a description of the message decoration.

Definition at line 36 of file logger.cpp.

Member Function Documentation

◆ clear()

void Logger::clear ( const bool  buffer = true,
const bool  variables = true 
)
virtual

Clear the thread-local data of the current thread.

This method is thread safe.

Parameters
bufferWhether to clear the backtrace buffer
variablesWhether to clear the log variables

Reimplemented in stefanfrings::DualFileLogger.

Definition at line 140 of file logger.cpp.

◆ installMsgHandler()

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.

◆ log()

void Logger::log ( const QtMsgType  type,
const QString &  message,
const QString &  file = "",
const QString &  function = "",
const int  line = 0 
)
virtual

Decorate and log the message, if type>=minLevel.

This method is thread safe.

Parameters
typeMessage type (level)
messageMessage text
fileName of the source file where the message was generated (usually filled with the macro FILE)
functionName of the function where the message was generated (usually filled with the macro LINE)
lineLine Number of the source file, where the message was generated (usually filles with the macro func or FUNCTION)
See also
LogMessage for a description of the message decoration.

Reimplemented in stefanfrings::DualFileLogger.

Definition at line 160 of file logger.cpp.

◆ set()

void Logger::set ( const QString &  name,
const QString &  value 
)
static

Sets a thread-local variable that may be used to decorate log messages.

This method is thread safe.

Parameters
nameName of the variable
valueValue of the variable

Definition at line 128 of file logger.cpp.

◆ write()

void Logger::write ( const LogMessage logMessage)
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.

Member Data Documentation

◆ bufferSize

int stefanfrings::Logger::bufferSize
protected

Size of backtrace buffer, number of messages per thread.

0=disabled

Definition at line 131 of file logger.h.


The documentation for this class was generated from the following files: