6#include "private/qdebug_p.h"
8#include <private/qlogging_p.h>
9#include <private/qtextstream_p.h>
10#include <private/qtools_p.h>
18using namespace QtMiscUtils;
21
22
23
24
25QByteArray QtDebugUtils::toPrintable(
const char *data, qint64 len, qsizetype maxSize)
31 for (qsizetype i = 0; i < qMin(len, maxSize); ++i) {
33 if (isAsciiPrintable(c)) {
50 toHexLower(uchar(c) / 16),
51 toHexLower(uchar(c) % 16),
70
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
120
121
122
123
126
127
128
129
132
133
134
135
138
139
140
141
144
145
146
147
148
151
152
153
154
157 if (stream && !--stream->ref) {
158 if (stream->space && stream->buffer.endsWith(u' '))
159 stream->buffer.chop(1);
160 if (stream->message_output) {
161 QInternalMessageLogContext ctxt(stream->context);
162 qt_message_output(stream->type,
171
172
173void QDebug::putUcs4(uint ucs4)
177 stream->ts <<
"\\x" << Qt::hex << ucs4 << Qt::reset;
178 }
else if (ucs4 < 0x80) {
179 stream->ts <<
char(ucs4);
182 stream->ts <<
"\\u" << qSetFieldWidth(4);
184 stream->ts <<
"\\U" << qSetFieldWidth(8);
185 stream->ts << Qt::hex << qSetPadChar(u'0') << ucs4 << Qt::reset;
193static inline bool isPrintable(
char32_t ucs4) {
return QChar::isPrint(ucs4); }
194static inline bool isPrintable(
char16_t uc) {
return QChar::isPrint(uc); }
196{
return isAsciiPrintable(c); }
198template <
typename Char>
199static inline void putEscapedString(QTextStreamPrivate *d,
const Char *begin, size_t length,
bool isUnicode =
true)
204 bool lastWasHexEscape =
false;
205 const Char *end = begin + length;
206 for (
const Char *p = begin; p != end; ++p) {
208 if (Q_UNLIKELY(lastWasHexEscape)) {
209 if (fromHex(*p) != -1) {
211 QChar quotes[] = { quote, quote };
214 lastWasHexEscape =
false;
217 if (
sizeof(Char) ==
sizeof(QChar)) {
219 qsizetype runLength = 0;
220 while (p + runLength != end &&
221 isPrintable(p[runLength]) && p[runLength] !=
'\\' && p[runLength] !=
'"')
224 d->write(
reinterpret_cast<
const QChar *>(p), runLength);
228 }
else if (isPrintable(*p) && *p !=
'\\' && *p !=
'"') {
229 QChar c = QLatin1Char(*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(
reinterpret_cast<QChar *>(buf), buflen);
308
309
310
311void QDebug::putString(
const QChar *begin, size_t length)
313 if (stream->noQuotes) {
316 stream->ts.d_ptr->putString(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) {
334 QString string = content == ContainsLatin1 ? QString::fromLatin1(begin, qsizetype(length))
335 : QString::fromUtf8(begin, qsizetype(length));
336 stream->ts.d_ptr->putString(string);
339 QDebugStateSaver saver(*
this);
340 stream->ts.d_ptr->params.reset();
341 putEscapedString(stream->ts.d_ptr.get(),
reinterpret_cast<
const uchar *>(begin),
342 length, content == ContainsLatin1);
348 using namespace std::chrono;
351 if (num == 1 && den > 1) {
354 auto tryprefix = [&](
auto d,
char c) {
355 static_assert(
decltype(d)::num == 1,
"not an SI prefix");
356 if (den ==
decltype(d)::den)
361 tryprefix(std::milli{},
'm');
362 tryprefix(std::micro{},
'u');
363 tryprefix(std::nano{},
'n');
364 tryprefix(std::pico{},
'p');
365 tryprefix(std::femto{},
'f');
366 tryprefix(std::atto{},
'a');
368 tryprefix(std::centi{},
'c');
369 tryprefix(std::deci{},
'd');
371 char unit[3] = { prefix,
's' };
372 return QByteArray(unit,
sizeof(unit) - 1);
376 const char *unit =
nullptr;
377 if (num > 1 && den == 1) {
379 auto tryunit = [&](
auto d,
const char *name) {
380 static_assert(
decltype(d)::period::den == 1,
"not a multiple of a second");
381 if (unit || num %
decltype(d)::period::num)
384 num /=
decltype(d)::period::num;
386 tryunit(years{},
"yr");
387 tryunit(weeks{},
"wk");
388 tryunit(days{},
"d");
389 tryunit(hours{},
"h");
390 tryunit(minutes{},
"min");
395 if (num == 1 && den == 1)
397 if (Q_UNLIKELY(num < 1 || den < 1))
398 return QString::asprintf(
"<invalid time unit %lld/%lld>", num, den).toLatin1();
402 char buf[2 * (std::numeric_limits<qint64>::digits10 + 2) + 10];
404 auto appendChar = [&](
char c) {
405 Q_ASSERT(len <
sizeof(buf));
408 auto appendNumber = [&](qint64 value) {
409 if (value >= 10'000 && (value % 1000) == 0)
410 len += std::snprintf(buf + len,
sizeof(buf) - len,
"%.6g",
double(value));
412 len += std::snprintf(buf + len,
sizeof(buf) - len,
"%lld", value);
421 memcpy(buf + len, unit, strlen(unit));
422 return QByteArray(buf, len + strlen(unit));
426
427
428
429
430void QDebug::putTimeUnit(qint64 num, qint64 den)
432 stream->ts << timeUnit(num, den);
437#ifdef QT_SUPPORTS_INT128
439constexpr char Q_INT128_MIN_STR[] =
"-170141183460469231731687303715884105728";
441constexpr int Int128BufferSize =
sizeof(Q_INT128_MIN_STR);
442using Int128Buffer = std::array<
char, Int128BufferSize>;
445static char *i128ToStringHelper(Int128Buffer &buffer, quint128 n)
447 auto dst = buffer.data() + buffer.size();
453 *--dst =
"0123456789"[n % 10];
462static const char *int128Warning()
464 const char *msg =
"Qt was not compiled with int128 support.";
472
473
474
475
476void QDebug::putInt128([[maybe_unused]]
const void *p)
478#ifdef QT_SUPPORTS_INT128
481 memcpy(&i, p,
sizeof(i));
482 if (i == Q_INT128_MIN) {
484 stream->ts << Q_INT128_MIN_STR;
487 auto dst = i128ToStringHelper(buffer, i < 0 ? -i : i);
494 stream->ts << int128Warning();
498
499
500
501
502void QDebug::putUInt128([[maybe_unused]]
const void *p)
504#ifdef QT_SUPPORTS_INT128
507 memcpy(&i, p,
sizeof(i));
509 stream->ts << i128ToStringHelper(buffer, i);
512 stream->ts << int128Warning();
516
517
518
519
520
521
522void QDebug::putQtOrdering(QtOrderingPrivate::QtOrderingTypeFlag flags, Qt::partial_ordering order)
524 using QtOrderingPrivate::QtOrderingType;
526 if ((flags & QtOrderingType::StdOrder) == QtOrderingType::StdOrder)
528 else if ((flags & QtOrderingType::QtOrder) == QtOrderingType::QtOrder)
532 const bool isStrong = ((flags & QtOrderingType::Strong) == QtOrderingType::Strong);
535 else if ((flags & QtOrderingType::Weak) == QtOrderingType::Weak)
537 else if ((flags & QtOrderingType::Partial) == QtOrderingType::Partial)
539 result +=
"_ordering::";
541 if (order == Qt::partial_ordering::equivalent) {
545 result +=
"equivalent";
546 }
else if (order == Qt::partial_ordering::greater) {
548 }
else if (order == Qt::partial_ordering::less) {
551 result +=
"unordered";
553 stream->ts << result.data();
557
558
559
560
563
564
565
566
567
568QDebug &QDebug::resetFormat()
571 stream->space =
true;
572 stream->noQuotes =
false;
573 stream->verbosity = DefaultVerbosity;
578
579
580
581
582
583
584
585
586
587
590
591
592
593
594
595
598
599
600
601
602
603
604
607
608
609
610
611
612
613
614
615
618
619
620
621
622
623
624
625
626
630
631
632
633
634
635
636
637
640
641
642
643
644
645
646
647
648
649
653
654
655
656
657
658
659
660
661
662
665
666
667
668
669
670
671
672
673
674
675
678
679
680
681
682
683
684
685
686
687
690
691
692
693
694
695
696
697
698
699
700
701
702
705
706
707
708
709
710
711
712
713
716
717
718
719
720
721
722
723
724
727
728
729
730
731
732
733
734
735
736
737
740
741
742
743
744
745
746
747
748
751
752
753
754
755
758
759
760
761
762
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
840
841
842
843
846
847
848
849
850
851
852
853
854
855
858
859
860
861
862
863
866
867
868
869
870
871
874
875
876
877
878
879
880
881
882
883
884
885
886
889
890
891
892
893
894
895
896
897
898
899
900
901
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
921
922
923
924
925
926
927
928
929
930
931
932
935
936
937
938
939
940
941
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
975
976
977
978
981
982
983
986
987
988
991
992
993
994
995
996
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1046
1047
1048
1049
1050
1051
1052
1055QString QDebug::toStringImpl(StreamTypeErased s,
const void *obj)
1060 s(d.nospace(), obj);
1066
1067
1068
1069
1070
1071
1072
1073
1076QByteArray QDebug::toBytesImpl(StreamTypeErased s,
const void *obj)
1081 s(d.nospace(), obj);
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097QDebug &QDebug::putTupleLikeImplImpl(
const char *ns,
const char *what,
1098 size_t n, StreamTypeErased *ops,
const void **data)
1100 const QDebugStateSaver saver(*
this);
1103 *
this << ns <<
"::";
1104 *
this << what <<
'(';
1106 (*ops++)(*
this, *data++);
1110 return *
this <<
')';
1114
1115
1116
1117
1118
1119
1122
1123
1124
1125
1126
1127
1128
1131
1132
1133
1134
1135
1136
1137
1140
1141
1142
1143
1144
1145
1146
1149
1150
1151
1152
1153
1154
1155
1158
1159
1160
1161
1162
1163
1166
1167
1168
1169
1170
1171
1174
1175
1176
1177
1178
1179
1182
1183
1184
1185
1186
1187
1188
1191
1192
1193
1194
1195
1196
1197
1200
1201
1202
1203
1204
1205
1206
1209
1210
1211
1212
1213
1214
1215
1218
1219
1220
1221
1222
1223
1224
1227
1228
1229
1230
1231
1232
1233
1236
1237
1238
1239
1240
1241
1244
1245
1246
1247
1248
1249
1252
1253
1254
1255
1256
1257
1260
1261
1262
1263
1264
1265
1268
1269
1270
1271
1272
1273
1276
1277
1278
1279
1280
1281
1285
1286
1287
1288
1289
1292
1293
1294
1295
1296
1297
1298
1299
1300
1303
1304
1305
1308
1309
1310
1311
1312
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1350 const bool currentSpaces = m_stream->space;
1351 if (currentSpaces && !m_spaces)
1352 if (m_stream->buffer.endsWith(u' '))
1353 m_stream->buffer.chop(1);
1355 m_stream->space = m_spaces;
1356 m_stream->noQuotes = m_noQuotes;
1357 m_stream->ts.d_ptr->params = m_streamParams;
1358 m_stream->verbosity = m_verbosity;
1360 if (!currentSpaces && m_spaces)
1361 m_stream->ts <<
' ';
1377
1378
1379
1380
1381
1382QDebugStateSaver::QDebugStateSaver(QDebug &dbg)
1383 : d(
new QDebugStateSaverPrivate(dbg.stream))
1388
1389
1390
1391
1392
1393QDebugStateSaver::~QDebugStateSaver()
1399
1400
1401
1402
1403
1404
1405
1408 qt_QMetaEnum_flagDebugOperator(debug, sizeofT, quint64(value));
1412
1413
1414
1417 qt_QMetaEnum_flagDebugOperator<quint64>(debug, sizeofT, value);
1421#ifndef QT_NO_QOBJECT
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1459 QDebugStateSaver saver(dbg);
1461 QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1463 const int verbosity = dbg.verbosity();
1464 if (verbosity >= QDebug::DefaultVerbosity) {
1465 if (
const char *scope = me.scope())
1466 dbg << scope << u"::";
1469 const char *key = me.valueToKey(
static_cast<
int>(value));
1470 const bool scoped = me.isScoped() || verbosity & 1;
1472 dbg << me.enumName() << (!key ? u"(" : u"::");
1477 dbg << value <<
')';
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
1513 const int verbosity = debug.verbosity();
1515 QDebugStateSaver saver(debug);
1516 debug.resetFormat();
1520 const QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
1522 const bool classScope = verbosity >= QDebug::DefaultVerbosity;
1524 debug << u"QFlags<";
1526 if (
const char *scope = me.scope())
1527 debug << scope << u"::";
1530 const bool enumScope = me.isScoped() || verbosity > QDebug::MinimumVerbosity;
1532 debug << me.enumName();
1538 debug << me.valueToKeys(value);
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)