-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLogHandler.cpp
84 lines (71 loc) · 2.36 KB
/
LogHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-FileCopyrightText: 2017 Linus Jahn <[email protected]>
// SPDX-FileCopyrightText: 2020 Melvin Keskin <[email protected]>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "LogHandler.h"
#include "kaidan_debug.h"
// Qt
#include <QDebug>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
// QXmpp
#include <QXmppClient.h>
LogHandler::LogHandler(QXmppClient *client, bool enable, QObject *parent)
: QObject(parent)
, m_client(client)
{
client->logger()->setLoggingType(QXmppLogger::SignalLogging);
enableLogging(enable);
}
void LogHandler::enableLogging(bool enabled)
{
// check if we need to change something
if (this->enabled == enabled)
return;
// update enabled status
this->enabled = enabled;
// apply change: enable or disable
if (enabled)
connect(m_client->logger(), &QXmppLogger::message, this, &LogHandler::handleLog);
else
disconnect(m_client->logger(), &QXmppLogger::message, this, &LogHandler::handleLog);
}
void LogHandler::handleLog(QXmppLogger::MessageType type, const QString &text)
{
switch (type) {
case QXmppLogger::ReceivedMessage:
qCDebug(KAIDAN_LOG) << "[client] [incoming] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
qDebug().noquote() << makeXmlPretty(text);
break;
case QXmppLogger::SentMessage:
qCDebug(KAIDAN_LOG) << "[client] [outgoing] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
qDebug().noquote() << makeXmlPretty(text);
break;
case QXmppLogger::WarningMessage:
qDebug().noquote() << "[client] [warn]" << text;
break;
default:
break;
}
}
QString LogHandler::makeXmlPretty(QString xmlIn)
{
QString xmlOut;
QXmlStreamReader reader(xmlIn);
QXmlStreamWriter writer(&xmlOut);
writer.setAutoFormatting(true);
while (!reader.atEnd()) {
reader.readNext();
if (!reader.isWhitespace() && !reader.hasError()) {
writer.writeCurrentToken(reader);
}
}
// remove xml header
xmlOut.replace(QStringLiteral("<?xml version=\"1.0\"?>"), QStringLiteral(""));
// remove first & last char (\n)
// first char is needed due to header replacement
xmlOut = xmlOut.right(xmlOut.size() - 1);
xmlOut = xmlOut.left(xmlOut.size() - 1);
return xmlOut;
}
#include "moc_LogHandler.cpp"