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 Container, typename ...T>
247using QDebugIfHasDebugStreamContainer =
248 std::enable_if_t<std::conjunction_v<QTypeTraits::has_ostream_operator_container<QDebug, Container, T>...>, QDebug>;
249
250template<typename T>
251inline QDebugIfHasDebugStreamContainer<QList<T>, T> operator<<(QDebug debug, const QList<T> &vec)
252{
253 return QtPrivate::printSequentialContainer(debug, "QList", vec);
254}
255
256template <typename T, typename Alloc>
257inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
258{
259 return QtPrivate::printSequentialContainer(debug, "std::vector", vec);
260}
261
262template <typename T, typename Alloc>
263inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const std::list<T, Alloc> &vec)
264{
265 return QtPrivate::printSequentialContainer(debug, "std::list", vec);
266}
267
268template <typename Key, typename T, typename Compare, typename Alloc>
269inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
270{
271 return QtPrivate::printSequentialContainer(debug, "std::map", map); // yes, sequential: *it is std::pair
272}
273
274template <typename Key, typename T, typename Compare, typename Alloc>
275inline QDebugIfHasDebugStream<Key, T> operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
276{
277 return QtPrivate::printSequentialContainer(debug, "std::multimap", map); // yes, sequential: *it is std::pair
278}
279
280template <class Key, class T>
281inline QDebugIfHasDebugStreamContainer<QMap<Key, T>, Key, T> operator<<(QDebug debug, const QMap<Key, T> &map)
282{
283 return QtPrivate::printAssociativeContainer(debug, "QMap", map);
284}
285
286template <class Key, class T>
287inline QDebugIfHasDebugStreamContainer<QMultiMap<Key, T>, Key, T> operator<<(QDebug debug, const QMultiMap<Key, T> &map)
288{
289 return QtPrivate::printAssociativeContainer(debug, "QMultiMap", map);
290}
291
292template <class Key, class T>
293inline QDebugIfHasDebugStreamContainer<QHash<Key, T>, Key, T> operator<<(QDebug debug, const QHash<Key, T> &hash)
294{
295 return QtPrivate::printAssociativeContainer(debug, "QHash", hash);
296}
297
298template <class Key, class T>
299inline QDebugIfHasDebugStreamContainer<QMultiHash<Key, T>, Key, T> operator<<(QDebug debug, const QMultiHash<Key, T> &hash)
300{
301 return QtPrivate::printAssociativeContainer(debug, "QMultiHash", hash);
302}
303
304template <class T1, class T2>
305inline QDebugIfHasDebugStream<T1, T2> operator<<(QDebug debug, const std::pair<T1, T2> &pair)
306{
307 const QDebugStateSaver saver(debug);
308 debug.nospace() << "std::pair(" << pair.first << ',' << pair.second << ')';
309 return debug;
310}
311
312template <typename T>
313inline QDebugIfHasDebugStreamContainer<QSet<T>, T> operator<<(QDebug debug, const QSet<T> &set)
314{
315 return QtPrivate::printSequentialContainer(debug, "QSet", set);
316}
317
318template <class T>
319inline QDebugIfHasDebugStream<T> operator<<(QDebug debug, const QContiguousCache<T> &cache)
320{
321 const QDebugStateSaver saver(debug);
322 debug.nospace() << "QContiguousCache(";
323 for (int i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
324 debug << cache[i];
325 if (i != cache.lastIndex())
326 debug << ", ";
327 }
328 debug << ')';
329 return debug;
330}
331
332template <class T>
333inline QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
334{
335 QDebugStateSaver saver(debug);
336 debug.nospace() << "QSharedPointer(" << ptr.data() << ")";
337 return debug;
338}
339
340template <typename T, typename Tag> class QTaggedPointer;
341
342template <typename T, typename Tag>
343inline QDebug operator<<(QDebug debug, const QTaggedPointer<T, Tag> &ptr)
344{
345 QDebugStateSaver saver(debug);
346 debug.nospace() << "QTaggedPointer(" << ptr.pointer() << ", " << ptr.tag() << ")";
347 return debug;
348}
349
350Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, int value);
351
352template <typename Int>
353void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
354{
355 const QDebugStateSaver saver(debug);
356 debug.resetFormat();
357 debug.nospace() << "QFlags(" << Qt::hex << Qt::showbase;
358 bool needSeparator = false;
359 for (uint i = 0; i < sizeofT * 8; ++i) {
360 if (value & (Int(1) << i)) {
361 if (needSeparator)
362 debug << '|';
363 else
364 needSeparator = true;
365 debug << (Int(1) << i);
366 }
367 }
368 debug << ')';
369}
370
371#if !defined(QT_NO_QOBJECT) && !defined(Q_QDOC)
372Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, qint64 value, const QMetaObject *meta, const char *name);
373Q_CORE_EXPORT QDebug qt_QMetaEnum_flagDebugOperator(QDebug &dbg, quint64 value, const QMetaObject *meta, const char *name);
374
375template<typename T>
376typename std::enable_if<QtPrivate::IsQEnumHelper<T>::Value, QDebug>::type
377operator<<(QDebug dbg, T value)
378{
379 const QMetaObject *obj = qt_getEnumMetaObject(value);
380 const char *name = qt_getEnumName(value);
381 return qt_QMetaEnum_debugOperator(dbg, static_cast<typename std::underlying_type<T>::type>(value), obj, name);
382}
383
384template<typename T,
385 typename A = typename std::enable_if<std::is_enum<T>::value, void>::type,
386 typename B = typename std::enable_if<sizeof(T) <= sizeof(int), void>::type,
387 typename C = typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value, void>::type,
388 typename D = typename std::enable_if<QtPrivate::IsQEnumHelper<QFlags<T>>::Value, void>::type>
389inline QDebug operator<<(QDebug dbg, T value)
390{
391 typedef QFlags<T> FlagsT;
392 const QMetaObject *obj = qt_getEnumMetaObject(FlagsT());
393 const char *name = qt_getEnumName(FlagsT());
394 return qt_QMetaEnum_debugOperator(dbg, typename FlagsT::Int(value), obj, name);
395}
396
397template <class T>
398inline typename std::enable_if<
399 QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
400 QDebug>::type
401qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
402{
403 const QMetaObject *obj = qt_getEnumMetaObject(T());
404 const char *name = qt_getEnumName(T());
405 return qt_QMetaEnum_flagDebugOperator(debug, flags.toInt(), obj, name);
406}
407
408template <class T>
409inline typename std::enable_if<
410 !QtPrivate::IsQEnumHelper<T>::Value && !QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
411 QDebug>::type
412qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
413#else // !QT_NO_QOBJECT && !Q_QDOC
414template <class T>
415inline QDebug qt_QMetaEnum_flagDebugOperator_helper(QDebug debug, const QFlags<T> &flags)
416#endif
417{
418 qt_QMetaEnum_flagDebugOperator(debug, sizeof(T), typename QFlags<T>::Int(flags));
419 return debug;
420}
421
422template<typename T>
423inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
424{
425 // We have to use an indirection otherwise specialisation of some other overload of the
426 // operator<< the compiler would try to instantiate QFlags<T> for the std::enable_if
427 return qt_QMetaEnum_flagDebugOperator_helper(debug, flags);
428}
429
430inline QDebug operator<<(QDebug debug, QKeyCombination combination)
431{
432 QDebugStateSaver saver(debug);
433 debug.nospace() << "QKeyCombination("
434 << combination.keyboardModifiers()
435 << ", "
436 << combination.key()
437 << ")";
438 return debug;
439}
440
441#ifdef Q_OS_MAC
442
443// We provide QDebug stream operators for commonly used Core Foundation
444// and Core Graphics types, as well as NSObject. Additional CF/CG types
445// may be added by the user, using Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE.
446
447#define QT_FOR_EACH_CORE_FOUNDATION_TYPE(F) \
448 F(CFArray) \
449 F(CFURL) \
450 F(CFData) \
451 F(CFNumber) \
452 F(CFDictionary) \
453 F(CFLocale) \
454 F(CFDate) \
455 F(CFBoolean) \
456 F(CFTimeZone) \
457
458#define QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(F) \
459 F(CFError) \
460 F(CFBundle) \
461
462#define QT_FOR_EACH_CORE_GRAPHICS_TYPE(F) \
463 F(CGPath) \
464
465#define QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(F) \
466 F(CGColorSpace) \
467 F(CGImage) \
468 F(CGFont) \
469 F(CGColor) \
470
471#define QT_FORWARD_DECLARE_CF_TYPE(type) Q_FORWARD_DECLARE_CF_TYPE(type);
472#define QT_FORWARD_DECLARE_MUTABLE_CF_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CF_TYPE(type);
473#define QT_FORWARD_DECLARE_CG_TYPE(type) Q_FORWARD_DECLARE_CG_TYPE(type);
474#define QT_FORWARD_DECLARE_MUTABLE_CG_TYPE(type) Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(type);
475
476QT_END_NAMESPACE
477Q_FORWARD_DECLARE_CF_TYPE(CFString);
478Q_FORWARD_DECLARE_OBJC_CLASS(NSObject);
479QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_CF_TYPE)
480QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_MUTABLE_CF_TYPE)
481QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_CG_TYPE)
482QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_MUTABLE_CG_TYPE)
483QT_BEGIN_NAMESPACE
484
485#define QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
486 Q_CORE_EXPORT QDebug operator<<(QDebug, CFType##Ref);
487
488#define Q_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE(CFType) \
489 QDebug operator<<(QDebug debug, CFType##Ref ref) \
490 { \
491 if (!ref) \
492 return debug << QT_STRINGIFY(CFType) "Ref(0x0)"; \
493 if (CFStringRef description = CFCopyDescription(ref)) { \
494 QDebugStateSaver saver(debug); \
495 debug.noquote() << description; \
496 CFRelease(description); \
497 } \
498 return debug; \
499 }
500
501// Defined in qcore_mac_objc.mm
502Q_CORE_EXPORT QDebug operator<<(QDebug, const NSObject *);
503Q_CORE_EXPORT QDebug operator<<(QDebug, CFStringRef);
504
505QT_FOR_EACH_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
506QT_FOR_EACH_MUTABLE_CORE_FOUNDATION_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
507QT_FOR_EACH_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
508QT_FOR_EACH_MUTABLE_CORE_GRAPHICS_TYPE(QT_FORWARD_DECLARE_QDEBUG_OPERATOR_FOR_CF_TYPE)
509
510#undef QT_FORWARD_DECLARE_CF_TYPE
511#undef QT_FORWARD_DECLARE_MUTABLE_CF_TYPE
512#undef QT_FORWARD_DECLARE_CG_TYPE
513#undef QT_FORWARD_DECLARE_MUTABLE_CG_TYPE
514
515#endif // Q_OS_MAC
516
517QT_END_NAMESPACE
518
519#endif // QDEBUG_H
520

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