Skip to content

Commit

Permalink
DebugMessageService: Also pass Debug Context Info
Browse files Browse the repository at this point in the history
Use QMessageHandler which provides context information such
as line, file and function for the debug output.

Change-Id: I475faf4a1363d8419dec910b8a23cc44666c1908
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
  • Loading branch information
Aurindam Jana authored and Qt by Nokia committed Feb 21, 2012
1 parent 784d867 commit a73ad90
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
23 changes: 14 additions & 9 deletions src/declarative/debugger/qdebugmessageservice.cpp
Expand Up @@ -46,9 +46,10 @@ QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QDebugMessageService, declarativeDebugMessageService)

void DebugMessageHandler(QtMsgType type, const char *buf)
void DebugMessageHandler(QtMsgType type, const QMessageLogContext &ctxt,
const char *buf)
{
QDebugMessageService::instance()->sendDebugMessage(type, buf);
QDebugMessageService::instance()->sendDebugMessage(type, ctxt, buf);
}

class QDebugMessageServicePrivate : public QDeclarativeDebugServicePrivate
Expand All @@ -60,7 +61,7 @@ class QDebugMessageServicePrivate : public QDeclarativeDebugServicePrivate
{
}

QtMsgHandler oldMsgHandler;
QMessageHandler oldMsgHandler;
QDeclarativeDebugService::State prevState;
};

Expand All @@ -72,7 +73,7 @@ QDebugMessageService::QDebugMessageService(QObject *parent) :

registerService();
if (state() == Enabled) {
d->oldMsgHandler = qInstallMsgHandler(DebugMessageHandler);
d->oldMsgHandler = qInstallMessageHandler(DebugMessageHandler);
d->prevState = Enabled;
}
}
Expand All @@ -82,7 +83,9 @@ QDebugMessageService *QDebugMessageService::instance()
return declarativeDebugMessageService();
}

void QDebugMessageService::sendDebugMessage(QtMsgType type, const char *buf)
void QDebugMessageService::sendDebugMessage(QtMsgType type,
const QMessageLogContext &ctxt,
const char *buf)
{
Q_D(QDebugMessageService);

Expand All @@ -92,24 +95,26 @@ void QDebugMessageService::sendDebugMessage(QtMsgType type, const char *buf)
QByteArray message;
QDataStream ws(&message, QIODevice::WriteOnly);
ws << QByteArray("MESSAGE") << type << QString::fromLocal8Bit(buf).toUtf8();
ws << ctxt.version << QString::fromLatin1(ctxt.file).toUtf8();
ws << ctxt.line << QString::fromLatin1(ctxt.function).toUtf8();

sendMessage(message);
if (d->oldMsgHandler)
(*d->oldMsgHandler)(type, buf);
(*d->oldMsgHandler)(type, ctxt, buf);
}

void QDebugMessageService::stateChanged(State state)
{
Q_D(QDebugMessageService);

if (state != Enabled && d->prevState == Enabled) {
QtMsgHandler handler = qInstallMsgHandler(d->oldMsgHandler);
QMessageHandler handler = qInstallMessageHandler(d->oldMsgHandler);
// has our handler been overwritten in between?
if (handler != DebugMessageHandler)
qInstallMsgHandler(handler);
qInstallMessageHandler(handler);

} else if (state == Enabled && d->prevState != Enabled) {
d->oldMsgHandler = qInstallMsgHandler(DebugMessageHandler);
d->oldMsgHandler = qInstallMessageHandler(DebugMessageHandler);

}

Expand Down
5 changes: 4 additions & 1 deletion src/declarative/debugger/qdebugmessageservice_p.h
Expand Up @@ -55,6 +55,8 @@

#include "qdeclarativedebugservice_p.h"

#include <QtCore/qlogging.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE
Expand All @@ -71,7 +73,8 @@ class QDebugMessageService : public QDeclarativeDebugService

static QDebugMessageService *instance();

void sendDebugMessage(QtMsgType type, const char *buf);
void sendDebugMessage(QtMsgType type, const QMessageLogContext &ctxt,
const char *buf);

protected:
void stateChanged(State);
Expand Down
Expand Up @@ -81,13 +81,19 @@ struct LogEntry {

QtMsgType type;
QString message;
int version;
int line;
QString file;
QString function;

QString toString() const { return QString::number(type) + ": " + message; }
};

bool operator==(const LogEntry &t1, const LogEntry &t2)
{
return t1.type == t2.type && t1.message == t2.message;
return t1.type == t2.type && t1.message == t2.message
&& t1.line == t2.line && t1.file == t2.file
&& t1.function == t2.function;
}

class QDeclarativeDebugMsgClient : public QDeclarativeDebugClient
Expand Down Expand Up @@ -127,13 +133,22 @@ void QDeclarativeDebugMsgClient::messageReceived(const QByteArray &data)
if (command == "MESSAGE") {
int type;
QByteArray message;
ds >> type >> message;
QByteArray file;
QByteArray function;
int line;
int version;
ds >> type >> message >> version >> file >> line >> function;
QVERIFY(ds.atEnd());

QVERIFY(type >= QtDebugMsg);
QVERIFY(type <= QtFatalMsg);

logBuffer << LogEntry((QtMsgType)type, QString::fromUtf8(message));
LogEntry entry((QtMsgType)type, QString::fromUtf8(message));
entry.line = line;
entry.version = version;
entry.file = QString::fromLatin1(file);
entry.function = QString::fromLatin1(function);
logBuffer << entry;
emit debugOutput();
} else {
QFAIL("Unknown message");
Expand Down Expand Up @@ -210,8 +225,19 @@ void tst_QDebugMessageService::retrieveDebugOutput()

QVERIFY(m_client->logBuffer.size() >= 2);

QVERIFY(m_client->logBuffer.contains(LogEntry(QtDebugMsg, QLatin1String("console.log"))));
QVERIFY(m_client->logBuffer.contains(LogEntry(QtDebugMsg, QLatin1String("console.count: 1"))));
const QString path =
QUrl::fromLocalFile(QDeclarativeDataTest::instance()->testFile(QMLFILE)).toString();
LogEntry entry1(QtDebugMsg, QLatin1String("console.log"));
entry1.line = 48;
entry1.file = path;
entry1.function = QLatin1String("onCompleted");
LogEntry entry2(QtDebugMsg, QLatin1String("console.count: 1"));
entry2.line = 49;
entry2.file = path;
entry2.function = QLatin1String("onCompleted");

QVERIFY(m_client->logBuffer.contains(entry1));
QVERIFY(m_client->logBuffer.contains(entry2));
}

QTEST_MAIN(tst_QDebugMessageService)
Expand Down

0 comments on commit a73ad90

Please sign in to comment.