1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qdebugmessageservice.h"
5
6#include <private/qqmldebugconnector_p.h>
7#include <private/qversionedpacket_p.h>
8
9QT_BEGIN_NAMESPACE
10
11using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
12
13void DebugMessageHandler(QtMsgType type, const QMessageLogContext &ctxt,
14 const QString &buf)
15{
16 QQmlDebugConnector::service<QDebugMessageServiceImpl>()->sendDebugMessage(type, ctxt, buf);
17}
18
19QDebugMessageServiceImpl::QDebugMessageServiceImpl(QObject *parent) :
20 QDebugMessageService(2, parent), oldMsgHandler(nullptr),
21 prevState(QQmlDebugService::NotConnected)
22{
23 // don't execute stateChanged() in parallel
24 QMutexLocker lock(&initMutex);
25 timer.start();
26 if (state() == Enabled) {
27 oldMsgHandler = qInstallMessageHandler(DebugMessageHandler);
28 prevState = Enabled;
29 }
30}
31
32void QDebugMessageServiceImpl::sendDebugMessage(QtMsgType type,
33 const QMessageLogContext &ctxt,
34 const QString &buf)
35{
36 //We do not want to alter the message handling mechanism
37 //We just eavesdrop and forward the messages to a port
38 //only if a client is connected to it.
39 QQmlDebugPacket ws;
40 ws << QByteArray("MESSAGE") << int(type) << buf.toUtf8();
41 ws << QByteArray(ctxt.file) << ctxt.line << QByteArray(ctxt.function);
42 ws << QByteArray(ctxt.category) << timer.nsecsElapsed();
43
44 emit messageToClient(name: name(), message: ws.data());
45 if (oldMsgHandler)
46 (*oldMsgHandler)(type, ctxt, buf);
47}
48
49void QDebugMessageServiceImpl::stateChanged(State state)
50{
51 QMutexLocker lock(&initMutex);
52
53 if (state != Enabled && prevState == Enabled) {
54 QtMessageHandler handler = qInstallMessageHandler(oldMsgHandler);
55 // has our handler been overwritten in between?
56 if (handler != DebugMessageHandler)
57 qInstallMessageHandler(handler);
58
59 } else if (state == Enabled && prevState != Enabled) {
60 oldMsgHandler = qInstallMessageHandler(DebugMessageHandler);
61 }
62
63 prevState = state;
64}
65
66void QDebugMessageServiceImpl::synchronizeTime(const QElapsedTimer &otherTimer)
67{
68 QMutexLocker lock(&initMutex);
69 timer = otherTimer;
70}
71
72QT_END_NAMESPACE
73
74#include "moc_qdebugmessageservice.cpp"
75

source code of qtdeclarative/src/plugins/qmltooling/qmldbg_messages/qdebugmessageservice.cpp