Sierra Toolkit  Version of the Day
ReportHandler.cpp
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 #include <stk_util/environment/ReportHandler.hpp>
10 
11 #include <iostream>
12 #include <stdexcept>
13 
14 namespace stk_classic {
15 
16 namespace {
17 
18 // Global variables used to store the current handlers. We do not want direct
19 // external access to this state, so we put these in an empty namespace.
20 ErrorHandler s_assert_handler = &default_assert_handler;
21 ErrorHandler s_error_handler = &default_error_handler;
22 ErrorHandler s_invalid_arg_handler = &default_invalid_arg_handler;
23 REH s_reportHandler = &default_report_handler;
24 
25 template<class EXCEPTION>
26 void default_handler_req(const char* expr,
27  const std::string& location,
28  std::ostringstream& message)
29 {
30  std::string error_msg = "";
31  if (message.str() != "") {
32  error_msg = std::string("Error: ") + message.str() + "\n";
33  }
34 
35  throw EXCEPTION(
36  std::string("Requirement( ") + expr + " ) FAILED\n" +
37  "Error occured at: " + source_relative_path(location) + "\n" +
38  error_msg);
39 }
40 
41 template<class EXCEPTION>
42 void default_handler_exc(const char* expr,
43  const std::string& location,
44  std::ostringstream& message)
45 {
46  std::string error_msg = "";
47  if (message.str() != "") {
48  error_msg = std::string("Error: ") + message.str() + "\n";
49  }
50 
51  std::string expr_msg = "";
52  if (expr != std::string("")) {
53  expr_msg = std::string("Expr '") + expr + "' eval'd to true, throwing.\n";
54  }
55 
56  throw EXCEPTION(
57  expr_msg +
58  "Error occured at: " + source_relative_path(location) + "\n" +
59  error_msg);
60 }
61 
62 }
63 
64 void
66  const char * message,
67  int type)
68 {
69  std::cout << "Message type " << type << ": " << message << std::endl;
70 }
71 
72 void
74  const char * message,
75  int type)
76 {
77  (*s_reportHandler)(message, type);
78 }
79 
80 
81 REH
83  REH reh)
84 {
85  /* %TRACE[ON]% */ /* %TRACE% */
86  if (!reh)
87  throw std::runtime_error("Cannot set report handler to NULL");
88 
89  REH prev_reh = s_reportHandler;
90  s_reportHandler = reh;
91 
92  return prev_reh;
93 }
94 
95 
96 std::string
98  const std::string & path)
99 {
100  static const char *prefix[] = {"/src/", "/include/", "/Apps_", "/stk_"};
101 
102  for (unsigned int i = 0; i < sizeof(prefix)/sizeof(prefix[0]); ++i) {
103  std::string::size_type j = path.rfind(prefix[i], path.length());
104  if (j != std::string::npos) {
105  j = path.rfind("/", j - 1);
106  if (j != std::string::npos)
107  return path.substr(j + 1, path.length());
108  else
109  return path;
110  }
111  }
112  return path;
113 }
114 
115 void default_assert_handler(const char* expr,
116  const std::string& location,
117  std::ostringstream& message)
118 {
119  default_handler_req<std::logic_error>(expr, location, message);
120 }
121 
122 void default_error_handler(const char* expr,
123  const std::string& location,
124  std::ostringstream& message)
125 {
126  default_handler_exc<std::runtime_error>(expr, location, message);
127 }
128 
129 void default_invalid_arg_handler(const char* expr,
130  const std::string& location,
131  std::ostringstream& message)
132 {
133  default_handler_exc<std::invalid_argument>(expr, location, message);
134 }
135 
137 {
138  if (!handler)
139  throw std::runtime_error("Cannot set assert handler to NULL");
140 
141  ErrorHandler prev_handler = s_assert_handler;
142  s_assert_handler = handler;
143 
144  return prev_handler;
145 }
146 
148 {
149  if (!handler)
150  throw std::runtime_error("Cannot set error handler to NULL");
151 
152  ErrorHandler prev_handler = s_error_handler;
153  s_error_handler = handler;
154 
155  return prev_handler;
156 }
157 
159 {
160  if (!handler)
161  throw std::runtime_error("Cannot set invalid_arg handler to NULL");
162 
163  ErrorHandler prev_handler = s_invalid_arg_handler;
164  s_invalid_arg_handler = handler;
165 
166  return prev_handler;
167 }
168 
169 void handle_assert(const char* expr,
170  const std::string& location,
171  std::ostringstream& message)
172 {
173  (*s_assert_handler)(expr, location, message);
174 }
175 
176 void handle_error(const char* expr,
177  const std::string& location,
178  std::ostringstream& message)
179 {
180  (*s_error_handler)(expr, location, message);
181 }
182 
183 void handle_invalid_arg(const char* expr,
184  const std::string& location,
185  std::ostringstream& message)
186 {
187  (*s_invalid_arg_handler)(expr, location, message);
188 }
189 
190 } // namespace stk_classic
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)