1717#include < stdexcept>
1818#include < string>
1919
20- #ifdef __linux__
21- #include < execinfo.h>
22- #include < unistd.h>
23- #endif
24-
2520#ifdef REACTOR_CPP_VALIDATE
2621constexpr bool runtime_validation = true ;
2722#else
@@ -37,6 +32,33 @@ constexpr bool runtime_assertion = true;
3732// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
3833#define reactor_assert (x ) assert (x)
3934
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+
4062namespace reactor {
4163
4264class ValidationError : public std ::runtime_error {
@@ -48,23 +70,10 @@ public:
4870 : std::runtime_error(build_message(msg)) {}
4971};
5072
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-
6273constexpr inline void validate ([[maybe_unused]] bool condition, [[maybe_unused]] const std::string_view message) {
6374 if constexpr (runtime_validation) { // NOLINT
6475 if (!condition) {
65- #ifdef __linux__
66- print_debug_backtrace ();
67- #endif
76+ print_backtrace ();
6877 throw ValidationError (message);
6978 }
7079 }
0 commit comments