Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdebugmessageservice.cpp
Go to the documentation of this file.
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
5
6#include <private/qqmldebugconnector_p.h>
7#include <private/qversionedpacket_p.h>
8
10
11using QQmlDebugPacket = QVersionedPacket<QQmlDebugConnector>;
12
13void DebugMessageHandler(QtMsgType type, const QMessageLogContext &ctxt,
14 const QString &buf)
15{
16 if (QDebugMessageServiceImpl *service = QQmlDebugConnector::service<QDebugMessageServiceImpl>())
17 service->sendDebugMessage(type, ctxt, buf);
18}
19
20QDebugMessageServiceImpl::QDebugMessageServiceImpl(QObject *parent) :
21 QDebugMessageService(2, parent), oldMsgHandler(nullptr),
22 prevState(QQmlDebugService::NotConnected)
23{
24 // don't execute stateChanged() in parallel
25 QMutexLocker lock(&initMutex);
26 timer.start();
27 if (state() == Enabled) {
28 oldMsgHandler = qInstallMessageHandler(DebugMessageHandler);
29 prevState = Enabled;
30 }
31}
32
33QDebugMessageServiceImpl::~QDebugMessageServiceImpl()
34{
35 if (oldMsgHandler)
36 restoreOldMessageHandler();
37}
38
39void QDebugMessageServiceImpl::sendDebugMessage(QtMsgType type,
40 const QMessageLogContext &ctxt,
41 const QString &buf)
42{
43 //We do not want to alter the message handling mechanism
44 //We just eavesdrop and forward the messages to a port
45 //only if a client is connected to it.
46 QQmlDebugPacket ws;
47 ws << QByteArray("MESSAGE") << int(type) << buf.toUtf8();
48 ws << QByteArray(ctxt.file) << ctxt.line << QByteArray(ctxt.function);
49 ws << QByteArray(ctxt.category) << timer.nsecsElapsed();
50
51 emit messageToClient(name(), ws.data());
52 if (oldMsgHandler)
53 (*oldMsgHandler)(type, ctxt, buf);
54}
55
56void QDebugMessageServiceImpl::stateChanged(State state)
57{
58 QMutexLocker lock(&initMutex);
59
60 if (state != Enabled && prevState == Enabled)
61 restoreOldMessageHandler();
62 else if (state == Enabled && prevState != Enabled)
63 oldMsgHandler = qInstallMessageHandler(DebugMessageHandler);
64
65 prevState = state;
66}
67
68void QDebugMessageServiceImpl::restoreOldMessageHandler()
69{
70 QtMessageHandler handler = qInstallMessageHandler(oldMsgHandler);
71
72 // has our handler been overwritten in between?
73 // In that case, leave the other one alone and try again on destruction.
74 if (handler == DebugMessageHandler)
75 oldMsgHandler = nullptr;
76 else
77 qInstallMessageHandler(handler);
78}
79
80void QDebugMessageServiceImpl::synchronizeTime(const QElapsedTimer &otherTimer)
81{
82 QMutexLocker lock(&initMutex);
83 timer = otherTimer;
84}
85
86QT_END_NAMESPACE
87
88#include "moc_qdebugmessageservice.cpp"
void DebugMessageHandler(QtMsgType type, const QMessageLogContext &ctxt, const QString &buf)