CGRA-ME
Exception.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #ifndef __EXCEPTION_H__
12 #define __EXCEPTION_H__
13 
14 #include <cstdlib>
15 #include <exception>
16 #include <sstream>
17 #include <stdexcept>
18 #include <string>
19 
20 struct cgrame_error : public std::runtime_error
21 {
22  cgrame_error(std::string message)
23  : std::runtime_error((message.insert(0,"[ERROR] "), std::move(message)))
24  {}
25 };
26 
28 {
29  cgrame_mapper_error(std::string message)
30  : cgrame_error((message.insert(0,"[MAPPER] "), std::move(message)))
31  {}
32 };
33 
35 {
36  cgrame_visual_error(std::string message)
37  : cgrame_error((message.insert(0,"[VISUAL] "), std::move(message)))
38  {}
39 };
40 
42 {
43  cgrame_adl_error(std::string message)
44  : cgrame_error((message.insert(0,"[ADL] "), std::move(message)))
45  {}
46 };
47 
49 {
50  cgrame_model_error(std::string message)
51  : cgrame_error((message.insert(0,"[MODEL] "), std::move(message)))
52  {}
53 };
54 
65 template<typename EXCEPTION, typename FUNC>
66 [[noreturn]] void make_and_throw(FUNC func) {
67  std::ostringstream os;
68  func(os);
69  throw EXCEPTION(os.str());
70 }
71 
84 template<typename T, typename FUNC>
85 auto make_from_stream(FUNC func) {
86  std::ostringstream os;
87  func(os);
88  return T(os.str());
89 }
90 
94 template<typename F>
95 std::string string_from_stream(F&& f) {
96  std::ostringstream stream;
97  f(stream);
98  return stream.str();
99 }
100 
107 inline void tryToSafelyPrintExceptionInfo(std::exception_ptr eptr, std::ostream& os, const char* action_name) noexcept {
108  try {
109  if (eptr) {
110  std::rethrow_exception(eptr);
111  }
112  } catch (const std::exception& e) {
113  try {
114  os << "std::exception thrown when " << action_name << ": .what() = " << e.what() << std::endl;
115  } catch (...) {
116  std::fprintf(stderr, "exception encountered when handling std::exception when %s", action_name);
117  std::fflush(stderr);
118  }
119  } catch (...) {
120  try {
121  os << "unknown exception thrown when " << action_name << std::endl;
122  } catch (...) {
123  std::fprintf(stderr, "exception encountered when handling unknown exception when %s", action_name);
124  std::fflush(stderr);
125  }
126  }
127 }
128 
134 inline void printExceptionToOutAndErr(std::exception_ptr eptr, const char* line_prefix, const char* message) {
135  std::stringstream msg;
136  msg
137  << line_prefix << '\n'
138  << line_prefix << ' ' << message << ". Exception chain:\n"
139  ;
140 
141  int level = 0;
142  while (eptr) {
143  msg << line_prefix; for (int i = 0; i < level + 2; ++i) msg << ' '; // indentation
144 
145  // add a message based on the type of exception
146  try {
147  std::rethrow_exception(eptr);
148  } catch (const std::exception& e) {
149  msg << ".what(): " << e.what() << '\n';
150  } catch (...) {
151  msg << "Unknown exception\n";
152  }
153 
154  // determine if we should keep looping
155  try {
156  std::rethrow_exception(eptr);
157  } catch (const std::nested_exception& as_ne) {
158  try {
159  as_ne.rethrow_nested();
160  } catch (...) {
161  eptr = std::current_exception(); // move on to the nested exception
162  }
163  } catch (...) {
164  eptr = nullptr; // stop looping
165  }
166 
167  level += 1;
168  }
169 
170  msg << line_prefix;
171 
172  fprintf(stdout, "[stdout]\n%s\n", msg.str().c_str());
173  fflush(stdout);
174  fprintf(stderr, "[stderr]\n%s\n", msg.str().c_str());
175  fflush(stderr);
176 }
177 
178 
179 #endif
make_from_stream
auto make_from_stream(FUNC func)
Definition: Exception.h:85
make_and_throw
void make_and_throw(FUNC func)
Definition: Exception.h:66
tryToSafelyPrintExceptionInfo
void tryToSafelyPrintExceptionInfo(std::exception_ptr eptr, std::ostream &os, const char *action_name) noexcept
Definition: Exception.h:107
cgrame_model_error
Definition: Exception.h:48
cgrame_adl_error
Definition: Exception.h:41
cgrame_visual_error
Definition: Exception.h:34
string_from_stream
std::string string_from_stream(F &&f)
Definition: Exception.h:95
cgrame_visual_error::cgrame_visual_error
cgrame_visual_error(std::string message)
Definition: Exception.h:36
cgrame_model_error::cgrame_model_error
cgrame_model_error(std::string message)
Definition: Exception.h:50
cgrame_error::cgrame_error
cgrame_error(std::string message)
Definition: Exception.h:30
cgrame_mapper_error::cgrame_mapper_error
cgrame_mapper_error(std::string message)
Definition: Exception.h:29
cgrame_adl_error::cgrame_adl_error
cgrame_adl_error(std::string message)
Definition: Exception.h:43
printExceptionToOutAndErr
void printExceptionToOutAndErr(std::exception_ptr eptr, const char *line_prefix, const char *message)
Definition: Exception.h:134
cgrame_mapper_error
Definition: Exception.h:27
cgrame_error
Definition: Exception.h:20