ExceptionSvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // STD & STL
5 // ============================================================================
6 #include <cassert>
7 #include <algorithm>
8 // ============================================================================
9 // GaudiKernel
10 // ============================================================================
11 #include "GaudiKernel/ISvcLocator.h"
12 // ============================================================================
13 //Local
14 // ============================================================================
15 #include "ExceptionSvc.h"
16 // ============================================================================
17 
18 #include <boost/regex.hpp>
19 
20 using namespace std;
21 
22 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23 
25 
26 inline void toupper(std::string &s)
27 {
28  std::transform(s.begin(), s.end(), s.begin(),
29  (int(*)(int)) toupper);
30 }
31 
32 //
34 //
35 
36 ExceptionSvc::ExceptionSvc( const std::string& name, ISvcLocator* svc )
37  : base_class( name, svc )
38  , m_mode_exc ( ALL ), m_mode_err( NONE )
39  , m_log(msgSvc(), name )
40 {
41  // for exceptions
42  declareProperty( "Catch" , m_mode_exc_s="ALL" ) ;
43 
44  // for return codes
45  declareProperty( "Errors" , m_mode_err_s="NONE" ) ;
46 }
47 
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49 
51 
52 }
53 
54 
55 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
56 
61 
62  if ( status.isFailure() ) { return status ; } // RETURN
63 
64  string key = m_mode_exc_s.value();
65 
66  string::size_type loc = key.find(" ");
67  std::string mode;
68  if (loc == std::string::npos) {
69  mode = key;
70  } else {
71  mode = key.substr(0,loc);
72  }
73 
74  toupper(mode);
75 
76  if (mode == "NONE") {
77  m_mode_exc = NONE;
78  } else if (mode == "ALL") {
79  m_mode_exc = ALL;
80  } else {
81  m_log << MSG::ERROR << "Unknown mode for Exception handling: \"" << mode
82  << "\". Default must be one of \"ALL\" or \"NONE\"" << endmsg;
84  return StatusCode::FAILURE;
85  }
86 
87  if (loc == string::npos) {
88  key = "";
89  } else {
90  key = key.substr(loc+1);
91  }
92 
93  std::string VAL, TAG;
94 
95  static const boost::regex exp{"[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*([^[:space:]]+)"};
96  static const auto tok_end = boost::sregex_iterator();
97  for (auto tok_iter = boost::sregex_iterator(begin(key), end(key), exp);
98  tok_iter != tok_end; ++tok_iter)
99  {
100  TAG = (*tok_iter)[1];
101 
102  VAL = (*tok_iter)[2];
103  toupper(VAL);
104 
105  if (VAL == "SUCCESS") {
106  m_retCodesExc[TAG] = SUCCESS;
107  } else if ( VAL == "FAILURE" ) {
108  m_retCodesExc[TAG] = FAILURE;
109  } else if ( VAL == "REVOVERABLE" ) {
110  m_retCodesExc[TAG] = RECOVERABLE;
111  } else if ( VAL == "RETHROW" ) {
112  m_retCodesExc[TAG] = RETHROW;
113  } else if ( VAL == "DEFAULT" ) {
114  m_retCodesExc[TAG] = DEFAULT;
115  } else {
116  m_log << MSG::ERROR << "In JobOpts: unknown return code \"" << VAL
117  << "\" for Algorithm " << TAG << std::endl
118  << " Must be one of: DEFAULT, SUCCESS, FAILURE, RECOVERABLE, RETHROW"
119  << endmsg;
121  return StatusCode::FAILURE;
122  }
123 
124  m_log << MSG::DEBUG << "Will catch exceptions thrown by: " << TAG
125  << " -> action: " << VAL << endmsg;
126 
127  }
128 
129  // now process errors
130 
131  key = m_mode_err_s.value();
132 
133  loc = key.find(" ");
134  if (loc == std::string::npos) {
135  mode = key;
136  } else {
137  mode = key.substr(0,loc);
138  }
139 
140  toupper(mode);
141 
142  if (mode == "NONE") {
143  m_mode_err = NONE;
144  } else if (mode == "ALL") {
145  m_mode_err = ALL;
146  } else {
147  m_log << MSG::ERROR << "Unknown mode for Error handling: \"" << mode
148  << "\". Default must be one of \"ALL\" or \"NONE\"" << endmsg;
150  return StatusCode::FAILURE;
151  }
152 
153  if (loc == string::npos) {
154  key = "";
155  } else {
156  key = key.substr(loc+1);
157  }
158 
159  for (auto tok_iter = boost::sregex_iterator(begin(key), end(key), exp);
160  tok_iter != tok_end; ++tok_iter)
161  {
162  TAG = (*tok_iter)[1];
163 
164  VAL = (*tok_iter)[2];
165  toupper(VAL);
166 
167  if (VAL == "SUCCESS") {
168  m_retCodesErr[TAG] = SUCCESS;
169  } else if ( VAL == "FAILURE" ) {
170  m_retCodesErr[TAG] = FAILURE;
171  } else if ( VAL == "RECOVERABLE" ) {
172  m_retCodesErr[TAG] = RECOVERABLE;
173  } else {
174  m_log << MSG::ERROR << "In JobOpts: unknown return code \"" << VAL
175  << "\" for Algorithm " << TAG << std::endl
176  << " Must be one of: SUCCESS, FAILURE, RECOVERABLE"
177  << endmsg;
179  return StatusCode::FAILURE;
180  }
181 
182  m_log << MSG::DEBUG << "Will process Errors returned by: " << TAG
183  << " -> action: " << VAL << endmsg;
184 
185  }
186 
187  return status;
188 }
189 
190 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
191 
194  StatusCode status = Service::finalize();
195 
196  return status;
197 }
198 
199 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
200 
202 ( const INamedInterface& alg ,
203  const StatusCode& st ) const
204 {
205  m_log << MSG::DEBUG << "Handling Error from " << alg.name() << endmsg;
206 
207  // is this Alg special?
208  if (m_retCodesErr.find(alg.name()) != m_retCodesErr.end()) {
209  ReturnState iret = m_retCodesErr.find(alg.name())->second;
210 
211  switch ( iret ) {
212  case SUCCESS:
213  return StatusCode::SUCCESS;
214  case FAILURE:
215  return StatusCode::FAILURE;
216  case RECOVERABLE:
218  case RETHROW:
219  // should never get here
220  break;
221  case DEFAULT:
222  // should never get here
223  break;
224  }
225 
226  } else {
227 
228  if (m_mode_err == ALL) {
229  // turn it into a FAILURE
230  return StatusCode::FAILURE;
231 
232  } else {
233  assert (m_mode_err == NONE );
234  // don't touch the return code
235  return st;
236  }
237  }
238 
239  return StatusCode::FAILURE;
240 }
241 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
242 
244 ( const INamedInterface& alg ) const
245 {
246 
247  // is this Alg special?
248  if (m_retCodesExc.find(alg.name()) != m_retCodesExc.end()) {
249  ReturnState iret = m_retCodesExc.find(alg.name())->second;
250 
251  switch ( iret ) {
252  case DEFAULT:
253  // there is no default
254  return StatusCode::FAILURE;
255  case SUCCESS:
256  return StatusCode::SUCCESS;
257  case FAILURE:
258  return StatusCode::FAILURE;
259  case RECOVERABLE:
261  case RETHROW:
262  throw;
263  }
264 
265  } else {
266 
267  if (m_mode_exc == ALL) {
268  throw;
269  } else {
270  assert (m_mode_exc == NONE);
271  return StatusCode::FAILURE;
272  }
273  }
274 
275  return StatusCode::FAILURE;
276 }
277 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
278 
280 ( const INamedInterface& alg ) const
281 {
282  m_log << MSG::DEBUG << "Handling unknown exception for " << alg.name()
283  << endmsg;
284 
285  return process(alg);
286 }
287 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
288 
290 ( const INamedInterface& alg ,
291  const std::exception & exc ) const
292 {
293  m_log << MSG::DEBUG << "Handling std:except: \"" << exc.what() << "\" for "
294  << alg.name() << endmsg;
295 
296  return process(alg) ;
297 
298 }
299 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
300 
302 ( const INamedInterface& alg ,
303  const GaudiException & exc ) const
304 {
305  m_log << MSG::DEBUG << "Handling GaudiException: \"" << exc << "\" for "
306  << alg.name() << endmsg;
307 
308  return process(alg);
309 
310 }
311 
312 // ============================================================================
313 // The END
314 // ============================================================================
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:244
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:246
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual StatusCode initialize()
initialize the service
std::map< std::string, ReturnState > m_retCodesErr
Definition: ExceptionSvc.h:67
virtual StatusCode handleErr(const INamedInterface &o, const StatusCode &s) const
Handle errors.
STL namespace.
virtual StatusCode handle(const INamedInterface &o, const GaudiException &e) const
Handle caught GaudiExceptions.
Policy m_mode_exc
Definition: ExceptionSvc.h:65
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
constexpr double second
virtual const std::string & name() const =0
Retrieve the name of the instance.
StringProperty m_mode_err_s
Definition: ExceptionSvc.h:66
ExceptionSvc()
no default constructor
int ALL
message levels --------------------------------------------------------—
Definition: Constants.py:11
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Simple implementation of IExceptionSvc abstract interface.
Definition: ExceptionSvc.h:19
StringProperty m_mode_exc_s
Definition: ExceptionSvc.h:66
const TYPE & value() const
explicit conversion
Definition: Property.h:355
tuple end
Definition: IOTest.py:101
std::map< std::string, ReturnState > m_retCodesExc
Definition: ExceptionSvc.h:67
IInterface compliant class extending IInterface with the name() method.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:72
virtual StatusCode finalize()
finalize the service
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
string s
Definition: gaudirun.py:244
Templated class to add the standard messaging functionalities.
virtual ~ExceptionSvc()
Destructor.
Policy m_mode_err
Definition: ExceptionSvc.h:65
virtual StatusCode process(const INamedInterface &o) const
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:212
void toupper(std::string &s)
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:197
MsgStream m_log
Definition: ExceptionSvc.h:69