diff options
author | Alex Wilson <alex.wilson@nokia.com> | 2012-05-29 16:33:14 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-31 03:45:46 +0200 |
commit | 120d92e181427aeef73201d6b8002590b78e88ef (patch) | |
tree | e8c27facfe7da7ece40b76c5136e63b2f4625784 /src/tools | |
parent | 66737cdb9c6dae73328911412ec4017b17050157 (diff) |
UDP multicast logging for serviceframework
Slightly more organised way to receive debug messages. Also includes
a really basic (somewhat hacky) client app to receive the messages.
Change-Id: Ic578ddf7cc2fec9b97c5ac4f015a832137174625
Reviewed-by: Andrew Stanley-Jones <andrew.stanley-jones@nokia.com>
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/sfwlisten/main.cpp | 196 | ||||
-rw-r--r-- | src/tools/sfwlisten/sfwlisten.pro | 3 | ||||
-rw-r--r-- | src/tools/tools.pro | 2 |
3 files changed, 200 insertions, 1 deletions
diff --git a/src/tools/sfwlisten/main.cpp b/src/tools/sfwlisten/main.cpp new file mode 100644 index 00000000..96c6e9aa --- /dev/null +++ b/src/tools/sfwlisten/main.cpp @@ -0,0 +1,196 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSystems module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QUdpSocket> +#include <QHostAddress> +#include <QCoreApplication> +#include <QBuffer> +#include <QDataStream> +#include <QTime> +#include <QTimer> +#include <QNetworkInterface> +#include <cstdio> + +/* keep this in sync with qtsystems/src/serviceframework/qservicedebuglog_p.h */ +enum DataType { + Int32Type = 1, + FloatType = 2, + StringType = 3 +}; + +class SFWReceiver : public QObject +{ + Q_OBJECT +public: + SFWReceiver(const QString &intfFilter); + +public slots: + void makeSockets(); + void socketReadyRead(); + +private: + QString intfFilter; +}; + +SFWReceiver::SFWReceiver(const QString &intfFilter) : intfFilter(intfFilter) +{ + makeSockets(); +} + +void SFWReceiver::makeSockets() +{ + QList<QNetworkInterface> intfs = QNetworkInterface::allInterfaces(); + + bool gotone = false; + foreach (const QNetworkInterface &intf, intfs) { + if (!intfFilter.isEmpty() && !intf.name().startsWith(intfFilter)) + continue; + + if (intf.name() == "lo" || intf.name().startsWith("wifi") + || intf.name().startsWith("wl") || intf.name().startsWith("ppp") + || intf.name().startsWith("tun") || intf.name().startsWith("tap")) + continue; + + QUdpSocket *socket = new QUdpSocket(this); + printf("Trying interface %s ...", qPrintable(intf.name())); + if (!socket->bind(QHostAddress::AnyIPv4, 10520, QUdpSocket::ShareAddress)) { + printf("Couldn't bind: %s\n", qPrintable(socket->errorString())); + delete socket; + continue; + } + socket->setMulticastInterface(intf); + if (!socket->joinMulticastGroup(QHostAddress("224.0.105.201"), intf)) { + printf("Couldn't join group: %s\n", qPrintable(socket->errorString())); + delete socket; + continue; + } + + printf("ok\n"); + if (!gotone) { + connect(socket, SIGNAL(readyRead()), + this, SLOT(socketReadyRead())); + gotone = true; + } + } + + if (!gotone) { + QTimer::singleShot(200, this, SLOT(makeSockets())); + } +} + +void SFWReceiver::socketReadyRead() +{ + QUdpSocket *socket = qobject_cast<QUdpSocket *>(sender()); + QString intf = socket->multicastInterface().name(); + + while (socket->hasPendingDatagrams()) { + QByteArray dgram; + dgram.resize(socket->pendingDatagramSize()); + socket->readDatagram(dgram.data(), dgram.size()); + + QBuffer buff(&dgram); + buff.open(QIODevice::ReadOnly); + QDataStream ds(&buff); + + qint32 pid; + ds >> pid; + + char *str; + uint len; + + ds.readBytes(str, len); + QByteArray appName(str, len); + delete[] str; + + QTime t = QTime::currentTime(); + printf("{%4s} %2d:%02d:%02d.%03d ", qPrintable(intf), t.hour(), t.minute(), t.second(), t.msec()); + printf("[%5d/%10s] ", pid, appName.constData()); + while (!ds.atEnd()) { + ds.readBytes(str, len); + QByteArray termName(str, len); + delete[] str; + + printf("{%s, ", termName.constData()); + + qint8 type; + ds >> type; + DataType dt = static_cast<DataType>(type); + switch (dt) { + case Int32Type: + { + qint32 data; + ds >> data; + printf("%d} ", data); + } + break; + case FloatType: + { + float data; + ds >> data; + printf("%.4f} ", data); + } + break; + case StringType: + { + ds.readBytes(str, len); + QByteArray ba(str, len); + ba.truncate(35); + printf("'%s'} ", ba.constData()); + } + break; + } + } + + printf("\n"); + } +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QString filter; + if (argc > 1) + filter = QString::fromLatin1(argv[1]); + SFWReceiver recv(filter); + return app.exec(); +} + +#include "main.moc" diff --git a/src/tools/sfwlisten/sfwlisten.pro b/src/tools/sfwlisten/sfwlisten.pro new file mode 100644 index 00000000..9242616d --- /dev/null +++ b/src/tools/sfwlisten/sfwlisten.pro @@ -0,0 +1,3 @@ +TEMPLATE = app +SOURCES = main.cpp +QT = core network
\ No newline at end of file diff --git a/src/tools/tools.pro b/src/tools/tools.pro index 47fb72a4..3e815d51 100644 --- a/src/tools/tools.pro +++ b/src/tools/tools.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -!without-serviceframework: SUBDIRS = servicefw +!without-serviceframework: SUBDIRS = servicefw sfwlisten |