The Gaudi Framework  v29r0 (ff2e7097)
IssueLogger.cpp
Go to the documentation of this file.
1 #include "IssueLogger.h"
2 
6 #include "GaudiKernel/System.h"
7 #include "GaudiKernel/Time.h"
8 
9 #include <algorithm>
10 namespace
11 {
12 
13  std::string getTraceBack()
14  {
15  std::string stack;
16  constexpr int depth = 30;
17  constexpr int offset = 5;
18  System::backTrace( stack, depth, offset );
19  return stack;
20  }
21 
22  static const std::map<IssueSeverity::Level, MSG::Level> s_sevMsgMap = {
25  {IssueSeverity::DEBUG2, MSG::DEBUG}, {IssueSeverity::DEBUG3, MSG::DEBUG},
29 
30  static const std::map<IssueSeverity::Level, std::string> s_levelTrans = {
31  {IssueSeverity::VERBOSE, "VERBOSE"}, {IssueSeverity::DEBUG, "DEBUG"},
32  {IssueSeverity::DEBUG1, "DEBUG1"}, {IssueSeverity::DEBUG2, "DEBUG2"},
33  {IssueSeverity::DEBUG3, "DEBUG3"}, {IssueSeverity::INFO, "INFO"},
34  {IssueSeverity::WARNING, "WARNING"}, {IssueSeverity::RECOVERABLE, "RECOVERABLE"},
35  {IssueSeverity::ERROR, "ERROR"}, {IssueSeverity::FATAL, "FATAL"},
36  {IssueSeverity::ALWAYS, "ALWAYS"}};
37 
38  static const std::map<std::string, IssueSeverity::Level> s_levelSTrans = {
39  {"VERBOSE", IssueSeverity::VERBOSE}, {"DEBUG", IssueSeverity::DEBUG},
40  {"DEBUG1", IssueSeverity::DEBUG1}, {"DEBUG2", IssueSeverity::DEBUG2},
41  {"DEBUG3", IssueSeverity::DEBUG3}, {"INFO", IssueSeverity::INFO},
42  {"WARNING", IssueSeverity::WARNING}, {"RECOVERABLE", IssueSeverity::RECOVERABLE},
43  {"ERROR", IssueSeverity::ERROR}, {"FATAL", IssueSeverity::FATAL},
44  {"ALWAYS", IssueSeverity::ALWAYS}};
45 }
46 
48 
49 //*************************************************************************//
50 inline void toupper( std::string& s ) { std::transform( s.begin(), s.end(), s.begin(), (int ( * )( int ))toupper ); }
51 
52 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
53 
55 {
56 
57  m_reportLevelS.declareUpdateHandler( &IssueLogger::setupLevels, this );
58  m_traceLevelS.declareUpdateHandler( &IssueLogger::setupLevels, this );
59  m_outputfile.declareUpdateHandler( &IssueLogger::setupStreams, this );
60 
63 }
64 
65 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
66 
68 {
69 
71  if ( st.isSuccess() ) {
73  }
74  return st;
75 }
76 
77 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
78 
80 {
81 
82  warning() << "reinitialize not implemented" << endmsg;
83  return StatusCode::SUCCESS;
84 }
85 
86 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
87 
89 {
90 
91  debug() << "IssueLogger::finalize" << endmsg;
92  std::for_each( std::begin( m_log ), std::end( m_log ), []( logger_t& i ) { i.reset(); } );
93  return Service::finalize();
94 }
95 
96 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
97 
99 {
100 
101  auto loc = ident.find( " " );
102  using Parser = Gaudi::Utils::AttribStringParser;
103  // note: if loc == string::npos then loc + 1 == 0
104  for ( auto attrib : Parser( ident.substr( loc + 1 ) ) ) {
105  toupper( attrib.tag );
107  if ( attrib.tag == "DEBUG" ) {
108  level = IssueSeverity::DEBUG;
109  } else if ( attrib.tag == "INFO" ) {
110  level = IssueSeverity::INFO;
111  } else if ( attrib.tag == "WARNING" ) {
112  level = IssueSeverity::WARNING;
113  } else if ( attrib.tag == "RECOVERABLE" ) {
115  } else if ( attrib.tag == "ERROR" ) {
116  level = IssueSeverity::ERROR;
117  } else if ( attrib.tag == "FATAL" ) {
118  level = IssueSeverity::FATAL;
119  } else {
120  error() << "Unknown output level \"" << attrib.tag << "\"" << endmsg;
121  continue;
122  }
123 
124  if ( m_log[level] ) {
125  info() << "closing stream " << m_log[level].name() << endmsg;
126  m_log[level].reset();
127  }
128 
129  if ( attrib.value == "MsgSvc" ) {
130  m_log[level] = {new StreamLogger( msgSvc(), s_sevMsgMap.at( level ) ), &StreamLogger::WriteToMsgSvc};
131  } else if ( attrib.value == "STDERR" ) {
133  } else if ( attrib.value == "STDOUT" ) {
135  } else { // A file
136  try {
137  m_log[level] = {new StreamLogger( attrib.value ), &StreamLogger::WriteToStream};
138  } catch ( std::exception& ) {
139  m_log[level].reset();
140  error() << "Unable to open file \"" << attrib.value << "\" for writing issues at level " << attrib.tag
141  << endmsg;
142  return StatusCode::FAILURE;
143  }
144  }
145  debug() << "Writing " << s_levelTrans.at( level ) << " issues to " << m_log[level].name() << endmsg;
146  }
147  return StatusCode::SUCCESS;
148 }
149 
150 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
151 
153 {
154  if ( lev < m_reportLevel ) return;
155  std::string msg = s_levelTrans.at( lev ) + " " + org + " \"" + str + "\"";
156  if ( m_showTime ) msg += " [" + Gaudi::Time::current().format( true, "%H:%M:%S %Y/%m/%d %Z" ) + "]";
157  if ( lev >= m_traceLevel ) msg += "\n" + getTraceBack();
158  m_log[lev]( msg );
159 }
160 
161 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
162 
163 void IssueLogger::report( const IssueSeverity& err ) { report( err.getLevel(), err.getMsg(), err.getOrigin() ); }
164 
165 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
166 
168 {
169 
170  Gaudi::Property<std::string>* sap = dynamic_cast<Gaudi::Property<std::string>*>( &prop );
171  if ( !sap ) {
172  error() << "Could not convert " << prop.name() << "to a Gaudi::Property<std::string> (which it should be!)"
173  << endmsg;
174  return;
175  }
176 
177  const std::string& val = sap->value();
178  auto set = [&]( IssueSeverity::Level& key, IssueSeverity::Level def ) {
179  if ( s_levelSTrans.find( val ) == s_levelSTrans.end() ) {
180  key = def;
181  error() << "Option " << prop.name() << ": unknown Issue Severity level \"" << val << "\". Setting it "
182  << s_levelTrans.at( def ) << endmsg;
183  } else {
184  key = s_levelSTrans.at( val );
185  }
186  };
187 
188  if ( prop.name() == "ReportLevel" ) {
190  } else if ( prop.name() == "TracebackLevel" ) {
192  } else {
193  error() << "setting up unknown property \"" << prop.name() << "\"" << endmsg;
194  }
195 }
196 
197 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
198 
200 {
201 
203  if ( !sap ) {
204  error() << "Could not convert " << prop.name()
205  << "to a Gaudi::Property<std::vector<std::string>> (which it should be!)" << endmsg;
206  return;
207  }
208  for ( const auto& s : sap->value() ) {
209  if ( connect( s ).isFailure() ) {
210  error() << "Could not setup stream " << s << endmsg;
211  }
212  }
213 }
214 
215 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
216 
218 {
219  for ( int i = 1; i < IssueSeverity::NUM_LEVELS; ++i ) {
220  if ( !m_log[i] ) {
221  // default: dump to msgSvc
223  m_log[j] = {new StreamLogger( msgSvc(), s_sevMsgMap.at( j ) ), &StreamLogger::WriteToMsgSvc};
224  debug() << "Writing " << s_levelTrans.at( j ) << " issues to " << m_log[j].name() << endmsg;
225  }
226  }
227 }
Parse attribute strings allowing iteration over the various attributes.
StatusCode initialize() override
Definition: Service.cpp:64
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
StatusCode connect(const std::string &)
Definition: IssueLogger.cpp:98
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode finalize() override
Definition: Service.cpp:174
Implementation of property with value of concrete type.
Definition: Property.h:319
std::string getOrigin() const
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
void setupStreams(Gaudi::Details::PropertyBase &prop)
StatusCode finalize() override
Definition: IssueLogger.cpp:88
void toupper(std::string &s)
Definition: IssueLogger.cpp:50
const std::string name() const
property name
Definition: Property.h:40
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
void setupLevels(Gaudi::Details::PropertyBase &prop)
static Time current()
Returns the current time.
Definition: Time.cpp:112
STL namespace.
T end(T...args)
IssueSeverity::Level m_reportLevel
Definition: IssueLogger.h:32
STL class.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
GAUDI_API int backTrace(void **addresses, const int depth)
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
STL class.
T at(T...args)
void setupDefaultLogger()
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Gaudi::Property< bool > m_showTime
Definition: IssueLogger.h:30
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
StatusCode reinitialize() override
Definition: IssueLogger.cpp:79
STL class.
T find(T...args)
std::array< logger_t, IssueSeverity::NUM_LEVELS > m_log
Definition: IssueLogger.h:49
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T begin(T...args)
StatusCode initialize() override
Definition: IssueLogger.cpp:67
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: IssueLogger.h:27
string s
Definition: gaudirun.py:253
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
T substr(T...args)
void WriteToMsgSvc(const std::string &str)
Definition: StreamLogger.h:21
T transform(T...args)
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:479
Gaudi::Property< std::string > m_traceLevelS
Definition: IssueLogger.h:29
IssueSeverity::Level m_traceLevel
Definition: IssueLogger.h:32
T for_each(T...args)
IssueSeverity::Level getLevel() const
Definition: IssueSeverity.h:57
void WriteToStream(const std::string &str)
Definition: StreamLogger.h:20
std::string getMsg() const
Definition: IssueSeverity.h:58
void report(IssueSeverity::Level level, const std::string &msg, const std::string &origin) override
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
Gaudi::Property< std::string > m_reportLevelS
Definition: IssueLogger.h:28
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
Definition: Time.cpp:260
IssueLogger(const std::string &name, ISvcLocator *svc)
Definition: IssueLogger.cpp:54