7#include "private/qdebug_p.h"
9#include <private/qlogging_p.h>
10#include <private/qtextstream_p.h>
11#include <private/qtools_p.h>
19using namespace QtMiscUtils;
22
23
24
25
26QByteArray QtDebugUtils::toPrintable(
const char *data, qint64 len, qsizetype maxSize)
32 for (qsizetype i = 0; i < qMin(len, maxSize); ++i) {
34 if (isAsciiPrintable(c)) {
51 toHexLower(uchar(c) / 16),
52 toHexLower(uchar(c) % 16),
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
121
122
123
124
127
128
129
130
133
134
135
136
137
138
139
140
141
142
143
144
145
146
149
150
151
152
155
156
157
158
161
162
163
164
165
168
169
170
171
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,
188
189
190void QDebug::putUcs4(uint ucs4)
194 stream->ts <<
"\\x" << Qt::hex << ucs4 << Qt::reset;
195 }
else if (ucs4 < 0x80) {
196 stream->ts <<
char(ucs4);
199 stream->ts <<
"\\u" << qSetFieldWidth(4);
201 stream->ts <<
"\\U" << qSetFieldWidth(8);
202 stream->ts << Qt::hex << qSetPadChar(u'0') << ucs4 << Qt::reset;
210static inline bool isPrintable(
char32_t ucs4) {
return QChar::isPrint(ucs4); }
211static inline bool isPrintable(
char16_t uc) {
return QChar::isPrint(uc); }
213{
return isAsciiPrintable(c); }
215template <
typename Char>
216static inline void putEscapedString(QTextStreamPrivate *d,
const Char *begin, size_t length,
bool isUnicode =
true)
218 constexpr char16_t quotes[] = uR"("")";
219 constexpr char16_t quote = quotes[0];
222 bool lastWasHexEscape =
false;
223 const Char *end = begin + length;
224 for (
const Char *p = begin; p != end; ++p) {
226 if (Q_UNLIKELY(lastWasHexEscape)) {
227 if (fromHex(*p) != -1) {
231 lastWasHexEscape =
false;
234 if constexpr (
sizeof(Char) ==
sizeof(QChar)) {
236 qsizetype runLength = 0;
237 while (p + runLength != end &&
238 isPrintable(p[runLength]) && p[runLength] !=
'\\' && p[runLength] !=
'"')
241 d->write(QStringView{p, runLength});
245 }
else if (isPrintable(*p) && *p !=
'\\' && *p !=
'"') {
246 d->write(
char16_t{uchar(*p)});
251 qsizetype buflen = 2;
252 char16_t buf[
std::char_traits<
char>::length(
"\\U12345678")];
279 buf[2] = toHexUpper(uchar(*p) >> 4);
280 buf[3] = toHexUpper(uchar(*p));
282 lastWasHexEscape =
true;
285 if (QChar::isHighSurrogate(*p)) {
286 if ((p + 1) != end && QChar::isLowSurrogate(p[1])) {
288 char32_t ucs4 = QChar::surrogateToUcs4(*p, p[1]);
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);
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);
317 d->write(QStringView{buf, buflen});
324
325
326
327void QDebug::putString(
const QChar *begin, size_t length)
329 if (stream->noQuotes) {
332 stream->ts.d_ptr->putString(QStringView{begin, qsizetype(length)});
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);
342
343
344
345void QDebug::putByteArray(
const char *begin, size_t length, Latin1Content content)
347 if (stream->noQuotes) {
351 case Latin1Content::ContainsLatin1:
352 stream->ts.d_ptr->putString(QLatin1StringView{begin, qsizetype(length)});
354 case Latin1Content::ContainsBinary:
355 stream->ts.d_ptr->putString(QUtf8StringView{begin, qsizetype(length)});
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);
369 using namespace std::chrono;
372 if (num == 1 && den > 1) {
375 auto tryprefix = [&](
auto d,
char c) {
376 static_assert(
decltype(d)::num == 1,
"not an SI prefix");
377 if (den ==
decltype(d)::den)
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');
389 tryprefix(std::centi{},
'c');
390 tryprefix(std::deci{},
'd');
392 char unit[3] = { prefix,
's' };
393 return QByteArray(unit,
sizeof(unit) - 1);
397 const char *unit =
nullptr;
398 if (num > 1 && den == 1) {
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)
405 num /=
decltype(d)::period::num;
407 tryunit(years{},
"yr");
408 tryunit(weeks{},
"wk");
409 tryunit(days{},
"d");
410 tryunit(hours{},
"h");
411 tryunit(minutes{},
"min");
416 if (num == 1 && den == 1)
418 if (Q_UNLIKELY(num < 1 || den < 1))
419 return QString::asprintf(
"<invalid time unit %lld/%lld>", num, den).toLatin1();
423 char buf[2 * (std::numeric_limits<qint64>::digits10 + 2) + 10];
425 auto appendChar = [&](
char c) {
426 Q_ASSERT(len <
sizeof(buf));
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));
433 len += std::snprintf(buf + len,
sizeof(buf) - len,
"%lld", value);
442 memcpy(buf + len, unit, strlen(unit));
443 return QByteArray(buf, len + strlen(unit));
447
448
449
450
451void QDebug::putTimeUnit(qint64 num, qint64 den)
453 stream->ts << timeUnit(num, den);
458#ifdef QT_SUPPORTS_INT128
460constexpr char Q_INT128_MIN_STR[] =
"-170141183460469231731687303715884105728";
462constexpr int Int128BufferSize =
sizeof(Q_INT128_MIN_STR);
463using Int128Buffer = std::array<
char, Int128BufferSize>;
466static char *i128ToStringHelper(Int128Buffer &buffer, quint128 n)
468 auto dst = buffer.data() + buffer.size();
474 *--dst =
"0123456789"[n % 10];
483static const char *int128Warning()
485 const char *msg =
"Qt was not compiled with int128 support.";
493
494
495
496
497void QDebug::putInt128([[maybe_unused]]
const void *p)
499#ifdef QT_SUPPORTS_INT128
502 memcpy(&i, p,
sizeof(i));
503 if (i == Q_INT128_MIN) {
505 stream->ts << Q_INT128_MIN_STR;
508 auto dst = i128ToStringHelper(buffer, i < 0 ? -i : i);
515 stream->ts << int128Warning();
519
520
521
522
523void QDebug::putUInt128([[maybe_unused]]
const void *p)
525#ifdef QT_SUPPORTS_INT128
528 memcpy(&i, p,
sizeof(i));
530 stream->ts << i128ToStringHelper(buffer, i);
533 stream->ts << int128Warning();
537
538
539
540
541
542
543void QDebug::putQtOrdering(QtOrderingPrivate::QtOrderingTypeFlag flags, Qt::partial_ordering order)
545 using QtOrderingPrivate::QtOrderingType;
547 if ((flags & QtOrderingType::StdOrder) == QtOrderingType::StdOrder)
549 else if ((flags & QtOrderingType::QtOrder) == QtOrderingType::QtOrder)
553 const bool isStrong = ((flags & QtOrderingType::Strong) == QtOrderingType::Strong);
556 else if ((flags & QtOrderingType::Weak) == QtOrderingType::Weak)
558 else if ((flags & QtOrderingType::Partial) == QtOrderingType::Partial)
560 result +=
"_ordering::";
562 if (order == Qt::partial_ordering::equivalent) {
566 result +=
"equivalent";
567 }
else if (order == Qt::partial_ordering::greater) {
569 }
else if (order == Qt::partial_ordering::less) {
572 result +=
"unordered";
574 stream->ts << result.data();
578
579
580
581
582
583
586 const QDebugStateSaver saver(debug);
587 debug.nospace() <<
"std::error_code(" << code.category().name() <<
", "
588 << code.value() <<
", " << code.message() <<
')';
593
594
595
596
597
598
601 const QDebugStateSaver saver(debug);
602 debug.nospace() <<
"std::error_condition(" << condition.category().name() <<
", "
603 << condition.value() <<
", " << condition.message() <<
')';
608
609
610
611
614
615
616
617
618
619QDebug &QDebug::resetFormat()
622 stream->space =
true;
623 stream->noQuotes =
false;
624 stream->verbosity = DefaultVerbosity;
629
630
631
632
633
634
635
636
637
638
641
642
643
644
645
646
649
650
651
652
653
654
655
658
659
660
661
662
663
664
665
666
669
670
671
672
673
674
675
676
677
681
682
683
684
685
686
687
688
691
692
693
694
695
696
697
698
699
700
704
705
706
707
708
709
710
711
712
713
716
717
718
719
720
721
722
723
724
725
726
729
730
731
732
733
734
735
736
737
738
741
742
743
744
745
746
747
748
749
750
751
752
753
756
757
758
759
760
761
762
763
764
767
768
769
770
771
772
773
774
775
778
779
780
781
782
783
784
785
786
787
788
791
792
793
794
795
796
797
798
799
802
803
804
805
806
809
810
811
812
813
816
817
818
819
820
823
824
825
826
827
830
831
832
833
834
837
838
839
840
841
844
845
846
847
848
851
852
853
854
855
858
859
860
861
862
865
866
867
868
869
872
873
874
875
876
879
880
881
882
883
886
887
888
889
890
891
892
893
894
897
898
899
900
901
902
903
904
905
906
909
910
911
912
913
914
917
918
919
920
921
922
925
926
927
928
929
930
931
932
933
934
935
936
937
940
941
942
943
944
945
946
947
948
949
950
951
952
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
972
973
974
975
976
977
978
979
980
981
982
983
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1026
1027
1028
1029
1032
1033
1034
1037
1038
1039
1042
1043
1044
1045
1046
1047
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1114QString QDebug::toStringImpl(StreamTypeErased s,
const void *obj)
1119 s(d.nospace(), obj);
1125
1126
1127
1128
1129
1130
1131
1132
1135QByteArray QDebug::toBytesImpl(StreamTypeErased s,
const void *obj)
1140 s(d.nospace(), obj);
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156QDebug &QDebug::putTupleLikeImplImpl(
const char *ns,
const char *what,
1157 size_t n, StreamTypeErased *ops,
const void **data)
1159 const QDebugStateSaver saver(*
this);
1162 *
this << ns <<
"::";
1163 *
this << what <<
'(';
1165 (*ops++)(*
this, *data++);
1169 return *
this <<
')';
1173
1174
1175
1176
1177
1178
1181
1182
1183
1184
1185
1186
1187
1190
1191
1192
1193
1194
1195
1196
1199
1200
1201
1202
1203
1204
1205
1209
1210
1211
1212
1213
1214
1217
1218
1219
1220
1221
1222
1225
1226
1227
1228
1229
1230
1233
1234
1235
1236
1237
1238
1241
1242
1243
1244
1245
1246
1247
1250
1251
1252
1253
1254
1255
1256
1259
1260
1261
1262
1263
1264
1265
1268
1269
1270
1271
1272
1273
1274
1277
1278
1279
1280
1281
1282
1283
1286
1287
1288
1289
1290
1291
1292
1295
1296
1297
1298
1299
1300
1303
1304
1305
1306
1307
1308
1311
1312
1313
1314
1315
1316
1319
1320
1321
1322
1323
1324
1327
1328
1329
1330
1331
1332
1335
1336
1337
1338
1339
1340
1344
1345
1346
1347
1348
1351
1352
1353
1354
1355
1356
1357
1358
1359
1362
1363
1364
1367
1368
1369
1370
1371
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
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);
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;
1419 if (!currentSpaces && m_spaces)
1420 m_stream->ts <<
' ';
1436
1437
1438
1439
1440
1441QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
1442 : d(
new QDebugStateSaverPrivate(dbg.stream))
1447
1448
1449
1450
1451
1452QDebugStateSaver::~QDebugStateSaver()
1458
1459
1460
1461
1462
1463
1464
1467 qt_QMetaEnum_flagDebugOperator(debug, sizeofT, quint64(value));
1471
1472
1473
1476 qt_QMetaEnum_flagDebugOperator<quint64>(debug, sizeofT, value);
1480#ifndef QT_NO_QOBJECT
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1518 QDebugStateSaver saver(dbg);
1520 QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1522 const int verbosity = dbg.verbosity();
1523 if (verbosity >= QDebug::DefaultVerbosity) {
1524 if (
const char *scope = me.scope())
1525 dbg << scope << u"::";
1528 const char *key = me.valueToKey(
static_cast<
int>(value));
1529 const bool scoped = me.isScoped() || verbosity & 1;
1531 dbg << me.enumName() << (!key ? u"(" : u"::");
1536 dbg << value <<
')';
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1572 const int verbosity = debug.verbosity();
1574 QDebugStateSaver saver(debug);
1575 debug.resetFormat();
1579 const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1581 const bool classScope = verbosity >= QDebug::DefaultVerbosity;
1583 debug << u"QFlags<";
1585 if (
const char *scope = me.scope())
1586 debug << scope << u"::";
1589 const bool enumScope = me.isScoped() || verbosity > QDebug::MinimumVerbosity;
1591 debug << me.enumName();
1597 debug << me.valueToKeys(value);
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
const QTextStreamPrivate::Params m_streamParams
QDebugStateSaverPrivate(QDebug::Stream *stream)
Combined button and popup list for selecting options.
QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, qint64 value, const QMetaObject *meta, const char *name)
static void putEscapedString(QTextStreamPrivate *d, const Char *begin, size_t length, bool isUnicode=true)
static bool isPrintable(char16_t uc)
static QByteArray timeUnit(qint64 num, qint64 den)
static bool isPrintable(uchar c)
QDebug operator<<(QDebug debug, const std::error_code &code)
static bool isPrintable(char32_t ucs4)
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
QDebug operator<<(QDebug debug, const std::error_condition &condition)