-
Notifications
You must be signed in to change notification settings - Fork 1
/
MsgStream.h
702 lines (618 loc) · 19.8 KB
/
MsgStream.h
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
#ifndef RINGERCORE_MSGSTREAMMIRROR_H
#define RINGERCORE_MSGSTREAMMIRROR_H
#include <cstring>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include "RingerCore/defines.h"
/**
* Macro to be used within MsgService inherited classes.
*
* It will check if message is above level before requesting to enter it into
* MsgStreamMirror to check it.
**/
#define MSG_LVL_CHK(xmsg, lvl) do { \
if ( msgLevel( lvl ) ) { \
msg() << lvl << xmsg << endreq; \
} \
} while (0);
/**
* Macro for check and displaying DEBUG messages.
*
* We only display DEBUG messages if building with debug set
**/
#if defined(DBG_LEVEL) && DBG_LEVEL > 0
# define MSG_DEBUG(xmsg) MSG_LVL_CHK( xmsg, ::MSG::DEBUG )
//# pragma message "Compiled with debug messages!"
#else
# define MSG_DEBUG(xmsg) while(0) { std::ostringstream s; s << xmsg; }
#endif // DEBUG > 0
/**
* Macro for displaying INFO messages
**/
#define MSG_INFO(xmsg) MSG_LVL_CHK( xmsg, ::MSG::INFO )
/**
* Macro for displaying WARNING messages
**/
#define MSG_WARNING(xmsg) MSG_LVL_CHK( xmsg, ::MSG::WARNING )
/**
* Macro for displaying VERBOSE messages
**/
#define MSG_ERROR(xmsg) MSG_LVL_CHK( xmsg, ::MSG::ERROR )
/**
* Macro for displaying FATAL messages
*
* It will also raise a std::runtime_error with same message.
**/
#define MSG_FATAL(xmsg) \
msg() << MSG::FATAL << xmsg; \
auto e = std::runtime_error( msg().stream().str() ); \
msg().doOutput(); \
throw e;
namespace MSG
{
/**
* The multiple message levels available
**/
enum Level {
NIL = 0,
VERBOSE = 1,
DEBUG = 2,
INFO = 3,
WARNING = 4,
ERROR = 5,
FATAL = 6
};
} // namespace MSG
/**
* @brief Get C string from enum
**/
//==============================================================================
inline
const char* to_str(const MSG::Level lvl)
{
switch (lvl){
case MSG::VERBOSE:
return "VERBOSE";
case MSG::DEBUG:
return "DEBUG";
case MSG::INFO:
return "INFO";
case MSG::WARNING:
return "WARNING";
case MSG::ERROR:
return "ERROR";
case MSG::FATAL:
return "FATAL";
default:
throw std::runtime_error("Couldn't determine LEVEL enumeration.");
}
}
/**
* @brief Overload ostringstream to accept MSG::Level
*
* In fact, we only need this prototype for the compile to accept the macros
* declared above. It is implemented only for convinience, as it should be the
* wanted behavior in case anyone decides to enter the MSG::Level accidently to
* an ostringstream.
**/
inline
std::ostringstream& operator<<(std::ostringstream &s,
MSG::Level /*level*/)
{
return s;
}
/**
* @brief Overload operator << to accept display vectors
**/
template <typename T>
std::ostream& operator<< ( std::ostream& out, const std::vector< T >& vec )
{
// A little prefix:
out << "[";
// Print the contents:
for( size_t i = 0; i < vec.size(); ++i ) {
out << vec[ i ];
if( i < vec.size() - 1 ) {
out << ", ";
}
}
// A little postfix:
out << "]";
// Return the stream:
return out;
}
/**
* @brief Adds logging capability to inherited classes
*
* Based on Athena framework's MsgStream (almost a copy, but without using
* Gaudi infrastructure). It cannot have the same name as the Asg class,
* otherwise we'll have conflicts on PyROOT usage.
**/
class MsgStreamMirror
{
public:
/**
* @brief Helper class for displaying MsgStreamMirror messages
**/
class Message {
public:
/// Number of charatecters before displaying message
static constexpr unsigned space_between_log_and_msg = 45;
///
static constexpr const char* color[] = {
WHITE,
GRAY,
CYAN,
GREEN,
YELLOW,
RED,
BOLDRED,
};
/// Ctor
explicit Message( const std::string &logName,
const MSG::Level level,
const bool useColor,
const std::string &message )
: m_formatted_msg("")
{
if (useColor){ m_formatted_msg += color[level]; }
const char* lvl_str = to_str(level);
const size_t end_log_name = space_between_log_and_msg
- (std::strlen(lvl_str) + 2 );
m_formatted_msg += "Cxx.";
m_formatted_msg += logName.substr(0, end_log_name - 4 );
m_formatted_msg.append( end_log_name
- m_formatted_msg.length() + ((useColor)?5:0) + 1
, 0x20);
m_formatted_msg += lvl_str;
m_formatted_msg += 0x20;
m_formatted_msg += message;
if (useColor){ m_formatted_msg += RESET; }
}
/// Overloads std::cout printing capabilities
friend std::ostream &operator<<(
std::ostream &stream,
Message &msg );
private:
/// The formated message
std::string m_formatted_msg;
};
/// Ctor using standard configuration
/// @{
MsgStreamMirror()
: m_streamName("RingerCore_Log"),
m_level(MSG::INFO),
m_currentLevel(MSG::INFO),
m_useColor(false),
m_active(true){;}
/// Ctor using integer for setting logname and msgLevel
explicit MsgStreamMirror(const std::string &logname, const int msgLevel)
: m_streamName(logname),
m_level( ( msgLevel > MSG::FATAL ) ? ( MSG::FATAL )
: ( ( msgLevel < MSG::VERBOSE ) ? ( MSG::VERBOSE )
: ( static_cast<MSG::Level>( msgLevel ) )
)
),
m_currentLevel(MSG::INFO),
m_useColor(false),
m_active( m_level <= m_currentLevel )
{;}
/// Ctor setting logname and msgLevel
explicit MsgStreamMirror(const std::string &logname, const MSG::Level msgLevel)
: m_streamName(logname),
m_level(msgLevel),
m_currentLevel(MSG::INFO),
m_useColor(false),
m_active(m_level <= m_currentLevel)
{;}
/// Ctor using integer for setting logname and msgLevel and color
explicit MsgStreamMirror(const std::string &logname, const int msgLevel, const bool useColor)
: m_streamName(logname),
m_level( ( msgLevel > MSG::FATAL ) ? ( MSG::FATAL )
: ( ( msgLevel < MSG::VERBOSE ) ? ( MSG::VERBOSE )
: ( static_cast<MSG::Level>( msgLevel ) )
)
),
m_currentLevel(MSG::INFO),
m_useColor(useColor),
m_active( m_level <= m_currentLevel )
{;}
/// Ctor setting logname and msgLevel
explicit MsgStreamMirror(const std::string &logname, const MSG::Level msgLevel, const bool useColor)
: m_streamName(logname),
m_level(msgLevel),
m_currentLevel(MSG::INFO),
m_useColor(useColor),
m_active(m_level <= m_currentLevel)
{;}
/// Copy constructor
MsgStreamMirror(const MsgStreamMirror& msg)
: m_streamName(msg.m_streamName),
m_level(msg.m_level),
m_currentLevel(msg.m_currentLevel),
m_useColor(msg.m_useColor),
m_active(msg.m_active)
{
try { // ignore exception if we cannot copy the string
m_stream.flags( msg.m_stream.flags() );
} catch (...) {;}
}
/// @}
/// Inline methods
/// @{
/// Retrieve current message level
MSG::Level level() const
{
return m_level;
}
/// Retrieve a reference to current logname
const std::string& logName() const
{
return m_streamName;
}
/// Change logger level
bool useColor(){
return m_useColor;
}
/// Change logger level
void setLevel(MSG::Level msgLevel){
if ( msgLevel != m_level ){
m_level = msgLevel;
// Report change of level:
report(m_currentLevel);
}
}
/// Change logger level
void setUseColor(const bool useColor){
m_useColor = useColor;
}
/// Change this logger name
void setLogName(const std::string &logName)
{
m_streamName = logName;
}
/// oMsgStreamMirror flush emulation
MsgStreamMirror& flush() {
if ( isActive() ) m_stream.flush();
return *this;
}
/// Operators overload:
/// @{
/// Accept MsgStreamMirror modifiers (this will call endreq/endmsg)
MsgStreamMirror& operator<<(MsgStreamMirror& (*_f)(MsgStreamMirror&)) {
if ( isActive() ) _f(*this);
return *this;
}
/// Accept oMsgStreamMirror modifiers
MsgStreamMirror& operator<<(std::ostream& (*_f)(std::ostream&)) {
if ( isActive() ) _f(m_stream);
return *this;
}
/// Accept ios modifiers
MsgStreamMirror& operator<<(std::ios& (*_f)(std::ios&)) {
if ( isActive() ) _f(m_stream);
return *this;
}
/// Accept MsgStreamMirror activation using MsgStreamMirrorer operator
MsgStreamMirror& operator<< (MSG::Level level) {
return report(level);
}
/// Accept MsgStreamMirror activation using MsgStreamMirrorer operator
MsgStreamMirror& operator<<(long long arg) {
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
m_stream << arg;
} catch (...) {}
return *this;
}
/// @}
/// Initialize report of new message: activate if print level is sufficient.
MsgStreamMirror& report(MSG::Level lvl) {
if ( ( m_currentLevel = MSG::Level(lvl) ) >= level() ) {
activate();
} else {
deactivate();
}
return *this;
}
/// Activate MsgStreamMirror
void activate() {
m_active = true;
}
/// Deactivate MsgStreamMirror
void deactivate() {
m_active = false;
}
/// Accessor: is MsgStreamMirror active
bool isActive() const {
return m_active;
}
/// Check if logger is active at message level
bool msgLevel(const MSG::Level lvl) const {
return this->level() <= lvl;
}
/// IOS emulation
/// @{
long flags() const {
return isActive() ? m_stream.flags() : 0;
}
long flags(std::ios_base::fmtflags v) {
return isActive() ? m_stream.flags(v) : 0;
}
long setf(std::ios_base::fmtflags v) {
return isActive() ? m_stream.setf(v) : 0;
}
int width() const {
return isActive() ? m_stream.width() : 0;
}
int width(int v) {
return isActive() ? m_stream.width(v) : 0;
}
char fill() const {
return isActive() ? m_stream.fill() : (char)-1;
}
char fill(char v) {
return isActive() ? m_stream.fill(v) : (char)-1;
}
int precision() const {
return isActive() ? m_stream.precision(): 0;
}
int precision(int v) {
return isActive() ? m_stream.precision(v): 0;
}
int rdstate() const {
return isActive() ? m_stream.rdstate () : std::ios_base::failbit;
}
int good() const {
return isActive() ? m_stream.good () : 0;
}
int eof() const {
return isActive() ? m_stream.eof () : 0;
}
int bad() const {
return isActive() ? m_stream.bad() : 0;
}
long setf(std::ios_base::fmtflags _f, std::ios_base::fmtflags _m) {
return isActive() ? m_stream.setf(_f, _m) : 0;
}
void unsetf(std::ios_base::fmtflags _l) {
if ( isActive() ) m_stream.unsetf(_l);
}
void clear(std::ios_base::iostate _i = std::ios_base::failbit) {
if ( isActive() ) m_stream.clear(_i);
}
/// @}
/// @}
/// Print information about the log
void print( const MSG::Level lvl );
/// Output method
MsgStreamMirror& doOutput();
/// Access string MsgStreamMirror
std::ostringstream& stream(){
return m_stream;
}
private:
/// Stream name used for identifying the origin from the message display
std::string m_streamName;
/// The message level where the stream should accept the input
MSG::Level m_level;
/// Message level for the current input string
MSG::Level m_currentLevel;
/// Whether stream uses color
bool m_useColor;
/// The auxiliary caching string:
std::ostringstream m_stream;
/// Whether we are active
bool m_active;
};
/// MsgStreamMirror Modifier: endmsg. Calls the output method of the MsgStreamMirror
inline
MsgStreamMirror& endmsg(MsgStreamMirror& s)
{
return s.doOutput();
}
#define endreq endmsg
#if defined (__GNUC__) && ! defined(__clang__)
inline MsgStreamMirror& operator << (MsgStreamMirror& s,
const std::_Setiosflags &manip) {
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
if ( s.isActive() ) s.stream() << manip;
} catch(...) {}
return s;
}
inline MsgStreamMirror& operator << (MsgStreamMirror& s,
const std::_Resetiosflags &manip) {
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
if ( s.isActive() ) s.stream() << manip;
} catch (...) {}
return s;
}
inline MsgStreamMirror& operator << (MsgStreamMirror& s,
const std::_Setbase &manip) {
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
if ( s.isActive() ) s.stream() << manip;
} catch (...) {}
return s;
}
inline MsgStreamMirror& operator << (MsgStreamMirror& s,
const std::_Setprecision &manip) {
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
if ( s.isActive() ) s.stream() << manip;
} catch (...) {}
return s;
}
inline MsgStreamMirror& operator << (MsgStreamMirror& s,
const std::_Setw &manip) {
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
if ( s.isActive() ) s.stream() << manip;
} catch (...) {}
return s;
}
namespace MSG {
inline
MsgStreamMirror& dec(MsgStreamMirror& log) {
log.setf(std::ios_base::dec, std::ios_base::basefield);
return log;
}
inline
MsgStreamMirror& hex(MsgStreamMirror& log) {
log.setf(std::ios_base::hex, std::ios_base::basefield);
return log;
}
}
#endif // WIN32 or (__GNUC__)
/// Specialization to avoid the generation of implementations for char[].
/// \see {<a href="https://savannah.cern.ch/bugs/?87340">bug #87340</a>}
inline MsgStreamMirror& operator<< (MsgStreamMirror& s, const char *arg){
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
if ( s.isActive() ) s.stream() << arg;
} catch (...) {}
return s;
}
/// General templated stream operator
template <typename T>
MsgStreamMirror& operator<< (MsgStreamMirror& lhs, const T& arg) {
if(lhs.isActive())
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
lhs.stream() << arg;
}
catch (...) {}
return lhs;
}
#if defined(__GNUC__) && ! defined(__clang__)
/// compiler is stupid. Must specialize
template<typename T>
MsgStreamMirror& operator << (MsgStreamMirror& lhs, const std::_Setfill<T> &manip) {
if ( lhs.isActive() )
try {
// this may throw, and we cannot afford it if the stream is used in a catch block
lhs.stream() << manip;
} catch(...) {}
return lhs;
}
#endif
//==============================================================================
inline
std::ostream &operator<<(
std::ostream &stream,
MsgStreamMirror::Message &msg )
{
return stream << msg.m_formatted_msg;
}
/**
* @brief Adds messaging capability to interface
**/
class IMsgService
{
protected:
/// Stores the default stream name for this interface
std::string m_defName;
/// Default level for this interface
MSG::Level m_defLevel;
/// Default useColor for this interface
bool m_defUseColor;
public:
/**
* @brief Builds default interface
**/
IMsgService()
: m_defName("MsgStreamMirror"),
m_defLevel( MSG::INFO ),
m_defUseColor( false ){;}
/**
* @brief Defines default log name and default level for Messaging Service
**/
IMsgService( const std::string& defName,
const MSG::Level defLevel = MSG::INFO ,
const bool defUseColor = false )
: m_defName(defName),
m_defLevel(defLevel),
m_defUseColor( defUseColor ){;}
virtual ~IMsgService(){;}
/// Check if stream will display at message level
bool msgLevel(const MSG::Level lvl) const
{
return msg().msgLevel(lvl);
}
/// Change stream message level display
void setMsgLevel(const MSG::Level lvl)
{
msg().setLevel(lvl);
}
/// Get Level of output from MsgStreamMirror Manager
MSG::Level getMsgLevel() const
{
return msg().level();
}
/// Change whether stream use colored messages
void setUseColor(const bool useColor)
{
msg().setUseColor(useColor);
}
/// Get whether stream use colored messages
bool getUseColor() const
{
return msg().useColor();
}
/// Change stream display name
void setLogName(const std::string &name)
{
msg().setLogName(name);
}
/// Get stream name
const std::string& getLogName() const
{
return msg().logName();
}
/// Get streamer
virtual MsgStreamMirror& msg() = 0;
/// cv get streamer
virtual MsgStreamMirror& msg() const = 0;
};
/**
* @brief Consolidate message service capabilitiy
**/
class MsgService : virtual public IMsgService
{
public:
/**
* @brief Builds Message service using default configuration set by
* interface
**/
MsgService() :
m_log( m_defName, m_defLevel, m_defUseColor ){;}
explicit MsgService(const int lvl)
: m_log( m_defName, lvl, m_defUseColor ){;}
/**
* @brief Builds Message service changing level
**/
explicit MsgService(const MSG::Level lvl)
: m_log( m_defName, lvl, m_defUseColor ){;}
explicit MsgService(const int lvl, const bool useColor)
: m_log( m_defName, lvl, useColor ){;}
explicit MsgService(const MSG::Level lvl, const bool useColor)
: m_log( m_defName, lvl, useColor ){;}
/// Retrieve log
MsgStreamMirror& msg() override final {
return m_log;
}
/// Const Retrieve log
MsgStreamMirror& msg() const override final {
return m_log;
}
private:
/// Message Stream:
mutable MsgStreamMirror m_log;
};
#endif