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.
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
qtwebapp::Logger Class Reference

#include <logger.h>

+ Inheritance diagram for qtwebapp::Logger:
+ Collaboration diagram for qtwebapp::Logger:

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 LoggerdefaultLogger =0
 
static QThreadStorage< QHash< QString, QString > * > logVars
 

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

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 25 of file logger.cpp.

Referenced by qtwebapp::LoggerWithFile::LoggerWithFile().

26  : QObject(parent),
27  msgFormat("{timestamp} {type} {msg}"),
28  timestampFormat("yyyy-MM-dd HH:mm:ss.zzz"),
29  minLevel(QtDebugMsg),
30  bufferSize(0)
31  {}
QString timestampFormat
Definition: logger.h:133
QtMsgType minLevel
Definition: logger.h:136
QString msgFormat
Definition: logger.h:130
+ Here is the caller graph for this function:

◆ Logger() [2/2]

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.

Parameters
msgFormatFormat of the decoration, e.g. "{timestamp} {type} thread={thread}: {msg}"
timestampFormatFormat of timestamp, e.g. "yyyy-MM-dd HH:mm:ss.zzz"
minLevelMinimum severity that genertes an output (0=debug, 1=warning, 2=critical, 3=fatal).
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 34 of file logger.cpp.

References bufferSize, minLevel, msgFormat, and timestampFormat.

35  :QObject(parent)
36 {
37  this->msgFormat=msgFormat;
39  this->minLevel=minLevel;
40  this->bufferSize=bufferSize;
41 }
QString timestampFormat
Definition: logger.h:133
QtMsgType minLevel
Definition: logger.h:136
QString msgFormat
Definition: logger.h:130

◆ ~Logger()

Logger::~Logger ( )
virtual

Destructor

Definition at line 90 of file logger.cpp.

References defaultLogger.

91 {
92  if (defaultLogger==this)
93  {
94 #if QT_VERSION >= 0x050000
95  qInstallMessageHandler(0);
96 #else
97  qInstallMsgHandler(0);
98 #endif
99  defaultLogger=0;
100  }
101 }
static Logger * defaultLogger
Definition: logger.h:153

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

135 {
136  mutex.lock();
137  if (buffer && buffers.hasLocalData())
138  {
139  QList<LogMessage*>* buffer=buffers.localData();
140  while (buffer && !buffer->isEmpty()) {
141  LogMessage* logMessage=buffer->takeLast();
142  delete logMessage;
143  }
144  }
145  if (variables && logVars.hasLocalData())
146  {
147  logVars.localData()->clear();
148  }
149  mutex.unlock();
150 }
static QThreadStorage< QHash< QString, QString > * > logVars
Definition: logger.h:195
QThreadStorage< QList< LogMessage * > * > buffers
Definition: logger.h:198
static QMutex mutex
Definition: logger.h:142
+ Here is the caller graph for this function:

◆ getMinMessageLevel()

QtMsgType qtwebapp::Logger::getMinMessageLevel ( ) const
inline

Get the current message level

Definition at line 107 of file logger.h.

Referenced by qtwebapp::LoggerWithFile::getConsoleMinMessageLevelStr(), and qtwebapp::LoggerWithFile::getFileMinMessageLevelStr().

107  {
108  return minLevel;
109  }
QtMsgType minLevel
Definition: logger.h:136
+ Here is the caller graph for this function:

◆ 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 111 of file logger.cpp.

References defaultLogger, and msgHandler4().

Referenced by main().

112 {
113  defaultLogger=this;
114 #if QT_VERSION >= 0x050000
115  qInstallMessageHandler(msgHandler5);
116 #else
117  qInstallMsgHandler(msgHandler4);
118 #endif
119 }
static void msgHandler4(const QtMsgType type, const char *message)
Definition: logger.cpp:83
static Logger * defaultLogger
Definition: logger.h:153
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

154 {
155  mutex.lock();
156 
157  // If the buffer is enabled, write the message into it
158  if (bufferSize>0) {
159  // Create new thread local buffer, if necessary
160  if (!buffers.hasLocalData()) {
161  buffers.setLocalData(new QList<LogMessage*>());
162  }
163  QList<LogMessage*>* buffer=buffers.localData();
164  // Append the decorated log message
165  LogMessage* logMessage=new LogMessage(type,message,logVars.localData(),file,function,line);
166  buffer->append(logMessage);
167  // Delete oldest message if the buffer became too large
168  if (buffer->size()>bufferSize)
169  {
170  delete buffer->takeFirst();
171  }
172  // If the type of the message is high enough, print the whole buffer
173  if (type>=minLevel) {
174  while (!buffer->isEmpty())
175  {
176  LogMessage* logMessage=buffer->takeFirst();
177  write(logMessage);
178  delete logMessage;
179  }
180  }
181  }
182 
183  // Buffer is disabled, print the message if the type is high enough
184  else {
185  if (type>=minLevel)
186  {
187  LogMessage logMessage(type,message,logVars.localData(),file,function,line);
188  write(&logMessage);
189  }
190  }
191  mutex.unlock();
192 }
QtMsgType minLevel
Definition: logger.h:136
virtual void write(const LogMessage *logMessage)
Definition: logger.cpp:104
static QThreadStorage< QHash< QString, QString > * > logVars
Definition: logger.h:195
QThreadStorage< QList< LogMessage * > * > buffers
Definition: logger.h:198
static QMutex mutex
Definition: logger.h:142
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ msgHandler()

