Joos1W Compiler Framework
All Classes Functions Typedefs Pages
Error.cc
1 #include "utils/Error.h"
2 
3 #ifdef ENABLE_STACKTRACE
4 #include <third-party/backward.h>
5 #endif
6 
7 
8 namespace utils {
9 
10 std::string FatalError::get_trace(const std::string& what) {
11 #ifdef ENABLE_STACKTRACE
12  using namespace backward;
13  // Load trace from current location
14  StackTrace stackTrace;
15  TraceResolver resolver;
16  stackTrace.load_here();
17  resolver.load_stacktrace(stackTrace);
18  std::ostringstream ss;
19  // Print the stack trace
20  Printer p;
21  p.color_mode = ColorMode::always;
22  p.inliner_context_size = 1;
23  p.trace_context_size = 1;
24  p.print(stackTrace, ss);
25  // Print the error at the end for better visibility
26  if(!what.empty())
27  ss << "Error: " << what << std::endl;
28  return ss.str();
29 #else
30  return what;
31 #endif
32 }
33 
34 std::string AssertError::get_trace(const std::string& what) {
35 #ifdef ENABLE_STACKTRACE
36  using namespace backward;
37  // Load trace from current location
38  StackTrace stackTrace;
39  TraceResolver resolver;
40  stackTrace.load_here();
41  stackTrace.skip_n_firsts(5);
42  resolver.load_stacktrace(stackTrace);
43  std::ostringstream ss;
44  // Print the stack trace
45  Printer p;
46  p.color_mode = ColorMode::always;
47  p.inliner_context_size = 1;
48  p.trace_context_size = 1;
49  p.print(stackTrace, ss);
50  // Print the error at the end for better visibility
51  ss << what;
52  return ss.str();
53 #else
54  return what;
55 #endif
56 }
57 
58 } // namespace utils