QXmpp Version: 1.13.0
Loading...
Searching...
No Matches
QXmppLogger.h
1// SPDX-FileCopyrightText: 2009 Manjeet Dahiya <manjeetdahiya@gmail.com>
2// SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#ifndef QXMPPLOGGER_H
7#define QXMPPLOGGER_H
8
9#include "QXmppGlobal.h"
10
11#include <memory>
12
13#include <QObject>
14
15#ifdef QXMPP_LOGGABLE_TRACE
16#define qxmpp_loggable_trace(x) QString("%1(0x%2) %3").arg(metaObject()->className(), QString::number(reinterpret_cast<qint64>(this), 16), x)
17#else
18#define qxmpp_loggable_trace(x) (x)
19#endif
20
21class QXmppLoggerPrivate;
22
28class QXMPP_EXPORT QXmppLogger : public QObject
29{
30 Q_OBJECT
31 Q_FLAGS(MessageType MessageTypes)
32
33
34 Q_PROPERTY(QString logFilePath READ logFilePath WRITE setLogFilePath NOTIFY logFilePathChanged)
38 Q_PROPERTY(MessageTypes messageTypes READ messageTypes WRITE setMessageTypes NOTIFY messageTypesChanged)
39
40public:
48 Q_ENUM(LoggingType)
49
50
60 Q_DECLARE_FLAGS(MessageTypes, MessageType)
61
62 QXmppLogger(QObject *parent = nullptr);
63 ~QXmppLogger() override;
64
65 static QXmppLogger *getLogger();
66
67 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
71 Q_SIGNAL void loggingTypeChanged();
72
73 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
79 QString logFilePath();
80 void setLogFilePath(const QString &path);
81 Q_SIGNAL void logFilePathChanged();
82
83 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
85 QXmppLogger::MessageTypes messageTypes();
86 void setMessageTypes(QXmppLogger::MessageTypes types);
87 Q_SIGNAL void messageTypesChanged();
88
89 Q_SLOT virtual void setGauge(const QString &gauge, double value);
90 Q_SLOT virtual void updateCounter(const QString &counter, qint64 amount);
91
92 Q_SLOT void log(QXmppLogger::MessageType type, const QString &text);
93 Q_SLOT void reopen();
94
96 Q_SIGNAL void message(QXmppLogger::MessageType type, const QString &text);
97
98private:
99 static QXmppLogger *m_logger;
100 const std::unique_ptr<QXmppLoggerPrivate> d;
101};
102
108class QXMPP_EXPORT QXmppLoggable : public QObject
109{
110 Q_OBJECT
111
112public:
113 QXmppLoggable(QObject *parent = nullptr);
114
115protected:
117 void childEvent(QChildEvent *event) override;
119
121 void debug(const QString &message)
122 {
123 Q_EMIT logMessage(QXmppLogger::DebugMessage, qxmpp_loggable_trace(message));
124 }
125
127 void info(const QString &message)
128 {
129 Q_EMIT logMessage(QXmppLogger::InformationMessage, qxmpp_loggable_trace(message));
130 }
131
133 void warning(const QString &message)
134 {
135 Q_EMIT logMessage(QXmppLogger::WarningMessage, qxmpp_loggable_trace(message));
136 }
137
139 void logReceived(const QString &message)
140 {
141 Q_EMIT logMessage(QXmppLogger::ReceivedMessage, qxmpp_loggable_trace(message));
142 }
143
145 void logSent(const QString &message)
146 {
147 Q_EMIT logMessage(QXmppLogger::SentMessage, qxmpp_loggable_trace(message));
148 }
149
150public:
152 Q_SIGNAL void setGauge(const QString &gauge, double value);
153
155 Q_SIGNAL void logMessage(QXmppLogger::MessageType type, const QString &msg);
156
158 Q_SIGNAL void updateCounter(const QString &counter, qint64 amount = 1);
159};
160
161Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppLogger::MessageTypes)
162#endif // QXMPPLOGGER_H
void logSent(const QString &message)
Logs a sent packet.
Definition QXmppLogger.h:145
Q_SIGNAL void logMessage(QXmppLogger::MessageType type, const QString &msg)
This signal is emitted to send logging messages.
Q_SIGNAL void updateCounter(const QString &counter, qint64 amount=1)
Updates the given counter by amount.
void info(const QString &message)
Logs an informational message.
Definition QXmppLogger.h:127
QXmppLoggable(QObject *parent=nullptr)
Definition QXmppLogger.cpp:57
Q_SIGNAL void setGauge(const QString &gauge, double value)
Sets the given gauge to value.
void debug(const QString &message)
Logs a debugging message.
Definition QXmppLogger.h:121
void warning(const QString &message)
Logs a warning message.
Definition QXmppLogger.h:133
void logReceived(const QString &message)
Logs a received packet.
Definition QXmppLogger.h:139
The QXmppLogger class represents a sink for logging messages.
Definition QXmppLogger.h:29
void setLoggingType(QXmppLogger::LoggingType type)
Sets the handler for logging messages.
Definition QXmppLogger.cpp:139
virtual Q_SLOT void setGauge(const QString &gauge, double value)
Definition QXmppLogger.cpp:210
Q_SIGNAL void messageTypesChanged()
Q_SLOT void reopen()
If logging to a file, causes the file to be re-opened.
Definition QXmppLogger.cpp:257
Q_SIGNAL void loggingTypeChanged()
QString logFilePath
The path to which logging messages should be written.
Definition QXmppLogger.h:34
QXmppLogger::LoggingType loggingType()
Returns the handler for logging messages.
Q_SLOT void log(QXmppLogger::MessageType type, const QString &text)
Add a logging message.
Definition QXmppLogger.cpp:179
void setLogFilePath(const QString &path)
Definition QXmppLogger.cpp:239
MessageTypes messageTypes
The types of messages to log.
Definition QXmppLogger.h:38
LoggingType loggingType
The handler for logging messages.
Definition QXmppLogger.h:36
void setMessageTypes(QXmppLogger::MessageTypes types)
Sets the types of messages to log.
Definition QXmppLogger.cpp:162
MessageType
This enum describes a type of log message.
Definition QXmppLogger.h:51
@ AnyMessage
Any message type.
Definition QXmppLogger.h:58
@ ReceivedMessage
Message received from server.
Definition QXmppLogger.h:56
@ NoMessage
No message type.
Definition QXmppLogger.h:52
@ InformationMessage
Informational message.
Definition QXmppLogger.h:54
@ SentMessage
Message sent to server.
Definition QXmppLogger.h:57
@ DebugMessage
Debugging message.
Definition QXmppLogger.h:53
@ WarningMessage
Warning message.
Definition QXmppLogger.h:55
QString logFilePath()
LoggingType
This enum describes how log message are handled.
Definition QXmppLogger.h:42
@ FileLogging
Log messages are written to a file.
Definition QXmppLogger.h:44
@ NoLogging
Log messages are discarded.
Definition QXmppLogger.h:43
@ SignalLogging
Log messages are emitted as a signal.
Definition QXmppLogger.h:46
@ StdoutLogging
Log messages are written to the standard output.
Definition QXmppLogger.h:45
virtual Q_SLOT void updateCounter(const QString &counter, qint64 amount)
Definition QXmppLogger.cpp:221
Q_SIGNAL void message(QXmppLogger::MessageType type, const QString &text)
This signal is emitted whenever a log message is received.
QXmppLogger::MessageTypes messageTypes()
Returns the types of messages to log.
QXmppLogger(QObject *parent=nullptr)
Definition QXmppLogger.cpp:111
Q_SIGNAL void logFilePathChanged()