| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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/AttribStringParser.h"
4 #include "GaudiKernel/ISvcLocator.h"
5 #include "GaudiKernel/MsgStream.h"
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 = {
23 {IssueSeverity::NIL, MSG::NIL}, {IssueSeverity::VERBOSE, MSG::VERBOSE},
24 {IssueSeverity::DEBUG, MSG::DEBUG}, {IssueSeverity::DEBUG1, MSG::DEBUG},
25 {IssueSeverity::DEBUG2, MSG::DEBUG}, {IssueSeverity::DEBUG3, MSG::DEBUG},
26 {IssueSeverity::INFO, MSG::INFO}, {IssueSeverity::WARNING, MSG::WARNING},
27 {IssueSeverity::RECOVERABLE, MSG::ERROR}, {IssueSeverity::ERROR, MSG::ERROR},
28 {IssueSeverity::FATAL, MSG::FATAL}, {IssueSeverity::ALWAYS, MSG::ALWAYS}};
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
47 DECLARE_COMPONENT( IssueLogger )
48
49 //*************************************************************************//
50 inline void toupper( std::string& s ) { std::transform( s.begin(), s.end(), s.begin(), (int ( * )( int ))toupper ); }
51
52 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
53
54 IssueLogger::IssueLogger( const std::string& name, ISvcLocator* svc ) : base_class( name, svc )
55 {
56
57 m_reportLevelS.declareUpdateHandler( &IssueLogger::setupLevels, this );
58 m_traceLevelS.declareUpdateHandler( &IssueLogger::setupLevels, this );
59 m_outputfile.declareUpdateHandler( &IssueLogger::setupStreams, this );
60
61 m_reportLevel = IssueSeverity::WARNING;
62 m_traceLevel = IssueSeverity::ERROR;
63 }
64
65 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
66
67 StatusCode IssueLogger::initialize()
68 {
69
70 StatusCode st = Service::initialize();
71 if ( st.isSuccess() ) {
72 setupDefaultLogger();
73 }
74 return st;
75 }
76
77 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
78
79 StatusCode IssueLogger::reinitialize()
80 {
81
82 warning() << "reinitialize not implemented" << endmsg;
83 return StatusCode::SUCCESS;
84 }
85
86 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
87
88 StatusCode IssueLogger::finalize()
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
98 StatusCode IssueLogger::connect( const std::string& ident )
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 );
106 IssueSeverity::Level level;
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" ) {
114 level = IssueSeverity::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" ) {
132 m_log[level] = {new StreamLogger( std::cerr ), &StreamLogger::WriteToStream};
133 } else if ( attrib.value == "STDOUT" ) {
134 m_log[level] = {new StreamLogger( std::cout ), &StreamLogger::WriteToStream};
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
152 void IssueLogger::report( IssueSeverity::Level lev, const std::string& str, const std::string& org )
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
167 void IssueLogger::setupLevels( Gaudi::Details::PropertyBase& prop )
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" ) {
189 set( m_reportLevel, IssueSeverity::WARNING );
190 } else if ( prop.name() == "TracebackLevel" ) {
191 set( m_traceLevel, IssueSeverity::ERROR );
192 } else {
193 error() << "setting up unknown property \"" << prop.name() << "\"" << endmsg;
194 }
195 }
196
197 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
198
199 void IssueLogger::setupStreams( Gaudi::Details::PropertyBase& prop )
200 {
201
202 Gaudi::Property<std::vector<std::string>>* sap = dynamic_cast<Gaudi::Property<std::vector<std::string>>*>( &prop );
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
217 void IssueLogger::setupDefaultLogger()
218 {
219 for ( int i = 1; i < IssueSeverity::NUM_LEVELS; ++i ) {
220 if ( !m_log[i] ) {
221 // default: dump to msgSvc
222 IssueSeverity::Level j = IssueSeverity::Level( i );
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 }
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:64
StatusCode::SUCCESS
Definition: StatusCode.h:30
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:132
IssueLogger::connect
StatusCode connect(const std::string &)
Definition: IssueLogger.cpp:98
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
StatusCode::FAILURE
Definition: StatusCode.h:29
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:174
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:314
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:123
IssueLogger::setupStreams
void setupStreams(Gaudi::Details::PropertyBase &prop)
Definition: IssueLogger.cpp:199
IssueLogger::finalize
StatusCode finalize() override
Definition: IssueLogger.cpp:88
toupper
void toupper(std::string &s)
Definition: IssueLogger.cpp:50
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: Property.h:40
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:74
IssueSeverity::DEBUG2
Definition: IssueSeverity.h:51
IssueLogger::setupLevels
void setupLevels(Gaudi::Details::PropertyBase &prop)
Definition: IssueLogger.cpp:167
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:32
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:84
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:114
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::string
STL class.
std::string::at
T at(T...args)
IssueLogger::setupDefaultLogger
void setupDefaultLogger()
Definition: IssueLogger.cpp:217
MSG::FATAL
Definition: IMessageSvc.h:22
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:117
IssueLogger::logger_t
Definition: IssueLogger.h:34
IssueSeverity::VERBOSE
Definition: IssueSeverity.h:48
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:120
IssueSeverity::Level
Level
Definition: IssueSeverity.h:46
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::m_showTime
Gaudi::Property< bool > m_showTime
Definition: IssueLogger.h:30
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
IssueLogger::reinitialize
StatusCode reinitialize() override
Definition: IssueLogger.cpp:79
IssueLogger::logger_t::reset
void reset()
Definition: IssueLogger.h:47
Time.h
std::exception
STL class.
IssueSeverity::FATAL
Definition: IssueSeverity.h:57
std::string::find
T find(T...args)
IssueLogger
Definition: IssueLogger.h:13
IssueLogger::m_log
std::array< logger_t, IssueSeverity::NUM_LEVELS > m_log
Definition: IssueLogger.h:49
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:126
std::begin
T begin(T...args)
IssueLogger::initialize
StatusCode initialize() override
Definition: IssueLogger.cpp:67
IssueSeverity::NUM_LEVELS
Definition: IssueSeverity.h:59
IssueLogger::m_outputfile
Gaudi::Property< std::vector< std::string > > m_outputfile
Definition: IssueLogger.h:27
gaudirun.s
string s
Definition: gaudirun.py:245
AttribStringParser.h
CommonMessaging
Definition: CommonMessaging.h:55
CommonMessagingBase::msgSvc
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:70
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
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:475
IssueLogger::m_traceLevelS
Gaudi::Property< std::string > m_traceLevelS
Definition: IssueLogger.h:29
IssueLogger::m_traceLevel
IssueSeverity::Level m_traceLevel
Definition: IssueLogger.h:32
MSG::INFO
Definition: IMessageSvc.h:19
std::for_each
T for_each(T...args)
IssueSeverity::DEBUG3
Definition: IssueSeverity.h:52
std::cerr
ISvcLocator.h
IssueSeverity::getLevel
IssueSeverity::Level getLevel() const
Definition: IssueSeverity.h:82
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:152
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
IssueLogger::m_reportLevelS
Gaudi::Property< std::string > m_reportLevelS
Definition: IssueLogger.h:28
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:54