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