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