void Logger::msgHandler ( const QtMsgType  type,
const QString &  message,
const QString &  file = "",
const QString &  function = "",
const int  line = 0 
)
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.

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)

Definition at line 44 of file logger.cpp.

References defaultLogger, and log().

Referenced by msgHandler4().

45 {
46  static QMutex recursiveMutex(QMutex::Recursive);
47  static QMutex nonRecursiveMutex(QMutex::NonRecursive);
48 
49  // Prevent multiple threads from calling this method simultaneoulsy.
50  // But allow recursive calls, which is required to prevent a deadlock
51  // if the logger itself produces an error message.
52  recursiveMutex.lock();
53 
54  // Fall back to stderr when this method has been called recursively.
55  if (defaultLogger && nonRecursiveMutex.tryLock())
56  {
57  defaultLogger->log(type, message, file, function, line);
58  nonRecursiveMutex.unlock();
59  }
60  else
61  {
62  fputs(qPrintable(message),stderr);
63  fflush(stderr);
64  }
65 
66  // Abort the program after logging a fatal message
67  if (type==QtFatalMsg)
68  {
69  abort();
70  }
71 
72  recursiveMutex.unlock();
73 }
virtual void log(const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0)
Definition: logger.cpp:153
static Logger * defaultLogger
Definition: logger.h:153
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ msgHandler4()

void Logger::msgHandler4 ( const QtMsgType  type,
const char *  message 
)
staticprivate

Wrapper for QT version 4.

Parameters
typeMessage type (level)
messageMessage text
See also
msgHandler()

Definition at line 83 of file logger.cpp.

References msgHandler().

Referenced by installMsgHandler().

84  {
85  msgHandler(type,message);
86  }
static void msgHandler(const QtMsgType type, const QString &message, const QString &file="", const QString &function="", const int line=0)
Definition: logger.cpp:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 122 of file logger.cpp.

References logVars, and mutex.

123 {
124  mutex.lock();
125  if (!logVars.hasLocalData())
126  {
127  logVars.setLocalData(new QHash<QString,QString>);
128  }
129  logVars.localData()->insert(name,value);
130  mutex.unlock();
131 }
static QThreadStorage< QHash< QString, QString > * > logVars
Definition: logger.h:195
static QMutex mutex
Definition: logger.h:142

◆ setMinMessageLevel()

void qtwebapp::Logger::setMinMessageLevel ( const QtMsgType &  minMsgLevel)
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().

100  {
101  minLevel = minMsgLevel;
102  }
QtMsgType minLevel
Definition: logger.h:136
+ Here is the caller graph for this function:

◆ 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 qtwebapp::FileLogger.

Definition at line 104 of file logger.cpp.

References msgFormat, timestampFormat, and qtwebapp::LogMessage::toString().

Referenced by log(), and qtwebapp::FileLogger::write().

105 {
106  fputs(qPrintable(logMessage->toString(msgFormat,timestampFormat)),stderr);
107  fflush(stderr);
108 }
QString timestampFormat
Definition: logger.h:133
QString toString(const QString &msgFormat, const QString &timestampFormat) const
Definition: logmessage.cpp:29
QString msgFormat
Definition: logger.h:130
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ buffers

QThreadStorage<QList<LogMessage*>*> qtwebapp::Logger::buffers
private

Thread local backtrace buffers

Definition at line 198 of file logger.h.

Referenced by clear(), and log().

◆ bufferSize

int qtwebapp::Logger::bufferSize
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().

◆ defaultLogger

Logger * Logger::defaultLogger =0
staticprivate

Pointer to the default logger, used by msgHandler()

Definition at line 153 of file logger.h.

Referenced by installMsgHandler(), msgHandler(), and ~Logger().

◆ logVars

QThreadStorage< QHash< QString, QString > * > Logger::logVars
staticprivate

Thread local variables to be used in log messages

Definition at line 195 of file logger.h.

Referenced by clear(), log(), and set().

◆ minLevel

QtMsgType qtwebapp::Logger::minLevel
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().

◆ msgFormat

QString qtwebapp::Logger::msgFormat
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().

◆ mutex

QMutex Logger::mutex
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().

◆ timestampFormat

QString qtwebapp::Logger::timestampFormat
protected

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