1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QDEBUG_H
42#define QDEBUG_H
43
44#include <QtCore/qalgorithms.h>
45#include <QtCore/qhash.h>
46#include <QtCore/qlist.h>
47#include <QtCore/qmap.h>
48#include <QtCore/qpair.h>
49#include <QtCore/qtextstream.h>
50#include <QtCore/qstring.h>
51#include <QtCore/qvector.h>
52#include <QtCore/qset.h>
53#include <QtCore/qcontiguouscache.h>
54#include <QtCore/qsharedpointer.h>
55
56// all these have already been included by various headers above, but don't rely on indirect includes:
57#include <vector>
58#include <list>
59#include <map>
60#include <utility>
61
62QT_BEGIN_NAMESPACE
63
64
65class Q_CORE_EXPORT QDebug
66{
67 friend class QMessageLogger;
68 friend class QDebugStateSaverPrivate;
69 struct Stream {
70 enum { DefaultVerbosity = 2, VerbosityShift = 29, VerbosityMask = 0x7 };
71
72 Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg),
73 space(true), message_output(false), flags(DefaultVerbosity << VerbosityShift) {}
74 Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg),
75 space(true), message_output(false), flags(DefaultVerbosity << VerbosityShift) {}
76 Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t),
77 space(true), message_output(true), flags(DefaultVerbosity << VerbosityShift) {}
78 QTextStream ts;
79 QString buffer;
80 int ref;
81 QtMsgType type;
82 bool space;
83 bool message_output;
84 QMessageLogContext context;
85
86 enum FormatFlag { // Note: Bits 29..31 are reserved for the verbose level introduced in 5.6.
87 NoQuotes = 0x1
88 };
89
90 // ### Qt 6: unify with space, introduce own version member
91 bool testFlag(FormatFlag flag) const { return (context.version > 1) ? (flags & flag) : false; }
92 void setFlag(FormatFlag flag) { if (context.version > 1) { flags |= flag; } }
93 void unsetFlag(FormatFlag flag) { if (context.version > 1) { flags &= ~flag; } }
94 int verbosity() const
95 { return context.version > 1 ? (flags >> VerbosityShift) & VerbosityMask : int(Stream::DefaultVerbosity); }
96 void setVerbosity(int v)
97 {
98 if (context.version > 1) {
99 flags &= ~(VerbosityMask << VerbosityShift);
100 flags |= (v & VerbosityMask) << VerbosityShift;
101 }
102 }
103 // added in 5.4
104 int flags;
105 } *stream;
106
107 enum Latin1Content { ContainsBinary = 0, ContainsLatin1 };
108
109 void putUcs4(uint ucs4);
110 void putString(const QChar *begin, size_t length);
111 void putByteArray(const char *begin, size_t length, Latin1Content content);
112public:
113 inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
114 inline QDebug(QString *string) : stream(new Stream(string)) {}
115 inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
116 inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
117 inline QDebug &operator=(const QDebug &other);
118 ~QDebug();
119 inline void swap(QDebug &other) Q_DECL_NOTHROW { qSwap(stream, other.stream); }
120
121 QDebug &resetFormat();
122
123 inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; }
124 inline QDebug &nospace() { stream->space = false; return *this; }
125 inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
126 int verbosity() const { return stream->verbosity(); }
127 void setVerbosity(int verbosityLevel) { stream->setVerbosity(verbosityLevel); }
128
129 bool autoInsertSpaces() const { return stream->space; }
130 void setAutoInsertSpaces(bool b) { stream->space = b; }
131
132 inline QDebug &quote() { stream->unsetFlag(Stream::NoQuotes); return *this; }
133 inline QDebug &noquote() { stream->setFlag(Stream::NoQuotes); return *this; }
134 inline QDebug &maybeQuote(char c = '"') { if (!(stream->testFlag(Stream::NoQuotes))) stream->ts << c; return *this; }
135
136 inline QDebug &operator<<(QChar t) { putUcs4(t.unicode()); return maybeSpace(); }
137 inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
138 inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
139 inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
140 inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
141#ifdef Q_COMPILER_UNICODE_STRINGS
142 inline QDebug &operator<<(char16_t t) { return *this << QChar(ushort(t)); }
143 inline QDebug &operator<<(char32_t t) { putUcs4(t); return maybeSpace(); }
144#endif
145 inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
146 inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
147 inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
148 inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
149 inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); }
150 inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); }
151 inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
152 inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
153 inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }
154#if QT_STRINGVIEW_LEVEL < 2
155 inline QDebug &operator<<(const QString & t) { putString(t.constData(), uint(t.length())); return maybeSpace(); }
156 inline QDebug &operator<<(const QStringRef & t) { putString(t.constData(), uint(t.length())); return maybeSpace(); }
157#endif
158 inline QDebug &operator<<(QStringView s) { putString(s.data(), size_t(s.size())); return maybeSpace(); }
159 inline QDebug &operator<<(QLatin1String t) { putByteArray(t.latin1(), t.size(), ContainsLatin1); return maybeSpace(); }
160 inline QDebug &operator<<(const QByteArray & t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
161 inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
162#ifdef Q_COMPILER_NULLPTR
163 inline QDebug &operator<<(std::nullptr_t) { stream->ts << "(nullptr)"; return maybeSpace(); }
164#endif
165 inline QDebug &operator<<(QTextStreamFunction f) {
166 stream->ts << f;
167 return *this;
168 }
169
170 inline QDebug &operator<<(QTextStreamManipulator m)
171 { stream->ts << m; return *this; }
172};
173
174Q_DECLARE_SHARED(QDebug)
175
176class QDebugStateSaverPrivate;
177class Q_CORE_EXPORT QDebugStateSaver
178{
179public:
180 QDebugStateSaver(QDebug &dbg);
181 ~QDebugStateSaver();
182private:
183 Q_DISABLE_COPY(QDebugStateSaver)
184 QScopedPointer<QDebugStateSaverPrivate> d;
185};
186
187class QNoDebug
188{
189public:
190 inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
191 inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
192 inline QNoDebug &space() { return *this; }
193 inline QNoDebug &nospace() { return *this; }
194 inline QNoDebug &maybeSpace() { return *this; }
195 inline QNoDebug &quote() { return *this; }
196 inline QNoDebug &noquote() { return *this; }
197 inline QNoDebug &maybeQuote(const char = '"') { return *this; }
198
199 template<typename T>
200 inline QNoDebug &operator<<(const T &) { return *this; }
201};
202
203inline QDebug &QDebug::operator=(const QDebug &other)
204{
205 if (this != &other) {
206 QDebug copy(other);
207 qSwap(stream, copy.stream);
208 }
209 return *this;
210}
211
212namespace QtPrivate {
213
214template <typename SequentialContainer>
215inline QDebug printSequentialContainer(QDebug debug, const char *which, const SequentialContainer &c)
216{
217 const bool oldSetting = debug.autoInsertSpaces();
218 debug.nospace() << which << '(';
219 typename SequentialContainer::const_iterator it = c.begin(), end = c.end();
220 if (it != end) {
221 debug << *it;
222 ++it;
223 }
224 while (it != end) {
225 debug << ", " << *it;
226 ++it;
227 }
228 debug << ')';
229 debug.setAutoInsertSpaces(oldSetting);
230 return debug.maybeSpace();
231}
232
233} // namespace QtPrivate
234
235template <class T>
236inline QDebug operator<<(QDebug debug, const QList<T> &list)
237{
238 return QtPrivate::printSequentialContainer(debug, "" /*for historical reasons*/, list);
239}
240
241template <typename T>
242inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
243{
244 return QtPrivate::printSequentialContainer(debug, "QVector", vec);
245}
246
247template <typename T, typename Alloc>
248inline QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
249{
250 return QtPrivate::printSequentialContainer(debug, "std::vector", vec);
251}
252
253template <typename T, typename Alloc>
254inline QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec)
255{
256 return QtPrivate::printSequentialContainer(debug, "std::list", vec);
257}
258
259template <typename Key, typename T, typename Compare, typename Alloc>
260inline QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
261{
262 return QtPrivate::printSequentialContainer(debug, "std::map", map); // yes, sequential: *it is std::pair
263}
264
265template <typename Key, typename T, typename Compare, typename Alloc>
266inline QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
267{
268 return QtPrivate::printSequentialContainer(debug, "std::multimap", map); // yes, sequential: *it is std::pair
269}
270
271template <class Key, class T>
272inline QDebug operator<<(QDebug debug, const QMap<Key, T> &map)
273{
274 const bool oldSetting = debug.autoInsertSpaces();
275 debug.nospace() << "QMap(";
276 for (typename QMap<Key, T>::const_iterator it = map.constBegin();
277 it != map.constEnd(); ++it) {
278 debug << '(' << it.key() << ", " << it.value() << ')';
279 }
280 debug << ')';
281 debug.setAutoInsertSpaces(oldSetting);
282 return debug.maybeSpace();
283}
284
285template <class Key, class T>
286inline QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
287{
288 const bool oldSetting = debug.autoInsertSpaces();
289 debug.nospace() << "QHash(";
290 for (typename QHash<Key, T>::const_iterator it = hash.constBegin();
291 it != hash.constEnd(); ++it)
292 debug << '(' << it.key() << ", " << it.value() << ')';
293 debug << ')';
294 debug.setAutoInsertSpaces(oldSetting);
295 return debug.maybeSpace();
296}
297
298template <class T1, class T2>
299inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
300{
301 const bool oldSetting = debug.autoInsertSpaces();
302 debug.nospace() << "QPair(" << pair.first << ',' << pair.second << ')';
303 debug.setAutoInsertSpaces(oldSetting);
304 return debug.maybeSpace();
305}
306
307template <class T1, class T2>
308inline QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair)
309{
310 const bool oldSetting = debug.autoInsertSpaces();
311 debug.nospace() << "std::pair(" << pair.first << ',' << pair.second << ')';
312 debug.setAutoInsertSpaces(oldSetting);
313 return debug.maybeSpace();
314}
315
316template <typename T>
317inline QDebug operator<<(QDebug debug, const QSet<T> &set)
318{
319 return QtPrivate::printSequentialContainer(debug, "QSet", set);
320}
321
322template <class T>
323inline QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
324{
325 const bool oldSetting = debug.autoInsertSpaces();
326 debug.nospace() << "QContiguousCache(";
327 for (int i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
328 debug << cache[i];
329 if (i != cache.lastIndex())
330 debug << ", ";
331 }
332 debug << ')';
333 debug.setAutoInsertSpaces(oldSetting);
334 return debug.maybeSpace();
335}
336
337template <class T>
338inline QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
339{
340 QDebugStateSaver saver(debug);
341 debug.nospace() << "QSharedPointer(" << ptr.data() << ")";
342 return debug;
343}
344
345Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, int value);
346
347template <typename Int>
348void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
349{
350 const QDebugStateSaver saver(debug);
351 debug.resetFormat();
352 debug.nospace() << "QFlags(" << hex << showbase;
353 bool needSeparator = false;
354 for (uint i = 0; i < sizeofT * 8; ++i) {
355 if (value & (Int(1) << i)) {
356 if (needSeparator)
357 debug << '|';
358 else
359 needSeparator = true;
360 debug << (Int(1) << i);
361 }
362 }
363 debug << ')';
364}
365
366#if !defined(QT_NO_QOBJECT) && !defined(Q_QDOC)
367Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, int value, const QMetaObject *meta, const char *name);
368Q_CORE_EXPORT QDebug qt_QMetaEnum_flagDebugOperator(QDebug &dbg, quint64 value, const QMetaObject *meta, const char *name);
369
370template<typename T>
371typename std::enable_if<QtPrivate::IsQEnumHelper<T>::Value, QDebug>::type
372operator<<(QDebug dbg, T value)
373{
374 const QMetaObject *obj = qt_getEnumMetaObject(value);
375 const char *name = qt_getEnumName(value);
376 return qt_QMetaEnum_debugOperator(dbg, typename QFlags<T>::Int(value), obj, name);
377}
378
379template<typename T,
380 typename A = typename std::enable_if<std::is_enum<T>::value, void>::type,
381 typename B = typename std::enable_if<sizeof(T) <= sizeof(int), void>::type,
382 typename C = typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value, void>::type,
383 typename D = typename std::enable_if<QtPrivate::IsQEnumHelper<QFlags<T>>::Value, void>::type>
384inline QDebug operator<<(QDebug dbg, T value)
385{
386 typedef QFlags<T> FlagsT;
387 const QMetaObject *obj = qt_getEnumMetaObject(FlagsT());
388 const char *name = qt_getEnumName(FlagsT());
389 return qt_QMetaEnum_debugOperator(dbg, typename FlagsT::Int(value), obj, name);
390}
391
392template <class T>
393inline typename std::enable_if<
394 QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
395 QDebug>::type
396qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
397{
398 const QMetaObject *obj = qt_getEnumMetaObject(T());
399 const char *name = qt_getEnumName(T());
400 return qt_QMetaEnum_flagDebugOperator(debug, quint64(flags), obj, name);
401}
402
403template <class T>
404inline typename std::enable_if<
405 !QtPrivate::IsQEnumHelper<T>::Value && !QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
406 QDebug>::type
407qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
408#else // !QT_NO_QOBJECT && !Q_QDOC
409template <class T>
410inline QDebug qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
411#endif
412{
413 qt_QMetaEnum_flagDebugOperator(debug, sizeof(T), typename QFlags<T>::Int(flags));
414 return debug;
415}
416
417template<typename T>
418inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
419{
420 // We have to use an indirection otherwise specialisation of some other overload of the
421 // operator<< the compiler would try to instantiate QFlags<T> for the std::enable_if
422 return qt_QMetaEnum_flagDebugOperator_helper(debug, flags);
423}
424
425#ifdef Q_OS_MAC
426
427// We provide QDebug stream operators for commonly used Core Foundation
428// and Core Graphics types, as well as NSObject. Additional CF/CG types
429// may be added by the user, using Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE.
430
431#define QT_FOR_EACH_CORE_FOUNDATION_TYPE(F) \
432 F(CFArray) \
433 F(CFURL) \
434 F(CFData) \
435 F(CFNumber) \
436 F(CFDictionary) \
437 F(CFLocale) \
438 F(CFDate) \
439 F(CFBoolean) \
440 F(CFTimeZone) \
441
442#define QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(F) \
443 F(CFError) \
444 F(CFBundle) \
445
446#define QT_FOR_EACH_CORE_GRAPHICS_TYPE(F) \
447 F(CGPath) \
448
449#define QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(F) \
450 F(CGColorSpace) \
451 F(CGImage) \
452 F(CGFont) \
453 F(CGColor) \
454
455#define QT_FORWARD_DECLARE_CF_TYPE(type) Q_FORWARD_DECLARE_CF_TYPE(type);
456#define QT_FORWARD_DECLARE_MUTABLE_CF_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CF_TYPE(type);
457#define QT_FORWARD_DECLARE_CG_TYPE(type) Q_FORWARD_DECLARE_CG_TYPE(type);
458#define QT_FORWARD_DECLARE_MUTABLE_CG_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(type);
459
460QT_END_NAMESPACE
461Q_FORWARD_DECLARE_CF_TYPE(CFString);
462Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
463QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_CF_TYPE)
464QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_MUTABLE_CF_TYPE)
465QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_CG_TYPE)
466QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_MUTABLE_CG_TYPE)
467QT_BEGIN_NAMESPACE
468
469#define QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
470 Q_CORE_EXPORT QDebug operator<<(QDebug, CFType##Ref);
471
472#define Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
473 QDebug operator<<(QDebug debug, CFType##Ref ref) \
474 { \
475 if (!ref) \
476 return debug << QT_STRINGIFY(CFType) "Ref(0x0)"; \
477 if (CFStringRef description = CFCopyDescription(ref)) { \
478 QDebugStateSaver saver(debug); \
479 debug.noquote() << description; \
480 CFRelease(description); \
481 } \
482 return debug; \
483 }
484
485// Defined in qcore_mac_objc.mm
486Q_CORE_EXPORT QDebug operator<<(QDebug, const NSObject *);
487Q_CORE_EXPORT QDebug operator<<(QDebug, CFStringRef);
488
489QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
490QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
491QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
492QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
493
494#undef QT_FORWARD_DECLARE_CF_TYPE
495#undef QT_FORWARD_DECLARE_MUTABLE_CF_TYPE
496#undef QT_FORWARD_DECLARE_CG_TYPE
497#undef QT_FORWARD_DECLARE_MUTABLE_CG_TYPE
498
499#endif // Q_OS_MAC
500
501QT_END_NAMESPACE
502
503#endif // QDEBUG_H
504