| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

MessageSvc.cpp
Go to the documentation of this file.
1 #ifdef _WIN32
2 // Avoid conflicts between windows and the message service.
3 #define NOMSG
4 #define NOGDI
5 #endif
6
7 #include "GaudiKernel/Kernel.h"
8 #include "GaudiKernel/StatusCode.h"
9 #include "GaudiKernel/Message.h"
10 #include "GaudiKernel/System.h"
11 #include "MessageSvc.h"
12
13 #include <sstream>
14 #include <iostream>
15 #include <fstream>
16
17
18 namespace {
19
20 // erase_if functions for containers which do NOT invalidate iterators
21 // after the erase point, eg.std::{unordered_}{,multi}map, std::{forward_,}list.
22 // To be explicit: this does NOT work with std::vector.
23
24 // TODO: replace with std::experimental::erase_if (Libraries Fundamental TS v2)
25
26 template < typename Container, typename Iterator, typename Predicate >
27 void erase_if( Container& c, Iterator first, Iterator last, Predicate pred) {
28 while ( first!=last ) {
29 if ( pred(*first) ) first = c.erase(first);
30 else ++first;
31 }
32 }
33
34 template< typename Container, typename Predicate >
35 void erase_if( Container& c, Predicate pred ) {
36 return erase_if(c, std::begin(c), std::end(c),
37 std::forward<Predicate>(pred) );
38 }
39
40 template< typename Container, typename Iterator, typename Predicate >
41 void erase_if( Container& c, std::pair<Iterator,Iterator> range, Predicate pred ) {
42 return erase_if(c, std::move(range.first), std::move(range.second),
43 std::forward<Predicate>(pred) );
44 }
45
46 }
47
48 // Instantiation of a static factory class used by clients to create
49 // instances of this service
50 DECLARE_COMPONENT(MessageSvc)
51
52 static const std::string levelNames[MSG::NUM_LEVELS] = {
53 "NIL", "VERBOSE", "DEBUG", "INFO",
54 "WARNING", "ERROR", "FATAL", "ALWAYS"
55 };
56
57 // Constructor
58 MessageSvc::MessageSvc( const std::string& name, ISvcLocator* svcloc )
59 : base_class( name, svcloc ) {
60 m_defaultStream = &std::cout;
61 declareProperty( "Format", m_defaultFormat = Message::getDefaultFormat() );
62 declareProperty( "timeFormat", m_defaultTimeFormat = Message::getDefaultTimeFormat() );
63 declareProperty( "showStats", m_stats = false );
64 declareProperty( "statLevel", m_statLevel = 0 );
65
66 // Special properties to control output level of individual sources
67 declareProperty( "setVerbose", m_thresholdProp[MSG::VERBOSE] );
68 declareProperty( "setDebug", m_thresholdProp[MSG::DEBUG] );
69 declareProperty( "setInfo", m_thresholdProp[MSG::INFO] );
70 declareProperty( "setWarning", m_thresholdProp[MSG::WARNING] );
71 declareProperty( "setError", m_thresholdProp[MSG::ERROR] );
72 declareProperty( "setFatal", m_thresholdProp[MSG::FATAL] );
73 declareProperty( "setAlways", m_thresholdProp[MSG::ALWAYS] );
74
75 declareProperty( "useColors", m_color=false)->declareUpdateHandler(&MessageSvc::initColors, this);
76
77 declareProperty( "fatalColorCode", m_logColors[MSG::FATAL] );
78 declareProperty( "errorColorCode", m_logColors[MSG::ERROR] );
79 declareProperty( "warningColorCode", m_logColors[MSG::WARNING] );
80 declareProperty( "infoColorCode", m_logColors[MSG::INFO] );
81 declareProperty( "debugColorCode", m_logColors[MSG::DEBUG] );
82 declareProperty( "verboseColorCode", m_logColors[MSG::VERBOSE] );
83 declareProperty( "alwaysColorCode", m_logColors[MSG::ALWAYS] );
84
85 const int defaultLimit = 500;
86 declareProperty( "fatalLimit", m_msgLimit[MSG::FATAL] = defaultLimit );
87 declareProperty( "errorLimit", m_msgLimit[MSG::ERROR] = defaultLimit );
88 declareProperty( "warningLimit", m_msgLimit[MSG::WARNING] = defaultLimit );
89 declareProperty( "infoLimit", m_msgLimit[MSG::INFO] = defaultLimit );
90 declareProperty( "debugLimit", m_msgLimit[MSG::DEBUG] = defaultLimit );
91 declareProperty( "verboseLimit", m_msgLimit[MSG::VERBOSE] = defaultLimit );
92 declareProperty( "alwaysLimit", m_msgLimit[MSG::ALWAYS] = 0 );
93
94 declareProperty( "defaultLimit", m_msgLimit[MSG::NIL] = defaultLimit );
95
96 declareProperty( "enableSuppression", m_suppress = false );
97 declareProperty( "countInactive", m_inactCount = false )->declareUpdateHandler( &MessageSvc::setupInactCount, this );
98 declareProperty( "tracedInactiveSources", m_tracedInactiveSources,
99 "for each message source specified, print a stack trace for"
100 "the unprotected and unseen messages" );
101 #ifndef NDEBUG
102 // initialize the MsgStream static flag.
103 MsgStream::enableCountInactive(m_inactCount);
104 #endif
105
106 declareProperty( "loggedStreams",
107 m_loggedStreamsName,
108 "MessageStream sources we want to dump into a logfile" );
109
110 for (int ic=0; ic<MSG::NUM_LEVELS; ++ic) {
111 m_logColors[ic].declareUpdateHandler(&MessageSvc::setupColors, this);
112 m_msgLimit[ic].declareUpdateHandler(&MessageSvc::setupLimits, this);
113 m_thresholdProp[ic].declareUpdateHandler(&MessageSvc::setupThreshold, this);
114 }
115
116 std::fill( std::begin(m_msgCount),std::end(m_msgCount), 0 );
117 }
118
119 //#############################################################################
120
121
123 StatusCode MessageSvc::initialize() {
124 StatusCode sc;
125 sc = Service::initialize();
126 if( sc.isFailure() ) return sc;
127 // Set my own properties
128 sc = setProperties();
129 if (sc.isFailure()) return sc;
130
131 #ifdef _WIN32
132 m_color = false;
133 #endif
134
135 //NOTE: m_colMap is used _before_ it is filled here,
136 // i.e. while it is still empty.
137 // Moving this initialization 'up' by eg. just
138 // having a 'static const' colMap does not leave
139 // the results invariant...
140 m_colMap["black"] = MSG::BLACK;
141 m_colMap["red"] = MSG::RED;
142 m_colMap["green"] = MSG::GREEN;
143 m_colMap["yellow"] = MSG::YELLOW;
144 m_colMap["blue"] = MSG::BLUE;
145 m_colMap["purple"] = MSG::PURPLE;
146 m_colMap["cyan"] = MSG::CYAN;
147 m_colMap["white"] = MSG::WHITE;
148
149 // make sure the map of logged stream names is initialized
150 setupLogStreams();
151
152 return StatusCode::SUCCESS;
153 }
154
155 //#############################################################################
156
158 StatusCode MessageSvc::reinitialize() {
159 m_state = Gaudi::StateMachine::OFFLINE;
160 return initialize();
161 }
162
163 //#############################################################################
164
165 void MessageSvc::initColors(Property& /*prop*/) {
166
167 if (m_color) {
168 static const std::array<std::pair<MSG::Level,std::vector<std::string>>,3> tbl{
169 { { MSG::FATAL, { { "[94;101;1m" } } },
170 { MSG::ERROR, { { "[97;101;1m" } } },
171 { MSG::WARNING, { { "[93;1m" } } } } };
172
173 for (const auto& p : tbl ) {
174 auto &lC = m_logColors[p.first];
175 if (lC.value().empty()) {
176 lC.set(p.second);
177 } else {
178 MessageSvc::setupColors( lC );
179 }
180 }
181
182 } else {
183
184 // reset all color codes;
185 for (int ic=0; ic<MSG::NUM_LEVELS; ++ic) {
186 m_logColors[ic].set( { } );
187 }
188
189 }
190
191 }
192
193 //#############################################################################
194
195 void MessageSvc::setupColors(Property& prop) {
196
197 if (! m_color) return;
198
199 static const std::array<std::pair<const char*,MSG::Level>,7> tbl {
200 { {"fatalColorCode", MSG::FATAL },
201 {"errorColorCode", MSG::ERROR },
202 {"warningColorCode", MSG::WARNING },
203 {"infoColorCode", MSG::INFO },
204 {"debugColorCode", MSG::DEBUG },
205 {"verboseColorCode", MSG::VERBOSE },
206 {"alwaysColorCode", MSG::ALWAYS } } };
207
208 auto i = std::find_if( std::begin(tbl),std::end(tbl),
209 [&](const std::pair<const char*,MSG::Level>& t) {
210 return prop.name() == t.first;
211 } );
212 if (i==std::end(tbl)) {
213 std::cout << "ERROR: Unknown message color parameter: " << prop.name()
214 << std::endl;
215 return;
216 }
217 int ic = i->second;
218
219 std::string code;
220 auto itr = m_logColors[ic].value().begin();
221
222 if ( m_logColors[ic].value().size() == 1 ) {
223
224 if (itr->empty()) {
225 code = "";
226 } else if (itr->compare(0,1,"[") == 0) {
227 code = "\033" + *itr;
228 } else {
229 code = "\033[" + colTrans(*itr, 90) + ";1m";
230 }
231
232 } else if (m_logColors[ic].value().size() == 2) {
233 auto itr2 = itr + 1;
234
235 code = "\033[" + colTrans(*itr, 90) + ";"
236 + colTrans(*itr2, 100) + ";1m";
237
238 }
239
240 m_logColorCodes[ic] = code;
241
242 }
243 //#############################################################################
244
245 void MessageSvc::setupLimits(Property& prop) {
246 // Just report problems in the settings of the limits and unknown limit parameters
247 if (prop.name() == "alwaysLimit") {
248 IntegerProperty *p = dynamic_cast<IntegerProperty*>(&prop);
249 if (p && p->value() != 0) {
250 std::cout << "MessageSvc ERROR: cannot suppress ALWAYS messages" << std::endl;
251 p->setValue(0);
252 }
253 } else if (prop.name() == "defaultLimit") {
254 for (int i = MSG::VERBOSE; i< MSG::NUM_LEVELS; ++i) {
255 if (i != MSG::ALWAYS) {
256 m_msgLimit[i] = m_msgLimit[MSG::NIL].value();
257 }
258 }
259 } else if (prop.name() != "fatalLimit" &&
260 prop.name() != "errorLimit" &&
261 prop.name() != "warningLimit" &&
262 prop.name() == "infoLimit" &&
263 prop.name() == "debugLimit" &&
264 prop.name() == "verboseLimit") {
265 std::cout << "MessageSvc ERROR: Unknown message limit parameter: "
266 << prop.name() << std::endl;
267 return;
268 }
269 }
270 //#############################################################################
271
272 void MessageSvc::setupThreshold(Property& prop) {
273
274 static const std::array<std::pair<const char*,MSG::Level>,7> tbl{
275 { { "setFatal", MSG::FATAL },
276 { "setError", MSG::ERROR },
277 { "setWarning", MSG::WARNING },
278 { "setInfo", MSG::INFO },
279 { "setDebug", MSG::DEBUG },
280 { "setVerbose", MSG::VERBOSE },
281 { "setAlways", MSG::ALWAYS } } };
282
283 auto i = std::find_if( std::begin(tbl),std::end(tbl),
284 [&](const std::pair<const char*,MSG::Level>& t) {
285 return prop.name() == t.first;
286 } );
287 if (i==std::end(tbl)) {
288 std::cerr << "MessageSvc ERROR: Unknown message threshold parameter: "
289 << prop.name() << std::endl;
290 return;
291 }
292 int ic = i->second;
293
294 StringArrayProperty *sap = dynamic_cast<StringArrayProperty*>( &prop);
295 if (!sap) {
296 std::cerr << "could not dcast " << prop.name()
297 << " to a StringArrayProperty (which it should be!)" << std::endl;
298 } else {
299 for ( auto& i : sap->value() ) setOutputLevel( i, ic );
300 }
301
302 }
303
304 //#############################################################################
305
306 #ifdef NDEBUG
307 void MessageSvc::setupInactCount(Property&) {}
308 #else
309 void MessageSvc::setupInactCount(Property&prop) {
310 if (prop.name() == "countInactive") {
311 BooleanProperty *p = dynamic_cast<BooleanProperty*>(&prop);
312 if (p)
313 MsgStream::enableCountInactive(p->value());
314 }
315 }
316 #endif
317
318
319 //#############################################################################
321 StatusCode MessageSvc::finalize() {
322
323 m_suppress = false;
324
325 {
326 std::ostringstream os;
327
328 if (m_stats) {
329 os << "Summarizing all message counts" << std::endl;
330 } else {
331 os << "Listing sources of suppressed message: " << std::endl;
332 }
333
334 os << "=====================================================" << std::endl;
335 os << " Message Source | Level | Count" << std::endl;
336 os << "-----------------------------+---------+-------------" << std::endl;
337
338
339 bool found(false);
340
341 for (auto itr=m_sourceMap.begin(); itr!=m_sourceMap.end(); ++itr) {
342 for (unsigned int ic = 0; ic < MSG::NUM_LEVELS; ++ic) {
343 if ( (itr->second.msg[ic] >= m_msgLimit[ic] && m_msgLimit[ic] != 0 ) ||
344 (m_stats && itr->second.msg[ic] > 0 && ic >= m_statLevel.value()) ) {
345 os << " ";
346 os.width(28);
347 os.setf(std::ios_base::left,std::ios_base::adjustfield);
348 os << itr->first;
349 os << "|";
350
351 os.width(8);
352 os.setf(std::ios_base::right,std::ios_base::adjustfield);
353 os << levelNames[ic];
354 os << " |";
355
356 os.width(9);
357 os << itr->second.msg[ic];
358 os << std::endl;
359
360 found = true;
361 }
362 }
363 }
364 os << "=====================================================" << std::endl;
365 if (found || m_stats) std::cout << os.str();
366 }
367
368 #ifndef NDEBUG
369 if (m_inactCount.value()) {
370
371 std::ostringstream os;
372 os << "Listing sources of Unprotected and Unseen messages\n";
373
374 bool found(false);
375
376 unsigned int ml(0);
377 for (const auto& itr : m_inactiveMap) {
378 for (unsigned int ic = 0; ic < MSG::NUM_LEVELS; ++ic) {
379 if (itr.second.msg[ic] != 0 && itr.first.length() > ml) {
380 ml = itr.first.length();
381 }
382 }
383 }
384
385 for (unsigned int i=0; i<ml+25; ++i) os << "=";
386
387 os << std::endl << " ";
388 os.width(ml+2);
389 os.setf(std::ios_base::left,std::ios_base::adjustfield);
390 os << "Message Source";
391 os.width(1);
392 os << "| Level | Count" << std::endl;
393
394 for (unsigned int i=0; i<ml+3; ++i) os << "-";
395 os << "+---------+-----------" << std::endl;
396
397
398 for (auto itr=m_inactiveMap.begin(); itr!=m_inactiveMap.end(); ++itr) {
399 for (unsigned int ic = 0; ic < MSG::NUM_LEVELS; ++ic) {
400 if (itr->second.msg[ic] != 0) {
401 os << " ";
402 os.width(ml+2);
403 os.setf(std::ios_base::left,std::ios_base::adjustfield);
404 os << itr->first;
405
406 os << "|";
407
408 os.width(8);
409 os.setf(std::ios_base::right,std::ios_base::adjustfield);
410 os << levelNames[ic];
411
412 os << " |";
413
414 os.width(9);
415 os << itr->second.msg[ic];
416
417 os << std::endl;
418
419 found = true;
420 }
421 }
422 }
423 for (unsigned int i=0; i<ml+25; ++i) os << "=";
424 os << std::endl;
425
426 if (found) std::cout << os.str();
427 }
428 #endif
429
430 return StatusCode::SUCCESS;
431 }
432
433 //#############################################################################
434 std::string MessageSvc::colTrans(std::string col, int offset) {
435 auto itr = m_colMap.find(col);
436 int icol = offset + ( (itr != m_colMap.end()) ? itr->second : 8 );
437 return std::to_string( icol );
438 }
439
440 //#############################################################################
441 // ---------------------------------------------------------------------------
442 // Routine: reportMessage
443 // Purpose: dispatches a message to the relevant streams.
444 // ---------------------------------------------------------------------------
445 //
446 void MessageSvc::reportMessage( const Message& msg, int outputLevel ) {
447 std::unique_lock<std::recursive_mutex> lock(m_reportMutex);
448 i_reportMessage(msg, outputLevel);
449 }
450
451 void MessageSvc::i_reportMessage( const Message& msg, int outputLevel ) {
452 int key = msg.getType();
453
454 ++m_msgCount[key];
455
456 const Message *cmsg = &msg;
457
458 // processing logged streams
459 if ( !m_loggedStreams.empty() ) {
460 auto iLog = m_loggedStreams.find( msg.getSource() );
461 if ( m_loggedStreams.end() != iLog ) {
462 (*iLog->second) << *cmsg << std::endl;
463 }
464 }
465
466 if ( m_suppress.value() || m_stats.value() ) {
467
468 // Increase the counter of 'key' type of messages for the source and
469 // get the new value.
470 const int nmsg = ++(m_sourceMap[msg.getSource()].msg[key]);
471
472 if (m_suppress.value() && m_msgLimit[key] != 0 ) {
473 if (nmsg > m_msgLimit[key]) return;
474 if (nmsg == m_msgLimit[key]) {
475 std::string txt = levelNames[key] + " message limit ("
476 + std::to_string( m_msgLimit[key].value() )
477 + ") reached for " + msg.getSource() + ". Suppressing further output.";
478 cmsg = new Message(msg.getSource(), MSG::WARNING, std::move(txt));
479 cmsg->setFormat(msg.getFormat());
480 }
481 }
482 }
483
484 auto range = m_streamMap.equal_range( key );
485 if ( range.first != m_streamMap.end() ) {
486 std::for_each( range.first,range.second, [&](StreamMap::const_reference sm) {
487 *sm.second.second << *cmsg << std::endl;
488 });
489 } else if ( key >= outputLevel ) {
490 msg.setFormat(m_defaultFormat);
491 msg.setTimeFormat(m_defaultTimeFormat);
492 if (!m_color) {
493 (*m_defaultStream) << *cmsg << std::endl << std::flush;
494 } else {
495 (*m_defaultStream) << m_logColorCodes[key] << *cmsg << "\033[m"
496 << std::endl << std::flush;
497 }
498 }
499
500 if (cmsg != &msg) { delete cmsg; }
501
502 }
503
504 //#############################################################################
505 // ---------------------------------------------------------------------------
506 // Routine: reportMessage
507 // Purpose: dispatches a message to the relevant streams.
508 // ---------------------------------------------------------------------------
509 //
510 void MessageSvc::reportMessage( const Message& msg ) {
511 reportMessage(msg, outputLevel(msg.getSource()));
512 }
513
514 //#############################################################################
515 // ---------------------------------------------------------------------------
516 // Routine: reportMessage
517 // Purpose: dispatches a message to the relevant streams.
518 // ---------------------------------------------------------------------------
519 //
520 void MessageSvc::reportMessage (const char* source,
521 int type,
522 const char* message) {
523 reportMessage( Message{ source, type, message } );
524 }
525
526 //#############################################################################
527 // ---------------------------------------------------------------------------
528 // Routine: reportMessage
529 // Purpose: dispatches a message to the relevant streams.
530 // ---------------------------------------------------------------------------
531 //
532 void MessageSvc::reportMessage (const std::string& source,
533 int type,
534 const std::string& message) {
535 reportMessage( Message{source, type, message} );
536 }
537
538 //#############################################################################
539 // ---------------------------------------------------------------------------
540 // Routine: sendMessage
541 // Purpose: finds a message for a given status code and dispatches it.
542 // ---------------------------------------------------------------------------
543 //
544 void MessageSvc::reportMessage (const StatusCode& code,
545 const std::string& source)
546 {
547 std::unique_lock<std::recursive_mutex> lock(m_messageMapMutex);
548 i_reportMessage(code, source);
549 }
550
551 void MessageSvc::i_reportMessage (const StatusCode& code,
552 const std::string& source)
553 {
554 int level = outputLevel(source);
555 auto report = [&](Message mesg) {
556 mesg.setSource( source );
557 Message stat_code( source, mesg.getType(), "Status Code " + std::to_string( code.getCode() ) );
558 i_reportMessage( std::move(stat_code), level );
559 i_reportMessage( std::move(mesg), level );
560 };
561
562 auto range = m_messageMap.equal_range( code );
563 if ( range.first != m_messageMap.end() ) {
564 std::for_each( range.first, range.second,
565 [&](MessageMap::const_reference sm) { report(sm.second); } );
566 } else {
567 report(m_defaultMessage);
568 }
569 }
570
571 //#############################################################################
572 // ---------------------------------------------------------------------------
573 // Routine: insertStream
574 // Purpose: inserts a stream for a message type.
575 // ---------------------------------------------------------------------------
576 //
577
578 void MessageSvc::insertStream (int key,
579 const std::string& name,
580 std::ostream *stream)
581 {
582 m_streamMap.emplace( key, NamedStream(name,stream) );
583 }
584
585 //#############################################################################
586 // ---------------------------------------------------------------------------
587 // Routine: eraseStream
588 // Purpose: erases all the streams for all the message types.
589 // ---------------------------------------------------------------------------
590 //
591
592 void MessageSvc::eraseStream()
593 {
594 m_streamMap.clear();
595 }
596
597 //#############################################################################
598 // ---------------------------------------------------------------------------
599 // Routine: eraseStream
600 // Purpose: erases all the streams for a message type.
601 // ---------------------------------------------------------------------------
602 //
603
604 void MessageSvc::eraseStream( int message_type )
605 {
606 m_streamMap.erase( message_type );
607 }
608
609 //#############################################################################
610 // ---------------------------------------------------------------------------
611 // Routine: eraseStream
612 // Purpose: erases one stream for a message type.
613 // ---------------------------------------------------------------------------
614 //
615
616 void MessageSvc::eraseStream( int key, std::ostream* stream ) {
617 if ( stream ) {
618 erase_if( m_streamMap, m_streamMap.equal_range(key),
619 [&](StreamMap::const_reference j)
620 { return j.second.second == stream; } );
621 }
622 }
623
624 //#############################################################################
625 // ---------------------------------------------------------------------------
626 // Routine: eraseStream
627 // Purpose: erases one stream for all message types.
628 // ---------------------------------------------------------------------------
629 //
630
631 void MessageSvc::eraseStream( std::ostream* stream ) {
632 if ( stream ) {
633 erase_if( m_streamMap, [&](StreamMap::const_reference j)
634 { return j.second.second == stream; } );
635 }
636 }
637
638 //#############################################################################
639 // ---------------------------------------------------------------------------
640 // Routine: insertMessage
641 // Purpose: inserts a message for a status code.
642 // ---------------------------------------------------------------------------
643 //
644
645 void MessageSvc::insertMessage( const StatusCode& key, const Message& msg )
646 {
647 std::unique_lock<std::recursive_mutex> lock(m_messageMapMutex);
648 m_messageMap.emplace( key, msg );
649 }
650
651 //#############################################################################
652 // ---------------------------------------------------------------------------
653 // Routine: eraseMessage
654 // Purpose: erases all the messages for all the status codes.
655 // ---------------------------------------------------------------------------
656 //
657
658 void MessageSvc::eraseMessage()
659 {
660 std::unique_lock<std::recursive_mutex> lock(m_messageMapMutex);
661 m_messageMap.clear();
662 }
663
664 //#############################################################################
665 // ---------------------------------------------------------------------------
666 // Routine: eraseMessage
667 // Purpose: erases all the messages for a status code.
668 // ---------------------------------------------------------------------------
669 //
670
671 void MessageSvc::eraseMessage( const StatusCode& key )
672 {
673 std::unique_lock<std::recursive_mutex> lock(m_messageMapMutex);
674 m_messageMap.erase( key );
675 }
676
677 //#############################################################################
678 // ---------------------------------------------------------------------------
679 // Routine: eraseMessage
680 // Purpose: erases one message for a status code.
681 // ---------------------------------------------------------------------------
682 //
683
684 void MessageSvc::eraseMessage( const StatusCode& key, const Message& msg )
685 {
686 std::unique_lock<std::recursive_mutex> lock(m_messageMapMutex);
687
688 erase_if( m_messageMap, m_messageMap.equal_range(key),
689 [&](MessageMap::const_reference j) { return j.second==msg; } ) ;
690 }
691
692 // ---------------------------------------------------------------------------
693 int MessageSvc::outputLevel() const {
694 // ---------------------------------------------------------------------------
695 return m_outputLevel;
696 }
697
698 // ---------------------------------------------------------------------------
699 int MessageSvc::outputLevel( const std::string& source ) const {
700 // ---------------------------------------------------------------------------
701 std::unique_lock<std::recursive_mutex> lock(m_thresholdMapMutex);
702 auto it = m_thresholdMap.find( source );
703 return it != m_thresholdMap.end() ? it->second : m_outputLevel.value();
704 }
705
706 // ---------------------------------------------------------------------------
707 void MessageSvc::setOutputLevel(int new_level) {
708 // ---------------------------------------------------------------------------
709 m_outputLevel = new_level;
710 }
711
712 // ---------------------------------------------------------------------------
713 void MessageSvc::setOutputLevel(const std::string& source, int level) {
714 // ---------------------------------------------------------------------------
715 std::unique_lock<std::recursive_mutex> lock(m_thresholdMapMutex);
716
717 // only write if we really have to...
718 auto i = m_thresholdMap.find( source );
719 if (i == m_thresholdMap.end()) {
720 m_thresholdMap[source] = level;
721 } else if (i->second!=level) {
722 i->second = level;
723 }
724 }
725
726 // ---------------------------------------------------------------------------
727 std::string MessageSvc::getLogColor(int logLevel) const {
728 // ---------------------------------------------------------------------------
729 return (logLevel < MSG::NUM_LEVELS) ? m_logColorCodes[logLevel] : "" ;
730 }
731
732 // ---------------------------------------------------------------------------
733 int MessageSvc::messageCount( MSG::Level level) const {
734
735 return m_msgCount[level];
736
737 }
738
739 // ---------------------------------------------------------------------------
740 void
741 MessageSvc::incrInactiveCount(MSG::Level level, const std::string& source) {
742 ++(m_inactiveMap[source].msg[level]);
743
744 if (std::find(begin(m_tracedInactiveSources),
745 end(m_tracedInactiveSources),
746 source) != end(m_tracedInactiveSources)) {
747 std::cout << "== inactive message detected from "
748 << source << " ==" << std::endl;
749 std::string t;
750 System::backTrace(t, 25, 0);
751 std::cout << t << std::endl;
752 }
753 }
754
755 // ---------------------------------------------------------------------------
756 void MessageSvc::setupLogStreams()
757 {
758 // reset state
759 m_loggedStreams.clear();
760
761 for ( auto& iProp : m_loggedStreamsName ) {
762
763 std::set<std::string> outFileNames;
764 for ( auto& jProp : m_loggedStreamsName ) {
765 if ( jProp.first != iProp.first ) {
766 outFileNames.insert( jProp.second );
767 }
768 }
769 tee( iProp.first, iProp.second, outFileNames );
770
771 }//> loop over property entries
772 }
773
774 // ---------------------------------------------------------------------------
775 void MessageSvc::tee( const std::string& sourceName,
776 const std::string& outFileName,
777 const std::set<std::string>& outFileNames )
778 {
779 const std::ios_base::openmode openMode = std::ios_base::out |
780 std::ios_base::trunc;
781
782 auto iStream = m_loggedStreams.find( sourceName );
783 if ( iStream != std::end(m_loggedStreams) ) {
784 m_loggedStreams.erase( iStream );
785 }
786
787 // before creating a new ofstream, make sure there is no already existing
788 // one with the same file name...
789 for (auto& iStream : m_loggedStreams ) {
790 if ( outFileNames.find( outFileName ) != outFileNames.end() ) {
791 m_loggedStreams[sourceName] = m_loggedStreams[iStream.first];
792 return;
793 }
794 }
795
796 auto out = std::make_shared<std::ofstream>( outFileName, openMode );
797 if ( out->good() ) m_loggedStreams[sourceName] = std::move(out);
798
799 }
System.h
MessageSvc::m_msgLimit
IntegerProperty m_msgLimit[MSG::NUM_LEVELS]
Definition: MessageSvc.h:150
Message::getFormat
const std::string & getFormat() const
Get the format string.
Definition: Message.cpp:176
MessageSvc::colTrans
std::string colTrans(std::string, int)
Definition: MessageSvc.cpp:434
std::ostringstream::setf
T setf(T...args)
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:68
Service::m_outputLevel
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:309
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:133
Kernel.h
std::map::empty
T empty(T...args)
MessageSvc::m_defaultStream
std::ostream * m_defaultStream
Pointer to the output stream.
Definition: MessageSvc.h:138
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
Service::m_state
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:311
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
MessageSvc::m_streamMap
StreamMap m_streamMap
Stream map.
Definition: MessageSvc.h:140
MessageSvc::m_msgCount
std::array< int, MSG::NUM_LEVELS > m_msgCount
Definition: MessageSvc.h:170
StatusCode::getCode
unsigned long getCode() const
Get the status code by value.
Definition: StatusCode.h:93
GaudiMP.FdsRegistry.msg
tuple msg
Definition: FdsRegistry.py:8
Property::declareUpdateHandler
virtual Property & declareUpdateHandler(std::function< void(Property &)> fun)
set new callback for update
Definition: Property.cpp:72
Property::name
const std::string & name() const
property name
Definition: Property.h:45
MessageSvc::i_reportMessage
virtual void i_reportMessage(const Message &msg, int outputLevel)
Internal implementation of reportMessage(const Message&,int) without lock.
Definition: MessageSvc.cpp:451
MessageSvc::m_colMap
ColorMap m_colMap
Definition: MessageSvc.h:168
MSG::CYAN
Definition: IMessageSvc.h:33
MessageSvc::m_defaultMessage
Message m_defaultMessage
Default Message.
Definition: MessageSvc.h:139
MSG::GREEN
Definition: IMessageSvc.h:29
gaudirun.c
tuple c
Definition: gaudirun.py:391
std::to_string
T to_string(T...args)
MSG::DEBUG
Definition: IMessageSvc.h:18
std::endl
T endl(T...args)
MessageSvc::m_messageMap
MessageMap m_messageMap
Message map.
Definition: MessageSvc.h:141
MessageSvc::setupLimits
void setupLimits(Property &prop)
Definition: MessageSvc.cpp:245
MessageSvc::setupInactCount
void setupInactCount(Property &prop)
Definition: MessageSvc.cpp:309
std
STL namespace.
MessageSvc::setupLogStreams
void setupLogStreams()
Definition: MessageSvc.cpp:756
PropertyWithVerifier::setValue
bool setValue(const TYPE &value) override
implementation of PropertyWithValue::setValue
Definition: Property.h:406
MessageSvc::MessageSvc
MessageSvc(const std::string &name, ISvcLocator *svcloc)
Definition: MessageSvc.cpp:58
std::end
T end(T...args)
PropertyWithVerifier::set
bool set(const TYPE &value)
update the value of the property/check the verifier
Definition: Property.h:434
MessageSvc::m_messageMapMutex
std::recursive_mutex m_messageMapMutex
Mutex to synchronize multiple access to m_messageMap.
Definition: MessageSvc.h:190
Message::getType
int getType() const
Get the message type.
Definition: Message.cpp:93
MessageSvc::reinitialize
StatusCode reinitialize() override
Reinitialize Service.
Definition: MessageSvc.cpp:158
MSG::VERBOSE
Definition: IMessageSvc.h:17
MSG::WHITE
Definition: IMessageSvc.h:34
merge_files.sc
int sc
Definition: merge_files.py:167
MessageSvc::NamedStream
std::pair< std::string, std::ostream * > NamedStream
Definition: MessageSvc.h:34
MessageSvc::eraseMessage
void eraseMessage() override
Definition: MessageSvc.cpp:658
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
System::backTrace
GAUDI_API int backTrace(void **addresses, const int depth)
MessageSvc.h
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
MessageSvc::m_statLevel
UnsignedIntegerProperty m_statLevel
Definition: MessageSvc.h:148
begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
std::string
STL class.
MessageSvc::eraseStream
void eraseStream() override
Definition: MessageSvc.cpp:592
MessageSvc::incrInactiveCount
void incrInactiveCount(MSG::Level level, const std::string &src) override
Definition: MessageSvc.cpp:741
MessageSvc::m_loggedStreams
std::map< std::string, std::shared_ptr< std::ostream > > m_loggedStreams
Definition: MessageSvc.h:173
SimpleProperty< int >
MessageSvc::m_sourceMap
std::map< std::string, MsgAry > m_sourceMap
Definition: MessageSvc.h:162
MSG::FATAL
Definition: IMessageSvc.h:22
MSG::Level
Level
Definition: IMessageSvc.h:15
MessageSvc::getLogColor
std::string getLogColor(int logLevel) const override
Definition: MessageSvc.cpp:727
MessageSvc::setOutputLevel
void setOutputLevel(int new_level) override
Definition: MessageSvc.cpp:707
MSG::RED
Definition: IMessageSvc.h:28
gaudirun.type
string type
Definition: gaudirun.py:151
MSG::BLUE
Definition: IMessageSvc.h:31
MessageSvc::initialize
StatusCode initialize() override
Initialize Service.
Definition: MessageSvc.cpp:123
Gaudi::range
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range form arbitrary container
Definition: NamedRange.h:130
MessageSvc::m_thresholdMap
ThresholdMap m_thresholdMap
Output level threshold map.
Definition: MessageSvc.h:142
Message::getDefaultTimeFormat
static const std::string getDefaultTimeFormat()
Get the default time format string.
Definition: Message.cpp:226
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::ostringstream
STL class.
std::multimap::erase
T erase(T...args)
Message::getDefaultFormat
static const std::string getDefaultFormat()
Get the default format string.
Definition: Message.cpp:187
MessageSvc::reportMessage
void reportMessage(const Message &message) override
Definition: MessageSvc.cpp:510
locker.lock
def lock(file)
Definition: locker.py:16
std::ostringstream::width
T width(T...args)
MSG::NIL
Definition: IMessageSvc.h:16
std::ostringstream::str
T str(T...args)
dump_component_properties.value
list value
Definition: dump_component_properties.py:12
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
MessageSvc::m_reportMutex
std::recursive_mutex m_reportMutex
Mutex to synchronize multiple threads printing.
Definition: MessageSvc.h:187
std::multimap::clear
T clear(T...args)
std::move
T move(T...args)
PropertyWithValue::value
const TYPE & value() const
explicit conversion
Definition: Property.h:341
Service::setProperties
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:363
std::pair
std::flush
T flush(T...args)
std::set::insert
T insert(T...args)
std::find_if
T find_if(T...args)
std::set< std::string >
std::unique_lock
MessageSvc::m_thresholdProp
StringArrayProperty m_thresholdProp[MSG::NUM_LEVELS]
Properties controlling.
Definition: MessageSvc.h:145
Message::setFormat
void setFormat(std::string msg) const
Set the format string.
Definition: Message.cpp:200
Property
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
Message
The Message class.
Definition: Message.h:14
MessageSvc::finalize
StatusCode finalize() override
Finalize Service.
Definition: MessageSvc.cpp:321
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:18
std::begin
T begin(T...args)
MSG::NUM_LEVELS
Definition: IMessageSvc.h:24
MessageSvc::m_defaultFormat
std::string m_defaultFormat
Default format for the messages.
Definition: MessageSvc.h:143
MSG
Print levels enumeration.
Definition: IMessageSvc.h:14
MSG::BLACK
Definition: IMessageSvc.h:27
Message::setTimeFormat
void setTimeFormat(std::string timeFormat) const
Set the time format string.
Definition: Message.cpp:239
MessageSvc::setupColors
void setupColors(Property &prop)
Definition: MessageSvc.cpp:195
std::multimap::emplace
T emplace(T...args)
std::array
STL class.
CommonMessaging
Definition: CommonMessaging.h:58
MessageSvc::m_inactiveMap
std::map< std::string, MsgAry > m_inactiveMap
Definition: MessageSvc.h:162
MessageSvc::initColors
void initColors(Property &prop)
Definition: MessageSvc.cpp:165
MSG::ERROR
Definition: IMessageSvc.h:21
MSG::PURPLE
Definition: IMessageSvc.h:32
MessageSvc::m_logColorCodes
std::string m_logColorCodes[MSG::NUM_LEVELS]
Definition: MessageSvc.h:152
Message::getSource
const std::string & getSource() const
Get the message source.
Definition: Message.cpp:115
MessageSvc::messageCount
int messageCount(MSG::Level logLevel) const override
Definition: MessageSvc.cpp:733
gaudirun.level
level
Definition: gaudirun.py:261
MessageSvc::m_color
BooleanProperty m_color
Definition: MessageSvc.h:146
MSG::WARNING
Definition: IMessageSvc.h:20
StatusCode.h
MessageSvc::m_inactCount
BooleanProperty m_inactCount
Definition: MessageSvc.h:163
std::fill
T fill(T...args)
MessageSvc::outputLevel
int outputLevel() const override
Definition: MessageSvc.cpp:693
Service::declareProperty
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:215
MessageSvc::tee
void tee(const std::string &sourceName, const std::string &logFileName, const std::set< std::string > &declaredOutFileNames)
Definition: MessageSvc.cpp:775
MessageSvc::m_stats
BooleanProperty m_stats
Definition: MessageSvc.h:147
MessageSvc::m_loggedStreamsName
std::map< std::string, std::string > m_loggedStreamsName
Definition: MessageSvc.h:172
MSG::INFO
Definition: IMessageSvc.h:19
MessageSvc::m_tracedInactiveSources
std::vector< std::string > m_tracedInactiveSources
Definition: MessageSvc.h:164
Gaudi::StateMachine::OFFLINE
Definition: StateMachine.h:13
std::for_each
T for_each(T...args)
MessageSvc::insertMessage
void insertMessage(const StatusCode &code, const Message &message) override
Definition: MessageSvc.cpp:645
MessageSvc
Definition: MessageSvc.h:30
ana.i
list i
Definition: ana.py:128
MSG::YELLOW
Definition: IMessageSvc.h:30
MessageSvc::m_suppress
BooleanProperty m_suppress
Definition: MessageSvc.h:163
std::cout
MessageSvc::setupThreshold
void setupThreshold(Property &prop)
Definition: MessageSvc.cpp:272
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:32
MessageSvc::m_defaultTimeFormat
std::string m_defaultTimeFormat
Default format for timestamps in the messages.
Definition: MessageSvc.h:144
MSG::ALWAYS
Definition: IMessageSvc.h:23
std::multimap::equal_range
T equal_range(T...args)
MessageSvc::insertStream
void insertStream(int message_type, const std::string &name, std::ostream *stream) override
Definition: MessageSvc.cpp:578
MessageSvc::m_logColors
StringArrayProperty m_logColors[MSG::NUM_LEVELS]
Definition: MessageSvc.h:149
Message.h
MessageSvc::m_thresholdMapMutex
std::recursive_mutex m_thresholdMapMutex
Mutex to synchronize multiple access to m_thresholdMap (.
Definition: MessageSvc.h:194