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
584
585
586
587
588
589QDebug &QDebug::resetFormat()
592 stream->space =
true;
593 stream->noQuotes =
false;
594 stream->verbosity = DefaultVerbosity;
599
600
601
602
603
604
605
606
607
608
611
612
613
614
615
616
619
620
621
622
623
624
625
628
629
630
631
632
633
634
635
636
639
640
641
642
643
644
645
646
647
651
652
653
654
655
656
657
658
661
662
663
664
665
666
667
668
669
670
674
675
676
677
678
679
680
681
682
683
686
687
688
689
690
691
692
693
694
695
696
699
700
701
702
703
704
705
706
707
708
711
712
713
714
715
716
717
718
719
720
721
722
723
726
727
728
729
730
731
732
733
734
737
738
739
740
741
742
743
744
745
748
749
750
751
752
753
754
755
756
757
758
761
762
763
764
765
766
767
768
769
772
773
774
775
776
779
780
781
782
783
786
787
788
789
790
793
794
795
796
797
800
801
802
803
804
807
808
809
810
811
814
815
816
817
818
821
822
823
824
825
828
829
830
831
832
835
836
837
838
839
842
843
844
845
846
849
850
851
852
853
856
857
858
859
860
861
862
863
864
867
868
869
870
871
872
873
874
875
876
879
880
881
882
883
884
887
888
889
890
891
892
895
896
897
898
899
900
901
902
903
904
905
906
907
910
911
912
913
914
915
916
917
918
919
920
921
922
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
942
943
944
945
946
947
948
949
950
951
952
953
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
996
997
998
999
1002
1003
1004
1007
1008
1009
1012
1013
1014
1015
1016
1017
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1084QString QDebug::toStringImpl(StreamTypeErased s,
const void *obj)
1089 s(d.nospace(), obj);
1095
1096
1097
1098
1099
1100
1101
1102
1105QByteArray QDebug::toBytesImpl(StreamTypeErased s,
const void *obj)
1110 s(d.nospace(), obj);
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126QDebug &QDebug::putTupleLikeImplImpl(
const char *ns,
const char *what,
1127 size_t n, StreamTypeErased *ops,
const void **data)
1129 const QDebugStateSaver saver(*
this);
1132 *
this << ns <<
"::";
1133 *
this << what <<
'(';
1135 (*ops++)(*
this, *data++);
1139 return *
this <<
')';
1143
1144
1145
1146
1147
1148
1151
1152
1153
1154
1155
1156
1157
1160
1161
1162
1163
1164
1165
1166
1169
1170
1171
1172
1173
1174
1175
1178
1179
1180
1181
1182
1183
1184
1187
1188
1189
1190
1191
1192
1195
1196
1197
1198
1199
1200
1203
1204
1205
1206
1207
1208
1211
1212
1213
1214
1215
1216
1217
1220
1221
1222
1223
1224
1225
1226
1229
1230
1231
1232
1233
1234
1235
1238
1239
1240
1241
1242
1243
1244
1247
1248
1249
1250
1251
1252
1253
1256
1257
1258
1259
1260
1261
1262
1265
1266
1267
1268
1269
1270
1273
1274
1275
1276
1277
1278
1281
1282
1283
1284
1285
1286
1289
1290
1291
1292
1293
1294
1297
1298
1299
1300
1301
1302
1305
1306
1307
1308
1309
1310
1314
1315
1316
1317
1318
1321
1322
1323
1324
1325
1326
1327
1328
1329
1332
1333
1334
1337
1338
1339
1340
1341
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
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);
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;
1389 if (!currentSpaces && m_spaces)
1390 m_stream->ts <<
' ';
1406
1407
1408
1409
1410
1411QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
1412 : d(
new QDebugStateSaverPrivate(dbg.stream))
1417
1418
1419
1420
1421
1422QDebugStateSaver::~QDebugStateSaver()
1428
1429
1430
1431
1432
1433
1434
1437 qt_QMetaEnum_flagDebugOperator(debug, sizeofT, quint64(value));
1441
1442
1443
1446 qt_QMetaEnum_flagDebugOperator<quint64>(debug, sizeofT, value);
1450#ifndef QT_NO_QOBJECT
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1488 QDebugStateSaver saver(dbg);
1490 QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1492 const int verbosity = dbg.verbosity();
1493 if (verbosity >= QDebug::DefaultVerbosity) {
1494 if (
const char *scope = me.scope())
1495 dbg << scope << u"::";
1498 const char *key = me.valueToKey(
static_cast<
int>(value));
1499 const bool scoped = me.isScoped() || verbosity & 1;
1501 dbg << me.enumName() << (!key ? u"(" : u"::");
1506 dbg << value <<
')';
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1542 const int verbosity = debug.verbosity();
1544 QDebugStateSaver saver(debug);
1545 debug.resetFormat();
1549 const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1551 const bool classScope = verbosity >= QDebug::DefaultVerbosity;
1553 debug << u"QFlags<";
1555 if (
const char *scope = me.scope())
1556 debug << scope << u"::";
1559 const bool enumScope = me.isScoped() || verbosity > QDebug::MinimumVerbosity;
1561 debug << me.enumName();
1567 debug << me.valueToKeys(value);
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
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
1685
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
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)
static bool isPrintable(char32_t ucs4)
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)