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
139
140
141
142
145
146
147
148
149
152
153
154
155
158 if (stream && !--stream->ref) {
159 if (stream->space && stream->buffer.endsWith(u' '))
160 stream->buffer.chop(1);
161 if (stream->message_output) {
162 QInternalMessageLogContext ctxt(stream->context);
163 qt_message_output(stream->type,
172
173
174void QDebug::putUcs4(uint ucs4)
178 stream->ts <<
"\\x" << Qt::hex << ucs4 << Qt::reset;
179 }
else if (ucs4 < 0x80) {
180 stream->ts <<
char(ucs4);
183 stream->ts <<
"\\u" << qSetFieldWidth(4);
185 stream->ts <<
"\\U" << qSetFieldWidth(8);
186 stream->ts << Qt::hex << qSetPadChar(u'0') << ucs4 << Qt::reset;
194static inline bool isPrintable(
char32_t ucs4) {
return QChar::isPrint(ucs4); }
195static inline bool isPrintable(
char16_t uc) {
return QChar::isPrint(uc); }
197{
return isAsciiPrintable(c); }
199template <
typename Char>
200static inline void putEscapedString(QTextStreamPrivate *d,
const Char *begin, size_t length,
bool isUnicode =
true)
202 constexpr char16_t quotes[] = uR"("")";
203 constexpr char16_t quote = quotes[0];
206 bool lastWasHexEscape =
false;
207 const Char *end = begin + length;
208 for (
const Char *p = begin; p != end; ++p) {
210 if (Q_UNLIKELY(lastWasHexEscape)) {
211 if (fromHex(*p) != -1) {
215 lastWasHexEscape =
false;
218 if constexpr (
sizeof(Char) ==
sizeof(QChar)) {
220 qsizetype runLength = 0;
221 while (p + runLength != end &&
222 isPrintable(p[runLength]) && p[runLength] !=
'\\' && p[runLength] !=
'"')
225 d->write(QStringView{p, runLength});
229 }
else if (isPrintable(*p) && *p !=
'\\' && *p !=
'"') {
230 d->write(
char16_t{uchar(*p)});
235 qsizetype buflen = 2;
236 char16_t buf[
std::char_traits<
char>::length(
"\\U12345678")];
263 buf[2] = toHexUpper(uchar(*p) >> 4);
264 buf[3] = toHexUpper(uchar(*p));
266 lastWasHexEscape =
true;
269 if (QChar::isHighSurrogate(*p)) {
270 if ((p + 1) != end && QChar::isLowSurrogate(p[1])) {
272 char32_t ucs4 = QChar::surrogateToUcs4(*p, p[1]);
281 buf[4] = toHexUpper(ucs4 >> 20);
282 buf[5] = toHexUpper(ucs4 >> 16);
283 buf[6] = toHexUpper(ucs4 >> 12);
284 buf[7] = toHexUpper(ucs4 >> 8);
285 buf[8] = toHexUpper(ucs4 >> 4);
286 buf[9] = toHexUpper(ucs4);
295 buf[2] = toHexUpper(
char16_t(*p) >> 12);
296 buf[3] = toHexUpper(
char16_t(*p) >> 8);
297 buf[4] = toHexUpper(*p >> 4);
298 buf[5] = toHexUpper(*p);
301 d->write(QStringView{buf, buflen});
308
309
310
311void QDebug::putString(
const QChar *begin, size_t length)
313 if (stream->noQuotes) {
316 stream->ts.d_ptr->putString(QStringView{begin, qsizetype(length)});
319 QDebugStateSaver saver(*
this);
320 stream->ts.d_ptr->params.reset();
321 putEscapedString(stream->ts.d_ptr.get(),
reinterpret_cast<
const char16_t *>(begin), length);
326
327
328
329void QDebug::putByteArray(
const char *begin, size_t length, Latin1Content content)
331 if (stream->noQuotes) {
335 case Latin1Content::ContainsLatin1:
336 stream->ts.d_ptr->putString(QLatin1StringView{begin, qsizetype(length)});
338 case Latin1Content::ContainsBinary:
339 stream->ts.d_ptr->putString(QUtf8StringView{begin, qsizetype(length)});
344 QDebugStateSaver saver(*
this);
345 stream->ts.d_ptr->params.reset();
346 putEscapedString(stream->ts.d_ptr.get(),
reinterpret_cast<
const uchar *>(begin),
347 length, content == ContainsLatin1);
353 using namespace std::chrono;
356 if (num == 1 && den > 1) {
359 auto tryprefix = [&](
auto d,
char c) {
360 static_assert(
decltype(d)::num == 1,
"not an SI prefix");
361 if (den ==
decltype(d)::den)
366 tryprefix(std::milli{},
'm');
367 tryprefix(std::micro{},
'u');
368 tryprefix(std::nano{},
'n');
369 tryprefix(std::pico{},
'p');
370 tryprefix(std::femto{},
'f');
371 tryprefix(std::atto{},
'a');
373 tryprefix(std::centi{},
'c');
374 tryprefix(std::deci{},
'd');
376 char unit[3] = { prefix,
's' };
377 return QByteArray(unit,
sizeof(unit) - 1);
381 const char *unit =
nullptr;
382 if (num > 1 && den == 1) {
384 auto tryunit = [&](
auto d,
const char *name) {
385 static_assert(
decltype(d)::period::den == 1,
"not a multiple of a second");
386 if (unit || num %
decltype(d)::period::num)
389 num /=
decltype(d)::period::num;
391 tryunit(years{},
"yr");
392 tryunit(weeks{},
"wk");
393 tryunit(days{},
"d");
394 tryunit(hours{},
"h");
395 tryunit(minutes{},
"min");
400 if (num == 1 && den == 1)
402 if (Q_UNLIKELY(num < 1 || den < 1))
403 return QString::asprintf(
"<invalid time unit %lld/%lld>", num, den).toLatin1();
407 char buf[2 * (std::numeric_limits<qint64>::digits10 + 2) + 10];
409 auto appendChar = [&](
char c) {
410 Q_ASSERT(len <
sizeof(buf));
413 auto appendNumber = [&](qint64 value) {
414 if (value >= 10'000 && (value % 1000) == 0)
415 len += std::snprintf(buf + len,
sizeof(buf) - len,
"%.6g",
double(value));
417 len += std::snprintf(buf + len,
sizeof(buf) - len,
"%lld", value);
426 memcpy(buf + len, unit, strlen(unit));
427 return QByteArray(buf, len + strlen(unit));
431
432
433
434
435void QDebug::putTimeUnit(qint64 num, qint64 den)
437 stream->ts << timeUnit(num, den);
442#ifdef QT_SUPPORTS_INT128
444constexpr char Q_INT128_MIN_STR[] =
"-170141183460469231731687303715884105728";
446constexpr int Int128BufferSize =
sizeof(Q_INT128_MIN_STR);
447using Int128Buffer = std::array<
char, Int128BufferSize>;
450static char *i128ToStringHelper(Int128Buffer &buffer, quint128 n)
452 auto dst = buffer.data() + buffer.size();
458 *--dst =
"0123456789"[n % 10];
467static const char *int128Warning()
469 const char *msg =
"Qt was not compiled with int128 support.";
477
478
479
480
481void QDebug::putInt128([[maybe_unused]]
const void *p)
483#ifdef QT_SUPPORTS_INT128
486 memcpy(&i, p,
sizeof(i));
487 if (i == Q_INT128_MIN) {
489 stream->ts << Q_INT128_MIN_STR;
492 auto dst = i128ToStringHelper(buffer, i < 0 ? -i : i);
499 stream->ts << int128Warning();
503
504
505
506
507void QDebug::putUInt128([[maybe_unused]]
const void *p)
509#ifdef QT_SUPPORTS_INT128
512 memcpy(&i, p,
sizeof(i));
514 stream->ts << i128ToStringHelper(buffer, i);
517 stream->ts << int128Warning();
521
522
523
524
525
526
527void QDebug::putQtOrdering(QtOrderingPrivate::QtOrderingTypeFlag flags, Qt::partial_ordering order)
529 using QtOrderingPrivate::QtOrderingType;
531 if ((flags & QtOrderingType::StdOrder) == QtOrderingType::StdOrder)
533 else if ((flags & QtOrderingType::QtOrder) == QtOrderingType::QtOrder)
537 const bool isStrong = ((flags & QtOrderingType::Strong) == QtOrderingType::Strong);
540 else if ((flags & QtOrderingType::Weak) == QtOrderingType::Weak)
542 else if ((flags & QtOrderingType::Partial) == QtOrderingType::Partial)
544 result +=
"_ordering::";
546 if (order == Qt::partial_ordering::equivalent) {
550 result +=
"equivalent";
551 }
else if (order == Qt::partial_ordering::greater) {
553 }
else if (order == Qt::partial_ordering::less) {
556 result +=
"unordered";
558 stream->ts << result.data();
562
563
564
565
568
569
570
571
572
573QDebug &QDebug::resetFormat()
576 stream->space =
true;
577 stream->noQuotes =
false;
578 stream->verbosity = DefaultVerbosity;
583
584
585
586
587
588
589
590
591
592
595
596
597
598
599
600
603
604
605
606
607
608
609
612
613
614
615
616
617
618
619
620
623
624
625
626
627
628
629
630
631
635
636
637
638
639
640
641
642
645
646
647
648
649
650
651
652
653
654
658
659
660
661
662
663
664
665
666
667
670
671
672
673
674
675
676
677
678
679
680
683
684
685
686
687
688
689
690
691
692
695
696
697
698
699
700
701
702
703
704
705
706
707
710
711
712
713
714
715
716
717
718
721
722
723
724
725
726
727
728
729
732
733
734
735
736
737
738
739
740
741
742
745
746
747
748
749
750
751
752
753
756
757
758
759
760
763
764
765
766
767
770
771
772
773
774
777
778
779
780
781
784
785
786
787
788
791
792
793
794
795
798
799
800
801
802
805
806
807
808
809
812
813
814
815
816
819
820
821
822
823
826
827
828
829
830
833
834
835
836
837
840
841
842
843
844
845
846
847
848
851
852
853
854
855
856
857
858
859
860
863
864
865
866
867
868
871
872
873
874
875
876
879
880
881
882
883
884
885
886
887
888
889
890
891
894
895
896
897
898
899
900
901
902
903
904
905
906
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
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
953
954
955
956
957
958
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
980
981
982
983
986
987
988
991
992
993
996
997
998
999
1000
1001
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1068QString QDebug::toStringImpl(StreamTypeErased s,
const void *obj)
1073 s(d.nospace(), obj);
1079
1080
1081
1082
1083
1084
1085
1086
1089QByteArray QDebug::toBytesImpl(StreamTypeErased s,
const void *obj)
1094 s(d.nospace(), obj);
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110QDebug &QDebug::putTupleLikeImplImpl(
const char *ns,
const char *what,
1111 size_t n, StreamTypeErased *ops,
const void **data)
1113 const QDebugStateSaver saver(*
this);
1116 *
this << ns <<
"::";
1117 *
this << what <<
'(';
1119 (*ops++)(*
this, *data++);
1123 return *
this <<
')';
1127
1128
1129
1130
1131
1132
1135
1136
1137
1138
1139
1140
1141
1144
1145
1146
1147
1148
1149
1150
1153
1154
1155
1156
1157
1158
1159
1162
1163
1164
1165
1166
1167
1168
1171
1172
1173
1174
1175
1176
1179
1180
1181
1182
1183
1184
1187
1188
1189
1190
1191
1192
1195
1196
1197
1198
1199
1200
1201
1204
1205
1206
1207
1208
1209
1210
1213
1214
1215
1216
1217
1218
1219
1222
1223
1224
1225
1226
1227
1228
1231
1232
1233
1234
1235
1236
1237
1240
1241
1242
1243
1244
1245
1246
1249
1250
1251
1252
1253
1254
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
1298
1299
1300
1301
1302
1305
1306
1307
1308
1309
1310
1311
1312
1313
1316
1317
1318
1321
1322
1323
1324
1325
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1363 const bool currentSpaces = m_stream->space;
1364 if (currentSpaces && !m_spaces)
1365 if (m_stream->buffer.endsWith(u' '))
1366 m_stream->buffer.chop(1);
1368 m_stream->space = m_spaces;
1369 m_stream->noQuotes = m_noQuotes;
1370 m_stream->ts.d_ptr->params = m_streamParams;
1371 m_stream->verbosity = m_verbosity;
1373 if (!currentSpaces && m_spaces)
1374 m_stream->ts <<
' ';
1390
1391
1392
1393
1394
1395QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
1396 : d(
new QDebugStateSaverPrivate(dbg.stream))
1401
1402
1403
1404
1405
1406QDebugStateSaver::~QDebugStateSaver()
1412
1413
1414
1415
1416
1417
1418
1421 qt_QMetaEnum_flagDebugOperator(debug, sizeofT, quint64(value));
1425
1426
1427
1430 qt_QMetaEnum_flagDebugOperator<quint64>(debug, sizeofT, value);
1434#ifndef QT_NO_QOBJECT
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1472 QDebugStateSaver saver(dbg);
1474 QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1476 const int verbosity = dbg.verbosity();
1477 if (verbosity >= QDebug::DefaultVerbosity) {
1478 if (
const char *scope = me.scope())
1479 dbg << scope << u"::";
1482 const char *key = me.valueToKey(
static_cast<
int>(value));
1483 const bool scoped = me.isScoped() || verbosity & 1;
1485 dbg << me.enumName() << (!key ? u"(" : u"::");
1490 dbg << value <<
')';
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1526 const int verbosity = debug.verbosity();
1528 QDebugStateSaver saver(debug);
1529 debug.resetFormat();
1533 const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1535 const bool classScope = verbosity >= QDebug::DefaultVerbosity;
1537 debug << u"QFlags<";
1539 if (
const char *scope = me.scope())
1540 debug << scope << u"::";
1543 const bool enumScope = me.isScoped() || verbosity > QDebug::MinimumVerbosity;
1545 debug << me.enumName();
1551 debug << me.valueToKeys(value);
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
const QTextStreamPrivate::Params m_streamParams
QDebugStateSaverPrivate(QDebug::Stream *stream)
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)