All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 // ============================================================================
12 #include "GaudiKernel/Tokenizer.h"
13 // ============================================================================
14 //Local
15 // ============================================================================
16 #include "ExceptionSvc.h"
17 // ============================================================================
18 
19 using namespace std;
20 
21 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
24 
25 inline void toupper(std::string &s)
26 {
27  std::transform(s.begin(), s.end(), s.begin(),
28  (int(*)(int)) toupper);
29 }
30 
31 //
33 //
34 
35 ExceptionSvc::ExceptionSvc( const std::string& name, ISvcLocator* svc )
36  : base_class( name, svc )
37  , m_mode_exc ( ALL ), m_mode_err( NONE )
38  , m_log(msgSvc(), name )
39 {
40  // for exceptions
41  declareProperty( "Catch" , m_mode_exc_s="ALL" ) ;
42 
43  // for return codes
44  declareProperty( "Errors" , m_mode_err_s="NONE" ) ;
45 }
46 
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48 
50 
51 }
52 
53 
54 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
55 
60 
61  if ( status.isFailure() ) { return status ; } // RETURN
62 
63  string key = m_mode_exc_s.value();
64 
65  string::size_type loc = key.find(" ");
66  std::string mode;
67  if (loc == std::string::npos) {
68  mode = key;
69  } else {
70  mode = key.substr(0,loc);
71  }
72 
73  toupper(mode);
74 
75  if (mode == "NONE") {
76  m_mode_exc = NONE;
77  } else if (mode == "ALL") {
78  m_mode_exc = ALL;
79  } else {
80  m_log << MSG::ERROR << "Unknown mode for Exception handling: \"" << mode
81  << "\". Default must be one of \"ALL\" or \"NONE\"" << endmsg;
83  return StatusCode::FAILURE;
84  }
85 
86  if (loc == string::npos) {
87  key = "";
88  } else {
89  key = key.substr(loc+1,key.length());
90  }
91 
92  Tokenizer tok(true);
93  std::string val,VAL,TAG;
94 
95  tok.analyse( key, " ", "", "", "=", "", "");
96 
97  for ( Tokenizer::Items::iterator i = tok.items().begin();
98  i != tok.items().end(); i++) {
99  const std::string& tag = (*i).tag();
100  TAG = tag;
101 
102  val = (*i).value();
103  VAL = val;
104  toupper(VAL);
105 
106  if (VAL == "SUCCESS") {
107  m_retCodesExc[TAG] = SUCCESS;
108  } else if ( VAL == "FAILURE" ) {
109  m_retCodesExc[TAG] = FAILURE;
110  } else if ( VAL == "REVOVERABLE" ) {
111  m_retCodesExc[TAG] = RECOVERABLE;
112  } else if ( VAL == "RETHROW" ) {
113  m_retCodesExc[TAG] = RETHROW;
114  } else if ( VAL == "DEFAULT" ) {
115  m_retCodesExc[TAG] = DEFAULT;
116  } else {
117  m_log << MSG::ERROR << "In JobOpts: unknown return code \"" << VAL
118  << "\" for Algorithm " << TAG << std::endl
119  << " Must be one of: DEFAULT, SUCCESS, FAILURE, RECOVERABLE, RETHROW"
120  << endmsg;
122  return StatusCode::FAILURE;
123  }
124 
125  m_log << MSG::DEBUG << "Will catch exceptions thrown by: " << TAG
126  << " -> action: " << VAL << endmsg;
127 
128  }
129 
130  // now process errors
131 
132  key = m_mode_err_s.value();
133 
134  loc = key.find(" ");
135  if (loc == std::string::npos) {
136  mode = key;
137  } else {
138  mode = key.substr(0,loc);
139  }
140 
141  toupper(mode);
142 
143  if (mode == "NONE") {
144  m_mode_err = NONE;
145  } else if (mode == "ALL") {
146  m_mode_err = ALL;
147  } else {
148  m_log << MSG::ERROR << "Unknown mode for Error handling: \"" << mode
149  << "\". Default must be one of \"ALL\" or \"NONE\"" << endmsg;
151  return StatusCode::FAILURE;
152  }
153 
154  if (loc == string::npos) {
155  key = "";
156  } else {
157  key = key.substr(loc+1,key.length());
158  }
159 
160  Tokenizer tok2(true);
161  tok2.analyse( key, " ", "", "", "=", "", "");
162 
163  for ( Tokenizer::Items::iterator i = tok2.items().begin();
164  i != tok2.items().end(); i++) {
165  const std::string& tag = (*i).tag();
166  TAG = tag;
167 
168  val = (*i).value();
169  VAL = val;
170  toupper(VAL);
171 
172  if (VAL == "SUCCESS") {
173  m_retCodesErr[TAG] = SUCCESS;
174  } else if ( VAL == "FAILURE" ) {
175  m_retCodesErr[TAG] = FAILURE;
176  } else if ( VAL == "RECOVERABLE" ) {
177  m_retCodesErr[TAG] = RECOVERABLE;
178  } else {
179  m_log << MSG::ERROR << "In JobOpts: unknown return code \"" << VAL
180  << "\" for Algorithm " << TAG << std::endl
181  << " Must be one of: SUCCESS, FAILURE, RECOVERABLE"
182  << endmsg;
184  return StatusCode::FAILURE;
185  }
186 
187  m_log << MSG::DEBUG << "Will process Errors returned by: " << TAG
188  << " -> action: " << VAL << endmsg;
189 
190  }
191 
192  return status;
193 }
194 
195 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
196 
199  StatusCode status = Service::finalize();
200 
201  return status;
202 }
203 
204 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
205 
207 ( const INamedInterface& alg ,
208  const StatusCode& st ) const
209 {
210  m_log << MSG::DEBUG << "Handling Error from " << alg.name() << endmsg;
211 
212  // is this Alg special?
213  if (m_retCodesErr.find(alg.name()) != m_retCodesErr.end()) {
214  ReturnState iret = m_retCodesErr.find(alg.name())->second;
215 
216  switch ( iret ) {
217  case SUCCESS:
218  return StatusCode::SUCCESS;
219  case FAILURE:
220  return StatusCode::FAILURE;
221  case RECOVERABLE:
223  case RETHROW:
224  // should never get here
225  break;
226  case DEFAULT:
227  // should never get here
228  break;
229  }
230 
231  } else {
232 
233  if (m_mode_err == ALL) {
234  // turn it into a FAILURE
235  return StatusCode::FAILURE;
236 
237  } else {
238  assert (m_mode_err == NONE );
239  // don't touch the return code
240  return st;
241  }
242  }
243 
244  return StatusCode::FAILURE;
245 }
246 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
247 
249 ( const INamedInterface& alg ) const
250 {
251 
252  // is this Alg special?
253  if (m_retCodesExc.find(alg.name()) != m_retCodesExc.end()) {
254  ReturnState iret = m_retCodesExc.find(alg.name())->second;
255 
256  switch ( iret ) {
257  case DEFAULT:
258  // there is no default
259  return StatusCode::FAILURE;
260  case SUCCESS:
261  return StatusCode::SUCCESS;
262  case FAILURE:
263  return StatusCode::FAILURE;
264  case RECOVERABLE:
266  case RETHROW:
267  throw;
268  }
269 
270  } else {
271 
272  if (m_mode_exc == ALL) {
273  throw;
274  } else {
275  assert (m_mode_exc == NONE);
276  return StatusCode::FAILURE;
277  }
278  }
279 
280  return StatusCode::FAILURE;
281 }
282 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
283 
285 ( const INamedInterface& alg ) const
286 {
287  m_log << MSG::DEBUG << "Handling unknown exception for " << alg.name()
288  << endmsg;
289 
290  return process(alg);
291 }
292 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
293 
295 ( const INamedInterface& alg ,
296  const std::exception & exc ) const
297 {
298  m_log << MSG::DEBUG << "Handling std:except: \"" << exc.what() << "\" for "
299  << alg.name() << endmsg;
300 
301  return process(alg) ;
302 
303 }
304 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
305 
307 ( const INamedInterface& alg ,
308  const GaudiException & exc ) const
309 {
310  m_log << MSG::DEBUG << "Handling GaudiException: \"" << exc << "\" for "
311  << alg.name() << endmsg;
312 
313  return process(alg);
314 
315 }
316 
317 // ============================================================================
318 // The END
319 // ============================================================================
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:243
int ALL
message levels --------------------------------------------------------—
Definition: Constants.py:11
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:245
Items & items()
Access token collection.
Definition: Tokenizer.h:99
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.
virtual StatusCode handle(const INamedInterface &o, const GaudiException &e) const
Handle caught GaudiExceptions.
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
Policy m_mode_exc
Definition: ExceptionSvc.h:65
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
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
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
STL Include files.
Definition: Tokenizer.h:24
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
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:74
virtual StatusCode finalize()
finalize the service
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
string s
Definition: gaudirun.py:210
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:211
list i
Definition: ana.py:128
void toupper(std::string &s)
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
MsgStream m_log
Definition: ExceptionSvc.h:69