Sierra Toolkit  Version of the Day
ReportHandler.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef STK_UTIL_ENVIRONMENT_REPORTHANDLER_HPP
10 #define STK_UTIL_ENVIRONMENT_REPORTHANDLER_HPP
11 
12 #include <iosfwd>
13 #include <string>
14 #include <sstream>
15 
16 namespace stk_classic {
17 
22 
28 typedef void (*REH)(const char *message, int type);
29 
36 typedef void (*ErrorHandler)(const char* expr, const std::string& location, std::ostringstream& message);
37 
50 void default_report_handler(const char *message, int type);
51 
61 
71 void report(const char *message, int type);
72 
82 std::string source_relative_path(const std::string &path);
83 
88 void default_assert_handler(const char* expr,
89  const std::string& location,
90  std::ostringstream& message);
91 
96 void default_error_handler(const char* expr,
97  const std::string& location,
98  std::ostringstream& message);
99 
104 void default_invalid_arg_handler(const char* expr,
105  const std::string& location,
106  std::ostringstream& message);
107 
114 
121 
128 
132 void handle_assert(const char* expr,
133  const std::string& location,
134  std::ostringstream& message);
135 
139 void handle_error(const char* expr,
140  const std::string& location,
141  std::ostringstream& message);
142 
146 void handle_invalid_arg(const char* expr,
147  const std::string& location,
148  std::ostringstream& message);
149 
150 
154 
155 } // namespace stk_classic
156 
161 
169 #define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
170 #define STR_TRACE_LINE(s) #s
171 
172 #ifdef __PRETTY_FUNCTION__
173 
174 #define COUT_TRACE " Function::Line="<<__PRETTY_FUNCTION__<<":"<<__LINE__
175 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__) + " in " + std::string(__PRETTY_FUNCTION__))
176 
177 #else
178 
179 #define COUT_TRACE " File::Line="<<__FILE__<<":"<<__LINE__
180 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
181 
182 #endif
183 
184 #define StackTrace std::string(std::string(" exception thrown from ") + stk_classic::source_relative_path(STR_TRACE))
185 
186 // The do-while is necessary to prevent usage of this macro from changing
187 // program semantics (e.g. dangling-else problem). The obvious implementation:
188 // if (expr) ; else throw ...
189 // is not adequate because it causes ambiguous else statements in this context:
190 // if (something)
191 // ThrowRequire(foo);
192 // The compiler does not know whether the else statement that the macro inserts
193 // applies to the "if (something) " or the "if (expr)".
194 #define ThrowGenericCond(expr, message, handler) \
195  do { \
196  if ( !(expr) ) { \
197  std::ostringstream stk_util_internal_throw_require_oss; \
198  stk_util_internal_throw_require_oss << message; \
199  stk_classic::handler( #expr, \
200  STR_TRACE, \
201  stk_util_internal_throw_require_oss ); \
202  } \
203  } while (false)
204 
205 // This generic macro is for unconditional throws. We pass "" as the expr
206 // string, the handler should be smart enough to realize that this means there
207 // was not expression checked, AKA, this throw was unconditional.
208 #define ThrowGeneric(message, handler) \
209  do { \
210  std::ostringstream stk_util_internal_throw_require_oss; \
211  stk_util_internal_throw_require_oss << message; \
212  stk_classic::handler( "", \
213  STR_TRACE, \
214  stk_util_internal_throw_require_oss ); \
215 } while (false)
216 
217 // The macros below define the exceptions that we want to support within
218 // STK. The intent is that STK developers will never call throw XXX
219 // directly. This will give us full control over the exceptions being generated
220 // by STK and how they are handled. These macros are also designed to make
221 // it as easy as possible for developers to throw exceptions that have good
222 // error messages and to reduce the volume of coded needed for error handling.
223 //
224 // We currently support the following exceptions in STK:
225 // logic_error <-> ThrowAssert, ThrowAsserMsg, ThrowRequire, ThrowRequireMsg
226 // runtime_error <-> ThrowErrorMsgIf, ThrowErrorMsg
227 // invalid_argument <-> ThrowInvalidArgMsgIf, ThrowInvalidArgIf
228 //
229 // Please note the logic of the errors is the opposite of the asserts. The
230 // asserts will throw exceptions if the given expression is false; for the
231 // error macros, exceptions will be thrown if the given expression is true.
232 //
233 // USE:
234 // All of the following have versions that do not require a message, but
235 // we strongly encourage developers to use the versions that take the
236 // message.
237 //
238 // ASSERTS:
239 // ThrowAssertMsg(expr, message);
240 // If NDEBUG is not defined, throw a logic error if expr evaluates to
241 // false, adding message to the error message . Use this for expensive
242 // logic-mistake checks that could impact performance.
243 //
244 // ThrowRequireMsg(code, message);
245 // Always throw a logic error if expr evaluates to false, adding message
246 // to error message. Use this for inexpensive logic-mistake checks
247 // that do not impact performance.
248 //
249 // ERRORS:
250 // ThrowErrorMsgIf(expr, message);
251 // Throw a runtime error if expr evaluates to true, adding message to
252 // the error message. Use this to generate errors dealing with system
253 // errors or other errors that do not involve invalid parameters being
254 // passed to functions.
255 //
256 // ThrowInvalidArgMsgIf(expr, message);
257 // Throw an invalid_argument error if expr evaluates to true, adding
258 // message to the error message. Use this to generate errors dealing with
259 // users passing invalid arguments to functions in the API.
260 //
261 // EXAMPLES:
262 //
263 // 1) Require that i equals j, demonstate use of put-tos in the message arg
264 // ThrowRequireMsg(i == j, "i(" << i << ") != j(" << j << ")");
265 //
266 // 2) Check method argument foo is not NULL
267 // ThrowInvalidArgMsgIf(foo != NULL, "Arg foo is NULL");
268 
269 #define ThrowRequireMsg(expr,message) ThrowGenericCond(expr, message, handle_assert)
270 #define ThrowRequire(expr) ThrowRequireMsg(expr, "")
271 
272 #ifdef NDEBUG
273 # define ThrowAssert(expr) ((void) (0))
274 # define ThrowAssertMsg(expr,message) ((void) (0))
275 #else
276 # define ThrowAssert(expr) ThrowRequire(expr)
277 # define ThrowAssertMsg(expr,message) ThrowRequireMsg(expr,message)
278 #endif
279 
280 #define ThrowErrorMsgIf(expr, message) ThrowGenericCond( !(expr), message, handle_error)
281 #define ThrowErrorIf(expr) ThrowErrorMsgIf(expr, "")
282 #define ThrowErrorMsg(message) ThrowGeneric( message, handle_error )
283 
284 #define ThrowInvalidArgMsgIf(expr, message) ThrowGenericCond( !(expr), message, handle_invalid_arg)
285 #define ThrowInvalidArgIf(expr) ThrowInvalidArgMsgIf(expr, "")
286 
290 
291 #endif // STK_UTIL_ENVIRONMENT_REPORTHANDLER_HPP
ErrorHandler set_assert_handler(ErrorHandler handler)
void handle_error(const char *expr, const std::string &location, std::ostringstream &message)
void(* ErrorHandler)(const char *expr, const std::string &location, std::ostringstream &message)
ErrorHandler defines the signature of functions that can be used to handle errors. expr is the expression of the failing error-check, location is a raw code location (something like file:line, no prose), and message is the error message.
ErrorHandler set_error_handler(ErrorHandler handler)
void handle_invalid_arg(const char *expr, const std::string &location, std::ostringstream &message)
void report(const char *message, int type)
Function report calls the current exception reporter to report the message in x.
void default_error_handler(const char *expr, const std::string &location, std::ostringstream &message)
void default_invalid_arg_handler(const char *expr, const std::string &location, std::ostringstream &message)
void(* REH)(const char *message, int type)
Type definition REH is a pointer to a function of type void that takes a const std::exception referen...
Sierra Toolkit.
void default_assert_handler(const char *expr, const std::string &location, std::ostringstream &message)
void handle_assert(const char *expr, const std::string &location, std::ostringstream &message)
REH set_report_handler(REH reh)
Function set_report_handler sets the exception report function to be called when an report_exception(...
void default_report_handler(const char *message, int type)
Function default_report_handler is the default error reporter for sierra exceptions. Note that it is implemented in Fmwk_sierra.C so that it can participate.
std::string source_relative_path(const std::string &path)
Function source_relative_path strips everything through "/src/", "/include/", "/App_", or "/stk_" so that error message output doesn&#39;t mention names.
ErrorHandler set_invalid_arg_handler(ErrorHandler handler)