| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

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
3 #include "GaudiKernel/ISvcLocator.h"
4 #include "GaudiKernel/MsgStream.h"
5 #include "GaudiKernel/Tokenizer.h"
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
17 DECLARE_COMPONENT(IssueLogger)
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
36 m_reportLevelS.declareUpdateHandler(&IssueLogger::setupLevels,this);
37 m_traceLevelS.declareUpdateHandler(&IssueLogger::setupLevels,this);
38 m_outputfile.declareUpdateHandler(&IssueLogger::setupStreams,this);
39
40 m_reportLevel = IssueSeverity::WARNING;
41 m_traceLevel = IssueSeverity::ERROR;
42
43 for (int i=0; i<IssueSeverity::NUM_LEVELS; ++i) {
44 m_logger[i] = 0;
45 }
46
47 m_msgSevMap[MSG::NIL] = IssueSeverity::NIL;
48 m_msgSevMap[MSG::VERBOSE] = IssueSeverity::VERBOSE;
49 m_msgSevMap[MSG::DEBUG] = IssueSeverity::DEBUG;
50 m_msgSevMap[MSG::INFO] = IssueSeverity::INFO;
51 m_msgSevMap[MSG::WARNING] = IssueSeverity::WARNING;
52 m_msgSevMap[MSG::ERROR] = IssueSeverity::ERROR;
53 m_msgSevMap[MSG::FATAL] = IssueSeverity::FATAL;
54 m_msgSevMap[MSG::ALWAYS] = IssueSeverity::ALWAYS;
55
56 m_sevMsgMap[IssueSeverity::NIL] = MSG::NIL;
57 m_sevMsgMap[IssueSeverity::VERBOSE] = MSG::VERBOSE;
58 m_sevMsgMap[IssueSeverity::DEBUG] = MSG::DEBUG;
59 m_sevMsgMap[IssueSeverity::DEBUG1] = MSG::DEBUG;
60 m_sevMsgMap[IssueSeverity::DEBUG2] = MSG::DEBUG;
61 m_sevMsgMap[IssueSeverity::DEBUG3] = MSG::DEBUG;
62 m_sevMsgMap[IssueSeverity::INFO] = MSG::INFO;
63 m_sevMsgMap[IssueSeverity::WARNING] = MSG::WARNING;
64 m_sevMsgMap[IssueSeverity::RECOVERABLE] = MSG::ERROR;
65 m_sevMsgMap[IssueSeverity::ERROR] = MSG::ERROR;
66 m_sevMsgMap[IssueSeverity::FATAL] = MSG::FATAL;
67 m_sevMsgMap[IssueSeverity::ALWAYS] = MSG::ALWAYS;
68
69 m_levelTrans[IssueSeverity::VERBOSE] = "VERBOSE";
70 m_levelTrans[IssueSeverity::DEBUG] = "DEBUG";
71 m_levelTrans[IssueSeverity::DEBUG1] = "DEBUG1";
72 m_levelTrans[IssueSeverity::DEBUG2] = "DEBUG2";
73 m_levelTrans[IssueSeverity::DEBUG3] = "DEBUG3";
74 m_levelTrans[IssueSeverity::INFO] = "INFO";
75 m_levelTrans[IssueSeverity::WARNING] = "WARNING";
76 m_levelTrans[IssueSeverity::RECOVERABLE] = "RECOVERABLE";
77 m_levelTrans[IssueSeverity::ERROR] = "ERROR";
78 m_levelTrans[IssueSeverity::FATAL] = "FATAL";
79 m_levelTrans[IssueSeverity::ALWAYS] = "ALWAYS";
80
81 m_levelSTrans["VERBOSE"] = IssueSeverity::VERBOSE;
82 m_levelSTrans["DEBUG"] = IssueSeverity::DEBUG;
83 m_levelSTrans["DEBUG1"] = IssueSeverity::DEBUG1;
84 m_levelSTrans["DEBUG2"] = IssueSeverity::DEBUG2;
85 m_levelSTrans["DEBUG3"] = IssueSeverity::DEBUG3;
86 m_levelSTrans["INFO"] = IssueSeverity::INFO;
87 m_levelSTrans["WARNING"] = IssueSeverity::WARNING;
88 m_levelSTrans["RECOVERABLE"] = IssueSeverity::RECOVERABLE;
89 m_levelSTrans["ERROR"] = IssueSeverity::ERROR;
90 m_levelSTrans["FATAL"] = IssueSeverity::FATAL;
91 m_levelSTrans["ALWAYS"] = IssueSeverity::ALWAYS;
92
93
94 }
95
96 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
97
98 IssueLogger::~IssueLogger() {
99
100 }
101
102 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
103
104 StatusCode
105 IssueLogger::initialize() {
106
107 StatusCode st = Service::initialize();
108 if (st.isFailure()) { return st; }
109
110 setupDefaultLogger();
111
112 return st;
113
114 }
115
116 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
117
118 StatusCode
119 IssueLogger::reinitialize() {
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
131 StatusCode
132 IssueLogger::finalize() {
133
134 MsgStream log ( msgSvc(), name() );
135 log << MSG::DEBUG << "IssueLogger::finalize" << endmsg;
136
137 for (int i=0; i<IssueSeverity::NUM_LEVELS; ++i) {
138 IssueSeverity::Level j = IssueSeverity::Level (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
156 StatusCode
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
180 IssueSeverity::Level level;
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") {
189 level = IssueSeverity::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
274 IssueLogger::report( const IssueSeverity &err ) {
275
276 report(err.getLevel(), err.getMsg(), err.getOrigin());
277
278 }
279
280 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
281
282 void
283 IssueLogger::setupLevels(Property& prop) {
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;
302 m_reportLevel = IssueSeverity::WARNING;
303 return;
304 } else {
305 m_reportLevel = m_levelSTrans[m_reportLevelS];
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;
313 m_traceLevel = IssueSeverity::ERROR;
314 return;
315 } else {
316 m_traceLevel = m_levelSTrans[m_traceLevelS];
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
330 IssueLogger::setupStreams(Property& prop) {
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
355 IssueLogger::setupDefaultLogger() {
356 for (int i=1; i<IssueSeverity::NUM_LEVELS; ++i) {
357 if (m_logger[i] == 0) {
358 // default: dump to msgSvc
359 IssueSeverity::Level j = IssueSeverity::Level (i);
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 }