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