-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodern-backtrace.cpp
65 lines (59 loc) · 1.83 KB
/
modern-backtrace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// modern-backtrace.cpp : demonstration exception class using C++20's <source_location>
// and C++23's <stacktrace> headers
// Note: compile with: g++ -std=c++23 modern-backtrace.cpp -lstdc++_libbacktrace
// Note: tested on Compiler Explorer
#include <source_location>
#include <stacktrace>
#include <exception>
#include <string_view>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
class LocatedException : public std::exception {
char *msg = nullptr;
public:
LocatedException(bool stacktrace = false,
std::string_view m = {},
const std::source_location loc = std::source_location::current(),
const std::stacktrace trc = std::stacktrace::current()) {
std::ostringstream oss;
oss << loc.file_name() << '(' << loc.line() << ':' << loc.column() << ')';
oss << " in function `" << loc.function_name() << "` " << m << '\n';
if (stacktrace) {
oss << "Stacktrace follows:\n" << trc;
}
msg = new char[oss.str().size() + 1];
strcpy(msg, oss.str().c_str());
}
LocatedException() = delete;
LocatedException(const LocatedException&) = delete;
LocatedException(LocatedException&&) = delete;
const LocatedException& operator=(const LocatedException&) = delete;
const LocatedException& operator=(LocatedException&&) = delete;
virtual const char *what() const noexcept override {
return msg;
}
virtual ~LocatedException() {
delete[] msg;
}
};
void h() {
throw LocatedException(true, "Whoops, shouldn't be here!");
}
void g() {
h();
}
void f() {
g();
}
int main() {
try {
f();
}
catch (std::exception &e) {
std::cerr << "Caught exception: " << e.what() << '\n';
return 1;
}
return 0;
}