SDRAngel
4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
|
#include <logger.h>
Public Member Functions | |
Logger (QObject *parent) | |
Logger (const QString msgFormat="{timestamp} {type} {msg}", const QString timestampFormat="yyyy-MM-dd HH:mm:ss.zzz", const QtMsgType minLevel=QtDebugMsg, const int bufferSize=0, QObject *parent=0) | |
virtual | ~Logger () |
virtual void | log (const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0) |
void | installMsgHandler () |
void | setMinMessageLevel (const QtMsgType &minMsgLevel) |
QtMsgType | getMinMessageLevel () const |
virtual void | clear (const bool buffer=true, const bool variables=true) |
Static Public Member Functions | |
static void | set (const QString &name, const QString &value) |
Protected Member Functions | |
virtual void | write (const LogMessage *logMessage) |
Protected Attributes | |
QString | msgFormat |
QString | timestampFormat |
QtMsgType | minLevel |
int | bufferSize |
Static Protected Attributes | |
static QMutex | mutex |
Static Private Member Functions | |
static void | msgHandler (const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0) |
static void | msgHandler4 (const QtMsgType type, const char *message) |
Private Attributes | |
QThreadStorage< QList< LogMessage * > * > | buffers |
Static Private Attributes | |
static Logger * | defaultLogger =0 |
static QThreadStorage< QHash< QString, QString > * > | logVars |
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 keeps a configurable number of messages in a ring-buffer. A log message with a severity >= minLevel flushes the buffer, so the stored messages get written out. If the buffer is disabled, then only messages with severity >= minLevel are written out.
If you enable the buffer and use minLevel=2, then the application logs only errors together with some buffered debug messages. But as long no error occurs, nothing gets written out.
Each thread has it's own buffer.
The logger can be registered to handle messages from the static global functions qDebug(), qWarning(), qCritical() and qFatal().
Logger::Logger | ( | QObject * | parent | ) |
Constructor. Uses the same defaults as the other constructor.
parent | Parent object |
Definition at line 25 of file logger.cpp.
Referenced by qtwebapp::LoggerWithFile::LoggerWithFile().
Logger::Logger | ( | const QString | msgFormat = "{timestamp} {type} {msg}" , |
const QString | timestampFormat = "yyyy-MM-dd HH:mm:ss.zzz" , |
||
const QtMsgType | minLevel = QtDebugMsg , |
||
const int | bufferSize = 0 , |
||
QObject * | parent = 0 |
||
) |
Constructor.
msgFormat | Format of the decoration, e.g. "{timestamp} {type} thread={thread}: {msg}" |
timestampFormat | Format of timestamp, e.g. "yyyy-MM-dd HH:mm:ss.zzz" |
minLevel | Minimum severity that genertes an output (0=debug, 1=warning, 2=critical, 3=fatal). |
bufferSize | Size of the backtrace buffer, number of messages per thread. 0=disabled. |
parent | Parent object |
Definition at line 34 of file logger.cpp.
References bufferSize, minLevel, msgFormat, and timestampFormat.
|
virtual |
|
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 qtwebapp::DualFileLogger, and qtwebapp::LoggerWithFile.
Definition at line 134 of file logger.cpp.
References buffers, logVars, and mutex.
Referenced by qtwebapp::LoggerWithFile::clear(), and qtwebapp::DualFileLogger::clear().
|
inline |
Get the current message level
Definition at line 107 of file logger.h.
Referenced by qtwebapp::LoggerWithFile::getConsoleMinMessageLevelStr(), and qtwebapp::LoggerWithFile::getFileMinMessageLevelStr().
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 111 of file logger.cpp.
References defaultLogger, and msgHandler4().
Referenced by main().
|
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 qtwebapp::DualFileLogger, and qtwebapp::LoggerWithFile.
Definition at line 153 of file logger.cpp.
References buffers, bufferSize, logVars, minLevel, mutex, and write().
Referenced by qtwebapp::LoggerWithFile::log(), qtwebapp::DualFileLogger::log(), qtwebapp::LoggerWithFile::logToFile(), and msgHandler().
|
staticprivate |
Message Handler for the global static logging functions (e.g. qDebug()). Forward calls to the default logger.
In case of a fatal message, the program will abort. Variables in the in the message are replaced by their values. 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) |
Definition at line 44 of file logger.cpp.
References defaultLogger, and log().
Referenced by msgHandler4().
|
staticprivate |
Wrapper for QT version 4.
Definition at line 83 of file logger.cpp.
References msgHandler().
Referenced by installMsgHandler().
|
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 122 of file logger.cpp.
References logVars, and mutex.
|
inline |
Sets the minimum message level on the fly
Definition at line 100 of file logger.h.
Referenced by qtwebapp::LoggerWithFile::setConsoleMinMessageLevel(), and qtwebapp::LoggerWithFile::setFileMinMessageLevel().
|
protectedvirtual |
Decorate and write a log message to stderr. Override this method to provide a different output medium.
Reimplemented in qtwebapp::FileLogger.
Definition at line 104 of file logger.cpp.
References msgFormat, timestampFormat, and qtwebapp::LogMessage::toString().
Referenced by log(), and qtwebapp::FileLogger::write().
|
private |
|
protected |
Size of backtrace buffer, number of messages per thread. 0=disabled
Definition at line 139 of file logger.h.
Referenced by log(), Logger(), qtwebapp::FileLogger::refreshFileLogSettings(), and qtwebapp::FileLogger::refreshQtSettings().
|
staticprivate |
Pointer to the default logger, used by msgHandler()
Definition at line 153 of file logger.h.
Referenced by installMsgHandler(), msgHandler(), and ~Logger().
|
staticprivate |
|
protected |
Minimum level of message types that are written out
Definition at line 136 of file logger.h.
Referenced by log(), Logger(), qtwebapp::FileLogger::refreshFileLogSettings(), and qtwebapp::FileLogger::refreshQtSettings().
|
protected |
Format string for message decoration
Definition at line 130 of file logger.h.
Referenced by Logger(), qtwebapp::FileLogger::refreshFileLogSettings(), qtwebapp::FileLogger::refreshQtSettings(), qtwebapp::FileLogger::write(), and write().
|
staticprotected |
Used to synchronize access of concurrent threads
Definition at line 142 of file logger.h.
Referenced by clear(), log(), qtwebapp::FileLogger::refreshSettings(), set(), and qtwebapp::FileLogger::timerEvent().
|
protected |
Format string of timestamps
Definition at line 133 of file logger.h.
Referenced by Logger(), qtwebapp::FileLogger::refreshFileLogSettings(), qtwebapp::FileLogger::refreshQtSettings(), qtwebapp::FileLogger::write(), and write().