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 operator<<(QDebug debug, const std::error_code &code)
579 \relates QDebug
580 \since 6.13
581
582 Writes \a code to \a debug.
583*/
584QDebug operator<<(QDebug debug, const std::error_code &code)
585{
586 const QDebugStateSaver saver(debug);
587 debug.nospace() << "std::error_code(" << code.category().name() << ", "
588 << code.value() << ", " << code.message() << ')';
589 return debug;
590}
591
592/*!
593 \fn QDebug operator<<(QDebug debug, const std::error_condition &condition)
594 \relates QDebug
595 \since 6.13
596
597 Writes \a condition to \a debug.
598*/
599QDebug operator<<(QDebug debug, const std::error_condition &condition)
600{
601 const QDebugStateSaver saver(debug);
602 debug.nospace() << "std::error_condition(" << condition.category().name() << ", "
603 << condition.value() << ", " << condition.message() << ')';
604 return debug;
605}
606
607/*!
608 \fn QDebug::swap(QDebug &other)
609 \since 5.0
610 \memberswap{debug stream instance}
611*/
612
613/*!
614 Resets the stream formatting options, bringing it back to its original constructed state.
615
616 \sa space(), quote()
617 \since 5.4
618*/
619QDebug &QDebug::resetFormat()
620{
621 stream->ts.reset();
622 stream->space = true;
623 stream->noQuotes = false;
624 stream->verbosity = DefaultVerbosity;
625 return *this;
626}
627
628/*!
629 \fn QDebug &QDebug::space()
630
631 Writes a space character to the debug stream and returns a reference to
632 the stream.
633
634 The stream remembers that automatic insertion of spaces is
635 enabled for future writes.
636
637 \sa nospace(), maybeSpace()
638*/
639
640/*!
641 \fn QDebug &QDebug::nospace()
642
643 Disables automatic insertion of spaces and returns a reference to the stream.
644
645 \sa space(), maybeSpace()
646*/
647
648/*!
649 \fn QDebug &QDebug::maybeSpace()
650
651 Writes a space character to the debug stream, depending on the current
652 setting for automatic insertion of spaces, and returns a reference to the stream.
653
654 \sa space(), nospace()
655*/
656
657/*!
658 \fn bool QDebug::autoInsertSpaces() const
659
660 Returns \c true if this QDebug instance will automatically insert spaces
661 between writes.
662
663 \since 5.0
664
665 \sa QDebugStateSaver
666*/
667
668/*!
669 \fn void QDebug::setAutoInsertSpaces(bool b)
670
671 Enables automatic insertion of spaces between writes if \a b is true; otherwise
672 automatic insertion of spaces is disabled.
673
674 \since 5.0
675
676 \sa QDebugStateSaver
677*/
678
679
680/*!
681 \fn bool QDebug::quoteStrings() const
682 \since 6.7
683
684 Returns \c true if this QDebug instance will quote strings streamed into
685 it (which is the default).
686
687 \sa QDebugStateSaver, quote(), noquote(), setQuoteStrings()
688*/
689
690/*!
691 \fn void QDebug::setQuoteStrings(bool b)
692 \since 6.7
693
694 Enables quoting of strings streamed into this QDebug instance if \a b is
695 \c true; otherwise quoting is disabled.
696
697 The default is to quote strings.
698
699 \sa QDebugStateSaver, quote(), noquote(), quoteStrings()
700*/
701
702
703/*!
704 \fn QDebug &QDebug::quote()
705 \since 5.4
706
707 Enables automatic insertion of quotation characters around QChar, QString and QByteArray
708 contents and returns a reference to the stream.
709
710 Quoting is enabled by default.
711
712 \sa noquote(), maybeQuote()
713*/
714
715/*!
716 \fn QDebug &QDebug::noquote()
717 \since 5.4
718
719 Disables automatic insertion of quotation characters around QChar, QString and QByteArray
720 contents and returns a reference to the stream.
721
722 When quoting is disabled, these types are printed without quotation
723 characters and without escaping of non-printable characters.
724
725 \sa quote(), maybeQuote()
726*/
727
728/*!
729 \fn QDebug &QDebug::maybeQuote(char c)
730 \since 5.4
731
732 Writes a character \a c to the debug stream, depending on the
733 current setting for automatic insertion of quotes, and returns a reference to the stream.
734
735 The default character is a double quote \c{"}.
736
737 \sa quote(), noquote()
738*/
739
740/*!
741 \fn int QDebug::verbosity() const
742 \since 5.6
743
744 Returns the verbosity of the debug stream.
745
746 Streaming operators can check the value to decide whether
747 verbose output is desired and print more information depending on the
748 level. Higher values indicate that more information is desired.
749
750 The allowed range is from 0 to 7. The default value is 2.
751
752 \sa setVerbosity(), VerbosityLevel
753*/
754
755/*!
756 \fn void QDebug::setVerbosity(int verbosityLevel)
757 \since 5.6
758
759 Sets the verbosity of the stream to \a verbosityLevel.
760
761 The allowed range is from 0 to 7. The default value is 2.
762
763 \sa verbosity(), VerbosityLevel
764*/
765
766/*!
767 \fn QDebug &QDebug::verbosity(int verbosityLevel)
768 \since 5.13
769
770 Sets the verbosity of the stream to \a verbosityLevel and returns a reference to the stream.
771
772 The allowed range is from 0 to 7. The default value is 2.
773
774 \sa verbosity(), setVerbosity(), VerbosityLevel
775*/
776
777/*!
778 \enum QDebug::VerbosityLevel
779 \since 5.13
780
781 This enum describes the range of verbosity levels.
782
783 \value MinimumVerbosity
784 \value DefaultVerbosity
785 \value MaximumVerbosity
786
787 \sa verbosity(), setVerbosity()
788*/
789
790/*!
791 \fn QDebug &QDebug::operator<<(QChar t)
792
793 Writes the character, \a t, to the stream and returns a reference to the
794 stream. Normally, QDebug prints control characters and non-US-ASCII
795 characters as their C escape sequences or their Unicode value (\\u1234). To
796 print non-printable characters without transformation, enable the noquote()
797 functionality, but note that some QDebug backends may not be 8-bit clean
798 and may not be able to represent \c t.
799*/
800
801/*!
802 \fn QDebug &QDebug::operator<<(bool t)
803
804 Writes the boolean value, \a t, to the stream and returns a reference to the
805 stream.
806*/
807
808/*!
809 \fn QDebug &QDebug::operator<<(char t)
810
811 Writes the character, \a t, to the stream and returns a reference to the
812 stream.
813*/
814
815/*!
816 \fn QDebug &QDebug::operator<<(signed short t)
817
818 Writes the signed short integer, \a t, to the stream and returns a reference
819 to the stream.
820*/
821
822/*!
823 \fn QDebug &QDebug::operator<<(unsigned short t)
824
825 Writes then unsigned short integer, \a t, to the stream and returns a
826 reference to the stream.
827*/
828
829/*!
830 \fn QDebug &QDebug::operator<<(signed int t)
831
832 Writes the signed integer, \a t, to the stream and returns a reference
833 to the stream.
834*/
835
836/*!
837 \fn QDebug &QDebug::operator<<(unsigned int t)
838
839 Writes then unsigned integer, \a t, to the stream and returns a reference to
840 the stream.
841*/
842
843/*!
844 \fn QDebug &QDebug::operator<<(signed long t)
845
846 Writes the signed long integer, \a t, to the stream and returns a reference
847 to the stream.
848*/
849
850/*!
851 \fn QDebug &QDebug::operator<<(unsigned long t)
852
853 Writes then unsigned long integer, \a t, to the stream and returns a reference
854 to the stream.
855*/
856
857/*!
858 \fn QDebug &QDebug::operator<<(qint64 t)
859
860 Writes the signed 64-bit integer, \a t, to the stream and returns a reference
861 to the stream.
862*/
863
864/*!
865 \fn QDebug &QDebug::operator<<(quint64 t)
866
867 Writes then unsigned 64-bit integer, \a t, to the stream and returns a
868 reference to the stream.
869*/
870
871/*!
872 \fn QDebug &QDebug::operator<<(float t)
873
874 Writes the 32-bit floating point number, \a t, to the stream and returns a
875 reference to the stream.
876*/
877
878/*!
879 \fn QDebug &QDebug::operator<<(double t)
880
881 Writes the 64-bit floating point number, \a t, to the stream and returns a
882 reference to the stream.
883*/
884
885/*!
886 \fn QDebug &QDebug::operator<<(const char *t)
887
888 Writes the '\\0'-terminated UTF-8 string, \a t, to the stream and returns a
889 reference to the stream. The string is never quoted or escaped for the
890 output. Note that QDebug buffers internally as UTF-16 and may need to
891 transform to 8-bit using the locale's codec in order to use some backends,
892 which may cause garbled output (mojibake). Restricting to US-ASCII strings
893 is recommended.
894*/
895
896/*!
897 \fn QDebug &QDebug::operator<<(const char16_t *t)
898 \since 6.0
899
900 Writes the u'\\0'-terminated UTF-16 string, \a t, to the stream and returns
901 a reference to the stream. The string is never quoted or escaped for the
902 output. Note that QDebug buffers internally as UTF-16 and may need to
903 transform to 8-bit using the locale's codec in order to use some backends,
904 which may cause garbled output (mojibake). Restricting to US-ASCII strings
905 is recommended.
906*/
907
908/*!
909 \fn QDebug &QDebug::operator<<(char16_t t)
910 \since 5.5
911
912 Writes the UTF-16 character, \a t, to the stream and returns a reference
913 to the stream.
914*/
915
916/*!
917 \fn QDebug &QDebug::operator<<(char32_t t)
918 \since 5.5
919
920 Writes the UTF-32 character, \a t, to the stream and returns a reference
921 to the stream.
922*/
923
924/*!
925 \fn QDebug &QDebug::operator<<(const QString &t)
926
927 Writes the string, \a t, to the stream and returns a reference to the
928 stream. Normally, QDebug prints the string inside quotes and transforms
929 non-printable characters to their Unicode values (\\u1234).
930
931 To print non-printable characters without transformation, enable the
932 noquote() functionality. Note that some QDebug backends might not be 8-bit
933 clean.
934
935 Output examples:
936 \snippet code/src_corelib_io_qdebug.cpp 0
937*/
938
939/*!
940 \since 5.10
941 \fn QDebug &QDebug::operator<<(QStringView s)
942
943 Writes the string view, \a s, to the stream and returns a reference to the
944 stream. Normally, QDebug prints the string inside quotes and transforms
945 non-printable characters to their Unicode values (\\u1234).
946
947 To print non-printable characters without transformation, enable the
948 noquote() functionality. Note that some QDebug backends might not be 8-bit
949 clean.
950
951 See the QString overload for examples.
952*/
953
954/*!
955 \since 6.0
956 \fn QDebug &QDebug::operator<<(QUtf8StringView s)
957
958 Writes the string view, \a s, to the stream and returns a reference to the
959 stream.
960
961 Normally, QDebug prints the data inside quotes and transforms control or
962 non-US-ASCII characters to their C escape sequences (\\xAB). This way, the
963 output is always 7-bit clean and the string can be copied from the output
964 and pasted back into C++ sources, if necessary.
965
966 To print non-printable characters without transformation, enable the
967 noquote() functionality. Note that some QDebug backends might not be 8-bit
968 clean.
969*/
970
971/*!
972 \fn QDebug &QDebug::operator<<(QLatin1StringView t)
973
974 Writes the string, \a t, to the stream and returns a reference to the
975 stream. Normally, QDebug prints the string inside quotes and transforms
976 non-printable characters to their Unicode values (\\u1234).
977
978 To print non-printable characters without transformation, enable the
979 noquote() functionality. Note that some QDebug backends might not be 8-bit
980 clean.
981
982 See the QString overload for examples.
983*/
984
985/*!
986 \fn QDebug &QDebug::operator<<(const QByteArray &t)
987
988 Writes the byte array, \a t, to the stream and returns a reference to the
989 stream. Normally, QDebug prints the array inside quotes and transforms
990 control or non-US-ASCII characters to their C escape sequences (\\xAB). This
991 way, the output is always 7-bit clean and the string can be copied from the
992 output and pasted back into C++ sources, if necessary.
993
994 To print non-printable characters without transformation, enable the
995 noquote() functionality. Note that some QDebug backends might not be 8-bit
996 clean.
997
998 Output examples:
999 \snippet code/src_corelib_io_qdebug.cpp 1
1000
1001 Note how QDebug needed to close and reopen the string in the way C and C++
1002 languages concatenate string literals so that the letter 'b' is not
1003 interpreted as part of the previous hexadecimal escape sequence.
1004*/
1005
1006/*!
1007 \since 6.0
1008 \fn QDebug &QDebug::operator<<(QByteArrayView t)
1009
1010 Writes the data of the observed byte array, \a t, to the stream and returns
1011 a reference to the stream.
1012
1013 Normally, QDebug prints the data inside quotes and transforms control or
1014 non-US-ASCII characters to their C escape sequences (\\xAB). This way, the
1015 output is always 7-bit clean and the string can be copied from the output
1016 and pasted back into C++ sources, if necessary.
1017
1018 To print non-printable characters without transformation, enable the
1019 noquote() functionality. Note that some QDebug backends might not be 8-bit
1020 clean.
1021
1022 See the QByteArray overload for examples.
1023*/
1024
1025/*!
1026 \fn QDebug &QDebug::operator<<(const void *t)
1027
1028 Writes a pointer, \a t, to the stream and returns a reference to the stream.
1029*/
1030
1031/*!
1032 \fn QDebug &QDebug::operator<<(QTextStreamFunction f)
1033 \internal
1034*/
1035
1036/*!
1037 \fn QDebug &QDebug::operator<<(QTextStreamManipulator m)
1038 \internal
1039*/
1040
1041/*!
1042 \fn template <typename T, QDebug::if_ordering_type<T>> QDebug::operator<<(QDebug debug, T t)
1043 \since 6.9
1044 Prints the Qt or std ordering value \a t to the \a debug object.
1045
1046 \constraints \c T is one of <Qt/Std>::<weak/partial/strong>_ordering.
1047*/
1048
1049/*!
1050 \since 6.5
1051 \fn template <typename Char, typename...Args> QDebug &QDebug::operator<<(const std::basic_string<Char, Args...> &s)
1052 \fn template <typename Char, typename...Args> QDebug &QDebug::operator<<(std::basic_string_view<Char, Args...> s)
1053
1054 Writes the string or string-view \a s to the stream and returns a reference
1055 to the stream.
1056
1057 These operators only participate in overload resolution if \c Char is one of
1058 \list
1059 \li char
1060 \li char8_t (C++20 only)
1061 \li char16_t
1062 \li char32_t
1063 \li wchar_t
1064 \endlist
1065*/
1066
1067/*!
1068 \since 6.6
1069 \fn template <typename Rep, typename Period> QDebug &QDebug::operator<<(std::chrono::duration<Rep, Period> duration)
1070
1071 Prints the time duration \a duration to the stream and returns a reference
1072 to the stream. The printed string is the numeric representation of the
1073 period followed by the time unit, similar to what the C++ Standard Library
1074 would produce with \c{std::ostream}.
1075
1076 The unit is not localized.
1077*/
1078
1079/*!
1080 \fn template <typename T, QDebug::if_qint128<T>> QDebug::operator<<(T i)
1081 \fn template <typename T, QDebug::if_quint128<T>> QDebug::operator<<(T i)
1082 \since 6.7
1083
1084 Prints the textual representation of the 128-bit integer \a i.
1085
1086 \note This operator is only available if Qt supports 128-bit integer types.
1087 If 128-bit integer types are available in your build, but the Qt libraries
1088 were compiled without, the operator will print a warning instead.
1089
1090 \note Because the operator is a function template, no implicit conversions
1091 are performed on its argument. It must be exactly qint128/quint128.
1092
1093 \sa QT_SUPPORTS_INT128
1094*/
1095
1096/*!
1097 \fn template <class T> QString QDebug::toString(const T &object)
1098 \since 6.0
1099
1100 Streams \a object into a QDebug instance that operates on a string,
1101 and then returns that string.
1102
1103 This function is useful for cases where you need the textual representation
1104 of an object for debugging, but cannot use \c {operator<<}. For example:
1105
1106 \snippet code/src_corelib_io_qdebug.cpp toString
1107
1108 The string is streamed using \l nospace().
1109
1110 \sa toBytes()
1111*/
1112
1113/*! \internal */
1114QString QDebug::toStringImpl(StreamTypeErased s, const void *obj)
1115{
1116 QString result;
1117 {
1118 QDebug d(&result);
1119 s(d.nospace(), obj);
1120 }
1121 return result;
1122}
1123
1124/*!
1125 \fn template <class T> QByteArray QDebug::toBytes(const T &object)
1126 \since 6.9
1127
1128 This is equivalent to passing \a object to
1129 \c{QDebug::toString(object).toUtf8()}, but more efficient.
1130
1131 \sa toString()
1132*/
1133
1134/*! \internal */
1135QByteArray QDebug::toBytesImpl(StreamTypeErased s, const void *obj)
1136{
1137 QByteArray result;
1138 {
1139 QDebug d(&result);
1140 s(d.nospace(), obj);
1141 }
1142 return result;
1143}
1144
1145/*!
1146 \internal
1147 \since 6.9
1148
1149 Outputs a heterogeneous product type (pair, tuple, or anything that
1150 implements the Tuple Protocol). The class name is described by "\a ns
1151 \c{::} \a what", while the addresses of the \a n elements are stored in the
1152 array \a data. The formatters are stored in the array \a ops.
1153
1154 If \a ns is empty, only \a what is used.
1155*/
1156QDebug &QDebug::putTupleLikeImplImpl(const char *ns, const char *what,
1157 size_t n, StreamTypeErased *ops, const void **data)
1158{
1159 const QDebugStateSaver saver(*this);
1160 nospace();
1161 if (ns && *ns)
1162 *this << ns << "::";
1163 *this << what << '(';
1164 while (n--) {
1165 (*ops++)(*this, *data++);
1166 if (n)
1167 *this << ", ";
1168 }
1169 return *this << ')';
1170}
1171
1172/*!
1173 \fn template <class T> QDebug operator<<(QDebug debug, const QList<T> &list)
1174 \relates QDebug
1175
1176 Writes the contents of \a list to \a debug. \c T needs to
1177 support streaming into QDebug.
1178*/
1179
1180/*!
1181 \fn template <class T, qsizetype P> QDebug operator<<(QDebug debug, const QVarLengthArray<T,P> &array)
1182 \relates QDebug
1183 \since 6.3
1184
1185 Writes the contents of \a array to \a debug. \c T needs to
1186 support streaming into QDebug.
1187*/
1188
1189/*!
1190 \fn template <typename T, typename Alloc> QDebug operator<<(QDebug debug, const std::list<T, Alloc> &vec)
1191 \relates QDebug
1192 \since 5.7
1193
1194 Writes the contents of list \a vec to \a debug. \c T needs to
1195 support streaming into QDebug.
1196*/
1197
1198/*!
1199 \fn template <typename T, typename Alloc> QDebug operator<<(QDebug debug, const std::vector<T, Alloc> &vec)
1200 \relates QDebug
1201 \since 5.7
1202
1203 Writes the contents of vector \a vec to \a debug. \c T needs to
1204 support streaming into QDebug.
1205*/
1206
1207/*!
1208 \fn template <typename T, std::size_t N> QDebug operator<<(QDebug debug, const std::array<T, N> &array)
1209 \relates QDebug
1210 \since 6.9
1211
1212 Writes the contents of \a array to \a debug. \c T needs to
1213 support streaming into QDebug.
1214*/
1215
1216/*!
1217 \fn template <typename T> QDebug operator<<(QDebug debug, const QSet<T> &set)
1218 \relates QDebug
1219
1220 Writes the contents of \a set to \a debug. \c T needs to
1221 support streaming into QDebug.
1222*/
1223
1224/*!
1225 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QMap<Key, T> &map)
1226 \relates QDebug
1227
1228 Writes the contents of \a map to \a debug. Both \c Key and
1229 \c T need to support streaming into QDebug.
1230*/
1231
1232/*!
1233 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QMultiMap<Key, T> &map)
1234 \relates QDebug
1235
1236 Writes the contents of \a map to \a debug. Both \c Key and
1237 \c T need to support streaming into QDebug.
1238*/
1239
1240/*!
1241 \fn template <typename Key, typename T, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::map<Key, T, Compare, Alloc> &map)
1242 \relates QDebug
1243 \since 5.7
1244
1245 Writes the contents of \a map to \a debug. Both \c Key and
1246 \c T need to support streaming into QDebug.
1247*/
1248
1249/*!
1250 \fn template <typename Key, typename T, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::multimap<Key, T, Compare, Alloc> &map)
1251 \relates QDebug
1252 \since 5.7
1253
1254 Writes the contents of \a map to \a debug. Both \c Key and
1255 \c T need to support streaming into QDebug.
1256*/
1257
1258/*!
1259 \fn template <typename Key, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::multiset<Key, Compare, Alloc> &multiset)
1260 \relates QDebug
1261 \since 6.9
1262
1263 Writes the contents of \a multiset to \a debug. The \c Key type
1264 needs to support streaming into QDebug.
1265*/
1266
1267/*!
1268 \fn template <typename Key, typename Compare, typename Alloc> QDebug operator<<(QDebug debug, const std::set<Key, Compare, Alloc> &set)
1269 \relates QDebug
1270 \since 6.9
1271
1272 Writes the contents of \a set to \a debug. The \c Key type
1273 needs to support streaming into QDebug.
1274*/
1275
1276/*!
1277 \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)
1278 \relates QDebug
1279 \since 6.9
1280
1281 Writes the contents of \a map to \a debug. Both \c Key and
1282 \c T need to support streaming into QDebug.
1283*/
1284
1285/*!
1286 \fn template <typename Key, typename Hash, typename KeyEqual, typename Alloc> QDebug operator<<(QDebug debug, const std::unordered_set<Key, Hash, KeyEqual, Alloc> &unordered_set)
1287 \relates QDebug
1288 \since 6.9
1289
1290 Writes the contents of \a unordered_set to \a debug. The \c Key type
1291 needs to support streaming into QDebug.
1292*/
1293
1294/*!
1295 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QHash<Key, T> &hash)
1296 \relates QDebug
1297
1298 Writes the contents of \a hash to \a debug. Both \c Key and
1299 \c T need to support streaming into QDebug.
1300*/
1301
1302/*!
1303 \fn template <class Key, class T> QDebug operator<<(QDebug debug, const QMultiHash<Key, T> &hash)
1304 \relates QDebug
1305
1306 Writes the contents of \a hash to \a debug. Both \c Key and
1307 \c T need to support streaming into QDebug.
1308*/
1309
1310/*!
1311 \fn template <class...Ts, QDebug::if_streamable<Ts...>> QDebug &QDebug::operator<<(const std::tuple<Ts...> &tuple)
1312 \since 6.9
1313
1314 Writes the contents of \a tuple to the stream. All \c Ts... need to support
1315 streaming into QDebug.
1316*/
1317
1318/*!
1319 \fn template <class T1, class T2> QDebug operator<<(QDebug debug, const std::pair<T1, T2> &pair)
1320 \relates QDebug
1321
1322 Writes the contents of \a pair to \a debug. Both \c T1 and
1323 \c T2 need to support streaming into QDebug.
1324*/
1325
1326/*!
1327 \since 6.7
1328 \fn template <class T, QDebug::if_streamable<T>> QDebug::operator<<(const std::optional<T> &opt)
1329
1330 Writes the contents of \a opt (or \c nullopt if not set) to this stream.
1331 \c T needs to support streaming into QDebug.
1332*/
1333
1334/*!
1335 \fn template <typename T> QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
1336 \relates QDebug
1337
1338 Writes the contents of \a cache to \a debug. \c T needs to
1339 support streaming into QDebug.
1340*/
1341
1342/*!
1343 \fn template<typename T> QDebug operator<<(QDebug debug, const QFlags<T> &flags)
1344 \relates QDebug
1345 \since 4.7
1346
1347 Writes \a flags to \a debug.
1348*/
1349
1350/*!
1351 \fn template<typename T> QDebug operator<<(QDebug debug, const QSharedPointer<T> &ptr)
1352 \relates QSharedPointer
1353 \since 5.7
1354
1355 Writes the pointer tracked by \a ptr into the debug object \a debug for
1356 debugging purposes.
1357
1358 \sa {Debugging Techniques}
1359*/
1360
1361/*!
1362 \fn QDebug &QDebug::operator<<(std::nullptr_t)
1363 \internal
1364 */
1365
1366/*!
1367 \since 6.7
1368 \fn QDebug &QDebug::operator<<(std::nullopt_t)
1369
1370 Writes nullopt to the stream.
1371*/
1372
1373/*!
1374 \class QDebugStateSaver
1375 \inmodule QtCore
1376 \brief Convenience class for custom QDebug operators.
1377
1378 Saves the settings used by QDebug, and restores them upon destruction,
1379 then calls \l {QDebug::maybeSpace()}{maybeSpace()}, to separate arguments with a space if
1380 \l {QDebug::autoInsertSpaces()}{autoInsertSpaces()} was true at the time of constructing the QDebugStateSaver.
1381
1382 The automatic insertion of spaces between writes is one of the settings
1383 that QDebugStateSaver stores for the duration of the current block.
1384
1385 The settings of the internal QTextStream are also saved and restored,
1386 so that using << Qt::hex in a QDebug operator doesn't affect other QDebug
1387 operators.
1388
1389 QDebugStateSaver is typically used in the implementation of an operator<<() for debugging:
1390
1391 \snippet customtype/customtypeexample.cpp custom type streaming operator
1392
1393 \since 5.1
1394*/
1395
1397{
1398public:
1408 {
1409 const bool currentSpaces = m_stream->space;
1410 if (currentSpaces && !m_spaces)
1411 if (m_stream->buffer.endsWith(u' '))
1412 m_stream->buffer.chop(1);
1413
1414 m_stream->space = m_spaces;
1415 m_stream->noQuotes = m_noQuotes;
1416 m_stream->ts.d_ptr->params = m_streamParams;
1417 m_stream->verbosity = m_verbosity;
1418
1419 if (!currentSpaces && m_spaces)
1420 m_stream->ts << ' ';
1421 }
1422
1424
1425 // QDebug state
1426 const bool m_spaces;
1427 const bool m_noQuotes;
1428 const int m_verbosity;
1429
1430 // QTextStream state
1432};
1433
1434
1435/*!
1436 Creates a QDebugStateSaver instance, which saves the settings
1437 currently used by \a dbg.
1438
1439 \sa QDebug::setAutoInsertSpaces(), QDebug::autoInsertSpaces()
1440*/
1441QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
1442 : d(new QDebugStateSaverPrivate(dbg.stream))
1443{
1444}
1445
1446/*!
1447 Destroys a QDebugStateSaver instance, which restores the settings
1448 used when the QDebugStateSaver instance was created.
1449
1450 \sa QDebug::setAutoInsertSpaces(), QDebug::autoInsertSpaces()
1451*/
1452QDebugStateSaver::~QDebugStateSaver()
1453{
1454 d->restoreState();
1455}
1456
1457/*!
1458 \internal
1459
1460 Specialization of the primary template in qdebug.h to out-of-line
1461 the common case of QFlags<T>::Int being 32-bit.
1462
1463 Just call the generic version so the two don't get out of sync.
1464*/
1465void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
1466{
1467 qt_QMetaEnum_flagDebugOperator(debug, sizeofT, quint64(value));
1468}
1469
1470/*!
1471 \internal
1472 Ditto, for 64-bit.
1473*/
1474void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value)
1475{
1476 qt_QMetaEnum_flagDebugOperator<quint64>(debug, sizeofT, value);
1477}
1478
1479
1480#ifndef QT_NO_QOBJECT
1481/*!
1482 \internal
1483
1484 Formats the given enum \a value for debug output.
1485
1486 The supported verbosity are:
1487
1488 0: Just the key, or value with enum name if no key is found:
1489
1490 MyEnum2
1491 MyEnum(123)
1492 MyScopedEnum::Enum3
1493 MyScopedEnum(456)
1494
1495 1: Same as 0, but treating all enums as scoped:
1496
1497 MyEnum::MyEnum2
1498 MyEnum(123)
1499 MyScopedEnum::Enum3
1500 MyScopedEnum(456)
1501
1502 2: The QDebug default. Same as 0, and includes class/namespace scope:
1503
1504 MyNamespace::MyClass::MyEnum2
1505 MyNamespace::MyClass::MyEnum(123)
1506 MyNamespace::MyClass::MyScopedEnum::Enum3
1507 MyNamespace::MyClass::MyScopedEnum(456)
1508
1509 3: Same as 2, but treating all enums as scoped:
1510
1511 MyNamespace::MyClass::MyEnum::MyEnum2
1512 MyNamespace::MyClass::MyEnum(123)
1513 MyNamespace::MyClass::MyScopedEnum::Enum3
1514 MyNamespace::MyClass::MyScopedEnum(456)
1515 */
1516QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, qint64 value, const QMetaObject *meta, const char *name)
1517{
1518 QDebugStateSaver saver(dbg);
1519 dbg.nospace();
1520 QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1521
1522 const int verbosity = dbg.verbosity();
1523 if (verbosity >= QDebug::DefaultVerbosity) {
1524 if (const char *scope = me.scope())
1525 dbg << scope << u"::";
1526 }
1527
1528 const char *key = me.valueToKey(static_cast<int>(value));
1529 const bool scoped = me.isScoped() || verbosity & 1;
1530 if (scoped || !key)
1531 dbg << me.enumName() << (!key ? u"(" : u"::");
1532
1533 if (key)
1534 dbg << key;
1535 else
1536 dbg << value << ')';
1537
1538 return dbg;
1539}
1540
1541/*!
1542 \fn QDebug qt_QMetaEnum_flagDebugOperator(QDebug &, quint64 value, const QMetaObject *, const char *name)
1543 \internal
1544
1545 Formats the given flag \a value for debug output.
1546
1547 The supported verbosity are:
1548
1549 0: Just the key(s):
1550
1551 MyFlag1
1552 MyFlag2|MyFlag3
1553 MyScopedFlag(MyFlag2)
1554 MyScopedFlag(MyFlag2|MyFlag3)
1555
1556 1: Same as 0, but treating all flags as scoped:
1557
1558 MyFlag(MyFlag1)
1559 MyFlag(MyFlag2|MyFlag3)
1560 MyScopedFlag(MyFlag2)
1561 MyScopedFlag(MyFlag2|MyFlag3)
1562
1563 2: The QDebug default. Same as 1, and includes class/namespace scope:
1564
1565 QFlags<MyNamespace::MyClass::MyFlag>(MyFlag1)
1566 QFlags<MyNamespace::MyClass::MyFlag>(MyFlag2|MyFlag3)
1567 QFlags<MyNamespace::MyClass::MyScopedFlag>(MyFlag2)
1568 QFlags<MyNamespace::MyClass::MyScopedFlag>(MyFlag2|MyFlag3)
1569 */
1570QDebug qt_QMetaEnum_flagDebugOperator(QDebug &debug, quint64 value, const QMetaObject *meta, const char *name)
1571{
1572 const int verbosity = debug.verbosity();
1573
1574 QDebugStateSaver saver(debug);
1575 debug.resetFormat();
1576 debug.noquote();
1577 debug.nospace();
1578
1579 const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1580
1581 const bool classScope = verbosity >= QDebug::DefaultVerbosity;
1582 if (classScope) {
1583 debug << u"QFlags<";
1584
1585 if (const char *scope = me.scope())
1586 debug << scope << u"::";
1587 }
1588
1589 const bool enumScope = me.isScoped() || verbosity > QDebug::MinimumVerbosity;
1590 if (enumScope) {
1591 debug << me.enumName();
1592 if (classScope)
1593 debug << '>';
1594 debug << '(';
1595 }
1596
1597 debug << me.valueToKeys(value);
1598
1599 if (enumScope)
1600 debug << ')';
1601
1602 return debug;
1603}
1604
1605/*!
1606 \macro QDebug qDebug()
1607 \relates QDebug
1608 \threadsafe
1609
1610 Returns a QDebug object that logs a debug message to the central message handler.
1611
1612 Example:
1613
1614 \snippet code/src_corelib_global_qglobal.cpp 25
1615
1616 Using qDebug() is an alternative to \l{qDebug(const char *, ...)},
1617 which follows the printf paradigm.
1618
1619 Note that QDebug and the type specific stream operators do add various
1620 formatting to make the debug message easier to read. See the
1621 \l{Formatting Options}{formatting options} documentation for more details.
1622
1623 This function does nothing if \c QT_NO_DEBUG_OUTPUT was defined during
1624 compilation.
1625
1626 \sa {qDebug(const char *, ...)}, qCDebug()
1627*/
1628
1629/*!
1630 \macro QDebug qInfo()
1631 \relates QDebug
1632 \threadsafe
1633
1634 Returns a QDebug object that logs an informational message to the central message handler.
1635
1636 Example:
1637
1638 \snippet code/src_corelib_global_qglobal.cpp qInfo_stream
1639
1640 Using qInfo() is an alternative to \l{qInfo(const char *, ...)},
1641 which follows the printf paradigm.
1642
1643 Note that QDebug and the type specific stream operators do add various
1644 formatting to make the debug message easier to read. See the
1645 \l{Formatting Options}{formatting options} documentation for more details.
1646
1647 This function does nothing if \c QT_NO_INFO_OUTPUT was defined during
1648 compilation.
1649
1650 \sa {qInfo(const char *, ...)}, qCInfo()
1651*/
1652
1653/*!
1654 \macro QDebug qWarning()
1655 \relates QDebug
1656 \threadsafe
1657
1658 Returns a QDebug object that logs a warning message to the central message handler.
1659
1660 Example:
1661
1662 \snippet code/src_corelib_global_qglobal.cpp 27
1663
1664 Using qWarning() is an alternative to \l{qWarning(const char *, ...)},
1665 which follows the printf paradigm.
1666
1667 Note that QDebug and the type specific stream operators do add various
1668 formatting to make the debug message easier to read. See the
1669 \l{Formatting Options}{formatting options} documentation for more details.
1670
1671 This function does nothing if \c QT_NO_WARNING_OUTPUT was defined during
1672 compilation.
1673
1674 For debugging purposes, it is sometimes convenient to let the
1675 program abort for warning 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_WARNINGS}
1678 to a number \c n. The program terminates then for the n-th warning.
1679 That is, if the environment variable is set to 1, it will terminate
1680 on the first call; if it contains the value 10, it will exit on the 10th
1681 call. Any non-numeric value in the environment variable is equivalent to 1.
1682
1683 \sa {qWarning(const char *, ...)}, qCWarning()
1684*/
1685
1686/*!
1687 \macro QDebug qCritical()
1688 \relates QDebug
1689 \threadsafe
1690
1691 Returns a QDebug object that logs a critical message to the central message handler.
1692
1693 Example:
1694
1695 \snippet code/src_corelib_global_qglobal.cpp 29
1696
1697 Using qCritical() is an alternative to \l{qCritical(const char *, ...)},
1698 which follows the printf paradigm.
1699
1700 Note that QDebug and the type specific stream operators do add various
1701 formatting to make the debug message easier to read. See the
1702 \l{Formatting Options}{formatting options} documentation for more details.
1703
1704 For debugging purposes, it is sometimes convenient to let the
1705 program abort for critical messages. This allows you then
1706 to inspect the core dump, or attach a debugger - see also \l{qFatal()}.
1707 To enable this, set the environment variable \c{QT_FATAL_CRITICALS}
1708 to a number \c n. The program terminates then for the n-th critical
1709 message.
1710 That is, if the environment variable is set to 1, it will terminate
1711 on the first call; if it contains the value 10, it will exit on the 10th
1712 call. Any non-numeric value in the environment variable is equivalent to 1.
1713
1714 \sa {qCritical(const char *, ...)}, qCCritical()
1715*/
1716
1717/*!
1718 \macro QDebug qFatal()
1719 \relates QDebug
1720 \threadsafe
1721
1722 Returns a QDebug object that logs a fatal message to the central message handler.
1723
1724 Using qFatal() is an alternative to \l{qFatal(const char *, ...)},
1725 which follows the printf paradigm.
1726
1727 Note that QDebug and the type specific stream operators do add various
1728 formatting to make the debug message easier to read. See the
1729 \l{Formatting Options}{formatting options} documentation for more details.
1730
1731 If you are using the \b{default message handler}, the returned stream will abort
1732 to create a core dump. On Windows, for debug builds,
1733 this function will report a _CRT_ERROR enabling you to connect a debugger
1734 to the application.
1735
1736 \sa {qFatal(const char *, ...)}, qCFatal()
1737*/
1738
1739#endif // !QT_NO_QOBJECT
1740
1741QT_END_NAMESPACE
const QTextStreamPrivate::Params m_streamParams
Definition qdebug.cpp:1431
QDebugStateSaverPrivate(QDebug::Stream *stream)
Definition qdebug.cpp:1399
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:1516
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
QDebug operator<<(QDebug debug, const std::error_code &code)
Definition qdebug.cpp:584
static bool isPrintable(char32_t ucs4)
Definition qdebug.cpp:210
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
Definition qdebug.cpp:1465
QDebug operator<<(QDebug debug, const std::error_condition &condition)
Definition qdebug.cpp:599