Commit ff3bdbbb authored by Linus Jahn's avatar Linus Jahn
Browse files

Add XmlLogHandler to print incoming/outgoing XML

This new XmlLogHandler will print all incoming and outgoing
XML in a pretty format onto the console. It's using the QXml module.
parent 997a1d2b
......@@ -14,4 +14,5 @@ set(KAIDAN_SOURCES
${CURDIR}/Notifications.cpp
${CURDIR}/PresenceHandler.cpp
${CURDIR}/ServiceDiscoveryManager.cpp
${CURDIR}/XmlLogHandler.cpp
)
......@@ -48,6 +48,7 @@ Kaidan::Kaidan(QObject *parent) : QObject(parent)
// setup components
messageModel = new MessageModel(database->getDatabase());
rosterModel = new RosterModel(database->getDatabase());
xmlLogHandler = new XmlLogHandler();
//
// Load settings data
......@@ -113,6 +114,10 @@ void Kaidan::mainConnect()
client->registerStanzaExtension(new gloox::Receipt(gloox::Receipt::Request));
client->registerStanzaExtension(new gloox::Receipt(gloox::Receipt::Received));
// Logging
client->logInstance().registerLogHandler(gloox::LogLevelDebug,
gloox::LogAreaXmlIncoming | gloox::LogAreaXmlOutgoing, xmlLogHandler);
client->connect(false);
// every 100 ms: fetch new packages from the socket
......
......@@ -36,6 +36,7 @@
#include "MessageSessionHandler.h"
#include "PresenceHandler.h"
#include "ServiceDiscoveryManager.h"
#include "XmlLogHandler.h"
class Kaidan : public QObject, public gloox::ConnectionListener
{
......@@ -102,6 +103,7 @@ private:
MessageSessionHandler *messageSessionHandler;
PresenceHandler *presenceHandler;
ServiceDiscoveryManager *serviceDiscoveryManager;
XmlLogHandler *xmlLogHandler;
QSettings *settings;
bool connected;
......
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2017 LNJ <git@lnj.li>
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#include "XmlLogHandler.h"
#include <QDebug>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
void XmlLogHandler::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string &message)
{
if (area == gloox::LogAreaXmlIncoming) {
qDebug() << "[XML] [Incoming] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
qDebug().noquote() << makeXmlPretty(QString::fromStdString(message));
} else if (area == gloox::LogAreaXmlOutgoing) {
qDebug() << "[XML] [Outgoing] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
qDebug().noquote() << makeXmlPretty(QString::fromStdString(message));
}
}
QString XmlLogHandler::makeXmlPretty(QString xmlIn)
{
QString xmlOut;
QXmlStreamReader reader(xmlIn);
QXmlStreamWriter writer(&xmlOut);
writer.setAutoFormatting(true);
while (!reader.atEnd()) {
reader.readNext();
if (!reader.isWhitespace()) {
writer.writeCurrentToken(reader);
}
}
// remove xml header
xmlOut.replace("<?xml version=\"1.0\"?>\n", "");
// remove last char (\n)
xmlOut = xmlOut.left(xmlOut.size() - 1);
return xmlOut;
}
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2017 LNJ <git@lnj.li>
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMLLOGHANDLER_H
#define XMLLOGHANDLER_H
#include <gloox/loghandler.h>
class QString;
class XmlLogHandler : public gloox::LogHandler
{
public:
virtual void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string &message);
static QString makeXmlPretty(QString inputXml);
};
#endif // XMLLOGHANDLER_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment