All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <sstream>
10 #include <streambuf>
11 #include <algorithm>
12 
13 #include "boost/bind.hpp"
14 
15 using namespace std;
16 
18 
19 //*************************************************************************//
20 inline void toupper(std::string &s)
21 {
22  std::transform(s.begin(), s.end(), s.begin(),
23  (int(*)(int)) toupper);
24 }
25 
26 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
27 
28 IssueLogger::IssueLogger( const std::string& name, ISvcLocator* svc )
29  : base_class(name, svc) {
30 
31  declareProperty ("Output", m_outputfile );
32  declareProperty ("ReportLevel", m_reportLevelS="WARNING");
33  declareProperty ("TracebackLevel", m_traceLevelS="ERROR");
34  declareProperty ("ShowTime", m_showTime=false);
35 
39 
42 
43  for (int i=0; i<IssueSeverity::NUM_LEVELS; ++i) {
44  m_logger[i] = 0;
45  }
46 
55 
68 
80 
92 
93 
94 }
95 
96 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
97 
99 
100 }
101 
102 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
103 
106 
108  if (st.isFailure()) { return st; }
109 
111 
112  return st;
113 
114 }
115 
116 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
117 
120 
121  MsgStream log ( msgSvc(), name() );
122  log << MSG::WARNING << "reinitialize not implemented" << endmsg;
123 
124 
125  return StatusCode::SUCCESS;
126 
127 }
128 
129 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
130 
133 
134  MsgStream log ( msgSvc(), name() );
135  log << MSG::DEBUG << "IssueLogger::finalize" << endmsg;
136 
137  for (int i=0; i<IssueSeverity::NUM_LEVELS; ++i) {
139  delete m_logger[j];
140  }
141 
142  return Service::finalize();
143 }
144 
145 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
146 
147 void
148 IssueLogger::getTraceBack(std::string& stack) {
149  const int depth = 30;
150  const int offset = 5;
151  System::backTrace(stack, depth, offset);
152 }
153 
154 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
155 
157 IssueLogger::connect(const std::string& ident) {
158 
159  MsgStream log ( msgSvc(), name() );
160  Tokenizer tok(true);
161 
162  string::size_type loc = ident.find(" ");
163 // string stream = ident.substr(0,loc); // icc remark #177: variable "stream" was declared but never referenced
164 // typedef std::pair<std::string,std::string> Prop;
165 // std::vector<Prop> props;
166  string val,VAL,TAG,filename;
167 
168  tok.analyse(ident.substr(loc+1,ident.length()), " ", "", "", "=", "'", "'");
169 
170  for ( Tokenizer::Items::iterator i = tok.items().begin();
171  i != tok.items().end(); i++) {
172  const std::string& tag = (*i).tag();
173  TAG = tag;
174  toupper(TAG);
175 
176  val = (*i).value();
177  VAL = val;
178  toupper(VAL);
179 
181 
182  if (TAG == "DEBUG") {
183  level = IssueSeverity::DEBUG;
184  } else if ( TAG == "INFO") {
185  level = IssueSeverity::INFO;
186  } else if ( TAG == "WARNING") {
187  level = IssueSeverity::WARNING;
188  } else if ( TAG == "RECOVERABLE") {
190  } else if ( TAG == "ERROR") {
191  level = IssueSeverity::ERROR;
192  } else if ( TAG == "FATAL") {
193  level = IssueSeverity::FATAL;
194  } else {
195  log << MSG::ERROR << "Unknown output level \"" << TAG << "\""
196  << endmsg;
197  continue;
198  }
199 
200  if (m_logger[level] != 0) {
201  log << MSG::INFO << "closing stream " << m_logger[level]->name()
202  << endmsg;
203  delete m_logger[level];
204  m_logger[level] = 0;
205  }
206 
207  if (val == "MsgSvc") {
208  m_logger[level] = new StreamLogger(msgSvc(), m_sevMsgMap[level]);
209  m_log[level] =
210  boost::bind(&StreamLogger::WriteToMsgSvc, m_logger[level],
211  _1);
212  } else if (val == "STDERR") {
213  m_logger[level] = new StreamLogger(std::cerr);
214  m_log[level] =
215  boost::bind(&StreamLogger::WriteToStream, m_logger[level],
216  _1);
217  } else if (val == "STDOUT") {
218  m_logger[level] = new StreamLogger(std::cout);
219  m_log[level] =
220  boost::bind(&StreamLogger::WriteToStream, m_logger[level],
221  _1);
222  } else { // A file
223  try {
224  m_logger[level] = new StreamLogger(val.c_str());
225  }
226  catch (std::exception&) {
227  m_logger[level] = 0;
228  log << MSG::ERROR << "Unable to open file \"" << VAL
229  << "\" for writing issues at level " << TAG << endmsg;
230  return StatusCode::FAILURE;
231  }
232  m_log[level] =
233  boost::bind(&StreamLogger::WriteToStream, m_logger[level], _1);
234  }
235  log << MSG::DEBUG << "Writing " << m_levelTrans[level]
236  << " issues to " << m_logger[level]->name() << endmsg;
237 
238  }
239 
240  return StatusCode::SUCCESS;
241 }
242 
243 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
244 
245 void
246 IssueLogger::report(IssueSeverity::Level lev, const std::string& str,
247  const std::string& org) {
248 
249  if ( lev < m_reportLevel) return;
250 
251  std::string msg = m_levelTrans[lev] + " " + org + " \"" + str + "\"";
252 
253  if (m_showTime) {
254  msg += " [" + Gaudi::Time::current().format(true, "%H:%M:%S %Y/%m/%d %Z") +"]";
255  }
256 
257  if (lev >= m_traceLevel) {
258  std::string stack;
259  getTraceBack(stack);
260  msg += "\n" + stack;
261  }
262 
263 
264  m_log[lev](msg);
265 
266 
267 }
268 
269 
270 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
271 
272 
273 void
275 
276  report(err.getLevel(), err.getMsg(), err.getOrigin());
277 
278 }
279 
280 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
281 
282 void
284 
285 
286  StringProperty *sap = dynamic_cast<StringProperty*> (&prop);
287  if (sap == 0) {
288  MsgStream log ( msgSvc(), name() );
289  log << MSG::ERROR << "Could not convert " << prop.name()
290  << "to a StringProperty (which it should be!)" << endmsg;
291  return;
292  }
293 
294  std::string val = sap->value();
295 
296  if (prop.name() == "ReportLevel") {
297  if (m_levelSTrans.find(val) == m_levelSTrans.end()) {
298  MsgStream log ( msgSvc(), name() );
299  log << MSG::ERROR
300  << "Option ReportLevel: unknown Issue Severity level \""
301  << val << "\". Setting it WARNING" << endmsg;
303  return;
304  } else {
306  }
307  } else if (prop.name() == "TracebackLevel") {
308  if (m_levelSTrans.find(val) == m_levelSTrans.end()) {
309  MsgStream log ( msgSvc(), name() );
310  log << MSG::ERROR
311  << "Option TracebackLevel: unknown Issue Severity level \""
312  << val << "\". Setting it to ERROR" << endmsg;
314  return;
315  } else {
317  }
318  } else {
319  MsgStream log ( msgSvc(), name() );
320  log << MSG::ERROR << "setting up unknown property \"" << prop.name()
321  << "\"" << endmsg;
322  return;
323  }
324 
325 }
326 
327 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
328 
329 void
331 
332  StringArrayProperty *sap = dynamic_cast<StringArrayProperty*>( &prop );
333  if (sap == 0) {
334  MsgStream log ( msgSvc(), name() );
335  log << MSG::ERROR << "Could not convert " << prop.name()
336  << "to a StringArrayProperty (which it should be!)" << endmsg;
337  return;
338  }
339 
340  vector<string>::const_iterator itr;
341  for (itr = sap->value().begin(); itr != sap->value().end(); ++itr) {
342  if (connect(*itr).isFailure()) {
343  MsgStream log ( msgSvc(), name() );
344  log << MSG::ERROR << "Could not setup stream " << *itr << endmsg;
345  }
346  }
347 
348  return;
349 
350 }
351 
352 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
353 
354 void
356  for (int i=1; i<IssueSeverity::NUM_LEVELS; ++i) {
357  if (m_logger[i] == 0) {
358  // default: dump to msgSvc
360 
361  m_logger[j] = new StreamLogger(msgSvc(), m_sevMsgMap[j]);
362  m_log[j] = boost::bind(&StreamLogger::WriteToMsgSvc, m_logger[j],
363  _1);
364 
365  MsgStream log ( msgSvc(), name() );
366  log << MSG::DEBUG << "Writing " << m_levelTrans[j]
367  << " issues to " << m_logger[j]->name() << endmsg;
368 
369  }
370  }
371 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
std::map< IssueSeverity::Level, std::string > m_levelTrans
Definition: IssueLogger.h:46
StatusCode connect(const std::string &)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
std::string getOrigin() const
Items & items()
Access token collection.
Definition: Tokenizer.h:99
const std::string & name() const
property name
Definition: Property.h:47
void toupper(std::string &s)
Definition: IssueLogger.cpp:20
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
static Time current(void)
Returns the current time.
Definition: Time.cpp:114
void report(IssueSeverity::Level level, const std::string &msg, const std::string &origin)
std::map< std::string, IssueSeverity::Level > m_levelSTrans
Definition: IssueLogger.h:47
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
StringArrayProperty m_outputfile
Definition: IssueLogger.h:35
void analyse(const std::string &s, const char *delim, const char *tagBegin, const char *tagEnd, const char *eq, const char *valBegin, const char *valEnd)
Analyse tokens from string.
Definition: Tokenizer.cpp:37
IssueSeverity::Level m_reportLevel
Definition: IssueLogger.h:39
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:72
StringProperty m_reportLevelS
Definition: IssueLogger.h:36
GAUDI_API int backTrace(void **addresses, const int depth)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:35
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
void setupDefaultLogger()
virtual void declareUpdateHandler(PropertyCallbackFunctor *pf)
set new callback for update
Definition: Property.cpp:141
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
StreamLogger * m_logger[IssueSeverity::NUM_LEVELS]
Definition: IssueLogger.h:41
STL Include files.
Definition: Tokenizer.h:24
const TYPE & value() const
explicit conversion
Definition: Property.h:355
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
void getTraceBack(std::string &stack)
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
void setupStreams(Property &prop)
void setupLevels(Property &prop)
virtual StatusCode reinitialize()
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
std
AIDA -> ROTO converter.
Definition: GaudiAlgs.py:73
string s
Definition: gaudirun.py:210
Templated class to add the standard messaging functionalities.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
void WriteToMsgSvc(const std::string &str)
Definition: StreamLogger.h:22
boost::function< void(const std::string &)> m_log[IssueSeverity::NUM_LEVELS]
Definition: IssueLogger.h:42
std::string name() const
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:209
IssueSeverity::Level m_traceLevel
Definition: IssueLogger.h:39
std::map< IssueSeverity::Level, MSG::Level > m_sevMsgMap
Definition: IssueLogger.h:45
BooleanProperty m_showTime
Definition: IssueLogger.h:37
list i
Definition: ana.py:128
std::map< MSG::Level, IssueSeverity::Level > m_msgSevMap
Definition: IssueLogger.h:44
IssueSeverity::Level getLevel() const
Definition: IssueSeverity.h:82
virtual ~IssueLogger()
Definition: IssueLogger.cpp:98
StringProperty m_traceLevelS
Definition: IssueLogger.h:36
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
void WriteToStream(const std::string &str)
Definition: StreamLogger.h:21
std::string getMsg() const
Definition: IssueSeverity.h:83
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:243
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
Definition: Time.cpp:280
IssueLogger(const std::string &name, ISvcLocator *svc)
Definition: IssueLogger.cpp:28