From f161773a0221393192b20bab25c62a8645e6cdd4 Mon Sep 17 00:00:00 2001 From: Aurindam Jana Date: Tue, 8 Nov 2011 11:47:33 +0100 Subject: [PATCH] QDeclarativeDebug: Add a debug message service. QDeclarativeDebugMsgService installs a QtMsgHandler which forwards debug output to a client defined port only if the service is Enabled. It also forwards the debug output to the previous message handler. Effectively, this service just eavesdrop on debug output, forwarding it to a port only if a client is connected. Change-Id: Ie0ee7bab57ef8f03a2de34d91921f054a7ec147f Reviewed-by: Kai Koehne --- src/declarative/debugger/debugger.pri | 6 +- src/declarative/debugger/qdebugmessageservice.cpp | 123 +++++++++++++ src/declarative/debugger/qdebugmessageservice_p.h | 88 +++++++++ src/declarative/qml/qdeclarativeengine.cpp | 2 + tests/auto/declarative/debugger/debugger.pro | 3 +- .../debugger/qdebugmessageservice/data/test.qml | 51 ++++++ .../qdebugmessageservice/qdebugmessageservice.pro | 21 +++ .../tst_qdebugmessageservice.cpp | 186 ++++++++++++++++++++ 8 files changed, 477 insertions(+), 3 deletions(-) create mode 100644 src/declarative/debugger/qdebugmessageservice.cpp create mode 100644 src/declarative/debugger/qdebugmessageservice_p.h create mode 100644 tests/auto/declarative/debugger/qdebugmessageservice/data/test.qml create mode 100644 tests/auto/declarative/debugger/qdebugmessageservice/qdebugmessageservice.pro create mode 100644 tests/auto/declarative/debugger/qdebugmessageservice/tst_qdebugmessageservice.cpp diff --git a/src/declarative/debugger/debugger.pri b/src/declarative/debugger/debugger.pri index e5508a2..34e88ae 100644 --- a/src/declarative/debugger/debugger.pri +++ b/src/declarative/debugger/debugger.pri @@ -9,7 +9,8 @@ SOURCES += \ $$PWD/qdeclarativeinspectorservice.cpp \ $$PWD/qv8debugservice.cpp \ $$PWD/qv8profilerservice.cpp \ - $$PWD/qdeclarativeenginedebugservice.cpp + $$PWD/qdeclarativeenginedebugservice.cpp \ + $$PWD/qdebugmessageservice.cpp HEADERS += \ $$PWD/qpacketprotocol_p.h \ @@ -27,4 +28,5 @@ HEADERS += \ $$PWD/qv8debugservice_p.h \ $$PWD/qv8profilerservice_p.h \ $$PWD/qdeclarativeenginedebugservice_p.h \ - $$PWD/qdeclarativedebug.h + $$PWD/qdeclarativedebug.h \ + $$PWD/qdebugmessageservice_p.h diff --git a/src/declarative/debugger/qdebugmessageservice.cpp b/src/declarative/debugger/qdebugmessageservice.cpp new file mode 100644 index 0000000..1604e35 --- /dev/null +++ b/src/declarative/debugger/qdebugmessageservice.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative 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 "qdebugmessageservice_p.h" +#include "qdeclarativedebugservice_p_p.h" + +QT_BEGIN_NAMESPACE + +Q_GLOBAL_STATIC(QDebugMessageService, declarativeDebugMessageService) + +void DebugMessageHandler(QtMsgType type, const char *buf) +{ + QDebugMessageService::instance()->sendDebugMessage(type, buf); +} + +class QDebugMessageServicePrivate : public QDeclarativeDebugServicePrivate +{ +public: + QDebugMessageServicePrivate() + : oldMsgHandler(0) + , prevStatus(QDeclarativeDebugService::NotConnected) + { + } + + QtMsgHandler oldMsgHandler; + QDeclarativeDebugService::Status prevStatus; +}; + +QDebugMessageService::QDebugMessageService(QObject *parent) : + QDeclarativeDebugService(*(new QDebugMessageServicePrivate()), + QLatin1String("DebugMessages"), 1, parent) +{ + Q_D(QDebugMessageService); + + registerService(); + if (status() == Enabled) { + d->oldMsgHandler = qInstallMsgHandler(DebugMessageHandler); + d->prevStatus = Enabled; + } +} + +QDebugMessageService *QDebugMessageService::instance() +{ + return declarativeDebugMessageService(); +} + +void QDebugMessageService::sendDebugMessage(QtMsgType type, const char *buf) +{ + Q_D(QDebugMessageService); + + //We do not want to alter the message handling mechanism + //We just eavesdrop and forward the messages to a port + //only if a client is connected to it. + QByteArray debugMessage; + QDataStream rs(&debugMessage, QIODevice::WriteOnly); + rs << type << QString::fromLocal8Bit(buf).toUtf8(); + + QByteArray message; + QDataStream ws(&message, QIODevice::WriteOnly); + ws << QByteArray("MESSAGE") << debugMessage; + + sendMessage(message); + if (d->oldMsgHandler) + (*d->oldMsgHandler)(type, buf); +} + +void QDebugMessageService::statusChanged(Status status) +{ + Q_D(QDebugMessageService); + + if (status != Enabled && d->prevStatus == Enabled) { + QtMsgHandler handler = qInstallMsgHandler(d->oldMsgHandler); + // has our handler been overwritten in between? + if (handler != DebugMessageHandler) + qInstallMsgHandler(handler); + + } else if (status == Enabled && d->prevStatus != Enabled) { + d->oldMsgHandler = qInstallMsgHandler(DebugMessageHandler); + + } + + d->prevStatus = status; +} + +QT_END_NAMESPACE diff --git a/src/declarative/debugger/qdebugmessageservice_p.h b/src/declarative/debugger/qdebugmessageservice_p.h new file mode 100644 index 0000000..c2951e6 --- /dev/null +++ b/src/declarative/debugger/qdebugmessageservice_p.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative 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$ +** +****************************************************************************/ + +#ifndef QDEBUGMESSAGESERVICE_P_H +#define QDEBUGMESSAGESERVICE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qdeclarativedebugservice_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QDebugMessageServicePrivate; + +class QDebugMessageService : public QDeclarativeDebugService +{ + Q_OBJECT +public: + QDebugMessageService(QObject *parent = 0); + + static QDebugMessageService *instance(); + + void sendDebugMessage(QtMsgType type, const char *buf); + +protected: + void statusChanged(Status); + +private: + Q_DISABLE_COPY(QDebugMessageService) + Q_DECLARE_PRIVATE(QDebugMessageService) +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif // QDEBUGMESSAGESERVICE_P_H diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 113abdc..1d613cc 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -70,6 +70,7 @@ #include #include #include +#include #include "qdeclarativeincubator.h" #include @@ -451,6 +452,7 @@ void QDeclarativeEnginePrivate::init() QV8DebugService::initialize(v8engine()); QV8ProfilerService::initialize(); QDeclarativeDebugTrace::initialize(); + QDebugMessageService::instance(); } } diff --git a/tests/auto/declarative/debugger/debugger.pro b/tests/auto/declarative/debugger/debugger.pro index bdcb528..6d7548c 100644 --- a/tests/auto/declarative/debugger/debugger.pro +++ b/tests/auto/declarative/debugger/debugger.pro @@ -8,7 +8,8 @@ PRIVATETESTS += \ qdeclarativeinspector \ qdeclarativedebugtrace \ qpacketprotocol \ - qv8profilerservice + qv8profilerservice \ + qdebugmessageservice contains(QT_CONFIG, private_tests) { SUBDIRS += $$PRIVATETESTS diff --git a/tests/auto/declarative/debugger/qdebugmessageservice/data/test.qml b/tests/auto/declarative/debugger/qdebugmessageservice/data/test.qml new file mode 100644 index 0000000..7377d45 --- /dev/null +++ b/tests/auto/declarative/debugger/qdebugmessageservice/data/test.qml @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite 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$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Item { + width: 360 + height: 360 + Timer { + interval: 100; running: true; repeat: true + onTriggered: console.log("Timer") + } +} diff --git a/tests/auto/declarative/debugger/qdebugmessageservice/qdebugmessageservice.pro b/tests/auto/declarative/debugger/qdebugmessageservice/qdebugmessageservice.pro new file mode 100644 index 0000000..698e960 --- /dev/null +++ b/tests/auto/declarative/debugger/qdebugmessageservice/qdebugmessageservice.pro @@ -0,0 +1,21 @@ +CONFIG += testcase +TARGET = tst_qdebugmessageservice +QT += network declarative-private testlib +macx:CONFIG -= app_bundle + +HEADERS += ../shared/debugutil_p.h + +SOURCES += tst_qdebugmessageservice.cpp \ + ../shared/debugutil.cpp + +INCLUDEPATH += ../shared + +include(../../../shared/util.pri) + +testDataFiles.files = data +testDataFiles.path = . +DEPLOYMENT += testDataFiles + +CONFIG += parallel_test + +OTHER_FILES += data/test.qml diff --git a/tests/auto/declarative/debugger/qdebugmessageservice/tst_qdebugmessageservice.cpp b/tests/auto/declarative/debugger/qdebugmessageservice/tst_qdebugmessageservice.cpp new file mode 100644 index 0000000..62f89e9 --- /dev/null +++ b/tests/auto/declarative/debugger/qdebugmessageservice/tst_qdebugmessageservice.cpp @@ -0,0 +1,186 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite 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 + +//QDeclarativeDebugTest +#include "../shared/debugutil_p.h" +#include "../../../shared/util.h" + +#include +#include + +const char *NORMALMODE = "-qmljsdebugger=port:3777"; +const char *QMLFILE = "test.qml"; + +class QDeclarativeDebugMsgClient; +class tst_QDebugMessageService : public QDeclarativeDataTest +{ + Q_OBJECT + +public: + tst_QDebugMessageService(); + +private slots: + void initTestCase(); + void cleanupTestCase(); + + void init(); + void cleanup(); + + void retrieveDebugOutput(); + +private: + QDeclarativeDebugProcess *m_process; + QDeclarativeDebugMsgClient *m_client; + QDeclarativeDebugConnection *m_connection; +}; + +class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient +{ + Q_OBJECT +public: + QDeclarativeDebugMsgClient(QDeclarativeDebugConnection *connection) + : QDeclarativeDebugClient(QLatin1String("DebugMessages"), connection) + { + } + +protected: + //inherited from QDeclarativeDebugClient + void statusChanged(Status status); + void messageReceived(const QByteArray &data); + +signals: + void enabled(); + void debugOutput(); + +public: + QByteArray debugMessage; +}; + +void QDeclarativeDebugMsgClient::statusChanged(Status status) +{ + if (status == Enabled) { + emit enabled(); + } +} + +void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data) +{ + QDataStream ds(data); + QByteArray command; + ds >> command; + + if (command == "MESSAGE") { + int type; + ds >> type >> debugMessage; + emit debugOutput(); + } +} + +tst_QDebugMessageService::tst_QDebugMessageService() +{ +} + +void tst_QDebugMessageService::initTestCase() +{ + QDeclarativeDataTest::initTestCase(); + m_process = 0; + m_client = 0; + m_connection = 0; +} + +void tst_QDebugMessageService::cleanupTestCase() +{ + if (m_process) + delete m_process; + + if (m_client) + delete m_client; + + if (m_connection) + delete m_connection; +} + +void tst_QDebugMessageService::init() +{ + m_connection = new QDeclarativeDebugConnection(); + m_process = new QDeclarativeDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene"); + m_client = new QDeclarativeDebugMsgClient(m_connection); + + m_process->start(QStringList() << QLatin1String(NORMALMODE) << QDeclarativeDataTest::instance()->testFile(QMLFILE)); + if (!m_process->waitForSessionStart()) { + QFAIL(QString("Could not launch app. Application output: \n%1").arg(m_process->output()).toAscii()); + } + + m_connection->connectToHost("127.0.0.1", 3777); + QVERIFY(m_connection->waitForConnected()); + + QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(enabled()))); +} + +void tst_QDebugMessageService::cleanup() +{ + if (QTest::currentTestFailed()) + qDebug() << m_process->output(); + if (m_process) + delete m_process; + + if (m_client) + delete m_client; + + if (m_connection) + delete m_connection; + + m_process = 0; + m_client = 0; + m_connection = 0; +} + +void tst_QDebugMessageService::retrieveDebugOutput() +{ + if (m_client->debugMessage.isEmpty()) + QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(debugOutput()))); +} + +QTEST_MAIN(tst_QDebugMessageService) + +#include "tst_qdebugmessageservice.moc" -- 1.7.2.5