Better handling for Exception stack trace testing.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-08-15 13:59:10 +02:00
parent 5ac7654be1
commit 1dee4cd4a8
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 22 additions and 6 deletions

View File

@ -18,26 +18,37 @@ namespace ArbUt {
[[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg.c_str(); }
[[nodiscard]] std::string GetStacktrace(size_t depth = 6) const {
[[nodiscard]] std::string GetStacktrace(size_t depth = 6, bool include_addr = true) const {
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()){
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{
} 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){
if (function.length() > 70) {
function = function.substr(0, 67);
function += "...";
}
ss << fileName << "[" << trace.source.line << "] " << function << std::endl;
}
}
return ss.str();

View File

@ -8,8 +8,13 @@ TEST_CASE("Throw exception, get stack trace") {
throw ArbUt::Exception("foobar");
}
catch (const ArbUt::Exception& e) {
#ifndef NDEBUG
REQUIRE(e.GetStacktrace(1) ==
"ExceptionTests.cpp[8] ____C_A_T_C_H____T_E_S_T____0()\n");
#else
REQUIRE(e.GetStacktrace(1, false) ==
"ArbutilsTests Catch::RunContext::runTest(Catch::TestCase const&)\n");
#endif
}
}