17
17
#include < stdexcept>
18
18
#include < string>
19
19
20
- #ifdef __linux__
21
- #include < execinfo.h>
22
- #include < unistd.h>
23
- #endif
24
-
25
20
#ifdef REACTOR_CPP_VALIDATE
26
21
constexpr bool runtime_validation = true ;
27
22
#else
@@ -37,6 +32,33 @@ constexpr bool runtime_assertion = true;
37
32
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
38
33
#define reactor_assert (x ) assert(x)
39
34
35
+ #ifdef REACTOR_CPP_USE_BACKTRACE
36
+
37
+ // NOLINTNEXTLINE
38
+ #include REACTOR_CPP_BACKTRACE_HEADER
39
+ #include < array>
40
+ #include < iostream>
41
+
42
+ namespace reactor {
43
+
44
+ constexpr std::size_t MAX_TRACE_SIZE{16 };
45
+
46
+ inline void print_backtrace () {
47
+ std::array<void *, MAX_TRACE_SIZE> trace{nullptr };
48
+ int size = backtrace (trace.data (), MAX_TRACE_SIZE);
49
+ char ** messages = backtrace_symbols (trace.data (), size);
50
+ for (int i{0 }; i < size; i++) {
51
+ std::cerr << " [backtrace] " << messages[i] << ' \n ' ; // NOLINT
52
+ }
53
+ }
54
+
55
+ } // namespace reactor
56
+ #else
57
+ namespace reactor {
58
+ inline void print_backtrace () {}
59
+ } // namespace reactor
60
+ #endif // REACTOR_CPP_BACKTRACE_SUPPORT
61
+
40
62
namespace reactor {
41
63
42
64
class ValidationError : public std ::runtime_error {
@@ -48,23 +70,10 @@ public:
48
70
: std::runtime_error(build_message(msg)) {}
49
71
};
50
72
51
- #ifdef __linux__
52
- constexpr std::size_t MAX_STACK_SIZE{10 };
53
-
54
- inline void print_debug_backtrace () {
55
- void * array[10 ]; // NOLINT
56
- // get void*'s for all entries on the stack
57
- int size = backtrace ((void **)array, MAX_STACK_SIZE);
58
- backtrace_symbols_fd ((void **)array, size, STDERR_FILENO);
59
- }
60
- #endif
61
-
62
73
constexpr inline void validate ([[maybe_unused]] bool condition, [[maybe_unused]] const std::string_view message) {
63
74
if constexpr (runtime_validation) { // NOLINT
64
75
if (!condition) {
65
- #ifdef __linux__
66
- print_debug_backtrace ();
67
- #endif
76
+ print_backtrace ();
68
77
throw ValidationError (message);
69
78
}
70
79
}
0 commit comments