1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtTest/private/qsignaldumper_p.h>
41
42#include <QtCore/qlist.h>
43#include <QtCore/qmetaobject.h>
44#include <QtCore/qmetatype.h>
45#include <QtCore/qobject.h>
46#include <QtCore/qvariant.h>
47
48#include <QtTest/private/qtestlog_p.h>
49
50#include <QtCore/private/qmetaobject_p.h>
51
52QT_BEGIN_NAMESPACE
53
54namespace QTest
55{
56
57inline static void qPrintMessage(const QByteArray &ba)
58{
59 QTestLog::info(msg: ba.constData(), file: nullptr, line: 0);
60}
61
62Q_GLOBAL_STATIC(QList<QByteArray>, ignoreClasses)
63static int iLevel = 0;
64static int ignoreLevel = 0;
65enum { IndentSpacesCount = 4 };
66
67static void qSignalDumperCallback(QObject *caller, int signal_index, void **argv)
68{
69 Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
70 const QMetaObject *mo = caller->metaObject();
71 Q_ASSERT(mo);
72 QMetaMethod member = QMetaObjectPrivate::signal(m: mo, signal_index);
73 Q_ASSERT(member.isValid());
74
75 if (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(t: mo->className())) {
76 ++QTest::ignoreLevel;
77 return;
78 }
79
80 QByteArray str;
81 str.fill(c: ' ', size: QTest::iLevel++ * QTest::IndentSpacesCount);
82 str += "Signal: ";
83 str += mo->className();
84 str += '(';
85
86 QString objname = caller->objectName();
87 str += objname.toLocal8Bit();
88 if (!objname.isEmpty())
89 str += ' ';
90 str += QByteArray::number(quintptr(caller), base: 16).rightJustified(width: 8, fill: '0');
91
92 str += ") ";
93 str += member.name();
94 str += " (";
95
96 QList<QByteArray> args = member.parameterTypes();
97 for (int i = 0; i < args.count(); ++i) {
98 const QByteArray &arg = args.at(i);
99 int typeId = QMetaType::type(typeName: args.at(i).constData());
100 if (arg.endsWith(c: '*') || arg.endsWith(c: '&')) {
101 str += '(';
102 str += arg;
103 str += ')';
104 if (arg.endsWith(c: '&'))
105 str += '@';
106
107 quintptr addr = quintptr(*reinterpret_cast<void **>(argv[i + 1]));
108 str.append(a: QByteArray::number(addr, base: 16).rightJustified(width: 8, fill: '0'));
109 } else if (typeId != QMetaType::UnknownType) {
110 Q_ASSERT(typeId != QMetaType::Void); // void parameter => metaobject is corrupt
111 str.append(a: arg)
112 .append(c: '(')
113 .append(a: QVariant(typeId, argv[i + 1]).toString().toLocal8Bit())
114 .append(c: ')');
115 }
116 str.append(s: ", ");
117 }
118 if (str.endsWith(c: ", "))
119 str.chop(n: 2);
120 str.append(c: ')');
121 qPrintMessage(ba: str);
122}
123
124static void qSignalDumperCallbackSlot(QObject *caller, int method_index, void **argv)
125{
126 Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
127 const QMetaObject *mo = caller->metaObject();
128 Q_ASSERT(mo);
129 QMetaMethod member = mo->method(index: method_index);
130 if (!member.isValid())
131 return;
132
133 if (QTest::ignoreLevel ||
134 (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(t: mo->className())))
135 return;
136
137 QByteArray str;
138 str.fill(c: ' ', size: QTest::iLevel * QTest::IndentSpacesCount);
139 str += "Slot: ";
140 str += mo->className();
141 str += '(';
142
143 QString objname = caller->objectName();
144 str += objname.toLocal8Bit();
145 if (!objname.isEmpty())
146 str += ' ';
147 str += QByteArray::number(quintptr(caller), base: 16).rightJustified(width: 8, fill: '0');
148
149 str += ") ";
150 str += member.methodSignature();
151 qPrintMessage(ba: str);
152}
153
154static void qSignalDumperCallbackEndSignal(QObject *caller, int /*signal_index*/)
155{
156 Q_ASSERT(caller); Q_ASSERT(caller->metaObject());
157 if (QTest::ignoreClasses()
158 && QTest::ignoreClasses()->contains(t: caller->metaObject()->className())) {
159 --QTest::ignoreLevel;
160 Q_ASSERT(QTest::ignoreLevel >= 0);
161 return;
162 }
163 --QTest::iLevel;
164 Q_ASSERT(QTest::iLevel >= 0);
165}
166
167}
168
169void QSignalDumper::startDump()
170{
171 static QSignalSpyCallbackSet set = { .signal_begin_callback: QTest::qSignalDumperCallback,
172 .slot_begin_callback: QTest::qSignalDumperCallbackSlot, .signal_end_callback: QTest::qSignalDumperCallbackEndSignal, .slot_end_callback: nullptr };
173 qt_register_signal_spy_callbacks(callback_set: &set);
174}
175
176void QSignalDumper::endDump()
177{
178 qt_register_signal_spy_callbacks(callback_set: nullptr);
179}
180
181void QSignalDumper::ignoreClass(const QByteArray &klass)
182{
183 if (QTest::ignoreClasses())
184 QTest::ignoreClasses()->append(t: klass);
185}
186
187void QSignalDumper::clearIgnoredClasses()
188{
189 if (QTest::ignoreClasses())
190 QTest::ignoreClasses()->clear();
191}
192
193QT_END_NAMESPACE
194

source code of qtbase/src/testlib/qsignaldumper.cpp