Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdebug.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parsing
5
6#include "qdebug.h"
7#include "private/qdebug_p.h"
8#include "qmetaobject.h"
9#include <private/qlogging_p.h>
10#include <private/qtextstream_p.h>
11#include <private/qtools_p.h>
12
13#include <array>
14#include <q20chrono.h>
15#include <cstdio>
16
18
19using namespace QtMiscUtils;
20
21/*
22 Returns a human readable representation of the first \a maxSize
23 characters in \a data. The size, \a len, is a 64-bit quantity to
24 avoid truncation due to implicit conversions in callers.
25*/
26QByteArray QtDebugUtils::toPrintable(const char *data, qint64 len, qsizetype maxSize)
27{
28 if (!data)
29 return "(null)";
30
31 QByteArray out;
32 for (qsizetype i = 0; i < qMin(len, maxSize); ++i) {
33 char c = data[i];
34 if (isAsciiPrintable(c)) {
35 out += c;
36 } else {
37 switch (c) {
38 case '\n':
39 out += "\\n";
40 break;
41 case '\r':
42 out += "\\r";
43 break;
44 case '\t':
45 out += "\\t";
46 break;
47 default: {
48 const char buf[] = {
49 '\\',
50 'x',
51 toHexLower(uchar(c) / 16),
52 toHexLower(uchar(c) % 16),
53 0
54 };
55 out += buf;
56 }
57 }
58 }
59 }
60
61 if (maxSize < len)
62 out += "...";
63
64 return out;
65}
66
67// This file is needed to force compilation of QDebug into the kernel library.
68
69/*!
70 \class QDebug
71 \inmodule QtCore
72 \ingroup shared
73
74 \brief The QDebug class provides an output stream for debugging information.
75
76 QDebug is used whenever the developer needs to write out debugging or tracing
77 information to a device, file, string or console.
78
79 \section1 Basic Use
80
81 In the common case, it is useful to call the qDebug() function to obtain a
82 default QDebug object to use for writing debugging information.
83
84 \snippet qdebug/qdebugsnippet.cpp 1
85
86 This constructs a QDebug object using the constructor that accepts a QtMsgType
87 value of QtDebugMsg. Similarly, the qInfo(), qWarning(), qCritical() and qFatal()
88 functions also return QDebug objects for the corresponding message types.
89
90 The class also provides several constructors for other situations, including
91 a constructor that accepts a QFile or any other QIODevice subclass that is
92 used to write debugging information to files and other devices. The constructor
93 that accepts a QString is used to write to a string for display or serialization.
94
95 \section1 Formatting Options
96
97 QDebug formats output so that it's easily readable. It automatically adds spaces
98 between arguments, and adds quotes around QString, QByteArray, QChar arguments.
99
100 You can tweak these options through the space(), nospace() and quote(), noquote()
101 methods. Furthermore, \l{QTextStream manipulators} can be piped into a QDebug
102 stream.
103
104 QDebugStateSaver limits changes to the formatting to the current scope.
105 resetFormat() resets the options to the default ones.
106
107 \section1 Writing Custom Types to a Stream
108
109 Many standard types can be written to QDebug objects, and Qt provides support for
110 most Qt value types. To add support for custom types, you need to implement a
111 streaming operator, as in the following example:
112
113 \snippet qdebug/qdebugsnippet.cpp 0
114
115 This is described in the \l{Debugging Techniques} and
116 \l{Creating Custom Qt Types#Making the Type Printable}{Creating Custom Qt Types}
117 documents.
118*/
119
120/*!
121 \fn QDebug::QDebug(QIODevice *device)
122
123 Constructs a debug stream that writes to the given \a device.
124*/
125
126/*!
127 \fn QDebug::QDebug(QString *string)
128
129 Constructs a debug stream that writes to the given \a string.
130*/
131
132/*!
133 \fn QDebug::QDebug(QByteArray *byteArray)
134 \since 6.9
135
136 Constructs a debug stream that writes to the given \a byteArray.
137
138 The data is encoded in UTF-8, which may not be the same as the system
139 locale, for example, on Windows.
140
141 With objects instantiated with this constructor, the data may be buffered
142 and won't be written to the byte array until the stream is flushed, for
143 example, by using \l Qt::flush.
144
145 \sa {QTextStream manipulators}
146*/
147
148/*!
149 \fn QDebug::QDebug(QtMsgType t)
150
151 Constructs a debug stream that writes to the handler for the message type \a t.
152*/
153
154/*!
155 \fn QDebug::QDebug(const QDebug &o)
156
157 Constructs a copy of the other debug stream \a o.
158*/
159
160/*!
161 \fn QDebug &QDebug::operator=(const QDebug &other)
162
163 Assigns the \a other debug stream to this stream and returns a reference to
164 this stream.
165*/
166
167/*!
168 \fn QDebug::~QDebug()
169
170 Flushes any pending data to be written and destroys the debug stream.
171*/
172QDebug::~QDebug()
173{
174 if (stream && !--stream->ref) {
175 if (stream->space && stream->buffer.endsWith(u' '))
176 stream->buffer.chop(1);
177 if (stream->message_output) {
178 QInternalMessageLogContext ctxt(stream->context);
179 qt_message_output(stream->type,
180 ctxt,
181 stream->buffer);
182 }
183 delete stream;
184 }
185}
186
187/*!
188 \internal
189*/
190void QDebug::putUcs4(uint ucs4)
191{
192 maybeQuote('\'');
193 if (ucs4 < 0x20) {
194 stream->ts << "\\x" << Qt::hex << ucs4 << Qt::reset;
195 } else if (ucs4 < 0x80) {
196 stream->ts << char(ucs4);
197 } else {
198 if (ucs4 < 0x10000)
199 stream->ts << "\\u" << qSetFieldWidth(4);
200 else
201 stream->ts << "\\U" << qSetFieldWidth(8);
202 stream->ts << Qt::hex << qSetPadChar(u'0') << ucs4 << Qt::reset;
203 }
204 maybeQuote('\'');
205}
206
207// These two functions return true if the character should be printed by QDebug.
208// For QByteArray, this is technically identical to US-ASCII isprint();
209// for QString, we use QChar::isPrint, which requires a full UCS-4 decode.
210static inline bool isPrintable(char32_t ucs4) { return QChar::isPrint(ucs4); }
211static inline bool isPrintable(char16_t uc) { return QChar::isPrint(uc); }
212static inline bool isPrintable(uchar c)
213{ return isAsciiPrintable(c); }
214
215template <typename Char>
216static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, size_t length, bool isUnicode = true)
217{
218 constexpr char16_t quotes[] = uR"("")";
219 constexpr char16_t quote = quotes[0];
220 d->write(quote);
221
222 bool lastWasHexEscape = false;
223 const Char *end = begin + length;
224 for (const Char *p = begin; p != end; ++p) {
225 // check if we need to insert "" to break an hex escape sequence
226 if (Q_UNLIKELY(lastWasHexEscape)) {
227 if (fromHex(*p) != -1) {
228 // yes, insert it
229 d->write(quotes);
230 }
231 lastWasHexEscape = false;
232 }
233
234 if constexpr (sizeof(Char) == sizeof(QChar)) {
235 // Surrogate characters are category Cs (Other_Surrogate), so isPrintable = false for them
236 qsizetype runLength = 0;
237 while (p + runLength != end &&
238 isPrintable(p[runLength]) && p[runLength] != '\\' && p[runLength] != '"')
239 ++runLength;
240 if (runLength) {
241 d->write(QStringView{p, runLength});
242 p += runLength - 1;
243 continue;
244 }
245 } else if (isPrintable(*p) && *p != '\\' && *p != '"') {
246 d->write(char16_t{uchar(*p)});
247 continue;
248 }
249
250 // print as an escape sequence (maybe, see below for surrogate pairs)
251 qsizetype buflen = 2;
252 char16_t buf[std::char_traits<char>::length("\\U12345678")];
253 buf[0] = '\\';
254
255 switch (*p) {
256 case '"':
257 case '\\':
258 buf[1] = *p;
259 break;
260 case '\b':
261 buf[1] = 'b';
262 break;
263 case '\f':
264 buf[1] = 'f';
265 break;
266 case '\n':
267 buf[1] = 'n';
268 break;
269 case '\r':
270 buf[1] = 'r';
271 break;
272 case '\t':
273 buf[1] = 't';
274 break;
275 default:
276 if (!isUnicode) {
277 // print as hex escape
278 buf[1] = 'x';
279 buf[2] = toHexUpper(uchar(*p) >> 4);
280 buf[3] = toHexUpper(uchar(*p));
281 buflen = 4;
282 lastWasHexEscape = true;
283 break;
284 }
285 if (QChar::isHighSurrogate(*p)) {
286 if ((p + 1) != end && QChar::isLowSurrogate(p[1])) {
287 // properly-paired surrogates
288 char32_t ucs4 = QChar::surrogateToUcs4(*p, p[1]);
289 if (isPrintable(ucs4)) {
290 buf[0] = *p;
291 buf[1] = p[1];
292 buflen = 2;
293 } else {
294 buf[1] = 'U';
295 buf[2] = '0'; // toHexUpper(ucs4 >> 28);
296 buf[3] = '0'; // toHexUpper(ucs4 >> 24);
297 buf[4] = toHexUpper(ucs4 >> 20);
298 buf[5] = toHexUpper(ucs4 >> 16);
299 buf[6] = toHexUpper(ucs4 >> 12);
300 buf[7] = toHexUpper(ucs4 >> 8);
301 buf[8] = toHexUpper(ucs4 >> 4);
302 buf[9] = toHexUpper(ucs4);
303 buflen = 10;
304 }
305 ++p;
306 break;
307 }
308 // improperly-paired surrogates, fall through
309 }
310 buf[1] = 'u';
311 buf[2] = toHexUpper(char16_t(*p) >> 12);
312 buf[3] = toHexUpper(char16_t(*p) >> 8);
313 buf[4] = toHexUpper(*p >> 4);
314 buf[5] = toHexUpper(*p);
315 buflen = 6;
316 }
317 d->write(QStringView{buf, buflen});
318 }
319
320 d->write(quote);
321}
322
323/*!
324 \internal
325 Duplicated from QtTest::toPrettyUnicode().
326*/
327void QDebug::putString(const QChar *begin, size_t length)
328{
329 if (stream->noQuotes) {
330 // no quotes, write the string directly too (no pretty-printing)
331 // this respects the QTextStream state, though
332 stream->ts.d_ptr->putString(QStringView{begin, qsizetype(length)});
333 } else {
334 // we'll reset the QTextStream formatting mechanisms, so save the state
335 QDebugStateSaver saver(*this);
336 stream->ts.d_ptr->params.reset();
337 putEscapedString(stream->ts.d_ptr.get(), reinterpret_cast<const char16_t *>(begin), length);
338 }
339}
340
341/*!
342 \internal
343 Duplicated from QtTest::toPrettyCString().
344*/
345void QDebug::putByteArray(const char *begin, size_t length, Latin1Content content)
346{
347 if (stream->noQuotes) {
348 // no quotes, write the string directly too (no pretty-printing)
349 // this respects the QTextStream state, though
350 switch (content) {
351 case Latin1Content::ContainsLatin1:
352 stream->ts.d_ptr->putString(QLatin1StringView{begin, qsizetype(length)});
353 break;
354 case Latin1Content::ContainsBinary:
355 stream->ts.d_ptr->putString(QUtf8StringView{begin, qsizetype(length)});
356 break;
357 }
358 } else {
359 // we'll reset the QTextStream formatting mechanisms, so save the state
360 QDebugStateSaver saver(*this);
361 stream->ts.d_ptr->params.reset();
362 putEscapedString(stream->ts.d_ptr.get(), reinterpret_cast<const uchar *>(begin),
363 length, content == ContainsLatin1);
364 }
365}
366
367static QByteArray timeUnit(qint64 num, qint64 den)
368{
369 using namespace std::chrono;
370 using namespace q20::chrono;
371
372 if (num == 1 && den > 1) {
373 // sub-multiple of seconds
374 char prefix = '\0';
375 auto tryprefix = [&](auto d, char c) {
376 static_assert(decltype(d)::num == 1, "not an SI prefix");
377 if (den == decltype(d)::den)
378 prefix = c;
379 };
380
381 // "u" should be "ยต", but debugging output is not always UTF-8-safe
382 tryprefix(std::milli{}, 'm');
383 tryprefix(std::micro{}, 'u');
384 tryprefix(std::nano{}, 'n');
385 tryprefix(std::pico{}, 'p');
386 tryprefix(std::femto{}, 'f');
387 tryprefix(std::atto{}, 'a');
388 // uncommon ones later
389 tryprefix(std::centi{}, 'c');
390 tryprefix(std::deci{}, 'd');
391 if (prefix) {
392 char unit[3] = { prefix, 's' };
393 return QByteArray(unit, sizeof(unit) - 1);
394 }
395 }
396
397 const char *unit = nullptr;
398 if (num > 1 && den == 1) {
399 // multiple of seconds - but we don't use SI prefixes
400 auto tryunit = [&](auto d, const char *name) {
401 static_assert(decltype(d)::period::den == 1, "not a multiple of a second");
402 if (unit || num % decltype(d)::period::num)
403 return;
404 unit = name;
405 num /= decltype(d)::period::num;
406 };
407 tryunit(years{}, "yr");
408 tryunit(weeks{}, "wk");
409 tryunit(days{}, "d");
410 tryunit(hours{}, "h");
411 tryunit(minutes{}, "min");
412 }
413 if (!unit)
414 unit = "s";
415
416 if (num == 1 && den == 1)
417 return unit;
418 if (Q_UNLIKELY(num < 1 || den < 1))
419 return QString::asprintf("<invalid time unit %lld/%lld>", num, den).toLatin1();
420
421 // uncommon units: will return something like "[2/3]s"
422 // strlen("[/]min") = 6
423 char buf[2 * (std::numeric_limits<qint64>::digits10 + 2) + 10];
424 size_t len = 0;
425 auto appendChar = [&](char c) {
426 Q_ASSERT(len < sizeof(buf));
427 buf[len++] = c;
428 };
429 auto appendNumber = [&](qint64 value) {
430 if (value >= 10'000 && (value % 1000) == 0)
431 len += std::snprintf(buf + len, sizeof(buf) - len, "%.6g", double(value)); // "1e+06"
432 else
433 len += std::snprintf(buf + len, sizeof(buf) - len, "%lld", value);
434 };
435 appendChar('[');
436 appendNumber(num);
437 if (den != 1) {
438 appendChar('/');
439 appendNumber(den);
440 }
441 appendChar(']');
442 memcpy(buf + len, unit, strlen(unit));
443 return QByteArray(buf, len + strlen(unit));
444}
445
446/*!
447 \since 6.6
448 \internal
449 Helper to the std::chrono::duration debug streaming output.
450 */
451void QDebug::putTimeUnit(qint64 num, qint64 den)
452{
453 stream->ts << timeUnit(num, den); // ### optimize
454}
455
456namespace {
457
458#ifdef QT_SUPPORTS_INT128
459
460constexpr char Q_INT128_MIN_STR[] = "-170141183460469231731687303715884105728";
461
462constexpr int Int128BufferSize = sizeof(Q_INT128_MIN_STR);
463using Int128Buffer = std::array<char, Int128BufferSize>;
464 // numeric_limits<qint128>::digits10 may not exist
465
466static char *i128ToStringHelper(Int128Buffer &buffer, quint128 n)
467{
468 auto dst = buffer.data() + buffer.size();
469 *--dst = '\0'; // NUL-terminate
470 if (n == 0) {
471 *--dst = '0'; // and done
472 } else {
473 while (n != 0) {
474 *--dst = "0123456789"[n % 10];
475 n /= 10;
476 }
477 }
478 return dst;
479}
480#endif // QT_SUPPORTS_INT128
481
482[[maybe_unused]]
483static const char *int128Warning()
484{
485 const char *msg = "Qt was not compiled with int128 support.";
486 qWarning("%s", msg);
487 return msg;
488}
489
490} // unnamed namespace
491
492/*!
493 \since 6.7
494 \internal
495 Helper to the qint128 debug streaming output.
496 */
497void QDebug::putInt128([[maybe_unused]] const void *p)
498{
499#ifdef QT_SUPPORTS_INT128
500 Q_ASSERT(p);
501 qint128 i;
502 memcpy(&i, p, sizeof(i)); // alignment paranoia
503 if (i == Q_INT128_MIN) {
504 // -i is not representable, hardcode the result:
505 stream->ts << Q_INT128_MIN_STR;
506 } else {
507 Int128Buffer buffer;
508 auto dst = i128ToStringHelper(buffer, i < 0 ? -i : i);
509 if (i < 0)
510 *--dst = '-';
511 stream->ts << dst;
512 }
513 return;
514#endif // QT_SUPPORTS_INT128
515 stream->ts << int128Warning();
516}
517
518/*!
519 \since 6.7
520 \internal
521 Helper to the quint128 debug streaming output.
522 */
523void QDebug::putUInt128([[maybe_unused]] const void *p)
524{
525#ifdef QT_SUPPORTS_INT128
526 Q_ASSERT(p);
527 quint128 i;
528 memcpy(&i, p, sizeof(i)); // alignment paranoia
529 Int128Buffer buffer;
530 stream->ts << i128ToStringHelper(buffer, i);
531 return;
532#endif // QT_SUPPORTS_INT128
533 stream->ts << int128Warning();
534}
535
536/*!
537 \since 6.9
538 \internal
539 Helper to the <Std/Qt>::<>_ordering debug output.
540 It generates the string in following format:
541 <Qt/Std>::<weak/partial/strong>_ordering::<less/equal/greater/unordered>
542 */
543void QDebug::putQtOrdering(QtOrderingPrivate::QtOrderingTypeFlag flags, Qt::partial_ordering order)
544{
545 using QtOrderingPrivate::QtOrderingType;
546 std::string result;
547 if ((flags & QtOrderingType::StdOrder) == QtOrderingType::StdOrder)
548 result += "std";
549 else if ((flags & QtOrderingType::QtOrder) == QtOrderingType::QtOrder)
550 result += "Qt";
551
552 result += "::";
553 const bool isStrong = ((flags & QtOrderingType::Strong) == QtOrderingType::Strong);
554 if (isStrong)
555 result += "strong";
556 else if ((flags & QtOrderingType::Weak) == QtOrderingType::Weak)
557 result += "weak";
558 else if ((flags & QtOrderingType::Partial) == QtOrderingType::Partial)
559 result += "partial";
560 result += "_ordering::";
561
562 if (order == Qt::partial_ordering::equivalent) {
563 if (isStrong)
564 result += "equal";
565 else
566 result += "equivalent";
567 } else if (order == Qt::partial_ordering::greater) {
568 result += "greater";
569 } else if (order == Qt::partial_ordering::less) {
570 result += "less";
571 } else {
572 result += "unordered";
573 }
574 stream->ts << result.data();
575}
576
577/*!
578 \fn QDebug::swap(QDebug &other)
579 \since 5.0
580 \memberswap{debug stream instance}
581*/
582
583/*!
584 Resets the stream formatting options, bringing it back to its original constructed state.
585
586 \sa space(), quote()
587 \since 5.4
588*/
589QDebug &QDebug::resetFormat()
590{
591 stream->ts.reset();
592 stream->space = true;
593 stream->noQuotes = false;
594 stream->verbosity = DefaultVerbosity;
595 return *this;
596}
597
598/*!
599 \fn QDebug &QDebug::space()
600
601 Writes a space character to the debug stream and returns a reference to
602 the stream.
603
604 The stream remembers that automatic insertion of spaces is
605 enabled for future writes.
606
607 \sa nospace(), maybeSpace()
608*/
609
610/*!
611 \fn QDebug &QDebug::nospace()
612
613 Disables automatic insertion of spaces and returns a reference to the stream.
614
615 \sa space(), maybeSpace()
616*/
617
618/*!
619 \fn QDebug &QDebug::maybeSpace()
620
621 Writes a space character to the debug stream, depending on the current
622 setting for automatic insertion of spaces, and returns a reference to the stream.
623
624 \sa space(), nospace()
625*/
626
627/*!
628 \fn bool QDebug::autoInsertSpaces() const
629
630 Returns \c true if this QDebug instance will automatically insert spaces
631 between writes.
632
633 \since 5.0
634
635 \sa QDebugStateSaver
636*/
637
638/*!
639 \fn void QDebug::setAutoInsertSpaces(bool b)
640
641 Enables automatic insertion of spaces between writes if \a b is true; otherwise
642 automatic insertion of spaces is disabled.
643
644 \since 5.0
645
646 \sa QDebugStateSaver
647*/
648
649
650/*!
651 \fn bool QDebug::quoteStrings() const
652 \since 6.7
653
654 Returns \c true if this QDebug instance will quote strings streamed into
655 it (which is the default).
656
657 \sa QDebugStateSaver, quote(), noquote(), setQuoteStrings()
658*/
659
660/*!
661 \fn void QDebug::setQuoteStrings(bool b)
662 \since 6.7
663
664 Enables quoting of strings streamed into this QDebug instance if \a b is
665 \c true; otherwise quoting is disabled.
666
667 The default is to quote strings.
668
669 \sa QDebugStateSaver, quote(), noquote(), quoteStrings()
670*/
671
672
673/*!
674 \fn QDebug &QDebug::quote()
675 \since 5.4
676
677 Enables automatic insertion of quotation characters around QChar, QString and QByteArray
678 contents and returns a reference to the stream.
679
680 Quoting is enabled by default.
681
682 \sa noquote(), maybeQuote()
683*/
684
685/*!
686 \fn QDebug &QDebug::noquote()
687 \since 5.4
688
689 Disables automatic insertion of quotation characters around QChar, QString and QByteArray
690 contents and returns a reference to the stream.
691
692 When quoting is disabled, these types are printed without quotation
693 characters and without escaping of non-printable characters.
694
695 \sa quote(), maybeQuote()
696*/
697
698/*!
699 \fn QDebug &QDebug::maybeQuote(char c)
700 \since 5.4
701
702 Writes a character \a c to the debug stream, depending on the
703 current setting for automatic insertion of quotes, and returns a reference to the stream.
704
705 The default character is a double quote \c{"}.
706
707 \sa quote(), noquote()
708*/
709
710/*!
711 \fn int QDebug::verbosity() const
712 \since 5.6
713
714 Returns the verbosity of the debug stream.
715
716 Streaming operators can check the value to decide whether
717 verbose output is desired and print more information depending on the
718 level. Higher values indicate that more information is desired.
719
720 The allowed range is from 0 to 7. The default value is 2.
721
722 \sa setVerbosity(), VerbosityLevel
723*/
724
725/*!
726 \fn void QDebug::setVerbosity(int verbosityLevel)
727 \since 5.6
728
729 Sets the verbosity of the stream to \a verbosityLevel.
730
731 The allowed range is from 0 to 7. The default value is 2.
732
733 \sa verbosity(), VerbosityLevel
734*/
735
736/*!
737 \fn QDebug &QDebug::verbosity(int verbosityLevel)
738 \since 5.13
739
740 Sets the verbosity of the stream to \a verbosityLevel and returns a reference to the stream.
741
742 The allowed range is from 0 to 7. The default value is 2.
743
744 \sa verbosity(), setVerbosity(), VerbosityLevel
745*/
746
747/*!
748 \enum QDebug::VerbosityLevel
749 \since 5.13
750
751 This enum describes the range of verbosity levels.
752
753 \value MinimumVerbosity
754 \value DefaultVerbosity
755 \value MaximumVerbosity
756
757 \sa verbosity(), setVerbosity()
758*/
759
760/*!
761 \fn QDebug &QDebug::operator<<(QChar t)
762
763 Writes the character, \a t, to the stream and returns a reference to the
764 stream. Normally, QDebug prints control characters and non-US-ASCII
765 characters as their C escape sequences or their Unicode value (\\u1234). To
766 print non-printable characters without transformation, enable the noquote()
767 functionality, but note that some QDebug backends may not be 8-bit clean
768 and may not be able to represent \c t.
769*/
770
771/*!
772 \fn QDebug &QDebug::operator<<(bool t)
773
774 Writes the boolean value, \a t, to the stream and returns a reference to the
775 stream.
776*/
777
778/*!
779 \fn QDebug &QDebug::operator<<(char t)
780
781 Writes the character, \a t, to the stream and returns a reference to the
782 stream.
783*/
784
785/*!
786 \fn QDebug &QDebug::operator<<(signed short t)
787
788 Writes the signed short integer, \a t, to the stream and returns a reference
789 to the stream.
790*/
791
792/*!
793 \fn QDebug &QDebug::operator<<(unsigned short t)
794
795 Writes then unsigned short integer, \a t, to the stream and returns a
796 reference to the stream.
797*/
798
799/*!
800 \fn QDebug &QDebug::operator<<(signed int t)
801
802 Writes the signed integer, \a t, to the stream and returns a reference
803 to the stream.
804*/
805
806/*!
807 \fn QDebug &QDebug::operator<<(unsigned int t)
808
809 Writes then unsigned integer, \a t, to the stream and returns a reference to
810 the stream.
811*/
812
813/*!
814 \fn QDebug &QDebug::operator<<(signed long t)
815
816 Writes the signed long integer, \a t, to the stream and returns a reference
817 to the stream.
818*/
819
820/*!
821 \fn QDebug &QDebug::operator<<(unsigned long t)
822
823 Writes then unsigned long integer, \a t, to the stream and returns a reference
824 to the stream.
825*/
826
827/*!
828 \fn QDebug &QDebug::operator<<(qint64 t)
829
830 Writes the signed 64-bit integer, \a t, to the stream and returns a reference
831 to the stream.
832*/
833
834/*!
835 \fn QDebug &QDebug::operator<<(quint64 t)
836
837 Writes then unsigned 64-bit integer, \a t, to the stream and returns a
838 reference to the stream.
839*/
840
841/*!
842 \fn QDebug &QDebug::operator<<(float t)
843
844 Writes the 32-bit floating point number, \a t, to the stream and returns a
845 reference to the stream.
846*/
847
848/*!
849 \fn QDebug &QDebug::operator<<(double t)
850
851 Writes the 64-bit floating point number, \a t, to the stream and returns a
852 reference to the stream.
853*/
854
855/*!
856 \fn QDebug &QDebug::operator<<(const char *t)
857
858 Writes the '\\0'-terminated UTF-8 string, \a t, to the stream and returns a
859 reference to the stream. The string is never quoted or escaped for the
860 output. Note that QDebug buffers internally as UTF-16 and may need to
861 transform to 8-bit using the locale's codec in order to use some backends,
862 which may cause garbled output (mojibake). Restricting to US-ASCII strings
863 is recommended.
864*/
865
866/*!
867 \fn QDebug &QDebug::operator<<(const char16_t *t)
868 \since 6.0
869
870 Writes the u'\\0'-terminated UTF-16 string, \a t, to the stream and returns
871 a reference to the stream. The string is never quoted or escaped for the
872 output. Note that QDebug buffers internally as UTF-16 and may need to
873 transform to 8-bit using the locale's codec in order to use some backends,
874 which may cause garbled output (mojibake). Restricting to US-ASCII strings
875 is recommended.
876*/
877
878/*!
879 \fn QDebug &QDebug::operator<<(char16_t t)
880 \since 5.5
881
882 Writes the UTF-16 character, \a t, to the stream and returns a reference
883 to the stream.
884*/
885
886/*!
887 \fn QDebug &QDebug::operator<<(char32_t t)
888 \since 5.5
889
890 Writes the UTF-32 character, \a t, to the stream and returns a reference
891 to the stream.
892*/
893
894/*!
895 \fn QDebug &QDebug::operator<<(const QString &t)
896
897 Writes the string, \a t, to the stream and returns a reference to the
898 stream. Normally, QDebug prints the string inside quotes and transforms
899 non-printable characters to their Unicode values (\\u1234).
900
901 To print non-printable characters without transformation, enable the
902 noquote() functionality. Note that some QDebug backends might not be 8-bit
903 clean.
904
905 Output examples:
906 \snippet code/src_corelib_io_qdebug.cpp 0
907*/
908
909/*!
910 \since 5.10
911 \fn QDebug &QDebug::operator<<(QStringView s)
912
913 Writes the string view, \a s, to the stream and returns a reference to the
914 stream. Normally, QDebug prints the string inside quotes and transforms
915 non-printable characters to their Unicode values (\\u1234).
916
917 To print non-printable characters without transformation, enable the
918 noquote() functionality. Note that some QDebug backends might not be 8-bit
919 clean.
920
921 See the QString overload for examples.
922*/
923
924/*!
925 \since 6.0
926 \fn QDebug &QDebug::operator<<(QUtf8StringView s)
927
928 Writes the string view, \a s, to the stream and returns a reference to the
929 stream.
930
931 Normally, QDebug prints the data inside quotes and transforms control or
932 non-US-ASCII characters to their C escape sequences (\\xAB). This way, the
933 output is always 7-bit clean and the string can be copied from the output
934 and pasted back into C++ sources, if necessary.
935
936 To print non-printable characters without transformation, enable the
937 noquote() functionality. Note that some QDebug backends might not be 8-bit
938 clean.
939*/
940
941/*!
942 \fn QDebug &QDebug::operator<<(QLatin1StringView t)
943
944 Writes the string, \a t, to the stream and returns a reference to the
945 stream. Normally, QDebug prints the string inside quotes and transforms
946 non-printable characters to their Unicode values (\\u1234).
947
948 To print non-printable characters without transformation, enable the
949 noquote() functionality. Note that some QDebug backends might not be 8-bit
950 clean.
951
952 See the QString overload for examples.
953*/
954
955/*!
956 \fn QDebug &QDebug::operator<<(const QByteArray &t)
957
958 Writes the byte array, \a t, to the stream and returns a reference to the
959 stream. Normally, QDebug prints the array inside quotes and transforms
960 control or non-US-ASCII characters to their C escape sequences (\\xAB). This
961 way, the output is always 7-bit clean and the string can be copied from the
962 output and pasted back into C++ sources, if necessary.
963
964 To print non-printable characters without transformation, enable the
965 noquote() functionality. Note that some QDebug backends might not be 8-bit
966 clean.
967
968 Output examples:
969 \snippet code/src_corelib_io_qdebug.cpp 1
970
971 Note how QDebug needed to close and reopen the string in the way C and C++
972 languages concatenate string literals so that the letter 'b' is not
973 interpreted as part of the previous hexadecimal escape sequence.
974*/
975
976/*!
977 \since 6.0
978 \fn QDebug &QDebug::operator<<(QByteArrayView t)
979
980 Writes the data of the observed byte array, \a t, to the stream and returns
981 a reference to the stream.
982
983 Normally, QDebug prints the data inside quotes and transforms control or
984 non-US-ASCII characters to their C escape sequences (\\xAB). This way, the
985 output is always 7-bit clean and the string can be copied from the output
986 and pasted back into C++ sources, if necessary.
987
988 To print non-printable characters without transformation, enable the
989 noquote() functionality. Note that some QDebug backends might not be 8-bit
990 clean.
991
992 See the QByteArray overload for examples.
993*/
994
995/*!
996 \fn QDebug &QDebug::operator<<(const void *t)
997
998 Writes a pointer, \a t, to the stream and returns a reference to the stream.
999*/
1000
1001/*!
1002 \fn QDebug &QDebug::operator<<(QTextStreamFunction f)
1003 \internal
1004*/
1005
1006/*!
1007 \fn QDebug &QDebug::operator<<(QTextStreamManipulator m)
1008 \internal
1009*/
1010
1011/*!
1012 \fn template <typename T, QDebug::if_ordering_type<T>> QDebug::operator<<(QDebug debug, T t)
1013 \since 6.9
1014 Prints the Qt or std ordering value \a t to the \a debug object.
1015
1016 \constraints \c T is one of <Qt/Std>::<weak/partial/strong>_ordering.
1017*/
1018
1019/*!
1020 \since 6.5
1021 \fn template <typename Char, typename...Args> QDebug &QDebug::operator<<(const std::basic_string<Char, Args...> &s)
1022 \fn template <typename Char, typename...Args> QDebug &QDebug::operator<<(std::basic_string_view<Char, Args...> s)
1023
1024 Writes the string or string-view \a s to the stream and returns a reference
1025 to the stream.
1026
1027 These operators only participate in overload resolution if \c Char is one of
1028 \list
1029 \li char
1030 \li char8_t (C++20 only)
1031 \li char16_t
1032 \li char32_t
1033 \li wchar_t
1034 \endlist
1035*/
1036
1037/*!
1038 \since 6.6
1039 \fn template <typename Rep, typename Period> QDebug &QDebug::operator<<(std::chrono::duration<Rep, Period> duration)
1040
1041 Prints the time duration \a duration to the stream and returns a reference
1042 to the stream. The printed string is the numeric representation of the
1043 period followed by the time unit, similar to what the C++ Standard Library
1044 would produce with \c{std::ostream}.
1045
1046 The unit is not localized.
1047*/
1048
1049/*!
1050 \fn template <typename T, QDebug::if_qint128<T>> QDebug::operator<<(T i)
1051 \fn template <typename T, QDebug::if_quint128<T>> QDebug::operator<<(T i)
1052 \since 6.7
1053
1054 Prints the textual representation of the 128-bit integer \a i.
1055
1056 \note This operator is only available if Qt supports 128-bit integer types.
1057 If 128-bit integer types are available in your build, but the Qt libraries
1058 were compiled without, the operator will print a warning instead.
1059
1060 \note Because the operator is a function template, no implicit conversions
1061 are performed on its argument. It must be exactly qint128/quint128.
1062
1063 \sa QT_SUPPORTS_INT128
1064*/
1065
1066/*!
1067 \fn template <class T> QString QDebug::toString(const T &object)
1068 \since 6.0
1069
1070 Streams \a object into a QDebug instance that operates on a string,
1071 and then returns that string.
1072
1073 This function is useful for cases where you need the textual representation
1074 of an object for debugging, but cannot use \c {operator<<}. For example:
1075
1076 \snippet code/src_corelib_io_qdebug.cpp toString
1077
1078 The string is streamed using \l nospace().
1079
1080 \sa toBytes()
1081*/
1082
1083/*! \internal */
1084QString QDebug::toStringImpl(StreamTypeErased s, const void *obj)
1085{
1086 QString result;
1087 {
1088 QDebug d(&result);
1089 s(d.nospace(), obj);
1090 }
1091 return result;
1092}
1093
1094/*!
1095 \fn template <class T> QByteArray QDebug::toBytes(const T &object)
1096 \since 6.9
1097
1098 This is equivalent to passing \a object to
1099 \c{QDebug::toString(object).toUtf8()}, but more efficient.
1100
1101 \sa toString()
1102*/
1103
1104/*! \internal */
1105QByteArray QDebug::toBytesImpl(StreamTypeErased s, const void *obj)
1106{
1107 QByteArray result;
1108 {
1109 QDebug d(&result);
1110 s(d.nospace(), obj);
1111 }
1112 return result;
1113}
1114
1115/*!
1116 \internal
1117 \since 6.9
1118
1119 Outputs a heterogeneous product type (pair, tuple, or anything that
1120 implements the Tuple Protocol). The class name is described by "\a ns
1121 \c{::} \a what", while the addresses of the \a n elements are stored in the
1122 array \a data. The formatters are stored in the array \a ops.
1123
1124 If \a ns is empty, only \a what is used.
1125*/
1126QDebug &QDebug::putTupleLikeImplImpl(const char *ns, const char *what,
1127 size_t n, StreamTypeErased *ops, const void **data)
1128{
1129 const QDebugStateSaver saver(*this);
1130 nospace();
1131 if (ns && *ns)
1132 *this << ns << "::";
1133 *this << what << '(';
1134 while (n--) {
1135 (*ops++)(*this, *data++);
1136 if (n)
1137 *this << ", ";
1138 }
1139 return *this << ')';
1140}
1141
1142/*!
1143 \fn template <class T> QDebug operator<<(QDebug debug, const QList<T> &list)
1144 \relates QDebug
1145
1146 Writes the contents of \a list to \a debug. \c T needs to
1147 support streaming into QDebug.
1148*/
1149
1150/*!
1151 \fn template <class T, qsizetype P> QDebug operator<<(QDebug debug, const QVarLengthArray<T,P> &array)
1152 \relates QDebug
1153 \since 6.3
1154
1155 Writes the contents of \a array to \a debug. \c T needs to
1156 support streaming into QDebug.
1157*/
1158
1159/*!
1160 \fn template <typename T, typename Alloc> QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec)
1161 \relates QDebug
1162 \since 5.7
1163
1164 Writes the contents of list \a vec to \a debug. \c T needs to
1165 support streaming into QDebug.
1166*/
1167
1168/*!
1169 \fn template <typename T, typename Alloc> QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
1170 \relates QDebug
1171 \since 5.7
1172
1173 Writes the contents of vector \a vec to \a debug. \c T needs to
1174 support streaming into QDebug.
1175*/
1176
1177/*!
1178 \fn template <typename T, std::size_t N> QDebug operator<<(QDebug debug, const std::array<T, N> &array)
1179 \relates QDebug
1180 \since 6.9
1181
1182 Writes the contents of \a array to \a debug. \c T needs to
1183 support streaming into QDebug.
1184*/
1185
1186/*!
1187 \fn template <typename T> QDebug operator<<(QDebug debug, const QSet<T> &set)
1188 \relates QDebug
1189
1190 Writes the contents of \a set to \a debug. \c T needs to
1191 support streaming into QDebug.
1192*/
1193
1194/*!
1195 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QMap<Key, T> &map)
1196 \relates QDebug
1197
1198 Writes the contents of \a map to \a debug. Both \c Key and
1199 \c T need to support streaming into QDebug.
1200*/
1201
1202/*!
1203 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QMultiMap<Key, T> &map)
1204 \relates QDebug
1205
1206 Writes the contents of \a map to \a debug. Both \c Key and
1207 \c T need to support streaming into QDebug.
1208*/
1209
1210/*!
1211 \fn template <typename Key, typename T, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
1212 \relates QDebug
1213 \since 5.7
1214
1215 Writes the contents of \a map to \a debug. Both \c Key and
1216 \c T need to support streaming into QDebug.
1217*/
1218
1219/*!
1220 \fn template <typename Key, typename T, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
1221 \relates QDebug
1222 \since 5.7
1223
1224 Writes the contents of \a map to \a debug. Both \c Key and
1225 \c T need to support streaming into QDebug.
1226*/
1227
1228/*!
1229 \fn template <typename Key, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset)
1230 \relates QDebug
1231 \since 6.9
1232
1233 Writes the contents of \a multiset to \a debug. The \c Key type
1234 needs to support streaming into QDebug.
1235*/
1236
1237/*!
1238 \fn template <typename Key, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
1239 \relates QDebug
1240 \since 6.9
1241
1242 Writes the contents of \a set to \a debug. The \c Key type
1243 needs to support streaming into QDebug.
1244*/
1245
1246/*!
1247 \fn template <typename Key, typename T, typename Hash, typename KeyEqual, typename Alloc> QDebug operator<<(QDebug debug, const std::unordered_map<Key, T, Hash, KeyEqual, Alloc> &map)
1248 \relates QDebug
1249 \since 6.9
1250
1251 Writes the contents of \a map to \a debug. Both \c Key and
1252 \c T need to support streaming into QDebug.
1253*/
1254
1255/*!
1256 \fn template <typename Key, typename Hash, typename KeyEqual, typename Alloc> QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
1257 \relates QDebug
1258 \since 6.9
1259
1260 Writes the contents of \a unordered_set to \a debug. The \c Key type
1261 needs to support streaming into QDebug.
1262*/
1263
1264/*!
1265 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
1266 \relates QDebug
1267
1268 Writes the contents of \a hash to \a debug. Both \c Key and
1269 \c T need to support streaming into QDebug.
1270*/
1271
1272/*!
1273 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QMultiHash<Key, T> &hash)
1274 \relates QDebug
1275
1276 Writes the contents of \a hash to \a debug. Both \c Key and
1277 \c T need to support streaming into QDebug.
1278*/
1279
1280/*!
1281 \fn template <class...Ts, QDebug::if_streamable<Ts...>> QDebug &QDebug::operator<<(const std::tuple<Ts...> &tuple)
1282 \since 6.9
1283
1284 Writes the contents of \a tuple to the stream. All \c Ts... need to support
1285 streaming into QDebug.
1286*/
1287
1288/*!
1289 \fn template <class T1, class T2> QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair)
1290 \relates QDebug
1291
1292 Writes the contents of \a pair to \a debug. Both \c T1 and
1293 \c T2 need to support streaming into QDebug.
1294*/
1295
1296/*!
1297 \since 6.7
1298 \fn template <class T, QDebug::if_streamable<T>> QDebug::operator<<(const std::optional<T> &opt)
1299
1300 Writes the contents of \a opt (or \c nullopt if not set) to this stream.
1301 \c T needs to support streaming into QDebug.
1302*/
1303
1304/*!
1305 \fn template <typename T> QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
1306 \relates QDebug
1307
1308 Writes the contents of \a cache to \a debug. \c T needs to
1309 support streaming into QDebug.
1310*/
1311
1312/*!
1313 \fn template<typename T> QDebug operator<<(QDebug debug, const QFlags<T> &flags)
1314 \relates QDebug
1315 \since 4.7
1316
1317 Writes \a flags to \a debug.
1318*/
1319
1320/*!
1321 \fn template<typename T> QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
1322 \relates QSharedPointer
1323 \since 5.7
1324
1325 Writes the pointer tracked by \a ptr into the debug object \a debug for
1326 debugging purposes.
1327
1328 \sa {Debugging Techniques}
1329*/
1330
1331/*!
1332 \fn QDebug &QDebug::operator<<(std::nullptr_t)
1333 \internal
1334 */
1335
1336/*!
1337 \since 6.7
1338 \fn QDebug &QDebug::operator<<(std::nullopt_t)
1339
1340 Writes nullopt to the stream.
1341*/
1342
1343/*!
1344 \class QDebugStateSaver
1345 \inmodule QtCore
1346 \brief Convenience class for custom QDebug operators.
1347
1348 Saves the settings used by QDebug, and restores them upon destruction,
1349 then calls \l {QDebug::maybeSpace()}{maybeSpace()}, to separate arguments with a space if
1350 \l {QDebug::autoInsertSpaces()}{autoInsertSpaces()} was true at the time of constructing the QDebugStateSaver.
1351
1352 The automatic insertion of spaces between writes is one of the settings
1353 that QDebugStateSaver stores for the duration of the current block.
1354
1355 The settings of the internal QTextStream are also saved and restored,
1356 so that using << Qt::hex in a QDebug operator doesn't affect other QDebug
1357 operators.
1358
1359 QDebugStateSaver is typically used in the implementation of an operator<<() for debugging:
1360
1361 \snippet customtype/customtypeexample.cpp custom type streaming operator
1362
1363 \since 5.1
1364*/
1365
1367{
1368public:
1378 {
1379 const bool currentSpaces = m_stream->space;
1380 if (currentSpaces && !m_spaces)
1381 if (m_stream->buffer.endsWith(u' '))
1382 m_stream->buffer.chop(1);
1383
1384 m_stream->space = m_spaces;
1385 m_stream->noQuotes = m_noQuotes;
1386 m_stream->ts.d_ptr->params = m_streamParams;
1387 m_stream->verbosity = m_verbosity;
1388
1389 if (!currentSpaces && m_spaces)
1390 m_stream->ts << ' ';
1391 }
1392
1394
1395 // QDebug state
1396 const bool m_spaces;
1397 const bool m_noQuotes;
1398 const int m_verbosity;
1399
1400 // QTextStream state
1402};
1403
1404
1405/*!
1406 Creates a QDebugStateSaver instance, which saves the settings
1407 currently used by \a dbg.
1408
1409 \sa QDebug::setAutoInsertSpaces(), QDebug::autoInsertSpaces()
1410*/
1411QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
1412 : d(new QDebugStateSaverPrivate(dbg.stream))
1413{
1414}
1415
1416/*!
1417 Destroys a QDebugStateSaver instance, which restores the settings
1418 used when the QDebugStateSaver instance was created.
1419
1420 \sa QDebug::setAutoInsertSpaces(), QDebug::autoInsertSpaces()
1421*/
1422QDebugStateSaver::~QDebugStateSaver()
1423{
1424 d->restoreState();
1425}
1426
1427/*!
1428 \internal
1429
1430 Specialization of the primary template in qdebug.h to out-of-line
1431 the common case of QFlags<T>::Int being 32-bit.
1432
1433 Just call the generic version so the two don't get out of sync.
1434*/
1435void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
1436{
1437 qt_QMetaEnum_flagDebugOperator(debug, sizeofT, quint64(value));
1438}
1439
1440/*!
1441 \internal
1442 Ditto, for 64-bit.
1443*/
1444void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value)
1445{
1446 qt_QMetaEnum_flagDebugOperator<quint64>(debug, sizeofT, value);
1447}
1448
1449
1450#ifndef QT_NO_QOBJECT
1451/*!
1452 \internal
1453
1454 Formats the given enum \a value for debug output.
1455
1456 The supported verbosity are:
1457
1458 0: Just the key, or value with enum name if no key is found:
1459
1460 MyEnum2
1461 MyEnum(123)
1462 MyScopedEnum::Enum3
1463 MyScopedEnum(456)
1464
1465 1: Same as 0, but treating all enums as scoped:
1466
1467 MyEnum::MyEnum2
1468 MyEnum(123)
1469 MyScopedEnum::Enum3
1470 MyScopedEnum(456)
1471
1472 2: The QDebug default. Same as 0, and includes class/namespace scope:
1473
1474 MyNamespace::MyClass::MyEnum2
1475 MyNamespace::MyClass::MyEnum(123)
1476 MyNamespace::MyClass::MyScopedEnum::Enum3
1477 MyNamespace::MyClass::MyScopedEnum(456)
1478
1479 3: Same as 2, but treating all enums as scoped:
1480
1481 MyNamespace::MyClass::MyEnum::MyEnum2
1482 MyNamespace::MyClass::MyEnum(123)
1483 MyNamespace::MyClass::MyScopedEnum::Enum3
1484 MyNamespace::MyClass::MyScopedEnum(456)
1485 */
1486QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, qint64 value, const QMetaObject *meta, const char *name)
1487{
1488 QDebugStateSaver saver(dbg);
1489 dbg.nospace();
1490 QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1491
1492 const int verbosity = dbg.verbosity();
1493 if (verbosity >= QDebug::DefaultVerbosity) {
1494 if (const char *scope = me.scope())
1495 dbg << scope << u"::";
1496 }
1497
1498 const char *key = me.valueToKey(static_cast<int>(value));
1499 const bool scoped = me.isScoped() || verbosity & 1;
1500 if (scoped || !key)
1501 dbg << me.enumName() << (!key ? u"(" : u"::");
1502
1503 if (key)
1504 dbg << key;
1505 else
1506 dbg << value << ')';
1507
1508 return dbg;
1509}
1510
1511/*!
1512 \fn QDebug qt_QMetaEnum_flagDebugOperator(QDebug &, quint64 value, const QMetaObject *, const char *name)
1513 \internal
1514
1515 Formats the given flag \a value for debug output.
1516
1517 The supported verbosity are:
1518
1519 0: Just the key(s):
1520
1521 MyFlag1
1522 MyFlag2|MyFlag3
1523 MyScopedFlag(MyFlag2)
1524 MyScopedFlag(MyFlag2|MyFlag3)
1525
1526 1: Same as 0, but treating all flags as scoped:
1527
1528 MyFlag(MyFlag1)
1529 MyFlag(MyFlag2|MyFlag3)
1530 MyScopedFlag(MyFlag2)
1531 MyScopedFlag(MyFlag2|MyFlag3)
1532
1533 2: The QDebug default. Same as 1, and includes class/namespace scope:
1534
1535 QFlags<MyNamespace::MyClass::MyFlag>(MyFlag1)
1536 QFlags<MyNamespace::MyClass::MyFlag>(MyFlag2|MyFlag3)
1537 QFlags<MyNamespace::MyClass::MyScopedFlag>(MyFlag2)
1538 QFlags<MyNamespace::MyClass::MyScopedFlag>(MyFlag2|MyFlag3)
1539 */
1540QDebug qt_QMetaEnum_flagDebugOperator(QDebug &debug, quint64 value, const QMetaObject *meta, const char *name)
1541{
1542 const int verbosity = debug.verbosity();
1543
1544 QDebugStateSaver saver(debug);
1545 debug.resetFormat();
1546 debug.noquote();
1547 debug.nospace();
1548
1549 const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1550
1551 const bool classScope = verbosity >= QDebug::DefaultVerbosity;
1552 if (classScope) {
1553 debug << u"QFlags<";
1554
1555 if (const char *scope = me.scope())
1556 debug << scope << u"::";
1557 }
1558
1559 const bool enumScope = me.isScoped() || verbosity > QDebug::MinimumVerbosity;
1560 if (enumScope) {
1561 debug << me.enumName();
1562 if (classScope)
1563 debug << '>';
1564 debug << '(';
1565 }
1566
1567 debug << me.valueToKeys(value);
1568
1569 if (enumScope)
1570 debug << ')';
1571
1572 return debug;
1573}
1574
1575/*!
1576 \macro QDebug qDebug()
1577 \relates QDebug
1578 \threadsafe
1579
1580 Returns a QDebug object that logs a debug message to the central message handler.
1581
1582 Example:
1583
1584 \snippet code/src_corelib_global_qglobal.cpp 25
1585
1586 Using qDebug() is an alternative to \l{qDebug(const char *, ...)},
1587 which follows the printf paradigm.
1588
1589 Note that QDebug and the type specific stream operators do add various
1590 formatting to make the debug message easier to read. See the
1591 \l{Formatting Options}{formatting options} documentation for more details.
1592
1593 This function does nothing if \c QT_NO_DEBUG_OUTPUT was defined during
1594 compilation.
1595
1596 \sa {qDebug(const char *, ...)}, qCDebug()
1597*/
1598
1599/*!
1600 \macro QDebug qInfo()
1601 \relates QDebug
1602 \threadsafe
1603
1604 Returns a QDebug object that logs an informational message to the central message handler.
1605
1606 Example:
1607
1608 \snippet code/src_corelib_global_qglobal.cpp qInfo_stream
1609
1610 Using qInfo() is an alternative to \l{qInfo(const char *, ...)},
1611 which follows the printf paradigm.
1612
1613 Note that QDebug and the type specific stream operators do add various
1614 formatting to make the debug message easier to read. See the
1615 \l{Formatting Options}{formatting options} documentation for more details.
1616
1617 This function does nothing if \c QT_NO_INFO_OUTPUT was defined during
1618 compilation.
1619
1620 \sa {qInfo(const char *, ...)}, qCInfo()
1621*/
1622
1623/*!
1624 \macro QDebug qWarning()
1625 \relates QDebug
1626 \threadsafe
1627
1628 Returns a QDebug object that logs a warning message to the central message handler.
1629
1630 Example:
1631
1632 \snippet code/src_corelib_global_qglobal.cpp 27
1633
1634 Using qWarning() is an alternative to \l{qWarning(const char *, ...)},
1635 which follows the printf paradigm.
1636
1637 Note that QDebug and the type specific stream operators do add various
1638 formatting to make the debug message easier to read. See the
1639 \l{Formatting Options}{formatting options} documentation for more details.
1640
1641 This function does nothing if \c QT_NO_WARNING_OUTPUT was defined during
1642 compilation.
1643
1644 For debugging purposes, it is sometimes convenient to let the
1645 program abort for warning messages. This allows you then
1646 to inspect the core dump, or attach a debugger - see also \l{qFatal()}.
1647 To enable this, set the environment variable \c{QT_FATAL_WARNINGS}
1648 to a number \c n. The program terminates then for the n-th warning.
1649 That is, if the environment variable is set to 1, it will terminate
1650 on the first call; if it contains the value 10, it will exit on the 10th
1651 call. Any non-numeric value in the environment variable is equivalent to 1.
1652
1653 \sa {qWarning(const char *, ...)}, qCWarning()
1654*/
1655
1656/*!
1657 \macro QDebug qCritical()
1658 \relates QDebug
1659 \threadsafe
1660
1661 Returns a QDebug object that logs a critical message to the central message handler.
1662
1663 Example:
1664
1665 \snippet code/src_corelib_global_qglobal.cpp 29
1666
1667 Using qCritical() is an alternative to \l{qCritical(const char *, ...)},
1668 which follows the printf paradigm.
1669
1670 Note that QDebug and the type specific stream operators do add various
1671 formatting to make the debug message easier to read. See the
1672 \l{Formatting Options}{formatting options} documentation for more details.
1673
1674 For debugging purposes, it is sometimes convenient to let the
1675 program abort for critical messages. This allows you then
1676 to inspect the core dump, or attach a debugger - see also \l{qFatal()}.
1677 To enable this, set the environment variable \c{QT_FATAL_CRITICALS}
1678 to a number \c n. The program terminates then for the n-th critical
1679 message.
1680 That is, if the environment variable is set to 1, it will terminate
1681 on the first call; if it contains the value 10, it will exit on the 10th
1682 call. Any non-numeric value in the environment variable is equivalent to 1.
1683
1684 \sa {qCritical(const char *, ...)}, qCCritical()
1685*/
1686
1687/*!
1688 \macro QDebug qFatal()
1689 \relates QDebug
1690 \threadsafe
1691
1692 Returns a QDebug object that logs a fatal message to the central message handler.
1693
1694 Using qFatal() is an alternative to \l{qFatal(const char *, ...)},
1695 which follows the printf paradigm.
1696
1697 Note that QDebug and the type specific stream operators do add various
1698 formatting to make the debug message easier to read. See the
1699 \l{Formatting Options}{formatting options} documentation for more details.
1700
1701 If you are using the \b{default message handler}, the returned stream will abort
1702 to create a core dump. On Windows, for debug builds,
1703 this function will report a _CRT_ERROR enabling you to connect a debugger
1704 to the application.
1705
1706 \sa {qFatal(const char *, ...)}, qCFatal()
1707*/
1708
1709#endif // !QT_NO_QOBJECT
1710
1711QT_END_NAMESPACE
const QTextStreamPrivate::Params m_streamParams
Definition qdebug.cpp:1401
QDebugStateSaverPrivate(QDebug::Stream *stream)
Definition qdebug.cpp:1369
Combined button and popup list for selecting options.
QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, qint64 value, const QMetaObject *meta, const char *name)
Definition qdebug.cpp:1486
static void putEscapedString(QTextStreamPrivate *d, const Char *begin, size_t length, bool isUnicode=true)
Definition qdebug.cpp:216
static bool isPrintable(char16_t uc)
Definition qdebug.cpp:211
static QByteArray timeUnit(qint64 num, qint64 den)
Definition qdebug.cpp:367
static bool isPrintable(uchar c)
Definition qdebug.cpp:212
static bool isPrintable(char32_t ucs4)
Definition qdebug.cpp:210
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
Definition qdebug.cpp:1435