#ifndef ARBUTILS_EXCEPTION_HPP #define ARBUTILS_EXCEPTION_HPP #include #include #include #include #if !WINDOWS #define BACKWARD_HAS_BFD 1 #include "../extern/backward.hpp" #endif namespace ArbUt { class Exception : std::logic_error { #if !WINDOWS backward::StackTrace _stack; #endif public: explicit Exception(const std::string& msg) : std::logic_error(msg) { #if !WINDOWS _stack.load_here(9); #endif } Exception(const Exception& e) noexcept : std::logic_error(e.what()), _stack(e._stack) {} const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return logic_error::what(); } [[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6, [[maybe_unused]] bool include_addr = true) const { #if !WINDOWS backward::TraceResolver tr; tr.load_stacktrace(_stack); std::stringstream ss; for (size_t i = 3; i < _stack.size() && i < depth + 3; ++i) { backward::ResolvedTrace trace = tr.resolve(_stack[i]); if (trace.source.filename.empty()) { auto objectName = (strrchr(trace.object_filename.c_str(), '/') ? strrchr(trace.object_filename.c_str(), '/') + 1 : trace.object_filename.c_str()); auto function = trace.object_function; if (function.length() > 70) { function = function.substr(0, 67); function += "..."; } ss << objectName; if (include_addr) { ss << "[" << trace.addr << "]"; } ss << " " << function << std::endl; } else { auto fileName = (strrchr(trace.source.filename.c_str(), '/') ? strrchr(trace.source.filename.c_str(), '/') + 1 : trace.source.filename.c_str()); auto function = trace.object_function; if (function.length() > 70) { function = function.substr(0, 67); function += "..."; } ss << fileName << "[" << trace.source.line << "] " << function << std::endl; } } return ss.str(); #else return "Stack trace not available on Windows."; #endif } }; } #endif // ARBUTILS_EXCEPTION_HPP