12 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
13 #include <QRecursiveMutex>
16 using namespace stefanfrings;
18 Logger* Logger::defaultLogger=
nullptr;
21 QThreadStorage<QHash<QString,QString>*> Logger::logVars;
29 msgFormat(
"{timestamp} {type} {msg}"),
30 timestampFormat(
"dd.MM.yyyy hh:mm:ss.zzz"),
36 Logger::Logger(
const QString msgFormat,
const QString timestampFormat,
const QtMsgType minLevel,
const int bufferSize, QObject* parent)
45 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
46 static QRecursiveMutex recursiveMutex;
47 static QMutex nonRecursiveMutex;
49 static QMutex recursiveMutex(QMutex::Recursive);
50 static QMutex nonRecursiveMutex(QMutex::NonRecursive);
53 void Logger::msgHandler(
const QtMsgType type,
const QString &message,
const QString &file,
const QString &
function,
const int line)
58 recursiveMutex.lock();
61 if (defaultLogger && nonRecursiveMutex.tryLock())
63 defaultLogger->
log(type, message, file,
function, line);
64 nonRecursiveMutex.unlock();
68 fputs(qPrintable(message),stderr);
78 recursiveMutex.unlock();
82 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
83 void Logger::msgHandler5(
const QtMsgType type,
const QMessageLogContext &context,
const QString &message)
86 msgHandler(type,message,context.file,context.function,context.line);
89 void Logger::msgHandler4(
const QtMsgType type,
const char* message)
91 msgHandler(type,message);
98 if (defaultLogger==
this)
100 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
101 qInstallMessageHandler(
nullptr);
103 qInstallMsgHandler(
nullptr);
105 defaultLogger=
nullptr;
120 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
121 qInstallMessageHandler(msgHandler5);
123 qInstallMsgHandler(msgHandler4);
131 if (!logVars.hasLocalData())
133 logVars.setLocalData(
new QHash<QString,QString>);
135 logVars.localData()->insert(name,value);
143 if (buffer && buffers.hasLocalData())
145 QList<LogMessage*>* buffer=buffers.localData();
146 while (buffer && !buffer->isEmpty())
152 if (variables && logVars.hasLocalData())
154 logVars.localData()->clear();
160 void Logger::log(
const QtMsgType type,
const QString& message,
const QString &file,
const QString &
function,
const int line)
175 #if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
187 #
if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
198 #
if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
222 if (!buffers.hasLocalData())
224 buffers.setLocalData(
new QList<LogMessage*>());
226 QList<LogMessage*>* buffer=buffers.localData();
230 buffer->append(logMessage);
235 delete buffer->takeFirst();
242 while (!buffer->isEmpty())
256 LogMessage logMessage(type,message,logVars.localData(),file,
function,line);
Represents a single log message together with some data that are used to decorate the log message.
QString toString(const QString &msgFormat, const QString ×tampFormat) const
Returns the log message as decorated string.
Decorates and writes log messages to the console, stderr.
Logger(QObject *parent)
Constructor.
QtMsgType minLevel
Minimum level of message types that are written out directly or trigger writing the buffered content.
QString timestampFormat
Format string of timestamps.
void installMsgHandler()
Installs this logger as the default message handler, so it can be used through the global static logg...
virtual void clear(const bool buffer=true, const bool variables=true)
Clear the thread-local data of the current thread.
virtual void write(const LogMessage *logMessage)
Decorate and write a log message to stderr.
static QMutex mutex
Used to synchronize access of concurrent threads.
int bufferSize
Size of backtrace buffer, number of messages per thread.
QString msgFormat
Format string for message decoration.
virtual ~Logger()
Destructor.
static void set(const QString &name, const QString &value)
Sets a thread-local variable that may be used to decorate log messages.
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.