forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.hpp
More file actions
170 lines (149 loc) · 4.87 KB
/
error.hpp
File metadata and controls
170 lines (149 loc) · 4.87 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef ERROR_HPP
#define ERROR_HPP
#include <exception>
#include <string>
#include <utility>
#include "platform/platform.hpp"
#include "utils/microfmt.hpp"
#if IS_MSVC
#define CPPTRACE_PFUNC __FUNCSIG__
#else
#define CPPTRACE_PFUNC __extension__ __PRETTY_FUNCTION__
#endif
namespace cpptrace {
namespace detail {
class internal_error : public std::exception {
std::string msg;
public:
internal_error(std::string message) : msg("Cpptrace internal error: " + std::move(message)) {}
template<typename... Args>
internal_error(const char* format, Args&&... args) : internal_error(microfmt::format(format, args...)) {}
const char* what() const noexcept override {
return msg.c_str();
}
};
// Lightweight std::source_location.
struct source_location {
const char* const file;
const int line;
constexpr source_location(
const char* _file,
int _line
) : file(_file), line(_line) {}
};
#define CPPTRACE_CURRENT_LOCATION ::cpptrace::detail::source_location(__FILE__, __LINE__)
enum class assert_type {
assert,
verify,
panic,
};
constexpr const char* assert_actions[] = {"assertion", "verification", "panic"};
constexpr const char* assert_names[] = {"ASSERT", "VERIFY", "PANIC"};
[[noreturn]] inline void assert_fail(
assert_type type,
const char* expression,
const char* signature,
source_location location,
const char* message
) {
const char* action = assert_actions[static_cast<std::underlying_type<assert_type>::type>(type)];
const char* name = assert_names[static_cast<std::underlying_type<assert_type>::type>(type)];
if(message == nullptr) {
throw internal_error(
"Cpptrace {} failed at {}:{}: {}\n"
" {}({});\n",
action, location.file, location.line, signature,
name, expression
);
} else {
throw internal_error(
"Cpptrace {} failed at {}:{}: {}: {}\n"
" {}({});\n",
action, location.file, location.line, signature, message,
name, expression
);
}
}
[[noreturn]] inline void panic(
const char* signature,
source_location location,
const std::string& message = ""
) {
if(message == "") {
throw internal_error(
"Cpptrace panic {}:{}: {}\n",
location.file, location.line, signature
);
} else {
throw internal_error(
"Cpptrace panic {}:{}: {}: {}\n",
location.file, location.line, signature, message.c_str()
);
}
}
template<typename T>
void nullfn() {
// this method doesn't do anything and is never called.
}
#define PHONY_USE(...) (nullfn<decltype(__VA_ARGS__)>())
// Work around a compiler warning
template<typename T>
bool as_bool(T&& value) {
return static_cast<bool>(std::forward<T>(value));
}
// Work around a compiler warning
template<typename T>
std::string as_string(T&& value) {
return std::string(std::forward<T>(value));
}
inline std::string as_string() {
return "";
}
// Check condition in both debug and release. std::runtime_error on failure.
#define PANIC(...) ((::cpptrace::detail::panic)(CPPTRACE_PFUNC, CPPTRACE_CURRENT_LOCATION, ::cpptrace::detail::as_string(__VA_ARGS__)))
template<typename T>
void assert_impl(
T condition,
const char* message,
assert_type type,
const char* args,
const char* signature,
source_location location
) {
if(!as_bool(condition)) {
assert_fail(type, args, signature, location, message);
}
}
template<typename T>
void assert_impl(
T condition,
assert_type type,
const char* args,
const char* signature,
source_location location
) {
assert_impl(
condition,
nullptr,
type,
args,
signature,
location
);
}
// Check condition in both debug and release. std::runtime_error on failure.
#define VERIFY(...) ( \
assert_impl(__VA_ARGS__, ::cpptrace::detail::assert_type::verify, #__VA_ARGS__, CPPTRACE_PFUNC, CPPTRACE_CURRENT_LOCATION) \
)
#ifndef NDEBUG
// Check condition in both debug. std::runtime_error on failure.
#define ASSERT(...) ( \
assert_impl(__VA_ARGS__, ::cpptrace::detail::assert_type::assert, #__VA_ARGS__, CPPTRACE_PFUNC, CPPTRACE_CURRENT_LOCATION) \
)
#else
// Check condition in both debug. std::runtime_error on failure.
#define ASSERT(...) PHONY_USE(__VA_ARGS__)
#endif
}
}
#endif