| The Gaudi Framework v27r1

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

- File List
- File Members

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