-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracepp.h
318 lines (258 loc) · 7.25 KB
/
tracepp.h
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***************************************************************************************************
tracepp
C++ tracing library for debugging with ease.
MIT License
Copyright (c) 2019 Morten Kristensen, me AT mortens DOT dev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***************************************************************************************************/
#ifndef TRACEPP_DEBUGGING_H
#define TRACEPP_DEBUGGING_H
#ifndef NDEBUG
#include <bitset>
#include <cctype>
#include <cstddef>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <utility>
namespace tracepp {
/// Type of a print function to use for tracing.
using PrintFunc = std::function<void(const std::string &, const int, const std::string &,
const std::string &, const std::string &)>;
[[maybe_unused]] static void stdoutPrint(const std::string &file, const int line,
const std::string &func, const std::string &expr,
const std::string &value)
{
std::cout << file << ":" << line << ":" << func << "(): " << expr << " = " << value << std::endl;
}
[[maybe_unused]] static void stderrPrint(const std::string &file, const int line,
const std::string &func, const std::string &expr,
const std::string &value)
{
std::cerr << file << ":" << line << ":" << func << "(): " << expr << " = " << value << std::endl;
}
namespace detail {
/// Active print function.
inline PrintFunc PRINT_FUNC = stdoutPrint;
} // namespace detail
[[maybe_unused]] static void setPrintFunc(const PrintFunc &func)
{
detail::PRINT_FUNC = func;
}
/// Prototype of general toString function.
template <typename T>
std::string toString(const T &val);
/* Special cases */
template <typename T1, typename T2>
inline std::string toString(const std::pair<T1, T2> &val)
{
return "(" + toString(val.first) + ", " + toString(val.second) + ")";
}
template <typename... T>
std::string toString(const std::tuple<T...> &val)
{
std::ostringstream oss;
oss << "(";
std::apply(
[&](T... args) {
char comma[3] = {'\0', ' ', '\0'};
((oss << comma << toString(args), comma[0] = ','), ...);
},
val);
oss << ")";
return oss.str();
}
template <std::size_t N>
inline std::string toString(const std::bitset<N> &val)
{
return val.to_string();
}
template <typename T, std::size_t N>
std::string toString(const T (&val)[N])
{
std::ostringstream oss;
oss << "[";
char comma[3] = {'\0', ' ', '\0'};
for (decltype(N) i = 0; i < N; ++i) {
oss << comma << toString(val[i]);
comma[0] = ',';
}
oss << "]";
return oss.str();
}
namespace detail {
template <typename T>
std::string mapToString(const T &map)
{
std::ostringstream oss;
oss << "{";
char comma[3] = {'\0', ' ', '\0'};
for (auto it = map.cbegin(); it != map.cend(); ++it) {
oss << comma << toString(it->first) << " -> " << toString(it->second);
comma[0] = ',';
}
oss << "}";
return oss.str();
}
} // namespace detail
template <typename K, typename V>
std::string toString(const std::map<K, V> &val)
{
return detail::mapToString(val);
}
template <typename K, typename V>
std::string toString(const std::multimap<K, V> &val)
{
return detail::mapToString(val);
}
template <typename K, typename V>
std::string toString(const std::unordered_map<K, V> &val)
{
return detail::mapToString(val);
}
template <typename T>
std::string toString(const std::queue<T> &val)
{
std::ostringstream oss;
oss << "[";
auto copy = val;
while (!copy.empty()) {
oss << toString(copy.front());
copy.pop();
if (!copy.empty()) {
oss << ", ";
}
}
oss << "]";
return oss.str();
}
/* General container solution */
template <typename T>
std::string toString(const T &val)
{
std::ostringstream oss;
oss << "[";
char comma[3] = {'\0', ' ', '\0'};
for (auto it = val.cbegin(); it != val.cend(); ++it) {
oss << comma << toString(*it);
comma[0] = ',';
}
oss << "]";
return oss.str();
}
/* Specializations */
template <>
inline std::string toString(const std::string &val)
{
return "\"" + val + "\"";
}
template <>
inline std::string toString(const bool &val)
{
return (val ? "true" : "false");
}
template <>
inline std::string toString(const int &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const unsigned int &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const short &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const unsigned short &val)
{
return std::to_string(val);
}
namespace detail {
static std::string charToString(const int &val)
{
// With MSVC in debug mode, std::isprint() triggers assertion if not: val >= -1 && val <= 255
if (val >= -1 && val <= 255 && std::isprint(val)) {
return "'" + std::string(1, static_cast<char>(val)) + "' (" + std::to_string(val) + ")";
}
return std::to_string(val);
}
} // namespace detail
template <>
inline std::string toString(const char &val)
{
return detail::charToString(val);
}
template <>
inline std::string toString(const unsigned char &val)
{
return detail::charToString(val);
}
template <>
inline std::string toString(const wchar_t &val)
{
return detail::charToString(val);
}
template <>
inline std::string toString(const long &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const unsigned long &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const long long &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const unsigned long long &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const float &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const double &val)
{
return std::to_string(val);
}
template <>
inline std::string toString(const long double &val)
{
return std::to_string(val);
}
} // namespace tracepp
#define TRACE(x) \
(tracepp::detail::PRINT_FUNC(__FILE__, __LINE__, __func__, #x, tracepp::toString(x)), x)
#else
#define TRACE(x) x
#endif
#endif // TRACEPP_DEBUGGING_H