forked from aristanetworks/quicktrace-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageFormatter.cpp
746 lines (687 loc) · 24.1 KB
/
MessageFormatter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Copyright (c) 2020, Arista Networks, Inc.
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Arista Networks nor the names of its contributors may
// be used to endorse or promote products derived from this software without
// specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL ARISTA NETWORKS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
#include <cassert>
#include <QuickTrace/MessageFormatter.h>
#include <QuickTrace/MessageFormatterCommon.h>
#include <arpa/inet.h>
#include <cstring>
#include <dlfcn.h>
#include <iomanip>
#include <iostream>
#include <dirent.h>
#include <errno.h>
namespace QuickTrace {
//
// -------- formatters for basic types provided by the QuickTrace package --------
//
template< typename T >
std::string
float_to_string( T t );
unsigned
lengthBool( const unsigned char * ) {
return 1;
}
unsigned
formatBool( const unsigned char * buf, std::ostream & os ) {
os << ( *buf ? "True" : "False" );
return lengthBool( buf );
}
unsigned
lengthChar( const unsigned char * ) {
return 1;
}
unsigned
formatChar( const unsigned char * buf, std::ostream & os ) {
char str[ 8 ];
switch ( *buf ) {
case '\n':
strcpy( str, "\\n" );
break;
case '\r':
strcpy( str, "\\r" );
break;
case '\t':
strcpy( str, "\\t" );
break;
case '\'':
strcpy( str, "\\'" );
break;
case '\\':
strcpy( str, "\\\\" );
break;
default:
if ( isprint( *buf ) ) {
sprintf( str, "%c", *buf );
} else {
sprintf( str, "\\x%02hhx", *buf );
}
}
os << str;
return lengthChar( buf );
}
template< typename T >
std::string
float_to_string( T t ) {
// format a float or double similar to Python
std::string str = std::to_string( t );
if ( str.find( '.' ) != std::string::npos ) {
// remove trailing zeros after the period except one
// e.g. 123.400000 -> 123.4; 123.000000 -> 123.0
std::string::size_type p = str.find_last_not_of( '0' );
if ( p != std::string::npos ) {
str.erase( str[ p ] == '.' ? p + 2 : p + 1 );
}
}
return str;
}
unsigned
lengthWallClock( const unsigned char * ) {
// Wall-clock timestamp from 'struct timeval' is saved as two uint64_t fields
return 2 * sizeof( uint64_t );
}
// The wall-clock time returned by gettimeofday is stored in RingBuf.
// Return formatted timestamp string from the RingBuf data.
unsigned
formatWallClockTimeStamp( const unsigned char * buf, std::ostream & os ) {
struct timeval tv;
memcpy( &tv, buf, sizeof( tv ) );
char tBuf_[ 24 ]; // formatted string representation of tv_sec
char usecBuf_[ 8 ]; // formatted string representation of tv_usec
strftime( tBuf_, sizeof( tBuf_ ), "%Y-%m-%d %H:%M:%S.", localtime( &tv.tv_sec ) );
sprintf( usecBuf_, "%06ld", tv.tv_usec );
os << tBuf_ << usecBuf_;
return lengthWallClock( buf );
}
unsigned
formatWallClockRingBuf( const unsigned char * buf, std::ostream & os ) {
// WallClock data is used only for printing timestamp preceding the actual
// message. Ignore this field while formatting message from RingBuf.
return lengthWallClock( buf );
}
unsigned
lengthDouble( const unsigned char * ) {
return 8;
}
unsigned
formatDouble( const unsigned char * buf, std::ostream & os ) {
double val;
memcpy( &val, buf, sizeof( val ) );
if ( ( os.flags() & std::ios_base::floatfield ) == std::ios_base::fixed ) {
// fixed decimal format was selected as part of a C format string
// just pass the value directly into the stream to honor width and precision
os << val;
} else {
// a decimal is being traced outside of a C format. Format it more python-like
os << float_to_string( val );
}
return lengthDouble( buf );
}
unsigned
lengthFloat( const unsigned char * ) {
return 4;
}
unsigned
formatFloat( const unsigned char * buf, std::ostream & os ) {
float val;
memcpy( &val, buf, sizeof( val ) );
if ( ( os.flags() & std::ios_base::floatfield ) == std::ios_base::fixed ) {
// fixed decimal format was selected as part of a C format string
// just pass the value directly into the stream to honor width and precision
os << val;
} else {
// a float is being traced outside of a C format. Format it more python-like
os << float_to_string( val );
}
return lengthFloat( buf );
}
unsigned
lengthString( const unsigned char * buf ) {
return 1U + *buf;
}
unsigned
formatString( const unsigned char * buf, std::ostream & os ) {
os.write( reinterpret_cast< const char * >( buf + 1 ), *buf );
return lengthString( buf );
}
unsigned
lengthU8( const unsigned char * ) {
return 1;
}
unsigned
formatU8( const unsigned char * buf, std::ostream & os ) {
os << static_cast< uint16_t >( *buf );
return lengthU8( buf );
}
unsigned
formatU8Hex( const unsigned char * buf, std::ostream & os ) {
os << std::hex << static_cast< uint16_t >( *buf ) << std::dec;
return lengthU8( buf );
}
unsigned
lengthU16( const unsigned char * ) {
return 2;
}
unsigned
formatU16( const unsigned char * buf, std::ostream & os ) {
uint16_t val;
memcpy( &val, buf, sizeof( val ) );
os << val;
return lengthU16( buf );
}
unsigned
formatU16Hex( const unsigned char * buf, std::ostream & os ) {
uint16_t val;
memcpy( &val, buf, sizeof( val ) );
os << std::hex << val << std::dec;
return lengthU16( buf );
}
unsigned
lengthU32( const unsigned char * ) {
return 4;
}
unsigned
formatU32( const unsigned char * buf, std::ostream & os ) {
uint32_t val;
memcpy( &val, buf, sizeof( val ) );
if ( ( os.flags() & std::ios_base::showpos ) == std::ios_base::showpos ) {
// streams never print a + for positive numbers but with C formats we want
// that if %+d is used
os << '+';
}
os << val;
return lengthU32( buf );
}
unsigned
formatU32Hex( const unsigned char * buf, std::ostream & os ) {
uint32_t val;
memcpy( &val, buf, sizeof( val ) );
os << std::hex << val << std::dec;
return lengthU32( buf );
}
unsigned
lengthU64( const unsigned char * ) {
return 8;
}
unsigned
formatU64( const unsigned char * buf, std::ostream & os ) {
uint64_t val;
memcpy( &val, buf, sizeof( val ) );
os << val;
return lengthU64( buf );
}
unsigned
formatU64Hex( const unsigned char * buf, std::ostream & os ) {
uint64_t val;
memcpy( &val, buf, sizeof( val ) );
os << std::hex << val << std::dec;
return lengthU64( buf );
}
unsigned
lengthOpt( const MessageFormatter::LengthType & length,
const unsigned char * buf ) {
uint8_t val;
memcpy( &val, buf, sizeof( val ) );
auto len = lengthU8( buf );
if ( val != 0 ) {
len += length( buf + len );
}
return len;
}
unsigned
formatOpt( const MessageFormatter::FormatterType & formatter,
const unsigned char * buf,
std::ostream & os ) {
uint8_t val;
memcpy( &val, buf, sizeof( val ) );
auto len = lengthU8( buf );
if ( val != 0 ) {
len += formatter( buf + len, os );
} else {
os << "OptionalNone";
}
return len;
}
MessageFormatter::MessageFormatter(
uint32_t msgId, std::string filename, uint32_t lineno, std::string msg,
std::string fmt ) :
Message( 0, std::move( filename ), lineno, std::move( msg ),
std::move( fmt ), msgId ) {
init();
}
void
MessageFormatter::formatWallClock( const unsigned char * buf,
std::ostream & os ) const {
formatWallClockTimeStamp( buf, os );
}
void
MessageFormatter::addPlugin() {
std::string libDir;
#if defined (__LP64__)
libDir = "/usr/lib64/";
#else
libDir = "/usr/lib/";
#endif
std::string path = libDir + "QtTailPlugin/";
if ( getenv( "QT_FORMATTER_DIR" ) ) {
path = getenv( "QT_FORMATTER_DIR" );
}
if ( path.back() != '/' ) {
path += "/";
}
DIR * dir = opendir( path.c_str() );
if ( dir == NULL ) {
// plugin dir may not exist and there are no plugins to be added.
if ( errno!= ENOENT ) {
std::cerr << "Failed to open " << path
<< " with error " << strerror( errno ) << std::endl;
}
return;
}
struct dirent * libName = readdir( dir );
for ( ; libName != NULL; libName = readdir( dir ) ) {
std::string fName = libName->d_name;
if ( fName.compare( ".." ) == 0 || fName.compare( "." ) == 0 ) {
continue;
}
std::string libPath = path + libName->d_name;
void * handle = dlopen( libPath.c_str(), RTLD_NOW );
if ( !handle ) {
std::cerr << "qttail failed to load library " << libPath
<< " with error : " << dlerror() << std::endl;
continue;
}
}
closedir( dir );
}
int
MessageFormatter::format( const unsigned char * buf, std::ostream & os ) const {
int n = 0;
for ( auto & formatter : formatters_ ) {
if ( formatter.second != nullptr ) {
// static text or C formatter specifications
formatter.first(
reinterpret_cast< const unsigned char * >( formatter.second ),
os );
} else {
// parameter to decode from the ring buffer
unsigned l = formatter.first( buf + n, os );
if ( l == 0 ) {
return -1; // failed to decode parameter
}
n += l;
}
}
return n;
}
int
MessageFormatter::length( const unsigned char * buf ) const {
int n = 0;
for ( auto & length : lengths_ ) {
unsigned l = length( buf + n );
if ( l == 0 ) {
return -1; // failed to decode parameters
}
n += l;
}
return n;
}
void
MessageFormatter::addNewFormattersById( std::string name,
MessageFormatter::FormatterType type ) {
formattersById_.emplace( name, type );
}
void
MessageFormatter::addNewLengthById( std::string name,
MessageFormatter::LengthType type ) {
lengthsById_.emplace( name, type );
}
unsigned
MessageFormatter::applyCFormatSpec( const unsigned char * buf, std::ostream & os ) {
CFormatSpec formatSpec;
formatSpec.value = reinterpret_cast< intptr_t >( buf );
if ( formatSpec.showPos ) {
os << std::showpos;
}
if ( formatSpec.zeroPad ) {
os << std::setfill( '0' );
}
if ( formatSpec.hasWidth ) {
os << std::fixed << std::setw( formatSpec.width );
}
if ( formatSpec.hasPrecision ) {
os << std::fixed << std::setprecision( formatSpec.precision );
}
return 0;
}
unsigned
MessageFormatter::formatStatic( const unsigned char * buf, std::ostream & os ) {
os << reinterpret_cast< const char * >( buf );
return 0;
}
MessageFormatter::FormatterType
MessageFormatter::selectFormatter( const std::string & fmt, bool isHex ) {
// First check if we have an exactly matching formatter
if ( isHex && hexFormattersById_.count( fmt ) ) {
return hexFormattersById_.at( fmt );
} else if ( !isHex && formattersById_.count( fmt ) ) {
return formattersById_.at( fmt );
}
// No matching formatter, it could be that this is the first time we run into a
// parameterized formatter.
for ( const auto & [ key, parameterizedFormatter ] :
parameterizedFormattersById() ) {
// We perform a reverse search, starting at index 0 so this checks for
// the prefix
if ( fmt.rfind( key, 0 ) == 0 ) {
// We found a parameterized formatter! Now resolve the inner formatter
auto innerFmt = fmt.substr( key.size() );
FormatterType innerFormatter = nullptr;
if ( isHex && hexFormattersById_.count( innerFmt ) ) {
innerFormatter = hexFormattersById_.at( innerFmt );
} else if ( formattersById_.count( innerFmt ) ) {
// Fall back to a non-hex formatter even if isHex is specified
innerFormatter = formattersById_.at( innerFmt );
}
if ( !innerFormatter ) {
return nullptr;
}
auto formatter = [ parameterizedFormatter, innerFormatter ](
const unsigned char * buf,
std::ostream & os ) -> unsigned {
return parameterizedFormatter( innerFormatter, buf, os );
};
// Record formatter in our map, so that following lookups can directly
// resolve it
if ( isHex ) {
hexFormattersById_.emplace( fmt, formatter );
} else {
formattersById_.emplace( fmt, formatter );
}
return formatter;
}
}
// It could be that we failed to find a hex matching formatter, fall back to a
// non-hex formatter in that case
if ( isHex && formattersById_.count( fmt ) ) {
return formattersById_.at( fmt );
} else {
return nullptr;
}
}
MessageFormatter::LengthType
MessageFormatter::selectLength( const std::string & fmt ) {
for ( const auto & [ key, parameterizedLen ] : parameterizedLengthsById_ ) {
// We perform a reverse search, starting at index 0 so this check for
// the prefix
if ( fmt.rfind( key, 0 ) == 0 ) {
auto innerFmt = fmt.substr( key.size() );
auto innerLen = lengthsById_.at( innerFmt );
return
[ parameterizedLen, innerLen ]( const unsigned char * buf ) -> unsigned {
return parameterizedLen( innerLen, buf );
};
}
}
return lengthsById_.at( fmt );
}
void
MessageFormatter::init() {
const char * fmtPtr = fmt().c_str();
tokenizedMsg_ = msg();
char * msgPtr = &tokenizedMsg_[ 0 ];
auto addFormatter = [&]( char conversionSpecifier ) {
const char * fmtEnd = strchr( fmtPtr, ',' );
if ( fmtEnd == nullptr ) {
fmtEnd = fmtPtr + strlen( fmtPtr );
}
std::string fmt( fmtPtr, fmtEnd - fmtPtr );
FormatterType formatter = nullptr;
switch ( conversionSpecifier ) {
case 'p':
case 'x': {
// Pick a hex formatter, or fallback to a default one if none are found
formatter = selectFormatter( fmt, true );
break;
}
default: {
formatter = selectFormatter( fmt, false );
break;
}
}
if ( !formatter ) {
std::cerr << "unsupported format: " << fmt << std::endl;
exit( EXIT_FAILURE );
}
formatters_.push_back( std::make_pair( formatter, nullptr ) );
MessageFormatter::LengthType lenById = selectLength( fmt );
lengths_.push_back( lenById );
fmtPtr = fmtEnd;
if ( *fmtEnd == ',' ) {
fmtPtr++;
}
};
auto getFormatParamSpec = []( const char * param ) ->
std::pair< unsigned, CFormatSpec > {
// return the length of a format parameter
// the parameter can have length or other modifiers, e.g. %08p or %#A
// zero if this is not a valid format parameter
unsigned len = 0;
CFormatSpec cFormatSpec;
cFormatSpec.value = 0;
if ( param != nullptr ) {
if ( param[ ++len ] == '%' ) {
// valid, % -> %%
len++;
} else {
while ( strchr( "#0- +'I", param[ len ] ) != nullptr ) {
// flags
switch ( param[ len++ ] ) {
case '+':
cFormatSpec.showPos = 1;
break;
case '0':
cFormatSpec.zeroPad = 1;
break;
default:
// ignore all other flags, they are not supported
break;
}
}
if ( isdigit( param[ len ] ) ) {
// width
cFormatSpec.hasWidth = 1;
unsigned pos = len;
while ( isdigit( param[ len ] ) ) {
len++;
}
cFormatSpec.width = std::stoi( std::string( param + pos,
len - pos ) );
}
if ( param[ len ] == '.' ) {
// precision
cFormatSpec.hasPrecision = 1;
unsigned pos = ++len;
while ( isdigit( param[ len ] ) ) {
len++;
}
if ( len > pos ) {
cFormatSpec.precision = std::stoi( std::string( param + pos,
len - pos ) );
}
}
while ( strchr( "hlqLjzZt", param[ len ] ) != nullptr ) {
// length modifier, ignored as it has no effect
len++;
}
if ( isalpha( param[ len ] ) ) {
// conversion specifier, consider any alphabetic character valid
len++;
} else {
len = 0; // indicates invalid format spec
}
}
}
return std::make_pair( len, cFormatSpec );
};
// The messages with wall-clock timestamp are using RingBuf to store the
// timestamp data and use file line-number value of 0 to differentiate such
// messages. Add special formatter to ignore these timestamp fields in
// RingBuf while printing the message.
// Note that the wall-clock time returned by gettimeofday is stored in
// RingBuf in two different fields (tv_sec & tv_usec) instead of storing
// in single 'struct timeval' format. If we store the timestamp data in
// 'struct timeval' format, we need a new formatString to decode that data.
// Such change will be incompatible with the older versions of qtcat/qttail
// file as it cannot parse this new formatString. By storing the timestamp in
// two different fields, we can reuse the existing formatString 'q'.
static const char * wcFormatStr = "q,q,";
static unsigned wcFormatStrLen = strlen( wcFormatStr );
if ( lineno() == 0 && strncmp( fmtPtr, wcFormatStr, wcFormatStrLen ) == 0 ) {
fmtPtr += wcFormatStrLen;
MessageFormatter::FormatterType formatter = selectFormatter( "w", false );
MessageFormatter::LengthType lenById = selectLength( "w" );
formatters_.emplace_back( formatter, nullptr );
lengths_.push_back( lenById );
}
while ( *msgPtr != '\0' ) {
char * param = strchr( msgPtr, '%' );
unsigned fpLen;
CFormatSpec cFormatSpec;
std::tie( fpLen, cFormatSpec ) = getFormatParamSpec( param );
while ( param != nullptr && fpLen == 0 ) {
param = strchr( param + 1, '%' );
std::tie( fpLen, cFormatSpec ) = getFormatParamSpec( param );
}
if ( param == nullptr ) {
// no more parameters, consume remaining message
formatters_.push_back( std::make_pair( formatStatic, msgPtr ) );
msgPtr += strlen( msgPtr );
} else {
char conversionSpecifier = param[ fpLen - 1 ];
if ( conversionSpecifier == '%' ) {
formatters_.push_back( std::make_pair( formatStatic, msgPtr ) );
param[ fpLen - 1 ] = '\0';
} else {
if ( *fmtPtr == '\0' ) {
// there are no more parameters, abort
break;
}
if ( param > msgPtr ) {
formatters_.push_back( std::make_pair( formatStatic, msgPtr ) );
*param = '\0';
}
if ( conversionSpecifier == 'p' ) {
// p format is hex with a 0x prefix
formatters_.push_back( std::make_pair( formatStatic, "0x" ) );
}
if ( cFormatSpec.value != 0 ) {
formatters_.push_back(
std::make_pair( applyCFormatSpec,
reinterpret_cast< const char * >(
cFormatSpec.value ) ) );
}
addFormatter( conversionSpecifier );
if ( cFormatSpec.value != 0 ) {
// undo the effects of applyCFormatSpec
formatters_.push_back(
std::make_pair( resetCFormatSpec,
reinterpret_cast< const char * >(
cFormatSpec.value ) ) );
}
}
msgPtr = param + fpLen;
}
}
if ( *msgPtr != '\0' || *fmtPtr != '\0' ) {
// a message might have a different number of parameters than placeholders
// due to programming errors. in these cases, for compatibility with qtcat,
// emit the log message and the parameters separately
lengths_.clear();
formatters_.clear();
formatters_.push_back( std::make_pair( formatStatic, &msg()[ 0 ] ) );
formatters_.push_back( std::make_pair( formatStatic, " % (" ) );
for ( fmtPtr = fmt().c_str(); *fmtPtr != '\0'; ) {
if ( fmtPtr != fmt().c_str() ) {
formatters_.push_back( std::make_pair( formatStatic, "," ) );
}
addFormatter( 's' );
}
formatters_.push_back( std::make_pair( formatStatic, ") " ) );
}
}
unsigned
MessageFormatter::resetCFormatSpec( const unsigned char *, std::ostream & os ) {
os << std::noshowpos << std::setfill( ' ' ) << std::defaultfloat;
return 0;
}
using ParameterizedFormatterMap = MessageFormatter::ParameterizedFormatterType;
const std::unordered_map< std::string, ParameterizedFormatterMap >
MessageFormatter::parameterizedFormattersById() {
static std::unordered_map< std::string, ParameterizedFormatterMap > map;
if ( map.empty() ) {
// If the map is being initialized, ensure that the parameterized format
// strings do not overlap with the non-parameterized ones.
map = { { "OPT", formatOpt } };
for ( const auto & [ nonParamFmt, nonParamFormatter ] : formattersById_ ) {
for ( const auto & [ fmt, formatter ] : map ) {
assert( nonParamFmt.rfind( fmt, 0 ) == std::string::npos );
}
}
}
return map;
}
std::unordered_map< std::string, MessageFormatter::FormatterType >
MessageFormatter::formattersById_{
// see qtcat for supported formats
{ "u", formatU8 }, // QuickTrace
{ "s", formatU16 }, // QuickTrace
{ "i", formatU32 }, // QuickTrace
{ "q", formatU64 }, // QuickTrace
{ "c", formatChar }, // QuickTrace
{ "p", formatString }, // QuickTrace
{ "b", formatBool }, // QuickTrace
{ "f", formatFloat }, // QuickTrace
{ "d", formatDouble }, // QuickTrace
{ "w", formatWallClockRingBuf } // Used internally by qttail
};
std::unordered_map< std::string, MessageFormatter::FormatterType >
MessageFormatter::hexFormattersById_{ { "u", formatU8Hex },
{ "s", formatU16Hex },
{ "i", formatU32Hex },
{ "q", formatU64Hex } };
std::unordered_map< std::string, MessageFormatter::LengthType >
MessageFormatter::lengthsById_{
{ "u", lengthU8 },
{ "s", lengthU16 },
{ "i", lengthU32 },
{ "q", lengthU64 },
{ "c", lengthChar },
{ "p", lengthString },
{ "b", lengthBool },
{ "f", lengthFloat },
{ "d", lengthDouble },
{ "w", lengthWallClock }
};
const std::unordered_map< std::string, MessageFormatter::ParameterizedLengthType >
MessageFormatter::parameterizedLengthsById_{ { "OPT", lengthOpt } };
} // namespace QuickTrace