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

source code of qtbase/src/corelib/io/qdebug.h