#include "IssueLogger.h" #include "GaudiKernel/SvcFactory.h" #include "GaudiKernel/ISvcLocator.h" #include "GaudiKernel/MsgStream.h" #include "GaudiKernel/Tokenizer.h" #include "GaudiKernel/System.h" #include "GaudiKernel/Time.h" #include #include #include #include "boost/bind.hpp" using namespace std; DECLARE_SERVICE_FACTORY(IssueLogger) //*************************************************************************// inline void toupper(std::string &s) { std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper); } //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// IssueLogger::IssueLogger( const std::string& name, ISvcLocator* svc ) : base_class(name, svc) { declareProperty ("Output", m_outputfile ); declareProperty ("ReportLevel", m_reportLevelS="WARNING"); declareProperty ("TracebackLevel", m_traceLevelS="ERROR"); declareProperty ("ShowTime", m_showTime=false); m_reportLevelS.declareUpdateHandler(&IssueLogger::setupLevels,this); m_traceLevelS.declareUpdateHandler(&IssueLogger::setupLevels,this); m_outputfile.declareUpdateHandler(&IssueLogger::setupStreams,this); m_reportLevel = IssueSeverity::WARNING; m_traceLevel = IssueSeverity::ERROR; for (int i=0; i Prop; // std::vector props; string val,VAL,TAG,filename; tok.analyse(ident.substr(loc+1,ident.length()), " ", "", "", "=", "'", "'"); for ( Tokenizer::Items::iterator i = tok.items().begin(); i != tok.items().end(); i++) { const std::string& tag = (*i).tag(); TAG = tag; toupper(TAG); val = (*i).value(); VAL = val; toupper(VAL); IssueSeverity::Level level; if (TAG == "DEBUG") { level = IssueSeverity::DEBUG; } else if ( TAG == "INFO") { level = IssueSeverity::INFO; } else if ( TAG == "WARNING") { level = IssueSeverity::WARNING; } else if ( TAG == "RECOVERABLE") { level = IssueSeverity::RECOVERABLE; } else if ( TAG == "ERROR") { level = IssueSeverity::ERROR; } else if ( TAG == "FATAL") { level = IssueSeverity::FATAL; } else { log << MSG::ERROR << "Unknown output level \"" << TAG << "\"" << endmsg; continue; } if (m_logger[level] != 0) { log << MSG::INFO << "closing stream " << m_logger[level]->name() << endmsg; delete m_logger[level]; m_logger[level] = 0; } if (val == "MsgSvc") { m_logger[level] = new StreamLogger(msgSvc(), m_sevMsgMap[level]); m_log[level] = boost::bind(&StreamLogger::WriteToMsgSvc, m_logger[level], _1); } else if (val == "STDERR") { m_logger[level] = new StreamLogger(std::cerr); m_log[level] = boost::bind(&StreamLogger::WriteToStream, m_logger[level], _1); } else if (val == "STDOUT") { m_logger[level] = new StreamLogger(std::cout); m_log[level] = boost::bind(&StreamLogger::WriteToStream, m_logger[level], _1); } else { // A file try { m_logger[level] = new StreamLogger(val.c_str()); } catch (std::exception&) { m_logger[level] = 0; log << MSG::ERROR << "Unable to open file \"" << VAL << "\" for writing issues at level " << TAG << endmsg; return StatusCode::FAILURE; } m_log[level] = boost::bind(&StreamLogger::WriteToStream, m_logger[level], _1); } log << MSG::DEBUG << "Writing " << m_levelTrans[level] << " issues to " << m_logger[level]->name() << endmsg; } return StatusCode::SUCCESS; } //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// void IssueLogger::report(IssueSeverity::Level lev, const std::string& str, const std::string& org) { if ( lev < m_reportLevel) return; std::string msg = m_levelTrans[lev] + " " + org + " \"" + str + "\""; if (m_showTime) { msg += " [" + Gaudi::Time::current().format(true, "%H:%M:%S %Y/%m/%d %Z") +"]"; } if (lev >= m_traceLevel) { std::string stack; getTraceBack(stack); msg += "\n" + stack; } m_log[lev](msg); } //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// void IssueLogger::report( const IssueSeverity &err ) { report(err.getLevel(), err.getMsg(), err.getOrigin()); } //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// void IssueLogger::setupLevels(Property& prop) { StringProperty *sap = dynamic_cast (&prop); if (sap == 0) { MsgStream log ( msgSvc(), name() ); log << MSG::ERROR << "Could not convert " << prop.name() << "to a StringProperty (which it should be!)" << endmsg; return; } std::string val = sap->value(); if (prop.name() == "ReportLevel") { if (m_levelSTrans.find(val) == m_levelSTrans.end()) { MsgStream log ( msgSvc(), name() ); log << MSG::ERROR << "Option ReportLevel: unknown Issue Severity level \"" << val << "\". Setting it WARNING" << endmsg; m_reportLevel = IssueSeverity::WARNING; return; } else { m_reportLevel = m_levelSTrans[m_reportLevelS]; } } else if (prop.name() == "TracebackLevel") { if (m_levelSTrans.find(val) == m_levelSTrans.end()) { MsgStream log ( msgSvc(), name() ); log << MSG::ERROR << "Option TracebackLevel: unknown Issue Severity level \"" << val << "\". Setting it to ERROR" << endmsg; m_traceLevel = IssueSeverity::ERROR; return; } else { m_traceLevel = m_levelSTrans[m_traceLevelS]; } } else { MsgStream log ( msgSvc(), name() ); log << MSG::ERROR << "setting up unknown property \"" << prop.name() << "\"" << endmsg; return; } } //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// void IssueLogger::setupStreams(Property& prop) { StringArrayProperty *sap = dynamic_cast( &prop ); if (sap == 0) { MsgStream log ( msgSvc(), name() ); log << MSG::ERROR << "Could not convert " << prop.name() << "to a StringArrayProperty (which it should be!)" << endmsg; return; } vector::const_iterator itr; for (itr = sap->value().begin(); itr != sap->value().end(); ++itr) { if (connect(*itr).isFailure()) { MsgStream log ( msgSvc(), name() ); log << MSG::ERROR << "Could not setup stream " << *itr << endmsg; } } return; } //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// void IssueLogger::setupDefaultLogger() { for (int i=1; iname() << endmsg; } } }