2020-08-15 11:31:16 +00:00
|
|
|
#ifndef ARBUTILS_EXCEPTION_HPP
|
|
|
|
#define ARBUTILS_EXCEPTION_HPP
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2020-08-15 12:22:32 +00:00
|
|
|
#if !WINDOWS
|
|
|
|
#define BACKWARD_HAS_BFD 1
|
2020-08-15 11:31:16 +00:00
|
|
|
#include "../extern/backward.hpp"
|
2020-08-15 12:22:32 +00:00
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
|
|
|
|
namespace ArbUt {
|
2020-08-15 12:50:17 +00:00
|
|
|
class Exception : std::logic_error {
|
2020-08-15 12:22:32 +00:00
|
|
|
#if !WINDOWS
|
2020-08-15 11:31:16 +00:00
|
|
|
backward::StackTrace _stack;
|
2020-08-15 12:22:32 +00:00
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
|
|
|
|
public:
|
2020-08-15 12:50:17 +00:00
|
|
|
explicit Exception(const std::string& msg) : std::logic_error(msg) {
|
2020-08-15 12:25:27 +00:00
|
|
|
#if !WINDOWS
|
|
|
|
_stack.load_here(9);
|
|
|
|
#endif
|
|
|
|
}
|
2020-08-15 11:31:16 +00:00
|
|
|
|
2020-08-15 14:14:06 +00:00
|
|
|
Exception(const Exception& e) noexcept : std::logic_error(e.what()), _stack(e._stack) {}
|
|
|
|
|
2020-08-15 12:50:17 +00:00
|
|
|
const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return logic_error::what(); }
|
2020-08-15 11:31:16 +00:00
|
|
|
|
2020-08-15 12:25:27 +00:00
|
|
|
[[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6,
|
|
|
|
[[maybe_unused]] bool include_addr = true) const {
|
2020-08-15 12:22:32 +00:00
|
|
|
#if !WINDOWS
|
2020-08-15 11:31:16 +00:00
|
|
|
backward::TraceResolver tr;
|
|
|
|
tr.load_stacktrace(_stack);
|
|
|
|
std::stringstream ss;
|
2020-08-15 11:48:34 +00:00
|
|
|
for (size_t i = 3; i < _stack.size() && i < depth + 3; ++i) {
|
2020-08-15 11:31:16 +00:00
|
|
|
backward::ResolvedTrace trace = tr.resolve(_stack[i]);
|
2020-08-15 11:59:10 +00:00
|
|
|
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;
|
2020-08-15 12:19:36 +00:00
|
|
|
if (include_addr) {
|
2020-08-15 11:59:10 +00:00
|
|
|
ss << "[" << trace.addr << "]";
|
|
|
|
}
|
|
|
|
ss << " " << function << std::endl;
|
2020-08-15 11:31:16 +00:00
|
|
|
|
2020-08-15 11:59:10 +00:00
|
|
|
} else {
|
2020-08-15 11:31:16 +00:00
|
|
|
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;
|
2020-08-15 11:59:10 +00:00
|
|
|
if (function.length() > 70) {
|
2020-08-15 11:31:16 +00:00
|
|
|
function = function.substr(0, 67);
|
|
|
|
function += "...";
|
|
|
|
}
|
|
|
|
ss << fileName << "[" << trace.source.line << "] " << function << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss.str();
|
2020-08-15 12:22:32 +00:00
|
|
|
#else
|
|
|
|
return "Stack trace not available on Windows.";
|
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ARBUTILS_EXCEPTION_HPP
|