Support new exception type with stack trace.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
fb6b04bf03
commit
f29e111b78
|
@ -27,7 +27,7 @@ if (TESTS)
|
||||||
# And create an executable from it. Also include catch.hpp.
|
# And create an executable from it. Also include catch.hpp.
|
||||||
add_executable(ArbutilsTests ${TEST_FILES} extern/catch.hpp)
|
add_executable(ArbutilsTests ${TEST_FILES} extern/catch.hpp)
|
||||||
# And finally link the library to the executable.
|
# And finally link the library to the executable.
|
||||||
target_link_libraries(ArbutilsTests Arbutils)
|
target_link_libraries(ArbutilsTests Arbutils -lbfd -ldl)
|
||||||
# Add a compilation definition to the code that we are building a test build.
|
# Add a compilation definition to the code that we are building a test build.
|
||||||
target_compile_definitions(ArbutilsTests PRIVATE TESTS_BUILD)
|
target_compile_definitions(ArbutilsTests PRIVATE TESTS_BUILD)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,48 @@
|
||||||
|
#ifndef ARBUTILS_EXCEPTION_HPP
|
||||||
|
#define ARBUTILS_EXCEPTION_HPP
|
||||||
|
#define BACKWARD_HAS_BFD 1
|
||||||
|
|
||||||
|
#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(); }
|
||||||
|
|
||||||
|
[[nodiscard]] std::string GetStacktrace() const {
|
||||||
|
backward::TraceResolver tr;
|
||||||
|
tr.load_stacktrace(_stack);
|
||||||
|
std::stringstream ss;
|
||||||
|
for (size_t i = 3; i < _stack.size(); ++i) {
|
||||||
|
backward::ResolvedTrace trace = tr.resolve(_stack[i]);
|
||||||
|
if (trace.source.filename.empty()){
|
||||||
|
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // ARBUTILS_EXCEPTION_HPP
|
|
@ -0,0 +1,21 @@
|
||||||
|
#ifdef TESTS_BUILD
|
||||||
|
#include "../extern/catch.hpp"
|
||||||
|
#include "../src/Exception.hpp"
|
||||||
|
using namespace ArbUt;
|
||||||
|
|
||||||
|
TEST_CASE("Throw exception, get stack trace") {
|
||||||
|
try {
|
||||||
|
throw ArbUt::Exception("foobar");
|
||||||
|
}
|
||||||
|
catch (const ArbUt::Exception& e) {
|
||||||
|
REQUIRE(e.GetStacktrace() ==
|
||||||
|
"ExceptionTests.cpp[8] ____C_A_T_C_H____T_E_S_T____0()\n"
|
||||||
|
"catch.hpp[14036] Catch::TestInvokerAsFunction::invoke() const\n"
|
||||||
|
"catch.hpp[13929] Catch::TestCase::invoke() const\n"
|
||||||
|
"catch.hpp[12791] Catch::RunContext::invokeActiveTestCase()\n"
|
||||||
|
"catch.hpp[12756] Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, ...\n"
|
||||||
|
"catch.hpp[12525] Catch::RunContext::runTest(Catch::TestCase const&)\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue