2020-08-15 11:31:16 +00:00
|
|
|
#ifndef ARBUTILS_EXCEPTION_HPP
|
|
|
|
#define ARBUTILS_EXCEPTION_HPP
|
2020-08-15 12:19:36 +00:00
|
|
|
#if !WINDOWS
|
2020-08-15 11:31:16 +00:00
|
|
|
#define BACKWARD_HAS_BFD 1
|
2020-08-15 12:19:36 +00:00
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include "../extern/backward.hpp"
|
|
|
|
|
|
|
|
namespace ArbUt {
|
|
|
|
class Exception : std::exception {
|
|
|
|
std::string _msg;
|
|
|
|
backward::StackTrace _stack;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Exception(std::string msg) : _msg(std::move(msg)) { _stack.load_here(9); }
|
|
|
|
|
|
|
|
[[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg.c_str(); }
|
|
|
|
|
2020-08-15 11:59:10 +00:00
|
|
|
[[nodiscard]] std::string GetStacktrace(size_t depth = 6, bool include_addr = true) const {
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ARBUTILS_EXCEPTION_HPP
|