7 #include "GaudiKernel/Kernel.h"
8 #include "GaudiKernel/StatusCode.h"
9 #include "GaudiKernel/Message.h"
10 #include "GaudiKernel/xtoa.h"
28 m_defaultStream = &std::cout;
32 declareProperty(
"showStats", m_stats =
false );
33 declareProperty(
"statLevel", m_statLevel = 0 );
36 declareProperty(
"setVerbose", m_thresholdProp[
MSG::VERBOSE] );
37 declareProperty(
"setDebug", m_thresholdProp[
MSG::DEBUG] );
38 declareProperty(
"setInfo", m_thresholdProp[
MSG::INFO] );
39 declareProperty(
"setWarning", m_thresholdProp[
MSG::WARNING] );
40 declareProperty(
"setError", m_thresholdProp[
MSG::ERROR] );
41 declareProperty(
"setFatal", m_thresholdProp[
MSG::FATAL] );
42 declareProperty(
"setAlways", m_thresholdProp[
MSG::ALWAYS] );
44 declareProperty(
"useColors", m_color=
false);
47 declareProperty(
"fatalColorCode", m_logColors[MSG::FATAL] );
48 declareProperty(
"errorColorCode", m_logColors[MSG::ERROR] );
49 declareProperty(
"warningColorCode", m_logColors[MSG::WARNING] );
50 declareProperty(
"infoColorCode", m_logColors[MSG::INFO] );
51 declareProperty(
"debugColorCode", m_logColors[MSG::DEBUG] );
52 declareProperty(
"verboseColorCode", m_logColors[MSG::VERBOSE] );
53 declareProperty(
"alwaysColorCode", m_logColors[MSG::ALWAYS] );
55 const int defaultLimit = 500;
56 declareProperty(
"fatalLimit", m_msgLimit[MSG::FATAL] = defaultLimit );
57 declareProperty(
"errorLimit", m_msgLimit[MSG::ERROR] = defaultLimit );
58 declareProperty(
"warningLimit", m_msgLimit[MSG::WARNING] = defaultLimit );
59 declareProperty(
"infoLimit", m_msgLimit[MSG::INFO] = defaultLimit );
60 declareProperty(
"debugLimit", m_msgLimit[MSG::DEBUG] = defaultLimit );
61 declareProperty(
"verboseLimit", m_msgLimit[MSG::VERBOSE] = defaultLimit );
62 declareProperty(
"alwaysLimit", m_msgLimit[MSG::ALWAYS] = 0 );
64 declareProperty(
"defaultLimit", m_msgLimit[
MSG::NIL] = defaultLimit );
66 declareProperty(
"enableSuppression", m_suppress =
false );
73 declareProperty(
"loggedStreams",
75 "MessageStream sources we want to dump into a logfile" );
83 levelNames[0] =
"NIL";
84 levelNames[1] =
"VERBOSE";
85 levelNames[2] =
"DEBUG";
86 levelNames[3] =
"INFO";
87 levelNames[4] =
"WARNING";
88 levelNames[5] =
"ERROR";
89 levelNames[6] =
"FATAL";
90 levelNames[7] =
"ALWAYS";
103 LoggedStreamsMap_t::iterator iStream = m_loggedStreams.begin();
104 LoggedStreamsMap_t::iterator endStream = m_loggedStreams.end();
105 for ( ; iStream != endStream; ++iStream ) {
106 delete iStream->second;
123 sc = setProperties();
157 if (m_color ==
true) {
160 vector<string> fatDef;
161 fatDef.push_back(
"[94;101;1m" );
168 vector<string> errDef;
169 errDef.push_back(
"[97;101;1m" );
176 vector<string> warDef;
177 warDef.push_back(
"[93;1m" );
188 m_logColors[ic].set( def );
199 if (! m_color)
return;
202 if (prop.
name() ==
"fatalColorCode") {
204 }
else if (prop.
name() ==
"errorColorCode") {
206 }
else if (prop.
name() ==
"warningColorCode") {
208 }
else if (prop.
name() ==
"infoColorCode") {
210 }
else if (prop.
name() ==
"debugColorCode") {
212 }
else if (prop.
name() ==
"verboseColorCode") {
214 }
else if (prop.
name() ==
"alwaysColorCode") {
217 cout <<
"ERROR: Unknown message color parameter: " << prop.
name()
223 vector<string>::const_iterator itr;
224 itr = m_logColors[ic].value().begin();
226 if ( m_logColors[ic].
value().size() == 1 ) {
230 }
else if (itr->substr(0,1) ==
"[") {
231 code =
"\033" + *itr;
233 code =
"\033[" + colTrans(*itr, 90) +
";1m";
236 }
else if (m_logColors[ic].
value().size() == 2) {
237 vector<string>::const_iterator itr2 = itr + 1;
239 code =
"\033[" + colTrans(*itr, 90) +
";"
240 + colTrans(*itr2, 100) +
";1m";
244 m_logColorCodes[ic] = code;
251 if (prop.
name() ==
"alwaysLimit") {
253 if (p && p->
value() != 0) {
254 cout <<
"MessageSvc ERROR: cannot suppress ALWAYS messages" << endl;
257 }
else if (prop.
name() ==
"defaultLimit") {
260 m_msgLimit[
i] = m_msgLimit[
MSG::NIL].value();
263 }
else if (prop.
name() !=
"fatalLimit" &&
264 prop.
name() !=
"errorLimit" &&
265 prop.
name() !=
"warningLimit" &&
266 prop.
name() ==
"infoLimit" &&
267 prop.
name() ==
"debugLimit" &&
268 prop.
name() ==
"verboseLimit") {
269 cout <<
"MessageSvc ERROR: Unknown message limit parameter: "
270 << prop.
name() << endl;
279 if (prop.
name() ==
"setFatal") {
281 }
else if (prop.
name() ==
"setError") {
283 }
else if (prop.
name() ==
"setWarning") {
285 }
else if (prop.
name() ==
"setInfo") {
287 }
else if (prop.
name() ==
"setDebug") {
289 }
else if (prop.
name() ==
"setVerbose") {
291 }
else if (prop.
name() ==
"setAlways") {
294 cerr <<
"MessageSvc ERROR: Unknown message threshold parameter: "
295 << prop.
name() << endl;
301 std::cerr <<
"could not dcast " << prop.
name()
302 <<
" to a StringArrayProperty (which it should be!)" << endl;
305 std::vector<std::string>::const_iterator itr;
306 for ( itr = sap->
value().begin();
307 itr != sap->
value().end();
309 setOutputLevel( *itr, ic );
321 if (prop.
name() ==
"countInactive") {
337 std::ostringstream os;
340 os <<
"Summarizing all message counts" << endl;
342 os <<
"Listing sources of suppressed message: " << endl;
345 os <<
"=====================================================" << endl;
346 os <<
" Message Source | Level | Count" << endl;
347 os <<
"-----------------------------+---------+-------------" << endl;
352 std::map<std::string,MsgAry>::const_iterator itr;
353 for (itr=m_sourceMap.begin(); itr!=m_sourceMap.end(); ++itr) {
355 if ( (itr->second.msg[ic] >= m_msgLimit[ic] && m_msgLimit[ic] != 0 ) ||
356 (m_stats && itr->second.msg[ic] > 0 && ic >= m_statLevel.value()) ) {
359 os.setf(ios_base::left,ios_base::adjustfield);
365 os.setf(ios_base::right,ios_base::adjustfield);
366 os << levelNames[ic];
371 os << itr->second.msg[ic];
379 os <<
"=====================================================" << endl;
381 if (found || m_stats) {
387 if (m_inactCount.value()) {
389 std::ostringstream os;
390 os <<
"Listing sources of Unprotected and Unseen messages\n";
395 std::map<std::string,MsgAry>::const_iterator itr;
396 for (itr=m_inactiveMap.begin(); itr!=m_inactiveMap.end(); ++itr) {
398 if (itr->second.msg[ic] != 0) {
399 if (itr->first.length() > ml) { ml = itr->first.length(); }
404 for (
unsigned int i=0;
i<ml+25; ++
i) {
410 os.setf(ios_base::left,ios_base::adjustfield);
411 os <<
"Message Source";
413 os <<
"| Level | Count" << endl;
415 for (
unsigned int i=0;
i<ml+3; ++
i) {
418 os <<
"+---------+-----------" << endl;
421 for (itr=m_inactiveMap.begin(); itr!=m_inactiveMap.end(); ++itr) {
423 if (itr->second.msg[ic] != 0) {
426 os.setf(ios_base::left,ios_base::adjustfield);
432 os.setf(ios_base::right,ios_base::adjustfield);
433 os << levelNames[ic];
438 os << itr->second.msg[ic];
446 for (
unsigned int i=0;
i<ml+25; ++
i) {
462 ColorMap::const_iterator itr = m_colMap.find(col);
464 if (itr != m_colMap.end()) {
465 icol = offset + itr->second;
469 std::ostringstream os1;
485 boost::recursive_mutex::scoped_lock
lock(m_reportMutex);
494 if ( !m_loggedStreams.empty() ) {
495 const LoggedStreamsMap_t::iterator iLog = m_loggedStreams.find( msg.
getSource() );
496 if ( m_loggedStreams.end() != iLog ) {
497 (*iLog->second) << *cmsg << std::endl;
501 if ( m_suppress.value() || m_stats.value() ) {
505 const int nmsg = ++(m_sourceMap[msg.
getSource()].msg[key]);
507 if (m_suppress.value()) {
509 if ( m_msgLimit[key] != 0 ) {
510 if (nmsg == m_msgLimit[key]) {
511 std::ostringstream txt;
512 txt << levelNames[key] <<
" message limit ("
513 << m_msgLimit[key].value()
515 << msg.
getSource() +
". Suppressing further output.";
518 }
else if (nmsg > m_msgLimit[key]) {
526 StreamMap::const_iterator first = m_streamMap.lower_bound( key );
527 if ( first != m_streamMap.end() ) {
528 StreamMap::const_iterator last = m_streamMap.upper_bound( key );
529 while( first != last ) {
530 std::ostream& stream = *( (*first).second.second );
531 stream << *cmsg << std::endl;
535 else if ( key >= outputLevel ) {
539 (*m_defaultStream) << *cmsg << std::endl << std::flush;
541 (*m_defaultStream) << m_logColorCodes[key] << *cmsg <<
"\033[m"
542 << std::endl << std::flush;
546 if (cmsg != &msg) {
delete cmsg; }
557 reportMessage(msg, outputLevel(msg.
getSource()));
568 const char* message) {
570 reportMessage( msg );
581 const std::string& message) {
583 reportMessage( msg );
594 const std::string& source)
596 boost::recursive_mutex::scoped_lock
lock(m_messageMapMutex);
598 MessageMap::const_iterator first = m_messageMap.lower_bound( key );
599 if ( first != m_messageMap.end() ) {
600 MessageMap::const_iterator last = m_messageMap.upper_bound( key );
601 while( first != last ) {
604 std::ostringstream os1;
605 os1 <<
"Status Code " << key.
getCode() << std::ends;
607 reportMessage( stat_code1 );
608 reportMessage( msg );
613 Message mesg = m_defaultMessage;
615 std::ostringstream os2;
616 os2 <<
"Status Code " << key.
getCode() << std::ends;
618 reportMessage( stat_code2 );
619 reportMessage( mesg );
631 const std::string& name,
632 std::ostream *stream)
634 typedef StreamMap::value_type value_type;
635 m_streamMap.insert( value_type( key,
NamedStream(name,stream) ) );
647 m_streamMap.erase( m_streamMap.begin(), m_streamMap.end() );
659 m_streamMap.erase( message_type );
674 StreamMap::iterator first = m_streamMap.lower_bound( key );
675 StreamMap::iterator last = m_streamMap.upper_bound( key );
676 while( first != last ) {
677 if ( (*first).second.second == stream ) {
678 m_streamMap.erase( first );
699 StreamMap::iterator first = m_streamMap.begin();
700 while( first != m_streamMap.end() ) {
701 if ( (*first).second.second == stream ) {
702 m_streamMap.erase( first );
721 boost::recursive_mutex::scoped_lock
lock(m_messageMapMutex);
723 typedef MessageMap::value_type value_type;
724 m_messageMap.insert( value_type( key, msg ) );
736 boost::recursive_mutex::scoped_lock
lock(m_messageMapMutex);
738 m_messageMap.erase( m_messageMap.begin(), m_messageMap.end() );
750 boost::recursive_mutex::scoped_lock
lock(m_messageMapMutex);
752 m_messageMap.erase( key );
764 boost::recursive_mutex::scoped_lock
lock(m_messageMapMutex);
769 MessageMap::iterator first = m_messageMap.lower_bound( key );
770 MessageMap::iterator last = m_messageMap.upper_bound( key );
771 while( first != last ) {
772 const Message& message = (*first).second;
773 if ( message == msg ) {
774 m_messageMap.erase( first );
785 return m_outputLevel;
791 boost::recursive_mutex::scoped_lock
lock(m_thresholdMapMutex);
793 ThresholdMap::const_iterator it;
795 it = m_thresholdMap.find( source );
796 if( it != m_thresholdMap.end() ) {
800 return m_outputLevel;
807 m_outputLevel = new_level;
813 boost::recursive_mutex::scoped_lock
lock(m_thresholdMapMutex);
824 m_thresholdMap[source] =
level;
831 return m_logColorCodes[logLevel];
840 return m_msgCount[
level];
848 ++(m_inactiveMap[source].msg[
level]);
856 for ( LoggedStreamsMap_t::iterator iLog = m_loggedStreams.begin();
857 iLog != m_loggedStreams.end();
861 m_loggedStreams.clear();
863 typedef std::map<std::string,std::string> StreamMap_t;
864 const StreamMap_t& streamMap = m_loggedStreamsName;
865 typedef StreamMap_t::const_iterator StreamMapIter;
867 for ( StreamMapIter iProp = streamMap.begin(), iEnd = streamMap.end();
871 const std::string sourceName = iProp->first;
872 const std::string outFileName = iProp->second;
874 std::set<std::string> outFileNames;
875 for ( StreamMapIter jProp = streamMap.begin();
878 if ( jProp->first != iProp->first ) {
879 outFileNames.insert( jProp->second );
883 tee( sourceName, outFileName, outFileNames );
892 const std::string& outFileName,
893 const std::set<std::string>& outFileNames )
895 const std::ios_base::openmode openMode = std::ios_base::out |
896 std::ios_base::trunc;
898 LoggedStreamsMap_t::iterator iEnd = m_loggedStreams.end();
899 LoggedStreamsMap_t::iterator iStream = m_loggedStreams.find( sourceName );
900 if ( iStream != iEnd ) {
901 delete iStream->second;
903 m_loggedStreams.erase( iStream );
908 iEnd = m_loggedStreams.end();
909 for ( iStream = m_loggedStreams.begin(); iStream != iEnd; ++iStream ) {
910 if ( outFileNames.find( outFileName ) != outFileNames.end() ) {
911 m_loggedStreams[sourceName] = m_loggedStreams[iStream->first];
916 std::ofstream * out =
new std::ofstream( outFileName.c_str(), openMode );
918 if ( !out->good() ) {
924 m_loggedStreams[sourceName] = out;