-
Notifications
You must be signed in to change notification settings - Fork 0
/
callstack_exception.hpp
210 lines (186 loc) · 6.96 KB
/
callstack_exception.hpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* CallstackLibrary - Library creating human-readable call stacks.
*
* Copyright (C) 2023 - 2024 mhahnFr
*
* This file is part of the CallstackLibrary.
*
* The CallstackLibrary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The CallstackLibrary is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with the
* CallstackLibrary, see the file LICENSE. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __lcs_callstack_exception_hpp
#define __lcs_callstack_exception_hpp
#include <cxxabi.h>
#include <exception>
#include <ostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include "callstack.h"
#include "callstack_cxx_compat.hpp"
/**
* @brief The namespace all C++ classes of the CallstackLibrary can be found in.
*
* The name stands for: **l**ib**c**all**s**tack.
*/
namespace lcs {
/**
* This exception class defines an exception capable to create and print
* the callstack where it initially has been constructed - usually where it was thrown.
*
* @since v1.1
*/
class exception: public std::exception {
/** The optional message of this exception. */
const std::string message;
/** Whether to automatically translate and add the callstack to the message. */
bool shouldPrintStacktrace;
/** The callstack where this object was constructed. */
mutable callstack cs;
/** A message buffer. */
mutable std::string messageBuffer;
/**
* Converts the given number to a string and returns it.
*
* @param number the number to be converted
* @return the string representation of the given number
*/
static inline std::string toString(unsigned long number) {
#ifdef LCS_CXX11
return std::to_string(number);
#else
std::stringstream stream;
stream << number;
return stream.str();
#endif
}
/**
* Returns the demangled name of the class the calling object belongs to.
*
* @return the name of the class of the calling object
*/
inline std::string getName() const LCS_NOEXCEPT {
std::string toReturn;
const char * rawName = typeid(*this).name();
int status;
char * dName = abi::__cxa_demangle(rawName, LCS_NULL, LCS_NULL, &status);
if (dName != LCS_NULL) {
toReturn = dName;
std::free(dName);
} else {
toReturn = rawName;
}
return toReturn;
}
public:
/**
* Trivial default constructor.
*
* @param printStacktrace whether to automatically append the stacktrace to the exception message
*/
explicit inline exception(const bool printStacktrace = true) LCS_NOEXCEPT
: std::exception(), shouldPrintStacktrace(printStacktrace) {}
/**
* @brief Constructs an exception with the given message.
*
* The message is copied.
*
* @param message the message
* @param printStacktrace whether to automatically append the stacktrace
*/
explicit inline exception(const char * message, const bool printStacktrace = true) LCS_NOEXCEPT
: std::exception(), message(message), shouldPrintStacktrace(printStacktrace) {}
/**
* Constructs an exception with the given message.
*
* @param message the message
* @param printStacktrace whether to automatically append the stacktrace
*/
explicit inline exception(const std::string & message, const bool printStacktrace = true) LCS_NOEXCEPT
: std::exception(), message(message), shouldPrintStacktrace(printStacktrace) {}
inline exception(const exception & other)
: std::exception(other), message(other.message), shouldPrintStacktrace(other.shouldPrintStacktrace), cs(other.cs) {}
inline virtual ~exception() LCS_NOEXCEPT {};
#ifdef LCS_CXX11
inline exception(exception &&) = default;
#endif
inline virtual const char * what() const LCS_NOEXCEPT LCS_OVERRIDE {
if (!messageBuffer.empty()) {
return messageBuffer.c_str();
}
if (shouldPrintStacktrace) {
std::stringstream stream;
printStacktrace(stream, true);
messageBuffer = stream.str();
} else {
messageBuffer = getName() + (message.empty() ? "" : (": \"" + message + "\""));
}
return messageBuffer.c_str();
}
/**
* Prints the stacktrace where this exception has been constructed to the given output
* stream.
*
* @param out the output stream to print to
* @param printMessage whether to print the message text as well
*/
inline void printStacktrace(std::ostream & out, const bool printMessage = true) const {
if (printMessage) {
out << getName() << (message.empty() ? "" : ": \"" + message + "\"");
}
out << ", stacktrace:" << std::endl;
const callstack_frame * frames = callstack_toArray(cs);
for (std::size_t i = 0; i < callstack_getFrameCount(cs); ++i) {
out << (i == 0 ? "At" : "in") << ": "
<< "(" << callstack_frame_getShortestNameOr(&frames[i], "<< Unknown >>") << ") "
<< (frames[i].function == LCS_NULL ? "<< Unknown >>" : frames[i].function)
<< (frames[i].sourceFile == LCS_NULL ? ""
: (" (" + std::string(callstack_frame_getShortestSourceFile(&frames[i])) + ":" + toString(frames[i].sourceLine) + ")"))
<< std::endl;
}
}
/**
* Sets whether to automatically append the stacktrace.
*
* @param printStacktrace whether to automatically append the stacktrace
*/
inline void setPrintStacktrace(const bool printStacktrace) LCS_NOEXCEPT {
shouldPrintStacktrace = printStacktrace;
}
/**
* Returns whether to automatically append the stacktrace.
*
* @return whether to automatically append the stacktrace
*/
inline LCS_CONSTEXPR bool getPrintStacktrace() const LCS_NOEXCEPT {
return shouldPrintStacktrace;
}
/**
* Returns the callstack where this exception has been constructed.
*
* @return the construction callstack
*/
inline LCS_CONSTEXPR callstack & getCallstack() const LCS_NOEXCEPT {
return cs;
}
/**
* Returns the message of this exception.
*
* @return the message text
*/
inline LCS_CONSTEXPR const std::string & getMessage() const LCS_NOEXCEPT {
return message;
}
};
}
#endif /* __lcs_callstack_exception_hpp */