Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes

Logger Class Reference

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

#include <logger.h>

Inheritance diagram for Logger:
DualFileLogger FileLogger

List of all members.

Public Member Functions

 Logger (QObject *parent)
 Constructor.
 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=0)
 Constructor.
virtual ~Logger ()
 Destructor.
virtual void log (const QtMsgType type, const QString &message)
 Decorate and log the message, if type>=minLevel.
void installMsgHandler ()
 Installs this logger as the default message handler, so it can be used through the global static logging functions (e.g.

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.
static void clear (const bool buffer=true, const bool variables=true)
 Clear the thread-local data of the current thread.

Protected Member Functions

virtual void write (const LogMessage *logMessage)
 Decorate and write a log message to stderr.

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.
int bufferSize
 Size of backtrace buffer, number of messages per thread.

Static Protected Attributes

static QMutex mutex
 Used to synchronize access to the static members.

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 buffer stores log messages of any level from the time before an error occurs. It can be used to provide detailed debug information when an error occurs, while keeping the logfile clean as long no error occurs. Using this buffer may reduce performance significantly.

The logger can be registered to handle messages from the static global functions qDebug(), qWarning(), qCritical() and qFatal().

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 39 of file logger.h.


Constructor & Destructor Documentation

Logger::Logger ( QObject *  parent  ) 

Constructor.

Uses the same defaults as the other constructor.

Parameters:
parent Parent object

Definition at line 25 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 = 0 
)

Constructor.

Parameters:
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 Minimum type of messages that are written out.
bufferSize Size of the backtrace buffer, number of messages per thread. 0=disabled.
parent Parent object
See also:
LogMessage for a description of the message decoration.

Definition at line 34 of file logger.cpp.


Member Function Documentation

void Logger::clear ( const bool  buffer = true,
const bool  variables = true 
) [static]

Clear the thread-local data of the current thread.

Parameters:
buffer Whether to clear the backtrace buffer
variables Whether to clear the log variables

Definition at line 103 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 87 of file logger.cpp.

void Logger::log ( const QtMsgType  type,
const QString &  message 
) [virtual]

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

This method is thread safe.

Parameters:
type Message type (level)
message Message text
See also:
LogMessage for a description of the message decoration.

Reimplemented in DualFileLogger.

Definition at line 119 of file logger.cpp.

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:
name Name of the variable
value Value of the variable

Definition at line 93 of file logger.cpp.

void Logger::write ( const LogMessage logMessage  )  [protected, virtual]

Decorate and write a log message to stderr.

Override this method to provide a different output medium.

Reimplemented in FileLogger.

Definition at line 81 of file logger.cpp.


Member Data Documentation

int Logger::bufferSize [protected]

Size of backtrace buffer, number of messages per thread.

0=disabled

Definition at line 108 of file logger.h.


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