QXmpp  Version:1.3.1
QXmppLogger.h
1 /*
2  * Copyright (C) 2008-2020 The QXmpp developers
3  *
4  * Author:
5  * Manjeet Dahiya
6  * Jeremy LainĂ©
7  *
8  * Source:
9  * https://github.com/qxmpp-project/qxmpp
10  *
11  * This file is a part of QXmpp library.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  */
24 
25 #ifndef QXMPPLOGGER_H
26 #define QXMPPLOGGER_H
27 
28 #include "QXmppGlobal.h"
29 
30 #include <QObject>
31 
32 #ifdef QXMPP_LOGGABLE_TRACE
33 #define qxmpp_loggable_trace(x) QString("%1(0x%2) %3").arg(metaObject()->className(), QString::number(reinterpret_cast<qint64>(this), 16), x)
34 #else
35 #define qxmpp_loggable_trace(x) (x)
36 #endif
37 
38 class QXmppLoggerPrivate;
39 
45 class QXMPP_EXPORT QXmppLogger : public QObject
46 {
47  Q_OBJECT
48  Q_FLAGS(MessageType MessageTypes)
49 
50 
51  Q_PROPERTY(QString logFilePath READ logFilePath WRITE setLogFilePath)
53  Q_PROPERTY(LoggingType loggingType READ loggingType WRITE setLoggingType)
55  Q_PROPERTY(MessageTypes messageTypes READ messageTypes WRITE setMessageTypes)
56 
57 public:
59  enum LoggingType {
60  NoLogging = 0,
61  FileLogging = 1,
62  StdoutLogging = 2,
63  SignalLogging = 4
64  };
65  Q_ENUM(LoggingType)
66 
67 
68  enum MessageType {
69  NoMessage = 0,
70  DebugMessage = 1,
71  InformationMessage = 2,
72  WarningMessage = 4,
73  ReceivedMessage = 8,
74  SentMessage = 16,
75  AnyMessage = 31
76  };
77  Q_DECLARE_FLAGS(MessageTypes, MessageType)
78 
79  QXmppLogger(QObject *parent = nullptr);
80  ~QXmppLogger() override;
81 
82  static QXmppLogger *getLogger();
83 
84  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
86  QXmppLogger::LoggingType loggingType();
87  void setLoggingType(QXmppLogger::LoggingType type);
88 
89  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
95  QString logFilePath();
96  void setLogFilePath(const QString &path);
97 
98  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
100  QXmppLogger::MessageTypes messageTypes();
101  void setMessageTypes(QXmppLogger::MessageTypes types);
102 
103 public Q_SLOTS:
104  virtual void setGauge(const QString &gauge, double value);
105  virtual void updateCounter(const QString &counter, qint64 amount);
106 
107  void log(QXmppLogger::MessageType type, const QString &text);
108  void reopen();
109 
110 Q_SIGNALS:
112  void message(QXmppLogger::MessageType type, const QString &text);
113 
114 private:
115  static QXmppLogger *m_logger;
116  QXmppLoggerPrivate *d;
117 };
118 
122 
123 class QXMPP_EXPORT QXmppLoggable : public QObject
124 {
125  Q_OBJECT
126 
127 public:
128  QXmppLoggable(QObject *parent = nullptr);
129 
130 protected:
132  void childEvent(QChildEvent *event) override;
134 
138 
139  void debug(const QString &message)
140  {
141  emit logMessage(QXmppLogger::DebugMessage, qxmpp_loggable_trace(message));
142  }
143 
147 
148  void info(const QString &message)
149  {
150  emit logMessage(QXmppLogger::InformationMessage, qxmpp_loggable_trace(message));
151  }
152 
156 
157  void warning(const QString &message)
158  {
159  emit logMessage(QXmppLogger::WarningMessage, qxmpp_loggable_trace(message));
160  }
161 
165 
166  void logReceived(const QString &message)
167  {
168  emit logMessage(QXmppLogger::ReceivedMessage, qxmpp_loggable_trace(message));
169  }
170 
174 
175  void logSent(const QString &message)
176  {
177  emit logMessage(QXmppLogger::SentMessage, qxmpp_loggable_trace(message));
178  }
179 
180 Q_SIGNALS:
182  void setGauge(const QString &gauge, double value);
183 
185  void logMessage(QXmppLogger::MessageType type, const QString &msg);
186 
188  void updateCounter(const QString &counter, qint64 amount = 1);
189 };
190 
191 Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppLogger::MessageTypes)
192 #endif // QXMPPLOGGER_H
QXmppLogger::DebugMessage
@ DebugMessage
Debugging message.
Definition: QXmppLogger.h:70
QXmppLoggable::warning
void warning(const QString &message)
Definition: QXmppLogger.h:157
QXmppLoggable::debug
void debug(const QString &message)
Definition: QXmppLogger.h:139
QXmppLogger::MessageType
MessageType
This enum describes a type of log message.
Definition: QXmppLogger.h:68
QXmppLogger::ReceivedMessage
@ ReceivedMessage
Message received from server.
Definition: QXmppLogger.h:73
QXmppLogger::SentMessage
@ SentMessage
Message sent to server.
Definition: QXmppLogger.h:74
QXmppLoggable::info
void info(const QString &message)
Definition: QXmppLogger.h:148
QXmppLogger::InformationMessage
@ InformationMessage
Informational message.
Definition: QXmppLogger.h:71
QXmppLogger::LoggingType
LoggingType
This enum describes how log message are handled.
Definition: QXmppLogger.h:59
QXmppLogger
The QXmppLogger class represents a sink for logging messages.
Definition: QXmppLogger.h:45
QXmppLogger::WarningMessage
@ WarningMessage
Warning message.
Definition: QXmppLogger.h:72
QXmppLoggable
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:123
QXmppLoggable::logReceived
void logReceived(const QString &message)
Definition: QXmppLogger.h:166
QXmppLoggable::logSent
void logSent(const QString &message)
Definition: QXmppLogger.h:175