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
qqmlinfo.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
5#include "qqmlinfo.h"
6
7#include "qqmldata_p.h"
9#include "qqmlengine_p.h"
11
12#include <QCoreApplication>
13
15
16/*!
17 \class QQmlInfo
18 \inmodule QtQml
19 \brief The QQmlInfo class allows logging of QML-related messages.
20
21 QQmlInfo is an opaque handle for QML-related diagnostic messages. You can
22 use the \c{<<} operator to add content to the message. When the QQmlInfo
23 object is destroyed, it prints the resulting message along with information
24 on the context.
25
26 \sa qmlDebug, qmlInfo, qmlWarning
27*/
28
29/*!
30 \fn QQmlInfo qmlDebug(const QObject *object)
31 \relates QQmlInfo
32 \since 5.9
33
34 Prints debug messages that include the file and line number for the
35 specified QML \a object.
36
37//! [qqmlinfo-desc]
38 When QML types produce logging messages, it improves traceability
39 if they include the QML file and line number on which the
40 particular instance was instantiated.
41
42 To include the file and line number, an object must be passed. If
43 the file and line number is not available for that instance
44 (either it was not instantiated by the QML engine or location
45 information is disabled), "unknown location" will be used instead.
46//! [qqmlinfo-desc]
47
48 For example,
49
50 \code
51 qmlDebug(object) << "Internal state: 42";
52 \endcode
53
54 prints
55
56 \badcode
57 QML MyCustomType (unknown location): Internal state: 42
58 \endcode
59
60 \sa qmlInfo, qmlWarning
61*/
62
63/*!
64 \fn QQmlInfo qmlInfo(const QObject *object)
65 \relates QQmlInfo
66
67 Prints informational messages that include the file and line number for the
68 specified QML \a object.
69
70 \include qqmlinfo.cpp qqmlinfo-desc
71
72 For example,
73
74 \code
75 qmlInfo(object) << tr("component property is a write-once property");
76 \endcode
77
78 prints
79
80 \badcode
81 QML MyCustomType (unknown location): component property is a write-once property
82 \endcode
83
84 \note In versions prior to Qt 5.9, qmlInfo reported messages using a warning
85 QtMsgType. For Qt 5.9 and above, qmlInfo uses an info QtMsgType. To send
86 warnings, use qmlWarning.
87
88 \sa qmlDebug, qmlWarning
89*/
90
91/*!
92 \fn QQmlInfo qmlWarning(const QObject *object)
93 \relates QQmlInfo
94 \since 5.9
95
96 Prints warning messages that include the file and line number for the
97 specified QML \a object.
98
99 \include qqmlinfo.cpp qqmlinfo-desc
100
101 For example,
102
103 \code
104 qmlInfo(object) << tr("property cannot be set to 0");
105 \endcode
106
107 prints
108
109 \badcode
110 QML MyCustomType (unknown location): property cannot be set to 0
111 \endcode
112
113 \sa qmlDebug, qmlInfo
114*/
115
116/*!
117 \fn QQmlInfo qmlDebug(const QObject *object, const QQmlError &error)
118 \internal
119*/
120
121/*!
122 \fn QQmlInfo qmlDebug(const QObject *object, const QList<QQmlError> &errors)
123 \internal
124*/
125
126/*!
127 \fn QQmlInfo qmlInfo(const QObject *object, const QQmlError &error)
128 \internal
129*/
130
131/*!
132 \fn QQmlInfo qmlInfo(const QObject *object, const QList<QQmlError> &errors)
133 \internal
134*/
135
136/*!
137 \fn QQmlInfo qmlWarning(const QObject *object, const QQmlError &error)
138 \internal
139*/
140
141/*!
142 \fn QQmlInfo qmlWarning(const QObject *object, const QList<QQmlError> &errors)
143 \internal
144*/
145
147{
148public:
149 QQmlInfoPrivate(QtMsgType type)
150 : ref (1)
151 , msgType(type)
152 , object(nullptr)
153 {}
154
155 int ref;
160};
161
162QQmlInfo::QQmlInfo(QQmlInfoPrivate *p)
163: QDebug(&p->buffer), d(p)
164{
165 nospace();
166}
167
168QQmlInfo::QQmlInfo(const QQmlInfo &other)
169: QDebug(other), d(other.d)
170{
171 d->ref++;
172}
173
174QQmlInfo::~QQmlInfo()
175{
176 if (0 == --d->ref) {
177 QList<QQmlError> errors = d->errors;
178
179 QQmlEngine *engine = nullptr;
180
181 if (!d->buffer.isEmpty()) {
182 QQmlError error;
183 error.setMessageType(d->msgType);
184
185 QObject *object = const_cast<QObject *>(d->object);
186
187 if (object) {
188 // Some objects (e.g. like attached objects created in C++) won't have an associated engine,
189 // but we can still try to look for a parent object that does.
190 QObject *objectWithEngine = object;
191 while (objectWithEngine) {
192 engine = qmlEngine(objectWithEngine);
193 if (engine)
194 break;
195 objectWithEngine = objectWithEngine->parent();
196 }
197
198 if (!objectWithEngine || objectWithEngine == object) {
199 d->buffer.prepend(QLatin1String("QML ") + QQmlMetaType::prettyTypeName(object) + QLatin1String(": "));
200 } else {
201 d->buffer.prepend(QLatin1String("QML ") + QQmlMetaType::prettyTypeName(objectWithEngine)
202 + QLatin1String(" (parent or ancestor of ") + QQmlMetaType::prettyTypeName(object) + QLatin1String("): "));
203 }
204
205 QQmlData *ddata = QQmlData::get(objectWithEngine ? objectWithEngine : object, false);
206 if (ddata && ddata->outerContext) {
207 error.setUrl(ddata->outerContext->url());
208 error.setLine(qmlConvertSourceCoordinate<quint16, int>(ddata->lineNumber));
209 error.setColumn(qmlConvertSourceCoordinate<quint16, int>(ddata->columnNumber));
210 }
211 }
212
213 error.setDescription(d->buffer);
214
215 errors.prepend(error);
216 }
217
218 QQmlEnginePrivate::warning(engine, errors);
219
220 delete d;
221 }
222}
223
224#define MESSAGE_FUNCS(FuncName, MessageLevel)
225 QQmlInfo FuncName(const QObject *me)
226 {
227 QQmlInfoPrivate *d = new QQmlInfoPrivate(MessageLevel);
228 d->object = me;
229 return QQmlInfo(d);
230 }
231 QQmlInfo FuncName(const QObject *me, const QQmlError &error)
232 {
233 QQmlInfoPrivate *d = new QQmlInfoPrivate(MessageLevel);
234 d->object = me;
235 d->errors << error;
236 return QQmlInfo(d);
237 }
238 QQmlInfo FuncName(const QObject *me, const QList<QQmlError> &errors)
239 {
240 QQmlInfoPrivate *d = new QQmlInfoPrivate(MessageLevel);
241 d->object = me;
242 d->errors = errors;
243 return QQmlInfo(d);
244 }
245
246MESSAGE_FUNCS(qmlDebug, QtMsgType::QtDebugMsg)
247MESSAGE_FUNCS(qmlInfo, QtMsgType::QtInfoMsg)
248MESSAGE_FUNCS(qmlWarning, QtMsgType::QtWarningMsg)
249
250QT_END_NAMESPACE
QList< QQmlError > errors
Definition qqmlinfo.cpp:159
QQmlInfoPrivate(QtMsgType type)
Definition qqmlinfo.cpp:149
const QObject * object
Definition qqmlinfo.cpp:157
QtMsgType msgType
Definition qqmlinfo.cpp:156
Combined button and popup list for selecting options.
#define MESSAGE_FUNCS(FuncName, MessageLevel)
Definition qqmlinfo.cpp:224