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
|
2020-08-16 14:59:13 +00:00
|
|
|
#define BACKWARD_HAS_DW 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
|
2020-08-16 14:59:13 +00:00
|
|
|
_stack.load_here(32);
|
2020-08-15 12:25:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-08-15 11:31:16 +00:00
|
|
|
|
2020-08-15 14:14:33 +00:00
|
|
|
Exception(const Exception& e) noexcept
|
|
|
|
: std::logic_error(e.what())
|
|
|
|
#if !WINDOWS
|
|
|
|
,
|
|
|
|
_stack(e._stack)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
2020-08-15 14:14:06 +00:00
|
|
|
|
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-16 08:43:54 +00:00
|
|
|
if (_stack.size() == 0) {
|
2020-08-16 08:41:10 +00:00
|
|
|
return "No stack trace could be retrieved.";
|
|
|
|
}
|
2020-08-15 11:31:16 +00:00
|
|
|
backward::TraceResolver tr;
|
2020-08-16 17:15:45 +00:00
|
|
|
backward::SnippetFactory snippetFactory;
|
2020-08-15 11:31:16 +00:00
|
|
|
tr.load_stacktrace(_stack);
|
|
|
|
std::stringstream ss;
|
2020-08-16 14:59:13 +00:00
|
|
|
ss << "Stacktrace with depth " << depth << ": " << std::endl;
|
|
|
|
bool foundExceptionClass = false;
|
2020-08-16 17:15:45 +00:00
|
|
|
size_t framesAppended = 0;
|
|
|
|
for (size_t i = 0; i < _stack.size() && framesAppended <= depth; ++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()) {
|
2020-08-16 14:59:13 +00:00
|
|
|
AppendNoSourceStack(ss, trace, include_addr);
|
2020-08-15 11:59:10 +00:00
|
|
|
} else {
|
2020-08-16 17:15:45 +00:00
|
|
|
AppendSourceStack(ss, trace.source, foundExceptionClass, snippetFactory);
|
|
|
|
if (foundExceptionClass) {
|
|
|
|
framesAppended++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& t : trace.inliners) {
|
|
|
|
AppendSourceStack(ss, t, foundExceptionClass, snippetFactory);
|
2020-08-16 15:34:58 +00:00
|
|
|
if (foundExceptionClass) {
|
2020-08-16 14:59:13 +00:00
|
|
|
framesAppended++;
|
|
|
|
if (framesAppended >= depth)
|
|
|
|
break;
|
2020-08-15 11:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2020-08-16 14:59:13 +00:00
|
|
|
|
2020-08-16 15:27:09 +00:00
|
|
|
#if !WINDOWS
|
2020-08-16 14:59:13 +00:00
|
|
|
private:
|
|
|
|
static void AppendNoSourceStack(std::stringstream& ss, const backward::ResolvedTrace& trace,
|
|
|
|
bool include_addr) {
|
|
|
|
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;
|
|
|
|
}
|
2020-08-16 17:15:45 +00:00
|
|
|
static void AppendSourceStack(std::stringstream& ss, const backward::ResolvedTrace::SourceLoc& source,
|
|
|
|
bool& foundExceptionClass, backward::SnippetFactory& snippetFactory) {
|
|
|
|
auto fileName = (strrchr(source.filename.c_str(), '/') ? strrchr(source.filename.c_str(), '/') + 1
|
|
|
|
: source.filename.c_str());
|
2020-08-16 14:59:13 +00:00
|
|
|
if (strcmp(fileName, "Exception.hpp") == 0) {
|
|
|
|
foundExceptionClass = true;
|
|
|
|
return;
|
|
|
|
} else if (!foundExceptionClass) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-16 17:15:45 +00:00
|
|
|
auto snippet = snippetFactory.get_snippet(source.filename, source.line, 1)[0].second;
|
|
|
|
if (snippet.length() > 70) {
|
|
|
|
snippet = snippet.substr(0, 67);
|
|
|
|
snippet += "...";
|
2020-08-16 14:59:13 +00:00
|
|
|
}
|
2020-08-16 17:15:45 +00:00
|
|
|
|
|
|
|
ss << fileName << "[" << source.line << "] " << snippet << std::endl;
|
2020-08-16 14:59:13 +00:00
|
|
|
}
|
2020-08-16 15:27:09 +00:00
|
|
|
#endif
|
2020-08-16 15:31:11 +00:00
|
|
|
};
|
2020-08-15 11:31:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ARBUTILS_EXCEPTION_HPP
|