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 { 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(DefaultVerbosity); }
96 void setVerbosity(int v)
97 {
98 if (context.version > 1) {
99 flags &= ~(uint(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 inline QDebug &verbosity(int verbosityLevel) { setVerbosity(verbosityLevel); return *this; }
127 int verbosity() const { return stream->verbosity(); }
128 void setVerbosity(int verbosityLevel) { stream->setVerbosity(verbosityLevel); }
129 enum VerbosityLevel { MinimumVerbosity = 0, DefaultVerbosity = 2, MaximumVerbosity = 7 };
130
131 bool autoInsertSpaces() const { return stream->space; }
132 void setAutoInsertSpaces(bool b) { stream->space = b; }
133
134 inline QDebug &quote() { stream->unsetFlag(Stream::NoQuotes); return *this; }
135 inline QDebug &noquote() { stream->setFlag(Stream::NoQuotes); return *this; }
136 inline QDebug &maybeQuote(char c = '"') { if (!(stream->testFlag(Stream::NoQuotes))) stream->ts << c; return *this; }
137
138 inline QDebug &operator<<(QChar t) { putUcs4(t.unicode()); return maybeSpace(); }
139 inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
140 inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
141 inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
142 inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
143#ifdef Q_COMPILER_UNICODE_STRINGS
144 inline QDebug &operator<<(char16_t t) { return *this << QChar(ushort(t)); }
145 inline QDebug &operator<<(char32_t t) { putUcs4(t); return maybeSpace(); }
146#endif
147 inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
148 inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
149 inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
150 inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
151 inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); }
152 inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); }
153 inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
154 inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
155 inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }
156#if QT_STRINGVIEW_LEVEL < 2
157 inline QDebug &operator<<(const QString & t) { putString(t.constData(), uint(t.length())); return maybeSpace(); }
158 inline QDebug &operator<<(const QStringRef & t) { putString(t.constData(), uint(t.length())); return maybeSpace(); }
159#endif
160 inline QDebug &operator<<(QStringView s) { putString(s.data(), size_t(s.size())); return maybeSpace(); }
161 inline QDebug &operator<<(QLatin1String t) { putByteArray(t.latin1(), t.size(), ContainsLatin1); return maybeSpace(); }
162 inline QDebug &operator<<(const QByteArray & t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
163 inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
164#ifdef Q_COMPILER_NULLPTR
165 inline QDebug &operator<<(std::nullptr_t) { stream->ts << "(nullptr)"; return maybeSpace(); }
166#endif
167 inline QDebug &operator<<(QTextStreamFunction f) {
168 stream->ts << f;
169 return *this;
170 }
171
172 inline QDebug &operator<<(QTextStreamManipulator m)
173 { stream->ts << m; return *this; }
174};
175
176Q_DECLARE_SHARED(QDebug)
177
178class QDebugStateSaverPrivate;
179class Q_CORE_EXPORT QDebugStateSaver
180{
181public:
182 QDebugStateSaver(QDebug &dbg);
183 ~QDebugStateSaver();
184private:
185 Q_DISABLE_COPY(QDebugStateSaver)
186 QScopedPointer<QDebugStateSaverPrivate> d;
187};
188
189class QNoDebug
190{
191public:
192 inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
193 inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
194 inline QNoDebug &space() { return *this; }
195 inline QNoDebug &nospace() { return *this; }
196 inline QNoDebug &maybeSpace() { return *this; }
197 inline QNoDebug &quote() { return *this; }
198 inline QNoDebug &noquote() { return *this; }
199 inline QNoDebug &maybeQuote(const char = '"') { return *this; }
200 inline QNoDebug &verbosity(int) { return *this; }
201
202 template<typename T>
203 inline QNoDebug &operator<<(const T &) { return *this; }
204};
205
206inline QDebug &QDebug::operator=(const QDebug &other)
207{
208 if (this != &other) {
209 QDebug copy(other);
210 qSwap(stream, copy.stream);
211 }
212 return *this;
213}
214
215namespace QtPrivate {
216
217template <typename SequentialContainer>
218inline QDebug printSequentialContainer(QDebug debug, const char *which, const SequentialContainer &c)
219{
220 const bool oldSetting = debug.autoInsertSpaces();
221 debug.nospace() << which << '(';
222 typename SequentialContainer::const_iterator it = c.begin(), end = c.end();
223 if (it != end) {
224 debug << *it;
225 ++it;
226 }
227 while (it != end) {
228 debug << ", " << *it;
229 ++it;
230 }
231 debug << ')';
232 debug.setAutoInsertSpaces(oldSetting);
233 return debug.maybeSpace();
234}
235
236} // namespace QtPrivate
237
238template <class T>
239inline QDebug operator<<(QDebug debug, const QList<T> &list)
240{
241 return QtPrivate::printSequentialContainer(debug, "" /*for historical reasons*/, list);
242}
243
244template <typename T>
245inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
246{
247 return QtPrivate::printSequentialContainer(debug, "QVector", vec);
248}
249
250template <typename T, typename Alloc>
251inline QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
252{
253 return QtPrivate::printSequentialContainer(debug, "std::vector", vec);
254}
255
256template <typename T, typename Alloc>
257inline QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec)
258{
259 return QtPrivate::printSequentialContainer(debug, "std::list", vec);
260}
261
262template <typename Key, typename T, typename Compare, typename Alloc>
263inline QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
264{
265 return QtPrivate::printSequentialContainer(debug, "std::map", map); // yes, sequential: *it is std::pair
266}
267
268template <typename Key, typename T, typename Compare, typename Alloc>
269inline QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
270{
271 return QtPrivate::printSequentialContainer(debug, "std::multimap", map); // yes, sequential: *it is std::pair
272}
273
274template <class Key, class T>
275inline QDebug operator<<(QDebug debug, const QMap<Key, T> &map)
276{
277 const bool oldSetting = debug.autoInsertSpaces();
278 debug.nospace() << "QMap(";
279 for (typename QMap<Key, T>::const_iterator it = map.constBegin();
280 it != map.constEnd(); ++it) {
281 debug << '(' << it.key() << ", " << it.value() << ')';
282 }
283 debug << ')';
284 debug.setAutoInsertSpaces(oldSetting);
285 return debug.maybeSpace();
286}
287
288template <class Key, class T>
289inline QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
290{
291 const bool oldSetting = debug.autoInsertSpaces();
292 debug.nospace() << "QHash(";
293 for (typename QHash<Key, T>::const_iterator it = hash.constBegin();
294 it != hash.constEnd(); ++it)
295 debug << '(' << it.key() << ", " << it.value() << ')';
296 debug << ')';
297 debug.setAutoInsertSpaces(oldSetting);
298 return debug.maybeSpace();
299}
300
301template <class T1, class T2>
302inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
303{
304 const bool oldSetting = debug.autoInsertSpaces();
305 debug.nospace() << "QPair(" << pair.first << ',' << pair.second << ')';
306 debug.setAutoInsertSpaces(oldSetting);
307 return debug.maybeSpace();
308}
309
310template <class T1, class T2>
311inline QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair)
312{
313 const bool oldSetting = debug.autoInsertSpaces();
314 debug.nospace() << "std::pair(" << pair.first << ',' << pair.second << ')';
315 debug.setAutoInsertSpaces(oldSetting);
316 return debug.maybeSpace();
317}
318
319template <typename T>
320inline QDebug operator<<(QDebug debug, const QSet<T> &set)
321{
322 return QtPrivate::printSequentialContainer(debug, "QSet", set);
323}
324
325template <class T>
326inline QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
327{
328 const bool oldSetting = debug.autoInsertSpaces();
329 debug.nospace() << "QContiguousCache(";
330 for (int i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
331 debug << cache[i];
332 if (i != cache.lastIndex())
333 debug << ", ";
334 }
335 debug << ')';
336 debug.setAutoInsertSpaces(oldSetting);
337 return debug.maybeSpace();
338}
339
340template <class T>
341inline QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
342{
343 QDebugStateSaver saver(debug);
344 debug.nospace() << "QSharedPointer(" << ptr.data() << ")";
345 return debug;
346}
347
348Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, int value);
349
350template <typename Int>
351void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
352{
353 const QDebugStateSaver saver(debug);
354 debug.resetFormat();
355 debug.nospace() << "QFlags(" << hex << showbase;
356 bool needSeparator = false;
357 for (uint i = 0; i < sizeofT * 8; ++i) {
358 if (value & (Int(1) << i)) {
359 if (needSeparator)
360 debug << '|';
361 else
362 needSeparator = true;
363 debug << (Int(1) << i);
364 }
365 }
366 debug << ')';
367}
368
369#if !defined(QT_NO_QOBJECT) && !defined(Q_QDOC)
370Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, int value, const QMetaObject *meta, const char *name);
371Q_CORE_EXPORT QDebug qt_QMetaEnum_flagDebugOperator(QDebug &dbg, quint64 value, const QMetaObject *meta, const char *name);
372
373template<typename T>
374typename std::enable_if<QtPrivate::IsQEnumHelper<T>::Value, QDebug>::type
375operator<<(QDebug dbg, T value)
376{
377 const QMetaObject *obj = qt_getEnumMetaObject(value);
378 const char *name = qt_getEnumName(value);
379 return qt_QMetaEnum_debugOperator(dbg, typename QFlags<T>::Int(value), obj, name);
380}
381
382template<typename T,
383 typename A = typename std::enable_if<std::is_enum<T>::value, void>::type,
384 typename B = typename std::enable_if<sizeof(T) <= sizeof(int), void>::type,
385 typename C = typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value, void>::type,
386 typename D = typename std::enable_if<QtPrivate::IsQEnumHelper<QFlags<T>>::Value, void>::type>
387inline QDebug operator<<(QDebug dbg, T value)
388{
389 typedef QFlags<T> FlagsT;
390 const QMetaObject *obj = qt_getEnumMetaObject(FlagsT());
391 const char *name = qt_getEnumName(FlagsT());
392 return qt_QMetaEnum_debugOperator(dbg, typename FlagsT::Int(value), obj, name);
393}
394
395template <class T>
396inline typename std::enable_if<
397 QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
398 QDebug>::type
399qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
400{
401 const QMetaObject *obj = qt_getEnumMetaObject(T());
402 const char *name = qt_getEnumName(T());
403 return qt_QMetaEnum_flagDebugOperator(debug, quint64(flags), obj, name);
404}
405
406template <class T>
407inline typename std::enable_if<
408 !QtPrivate::IsQEnumHelper<T>::Value && !QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
409 QDebug>::type
410qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
411#else // !QT_NO_QOBJECT && !Q_QDOC
412template <class T>
413inline QDebug qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
414#endif
415{
416 qt_QMetaEnum_flagDebugOperator(debug, sizeof(T), typename QFlags<T>::Int(flags));
417 return debug;
418}
419
420template<typename T>
421inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
422{
423 // We have to use an indirection otherwise specialisation of some other overload of the
424 // operator<< the compiler would try to instantiate QFlags<T> for the std::enable_if
425 return qt_QMetaEnum_flagDebugOperator_helper(debug, flags);
426}
427
428#ifdef Q_OS_MAC
429
430// We provide QDebug stream operators for commonly used Core Foundation
431// and Core Graphics types, as well as NSObject. Additional CF/CG types
432// may be added by the user, using Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE.
433
434#define QT_FOR_EACH_CORE_FOUNDATION_TYPE(F) \
435 F(CFArray) \
436 F(CFURL) \
437 F(CFData) \
438 F(CFNumber) \
439 F(CFDictionary) \
440 F(CFLocale) \
441 F(CFDate) \
442 F(CFBoolean) \
443 F(CFTimeZone) \
444
445#define QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(F) \
446 F(CFError) \
447 F(CFBundle) \
448
449#define QT_FOR_EACH_CORE_GRAPHICS_TYPE(F) \
450 F(CGPath) \
451
452#define QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(F) \
453 F(CGColorSpace) \
454 F(CGImage) \
455 F(CGFont) \
456 F(CGColor) \
457
458#define QT_FORWARD_DECLARE_CF_TYPE(type) Q_FORWARD_DECLARE_CF_TYPE(type);
459#define QT_FORWARD_DECLARE_MUTABLE_CF_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CF_TYPE(type);
460#define QT_FORWARD_DECLARE_CG_TYPE(type) Q_FORWARD_DECLARE_CG_TYPE(type);
461#define QT_FORWARD_DECLARE_MUTABLE_CG_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(type);
462
463QT_END_NAMESPACE
464Q_FORWARD_DECLARE_CF_TYPE(CFString);
465Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
466QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_CF_TYPE)
467QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_MUTABLE_CF_TYPE)
468QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_CG_TYPE)
469QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_MUTABLE_CG_TYPE)
470QT_BEGIN_NAMESPACE
471
472#define QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
473 Q_CORE_EXPORT QDebug operator<<(QDebug, CFType##Ref);
474
475#define Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
476 QDebug operator<<(QDebug debug, CFType##Ref ref) \
477 { \
478 if (!ref) \
479 return debug << QT_STRINGIFY(CFType) "Ref(0x0)"; \
480 if (CFStringRef description = CFCopyDescription(ref)) { \
481 QDebugStateSaver saver(debug); \
482 debug.noquote() << description; \
483 CFRelease(description); \
484 } \
485 return debug; \
486 }
487
488// Defined in qcore_mac_objc.mm
489Q_CORE_EXPORT QDebug operator<<(QDebug, const NSObject *);
490Q_CORE_EXPORT QDebug operator<<(QDebug, CFStringRef);
491
492QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
493QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
494QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
495QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
496
497#undef QT_FORWARD_DECLARE_CF_TYPE
498#undef QT_FORWARD_DECLARE_MUTABLE_CF_TYPE
499#undef QT_FORWARD_DECLARE_CG_TYPE
500#undef QT_FORWARD_DECLARE_MUTABLE_CG_TYPE
501
502#endif // Q_OS_MAC
503
504QT_END_NAMESPACE
505
506#endif // QDEBUG_H
507