forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_utils-inl.h
More file actions
278 lines (249 loc) Β· 9.15 KB
/
debug_utils-inl.h
File metadata and controls
278 lines (249 loc) Β· 9.15 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
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
#ifndef SRC_DEBUG_UTILS_INL_H_
#define SRC_DEBUG_UTILS_INL_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "debug_utils.h"
#include "env.h"
#include "util-inl.h"
#include <type_traits>
namespace node {
template <typename T>
concept StringViewConvertible = requires(T a) {
{
a.ToStringView()
} -> std::convertible_to<std::string_view>;
};
template <typename T>
concept StringConvertible = requires(T a) {
{
a.ToString()
} -> std::convertible_to<std::string>;
};
// For std::filesystem::path and similar types
template <typename T>
concept StringConvertibleFSPathLike = requires(T a) {
{
a.string()
} -> std::convertible_to<std::string>;
};
struct ToStringHelper {
template <typename T>
requires(StringConvertible<T>) && (!StringViewConvertible<T>)
static std::string Convert(const T& value) {
return value.ToString();
}
template <typename T>
requires(StringConvertibleFSPathLike<T>) && (!StringViewConvertible<T>) &&
(!StringConvertible<T>)
static std::string Convert(const T& value) {
return value.string();
}
template <typename T>
requires StringViewConvertible<T>
static std::string_view Convert(const T& value) {
return value.ToStringView();
}
template <typename T,
typename test_for_number = typename std::
enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>, bool>,
typename dummy = bool>
static std::string Convert(const T& value) {
return std::to_string(value);
}
static std::string_view Convert(const char* value) {
return value != nullptr ? value : "(null)";
}
static std::string Convert(const std::string& value) { return value; }
static std::string_view Convert(std::string_view value) { return value; }
static std::string Convert(bool value) { return value ? "true" : "false"; }
template <unsigned BASE_BITS,
typename T,
typename = std::enable_if_t<std::is_integral_v<T>>>
static std::string BaseConvert(const T& value) {
auto v = static_cast<uint64_t>(value);
char ret[3 * sizeof(T)];
char* ptr = ret + 3 * sizeof(T) - 1;
*ptr = '\0';
const char* digits = "0123456789abcdef";
do {
unsigned digit = v & ((1 << BASE_BITS) - 1);
*--ptr = (BASE_BITS < 4 ? static_cast<char>('0' + digit) : digits[digit]);
} while ((v >>= BASE_BITS) != 0);
return ptr;
}
template <unsigned BASE_BITS,
typename T,
typename = std::enable_if_t<!std::is_integral_v<T>>>
static auto BaseConvert(T&& value) {
return Convert(std::forward<T>(value));
}
};
template <typename T>
auto ToStringOrStringView(const T& value) {
return ToStringHelper::Convert(value);
}
template <typename T>
std::string ToString(const T& value) {
return std::string(ToStringOrStringView(value));
}
template <unsigned BASE_BITS, typename T>
auto ToBaseString(const T& value) {
return ToStringHelper::BaseConvert<BASE_BITS>(value);
}
inline std::string SPrintFImpl(std::string_view format) {
auto offset = format.find('%');
if (offset == std::string_view::npos) return std::string(format);
CHECK_LT(offset + 1, format.size());
CHECK_EQ(format[offset + 1],
'%'); // Only '%%' allowed when there are no arguments.
return std::string(format.substr(0, offset + 1)) +
SPrintFImpl(format.substr(offset + 2));
}
template <typename Arg, typename... Args>
std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
std::string_view format,
Arg&& arg,
Args&&... args) {
auto offset = format.find('%');
CHECK_NE(offset, std::string_view::npos); // If you hit this, you passed in
// too many arguments.
std::string ret(format.substr(0, offset));
// Ignore long / size_t modifiers
while (++offset < format.size() &&
(format[offset] == 'l' || format[offset] == 'z')) {
}
switch (offset == format.size() ? '\0' : format[offset]) {
case '%': {
return ret + '%' +
SPrintFImpl(format.substr(offset + 1),
std::forward<Arg>(arg),
std::forward<Args>(args)...);
}
default: {
return ret + '%' +
SPrintFImpl(format.substr(offset),
std::forward<Arg>(arg),
std::forward<Args>(args)...);
}
case 'd':
case 'i':
case 'u':
case 's':
ret += ToStringOrStringView(arg);
break;
case 'o':
ret += ToBaseString<3>(arg);
break;
case 'x':
ret += ToBaseString<4>(arg);
break;
case 'X':
ret += node::ToUpper(ToBaseString<4>(arg));
break;
case 'p': {
CHECK(std::is_pointer_v<typename std::remove_reference_t<Arg>>);
char out[20];
int n = snprintf(
out, sizeof(out), "%p", *reinterpret_cast<const void* const*>(&arg));
CHECK_GE(n, 0);
ret += out;
break;
}
}
return ret +
SPrintFImpl(format.substr(offset + 1), std::forward<Args>(args)...);
}
template <typename... Args>
std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string)
std::string_view format,
Args&&... args) {
return SPrintFImpl(format, std::forward<Args>(args)...);
}
template <typename... Args>
void COLD_NOINLINE FPrintF(FILE* file,
std::string_view format,
Args&&... args) {
FWrite(file, SPrintF(format, std::forward<Args>(args)...));
}
template <typename... Args>
inline void FORCE_INLINE Debug(EnabledDebugList* list,
DebugCategory cat,
const char* format,
Args&&... args) {
if (!list->enabled(cat)) [[unlikely]]
return;
FPrintF(stderr, format, std::forward<Args>(args)...);
}
inline void FORCE_INLINE Debug(EnabledDebugList* list,
DebugCategory cat,
const char* message) {
if (!list->enabled(cat)) [[unlikely]]
return;
FPrintF(stderr, "%s", message);
}
template <typename... Args>
inline void FORCE_INLINE
Debug(Environment* env, DebugCategory cat, const char* format, Args&&... args) {
Debug(env->enabled_debug_list(), cat, format, std::forward<Args>(args)...);
}
inline void FORCE_INLINE Debug(Environment* env,
DebugCategory cat,
const char* message) {
Debug(env->enabled_debug_list(), cat, message);
}
template <typename... Args>
inline void Debug(Environment* env,
DebugCategory cat,
const std::string& format,
Args&&... args) {
Debug(env->enabled_debug_list(),
cat,
format.c_str(),
std::forward<Args>(args)...);
}
// Used internally by the 'real' Debug(AsyncWrap*, ...) functions below, so that
// the FORCE_INLINE flag on them doesn't apply to the contents of this function
// as well.
// We apply COLD_NOINLINE to tell the compiler that it's not worth optimizing
// this function for speed and it should rather focus on keeping it out of
// hot code paths. In particular, we want to keep the string concatenating code
// out of the function containing the original `Debug()` call.
template <typename... Args>
void COLD_NOINLINE UnconditionalAsyncWrapDebug(AsyncWrap* async_wrap,
const char* format,
Args&&... args) {
Debug(async_wrap->env(),
static_cast<DebugCategory>(async_wrap->provider_type()),
async_wrap->diagnostic_name() + " " + format + "\n",
std::forward<Args>(args)...);
}
template <typename... Args>
inline void FORCE_INLINE Debug(AsyncWrap* async_wrap,
const char* format,
Args&&... args) {
DCHECK_NOT_NULL(async_wrap);
if (auto cat = static_cast<DebugCategory>(async_wrap->provider_type());
!async_wrap->env()->enabled_debug_list()->enabled(cat)) [[unlikely]] {
return;
}
UnconditionalAsyncWrapDebug(async_wrap, format, std::forward<Args>(args)...);
}
template <typename... Args>
inline void FORCE_INLINE Debug(AsyncWrap* async_wrap,
const std::string& format,
Args&&... args) {
Debug(async_wrap, format.c_str(), std::forward<Args>(args)...);
}
namespace per_process {
template <typename... Args>
inline void FORCE_INLINE Debug(DebugCategory cat,
const char* format,
Args&&... args) {
Debug(&enabled_debug_list, cat, format, std::forward<Args>(args)...);
}
inline void FORCE_INLINE Debug(DebugCategory cat, const char* message) {
Debug(&enabled_debug_list, cat, message);
}
} // namespace per_process
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_DEBUG_UTILS_INL_H_