Chris@50
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
Chris@50
|
2 // Licensed under the MIT License:
|
Chris@50
|
3 //
|
Chris@50
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
Chris@50
|
5 // of this software and associated documentation files (the "Software"), to deal
|
Chris@50
|
6 // in the Software without restriction, including without limitation the rights
|
Chris@50
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
Chris@50
|
8 // copies of the Software, and to permit persons to whom the Software is
|
Chris@50
|
9 // furnished to do so, subject to the following conditions:
|
Chris@50
|
10 //
|
Chris@50
|
11 // The above copyright notice and this permission notice shall be included in
|
Chris@50
|
12 // all copies or substantial portions of the Software.
|
Chris@50
|
13 //
|
Chris@50
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
Chris@50
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
Chris@50
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
Chris@50
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
Chris@50
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
Chris@50
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
Chris@50
|
20 // THE SOFTWARE.
|
Chris@50
|
21
|
Chris@50
|
22 // This file declares convenient macros for debug logging and error handling. The macros make
|
Chris@50
|
23 // it excessively easy to extract useful context information from code. Example:
|
Chris@50
|
24 //
|
Chris@50
|
25 // KJ_ASSERT(a == b, a, b, "a and b must be the same.");
|
Chris@50
|
26 //
|
Chris@50
|
27 // On failure, this will throw an exception whose description looks like:
|
Chris@50
|
28 //
|
Chris@50
|
29 // myfile.c++:43: bug in code: expected a == b; a = 14; b = 72; a and b must be the same.
|
Chris@50
|
30 //
|
Chris@50
|
31 // As you can see, all arguments after the first provide additional context.
|
Chris@50
|
32 //
|
Chris@50
|
33 // The macros available are:
|
Chris@50
|
34 //
|
Chris@50
|
35 // * `KJ_LOG(severity, ...)`: Just writes a log message, to stderr by default (but you can
|
Chris@50
|
36 // intercept messages by implementing an ExceptionCallback). `severity` is `INFO`, `WARNING`,
|
Chris@50
|
37 // `ERROR`, or `FATAL`. By default, `INFO` logs are not written, but for command-line apps the
|
Chris@50
|
38 // user should be able to pass a flag like `--verbose` to enable them. Other log levels are
|
Chris@50
|
39 // enabled by default. Log messages -- like exceptions -- can be intercepted by registering an
|
Chris@50
|
40 // ExceptionCallback.
|
Chris@50
|
41 //
|
Chris@50
|
42 // * `KJ_DBG(...)`: Like `KJ_LOG`, but intended specifically for temporary log lines added while
|
Chris@50
|
43 // debugging a particular problem. Calls to `KJ_DBG` should always be deleted before committing
|
Chris@50
|
44 // code. It is suggested that you set up a pre-commit hook that checks for this.
|
Chris@50
|
45 //
|
Chris@50
|
46 // * `KJ_ASSERT(condition, ...)`: Throws an exception if `condition` is false, or aborts if
|
Chris@50
|
47 // exceptions are disabled. This macro should be used to check for bugs in the surrounding code
|
Chris@50
|
48 // and its dependencies, but NOT to check for invalid input. The macro may be followed by a
|
Chris@50
|
49 // brace-delimited code block; if so, the block will be executed in the case where the assertion
|
Chris@50
|
50 // fails, before throwing the exception. If control jumps out of the block (e.g. with "break",
|
Chris@50
|
51 // "return", or "goto"), then the error is considered "recoverable" -- in this case, if
|
Chris@50
|
52 // exceptions are disabled, execution will continue normally rather than aborting (but if
|
Chris@50
|
53 // exceptions are enabled, an exception will still be thrown on exiting the block). A "break"
|
Chris@50
|
54 // statement in particular will jump to the code immediately after the block (it does not break
|
Chris@50
|
55 // any surrounding loop or switch). Example:
|
Chris@50
|
56 //
|
Chris@50
|
57 // KJ_ASSERT(value >= 0, "Value cannot be negative.", value) {
|
Chris@50
|
58 // // Assertion failed. Set value to zero to "recover".
|
Chris@50
|
59 // value = 0;
|
Chris@50
|
60 // // Don't abort if exceptions are disabled. Continue normally.
|
Chris@50
|
61 // // (Still throw an exception if they are enabled, though.)
|
Chris@50
|
62 // break;
|
Chris@50
|
63 // }
|
Chris@50
|
64 // // When exceptions are disabled, we'll get here even if the assertion fails.
|
Chris@50
|
65 // // Otherwise, we get here only if the assertion passes.
|
Chris@50
|
66 //
|
Chris@50
|
67 // * `KJ_REQUIRE(condition, ...)`: Like `KJ_ASSERT` but used to check preconditions -- e.g. to
|
Chris@50
|
68 // validate parameters passed from a caller. A failure indicates that the caller is buggy.
|
Chris@50
|
69 //
|
Chris@50
|
70 // * `KJ_SYSCALL(code, ...)`: Executes `code` assuming it makes a system call. A negative result
|
Chris@50
|
71 // is considered an error, with error code reported via `errno`. EINTR is handled by retrying.
|
Chris@50
|
72 // Other errors are handled by throwing an exception. If you need to examine the return code,
|
Chris@50
|
73 // assign it to a variable like so:
|
Chris@50
|
74 //
|
Chris@50
|
75 // int fd;
|
Chris@50
|
76 // KJ_SYSCALL(fd = open(filename, O_RDONLY), filename);
|
Chris@50
|
77 //
|
Chris@50
|
78 // `KJ_SYSCALL` can be followed by a recovery block, just like `KJ_ASSERT`.
|
Chris@50
|
79 //
|
Chris@50
|
80 // * `KJ_NONBLOCKING_SYSCALL(code, ...)`: Like KJ_SYSCALL, but will not throw an exception on
|
Chris@50
|
81 // EAGAIN/EWOULDBLOCK. The calling code should check the syscall's return value to see if it
|
Chris@50
|
82 // indicates an error; in this case, it can assume the error was EAGAIN because any other error
|
Chris@50
|
83 // would have caused an exception to be thrown.
|
Chris@50
|
84 //
|
Chris@50
|
85 // * `KJ_CONTEXT(...)`: Notes additional contextual information relevant to any exceptions thrown
|
Chris@50
|
86 // from within the current scope. That is, until control exits the block in which KJ_CONTEXT()
|
Chris@50
|
87 // is used, if any exception is generated, it will contain the given information in its context
|
Chris@50
|
88 // chain. This is helpful because it can otherwise be very difficult to come up with error
|
Chris@50
|
89 // messages that make sense within low-level helper code. Note that the parameters to
|
Chris@50
|
90 // KJ_CONTEXT() are only evaluated if an exception is thrown. This implies that any variables
|
Chris@50
|
91 // used must remain valid until the end of the scope.
|
Chris@50
|
92 //
|
Chris@50
|
93 // Notes:
|
Chris@50
|
94 // * Do not write expressions with side-effects in the message content part of the macro, as the
|
Chris@50
|
95 // message will not necessarily be evaluated.
|
Chris@50
|
96 // * For every macro `FOO` above except `LOG`, there is also a `FAIL_FOO` macro used to report
|
Chris@50
|
97 // failures that already happened. For the macros that check a boolean condition, `FAIL_FOO`
|
Chris@50
|
98 // omits the first parameter and behaves like it was `false`. `FAIL_SYSCALL` and
|
Chris@50
|
99 // `FAIL_RECOVERABLE_SYSCALL` take a string and an OS error number as the first two parameters.
|
Chris@50
|
100 // The string should be the name of the failed system call.
|
Chris@50
|
101 // * For every macro `FOO` above, there is a `DFOO` version (or `RECOVERABLE_DFOO`) which is only
|
Chris@50
|
102 // executed in debug mode, i.e. when KJ_DEBUG is defined. KJ_DEBUG is defined automatically
|
Chris@50
|
103 // by common.h when compiling without optimization (unless NDEBUG is defined), but you can also
|
Chris@50
|
104 // define it explicitly (e.g. -DKJ_DEBUG). Generally, production builds should NOT use KJ_DEBUG
|
Chris@50
|
105 // as it may enable expensive checks that are unlikely to fail.
|
Chris@50
|
106
|
Chris@50
|
107 #ifndef KJ_DEBUG_H_
|
Chris@50
|
108 #define KJ_DEBUG_H_
|
Chris@50
|
109
|
Chris@50
|
110 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
|
Chris@50
|
111 #pragma GCC system_header
|
Chris@50
|
112 #endif
|
Chris@50
|
113
|
Chris@50
|
114 #include "string.h"
|
Chris@50
|
115 #include "exception.h"
|
Chris@50
|
116
|
Chris@50
|
117 #ifdef ERROR
|
Chris@50
|
118 // This is problematic because windows.h #defines ERROR, which we use in an enum here.
|
Chris@50
|
119 #error "Make sure to to undefine ERROR (or just #include <kj/windows-sanity.h>) before this file"
|
Chris@50
|
120 #endif
|
Chris@50
|
121
|
Chris@50
|
122 namespace kj {
|
Chris@50
|
123
|
Chris@50
|
124 #if _MSC_VER
|
Chris@50
|
125 // MSVC does __VA_ARGS__ differently from GCC:
|
Chris@50
|
126 // - A trailing comma before an empty __VA_ARGS__ is removed automatically, whereas GCC wants
|
Chris@50
|
127 // you to request this behavior with "##__VA_ARGS__".
|
Chris@50
|
128 // - If __VA_ARGS__ is passed directly as an argument to another macro, it will be treated as a
|
Chris@50
|
129 // *single* argument rather than an argument list. This can be worked around by wrapping the
|
Chris@50
|
130 // outer macro call in KJ_EXPAND(), which appraently forces __VA_ARGS__ to be expanded before
|
Chris@50
|
131 // the macro is evaluated. I don't understand the C preprocessor.
|
Chris@50
|
132 // - Using "#__VA_ARGS__" to stringify __VA_ARGS__ expands to zero tokens when __VA_ARGS__ is
|
Chris@50
|
133 // empty, rather than expanding to an empty string literal. We can work around by concatenating
|
Chris@50
|
134 // with an empty string literal.
|
Chris@50
|
135
|
Chris@50
|
136 #define KJ_EXPAND(X) X
|
Chris@50
|
137
|
Chris@50
|
138 #define KJ_LOG(severity, ...) \
|
Chris@50
|
139 if (!::kj::_::Debug::shouldLog(::kj::LogSeverity::severity)) {} else \
|
Chris@50
|
140 ::kj::_::Debug::log(__FILE__, __LINE__, ::kj::LogSeverity::severity, \
|
Chris@50
|
141 "" #__VA_ARGS__, __VA_ARGS__)
|
Chris@50
|
142
|
Chris@50
|
143 #define KJ_DBG(...) KJ_EXPAND(KJ_LOG(DBG, __VA_ARGS__))
|
Chris@50
|
144
|
Chris@50
|
145 #define KJ_REQUIRE(cond, ...) \
|
Chris@50
|
146 if (KJ_LIKELY(cond)) {} else \
|
Chris@50
|
147 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \
|
Chris@50
|
148 #cond, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal())
|
Chris@50
|
149
|
Chris@50
|
150 #define KJ_FAIL_REQUIRE(...) \
|
Chris@50
|
151 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \
|
Chris@50
|
152 nullptr, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal())
|
Chris@50
|
153
|
Chris@50
|
154 #define KJ_SYSCALL(call, ...) \
|
Chris@50
|
155 if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, false)) {} else \
|
Chris@50
|
156 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \
|
Chris@50
|
157 _kjSyscallResult.getErrorNumber(), #call, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal())
|
Chris@50
|
158
|
Chris@50
|
159 #define KJ_NONBLOCKING_SYSCALL(call, ...) \
|
Chris@50
|
160 if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, true)) {} else \
|
Chris@50
|
161 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \
|
Chris@50
|
162 _kjSyscallResult.getErrorNumber(), #call, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal())
|
Chris@50
|
163
|
Chris@50
|
164 #define KJ_FAIL_SYSCALL(code, errorNumber, ...) \
|
Chris@50
|
165 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \
|
Chris@50
|
166 errorNumber, code, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal())
|
Chris@50
|
167
|
Chris@50
|
168 #define KJ_UNIMPLEMENTED(...) \
|
Chris@50
|
169 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::UNIMPLEMENTED, \
|
Chris@50
|
170 nullptr, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal())
|
Chris@50
|
171
|
Chris@50
|
172 #define KJ_CONTEXT(...) \
|
Chris@50
|
173 auto KJ_UNIQUE_NAME(_kjContextFunc) = [&]() -> ::kj::_::Debug::Context::Value { \
|
Chris@50
|
174 return ::kj::_::Debug::Context::Value(__FILE__, __LINE__, \
|
Chris@50
|
175 ::kj::_::Debug::makeDescription("" #__VA_ARGS__, __VA_ARGS__)); \
|
Chris@50
|
176 }; \
|
Chris@50
|
177 ::kj::_::Debug::ContextImpl<decltype(KJ_UNIQUE_NAME(_kjContextFunc))> \
|
Chris@50
|
178 KJ_UNIQUE_NAME(_kjContext)(KJ_UNIQUE_NAME(_kjContextFunc))
|
Chris@50
|
179
|
Chris@50
|
180 #define KJ_REQUIRE_NONNULL(value, ...) \
|
Chris@50
|
181 (*[&] { \
|
Chris@50
|
182 auto _kj_result = ::kj::_::readMaybe(value); \
|
Chris@50
|
183 if (KJ_UNLIKELY(!_kj_result)) { \
|
Chris@50
|
184 ::kj::_::Debug::Fault(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \
|
Chris@50
|
185 #value " != nullptr", "" #__VA_ARGS__, __VA_ARGS__).fatal(); \
|
Chris@50
|
186 } \
|
Chris@50
|
187 return _kj_result; \
|
Chris@50
|
188 }())
|
Chris@50
|
189
|
Chris@50
|
190 #define KJ_EXCEPTION(type, ...) \
|
Chris@50
|
191 ::kj::Exception(::kj::Exception::Type::type, __FILE__, __LINE__, \
|
Chris@50
|
192 ::kj::_::Debug::makeDescription("" #__VA_ARGS__, __VA_ARGS__))
|
Chris@50
|
193
|
Chris@50
|
194 #else
|
Chris@50
|
195
|
Chris@50
|
196 #define KJ_LOG(severity, ...) \
|
Chris@50
|
197 if (!::kj::_::Debug::shouldLog(::kj::LogSeverity::severity)) {} else \
|
Chris@50
|
198 ::kj::_::Debug::log(__FILE__, __LINE__, ::kj::LogSeverity::severity, \
|
Chris@50
|
199 #__VA_ARGS__, ##__VA_ARGS__)
|
Chris@50
|
200
|
Chris@50
|
201 #define KJ_DBG(...) KJ_LOG(DBG, ##__VA_ARGS__)
|
Chris@50
|
202
|
Chris@50
|
203 #define KJ_REQUIRE(cond, ...) \
|
Chris@50
|
204 if (KJ_LIKELY(cond)) {} else \
|
Chris@50
|
205 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \
|
Chris@50
|
206 #cond, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
|
Chris@50
|
207
|
Chris@50
|
208 #define KJ_FAIL_REQUIRE(...) \
|
Chris@50
|
209 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \
|
Chris@50
|
210 nullptr, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
|
Chris@50
|
211
|
Chris@50
|
212 #define KJ_SYSCALL(call, ...) \
|
Chris@50
|
213 if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, false)) {} else \
|
Chris@50
|
214 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \
|
Chris@50
|
215 _kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
|
Chris@50
|
216
|
Chris@50
|
217 #define KJ_NONBLOCKING_SYSCALL(call, ...) \
|
Chris@50
|
218 if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, true)) {} else \
|
Chris@50
|
219 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \
|
Chris@50
|
220 _kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
|
Chris@50
|
221
|
Chris@50
|
222 #define KJ_FAIL_SYSCALL(code, errorNumber, ...) \
|
Chris@50
|
223 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \
|
Chris@50
|
224 errorNumber, code, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
|
Chris@50
|
225
|
Chris@50
|
226 #define KJ_UNIMPLEMENTED(...) \
|
Chris@50
|
227 for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::UNIMPLEMENTED, \
|
Chris@50
|
228 nullptr, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
|
Chris@50
|
229
|
Chris@50
|
230 #define KJ_CONTEXT(...) \
|
Chris@50
|
231 auto KJ_UNIQUE_NAME(_kjContextFunc) = [&]() -> ::kj::_::Debug::Context::Value { \
|
Chris@50
|
232 return ::kj::_::Debug::Context::Value(__FILE__, __LINE__, \
|
Chris@50
|
233 ::kj::_::Debug::makeDescription(#__VA_ARGS__, ##__VA_ARGS__)); \
|
Chris@50
|
234 }; \
|
Chris@50
|
235 ::kj::_::Debug::ContextImpl<decltype(KJ_UNIQUE_NAME(_kjContextFunc))> \
|
Chris@50
|
236 KJ_UNIQUE_NAME(_kjContext)(KJ_UNIQUE_NAME(_kjContextFunc))
|
Chris@50
|
237
|
Chris@50
|
238 #define KJ_REQUIRE_NONNULL(value, ...) \
|
Chris@50
|
239 (*({ \
|
Chris@50
|
240 auto _kj_result = ::kj::_::readMaybe(value); \
|
Chris@50
|
241 if (KJ_UNLIKELY(!_kj_result)) { \
|
Chris@50
|
242 ::kj::_::Debug::Fault(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \
|
Chris@50
|
243 #value " != nullptr", #__VA_ARGS__, ##__VA_ARGS__).fatal(); \
|
Chris@50
|
244 } \
|
Chris@50
|
245 kj::mv(_kj_result); \
|
Chris@50
|
246 }))
|
Chris@50
|
247
|
Chris@50
|
248 #define KJ_EXCEPTION(type, ...) \
|
Chris@50
|
249 ::kj::Exception(::kj::Exception::Type::type, __FILE__, __LINE__, \
|
Chris@50
|
250 ::kj::_::Debug::makeDescription(#__VA_ARGS__, ##__VA_ARGS__))
|
Chris@50
|
251
|
Chris@50
|
252 #endif
|
Chris@50
|
253
|
Chris@50
|
254 #define KJ_ASSERT KJ_REQUIRE
|
Chris@50
|
255 #define KJ_FAIL_ASSERT KJ_FAIL_REQUIRE
|
Chris@50
|
256 #define KJ_ASSERT_NONNULL KJ_REQUIRE_NONNULL
|
Chris@50
|
257 // Use "ASSERT" in place of "REQUIRE" when the problem is local to the immediate surrounding code.
|
Chris@50
|
258 // That is, if the assert ever fails, it indicates that the immediate surrounding code is broken.
|
Chris@50
|
259
|
Chris@50
|
260 #ifdef KJ_DEBUG
|
Chris@50
|
261 #define KJ_DLOG KJ_LOG
|
Chris@50
|
262 #define KJ_DASSERT KJ_ASSERT
|
Chris@50
|
263 #define KJ_DREQUIRE KJ_REQUIRE
|
Chris@50
|
264 #else
|
Chris@50
|
265 #define KJ_DLOG(...) do {} while (false)
|
Chris@50
|
266 #define KJ_DASSERT(...) do {} while (false)
|
Chris@50
|
267 #define KJ_DREQUIRE(...) do {} while (false)
|
Chris@50
|
268 #endif
|
Chris@50
|
269
|
Chris@50
|
270 namespace _ { // private
|
Chris@50
|
271
|
Chris@50
|
272 class Debug {
|
Chris@50
|
273 public:
|
Chris@50
|
274 Debug() = delete;
|
Chris@50
|
275
|
Chris@50
|
276 typedef LogSeverity Severity; // backwards-compatibility
|
Chris@50
|
277
|
Chris@50
|
278 static inline bool shouldLog(LogSeverity severity) { return severity >= minSeverity; }
|
Chris@50
|
279 // Returns whether messages of the given severity should be logged.
|
Chris@50
|
280
|
Chris@50
|
281 static inline void setLogLevel(LogSeverity severity) { minSeverity = severity; }
|
Chris@50
|
282 // Set the minimum message severity which will be logged.
|
Chris@50
|
283 //
|
Chris@50
|
284 // TODO(someday): Expose publicly.
|
Chris@50
|
285
|
Chris@50
|
286 template <typename... Params>
|
Chris@50
|
287 static void log(const char* file, int line, LogSeverity severity, const char* macroArgs,
|
Chris@50
|
288 Params&&... params);
|
Chris@50
|
289
|
Chris@50
|
290 class Fault {
|
Chris@50
|
291 public:
|
Chris@50
|
292 template <typename... Params>
|
Chris@50
|
293 Fault(const char* file, int line, Exception::Type type,
|
Chris@50
|
294 const char* condition, const char* macroArgs, Params&&... params);
|
Chris@50
|
295 template <typename... Params>
|
Chris@50
|
296 Fault(const char* file, int line, int osErrorNumber,
|
Chris@50
|
297 const char* condition, const char* macroArgs, Params&&... params);
|
Chris@50
|
298 Fault(const char* file, int line, Exception::Type type,
|
Chris@50
|
299 const char* condition, const char* macroArgs);
|
Chris@50
|
300 Fault(const char* file, int line, int osErrorNumber,
|
Chris@50
|
301 const char* condition, const char* macroArgs);
|
Chris@50
|
302 ~Fault() noexcept(false);
|
Chris@50
|
303
|
Chris@50
|
304 KJ_NOINLINE KJ_NORETURN(void fatal());
|
Chris@50
|
305 // Throw the exception.
|
Chris@50
|
306
|
Chris@50
|
307 private:
|
Chris@50
|
308 void init(const char* file, int line, Exception::Type type,
|
Chris@50
|
309 const char* condition, const char* macroArgs, ArrayPtr<String> argValues);
|
Chris@50
|
310 void init(const char* file, int line, int osErrorNumber,
|
Chris@50
|
311 const char* condition, const char* macroArgs, ArrayPtr<String> argValues);
|
Chris@50
|
312
|
Chris@50
|
313 Exception* exception;
|
Chris@50
|
314 };
|
Chris@50
|
315
|
Chris@50
|
316 class SyscallResult {
|
Chris@50
|
317 public:
|
Chris@50
|
318 inline SyscallResult(int errorNumber): errorNumber(errorNumber) {}
|
Chris@50
|
319 inline operator void*() { return errorNumber == 0 ? this : nullptr; }
|
Chris@50
|
320 inline int getErrorNumber() { return errorNumber; }
|
Chris@50
|
321
|
Chris@50
|
322 private:
|
Chris@50
|
323 int errorNumber;
|
Chris@50
|
324 };
|
Chris@50
|
325
|
Chris@50
|
326 template <typename Call>
|
Chris@50
|
327 static SyscallResult syscall(Call&& call, bool nonblocking);
|
Chris@50
|
328
|
Chris@50
|
329 class Context: public ExceptionCallback {
|
Chris@50
|
330 public:
|
Chris@50
|
331 Context();
|
Chris@50
|
332 KJ_DISALLOW_COPY(Context);
|
Chris@50
|
333 virtual ~Context() noexcept(false);
|
Chris@50
|
334
|
Chris@50
|
335 struct Value {
|
Chris@50
|
336 const char* file;
|
Chris@50
|
337 int line;
|
Chris@50
|
338 String description;
|
Chris@50
|
339
|
Chris@50
|
340 inline Value(const char* file, int line, String&& description)
|
Chris@50
|
341 : file(file), line(line), description(mv(description)) {}
|
Chris@50
|
342 };
|
Chris@50
|
343
|
Chris@50
|
344 virtual Value evaluate() = 0;
|
Chris@50
|
345
|
Chris@50
|
346 virtual void onRecoverableException(Exception&& exception) override;
|
Chris@50
|
347 virtual void onFatalException(Exception&& exception) override;
|
Chris@50
|
348 virtual void logMessage(LogSeverity severity, const char* file, int line, int contextDepth,
|
Chris@50
|
349 String&& text) override;
|
Chris@50
|
350
|
Chris@50
|
351 private:
|
Chris@50
|
352 bool logged;
|
Chris@50
|
353 Maybe<Value> value;
|
Chris@50
|
354
|
Chris@50
|
355 Value ensureInitialized();
|
Chris@50
|
356 };
|
Chris@50
|
357
|
Chris@50
|
358 template <typename Func>
|
Chris@50
|
359 class ContextImpl: public Context {
|
Chris@50
|
360 public:
|
Chris@50
|
361 inline ContextImpl(Func& func): func(func) {}
|
Chris@50
|
362 KJ_DISALLOW_COPY(ContextImpl);
|
Chris@50
|
363
|
Chris@50
|
364 Value evaluate() override {
|
Chris@50
|
365 return func();
|
Chris@50
|
366 }
|
Chris@50
|
367 private:
|
Chris@50
|
368 Func& func;
|
Chris@50
|
369 };
|
Chris@50
|
370
|
Chris@50
|
371 template <typename... Params>
|
Chris@50
|
372 static String makeDescription(const char* macroArgs, Params&&... params);
|
Chris@50
|
373
|
Chris@50
|
374 private:
|
Chris@50
|
375 static LogSeverity minSeverity;
|
Chris@50
|
376
|
Chris@50
|
377 static void logInternal(const char* file, int line, LogSeverity severity, const char* macroArgs,
|
Chris@50
|
378 ArrayPtr<String> argValues);
|
Chris@50
|
379 static String makeDescriptionInternal(const char* macroArgs, ArrayPtr<String> argValues);
|
Chris@50
|
380
|
Chris@50
|
381 static int getOsErrorNumber(bool nonblocking);
|
Chris@50
|
382 // Get the error code of the last error (e.g. from errno). Returns -1 on EINTR.
|
Chris@50
|
383 };
|
Chris@50
|
384
|
Chris@50
|
385 template <typename... Params>
|
Chris@50
|
386 void Debug::log(const char* file, int line, LogSeverity severity, const char* macroArgs,
|
Chris@50
|
387 Params&&... params) {
|
Chris@50
|
388 String argValues[sizeof...(Params)] = {str(params)...};
|
Chris@50
|
389 logInternal(file, line, severity, macroArgs, arrayPtr(argValues, sizeof...(Params)));
|
Chris@50
|
390 }
|
Chris@50
|
391
|
Chris@50
|
392 template <>
|
Chris@50
|
393 inline void Debug::log<>(const char* file, int line, LogSeverity severity, const char* macroArgs) {
|
Chris@50
|
394 logInternal(file, line, severity, macroArgs, nullptr);
|
Chris@50
|
395 }
|
Chris@50
|
396
|
Chris@50
|
397 template <typename... Params>
|
Chris@50
|
398 Debug::Fault::Fault(const char* file, int line, Exception::Type type,
|
Chris@50
|
399 const char* condition, const char* macroArgs, Params&&... params)
|
Chris@50
|
400 : exception(nullptr) {
|
Chris@50
|
401 String argValues[sizeof...(Params)] = {str(params)...};
|
Chris@50
|
402 init(file, line, type, condition, macroArgs,
|
Chris@50
|
403 arrayPtr(argValues, sizeof...(Params)));
|
Chris@50
|
404 }
|
Chris@50
|
405
|
Chris@50
|
406 template <typename... Params>
|
Chris@50
|
407 Debug::Fault::Fault(const char* file, int line, int osErrorNumber,
|
Chris@50
|
408 const char* condition, const char* macroArgs, Params&&... params)
|
Chris@50
|
409 : exception(nullptr) {
|
Chris@50
|
410 String argValues[sizeof...(Params)] = {str(params)...};
|
Chris@50
|
411 init(file, line, osErrorNumber, condition, macroArgs,
|
Chris@50
|
412 arrayPtr(argValues, sizeof...(Params)));
|
Chris@50
|
413 }
|
Chris@50
|
414
|
Chris@50
|
415 inline Debug::Fault::Fault(const char* file, int line, int osErrorNumber,
|
Chris@50
|
416 const char* condition, const char* macroArgs)
|
Chris@50
|
417 : exception(nullptr) {
|
Chris@50
|
418 init(file, line, osErrorNumber, condition, macroArgs, nullptr);
|
Chris@50
|
419 }
|
Chris@50
|
420
|
Chris@50
|
421 inline Debug::Fault::Fault(const char* file, int line, kj::Exception::Type type,
|
Chris@50
|
422 const char* condition, const char* macroArgs)
|
Chris@50
|
423 : exception(nullptr) {
|
Chris@50
|
424 init(file, line, type, condition, macroArgs, nullptr);
|
Chris@50
|
425 }
|
Chris@50
|
426
|
Chris@50
|
427 template <typename Call>
|
Chris@50
|
428 Debug::SyscallResult Debug::syscall(Call&& call, bool nonblocking) {
|
Chris@50
|
429 while (call() < 0) {
|
Chris@50
|
430 int errorNum = getOsErrorNumber(nonblocking);
|
Chris@50
|
431 // getOsErrorNumber() returns -1 to indicate EINTR.
|
Chris@50
|
432 // Also, if nonblocking is true, then it returns 0 on EAGAIN, which will then be treated as a
|
Chris@50
|
433 // non-error.
|
Chris@50
|
434 if (errorNum != -1) {
|
Chris@50
|
435 return SyscallResult(errorNum);
|
Chris@50
|
436 }
|
Chris@50
|
437 }
|
Chris@50
|
438 return SyscallResult(0);
|
Chris@50
|
439 }
|
Chris@50
|
440
|
Chris@50
|
441 template <typename... Params>
|
Chris@50
|
442 String Debug::makeDescription(const char* macroArgs, Params&&... params) {
|
Chris@50
|
443 String argValues[sizeof...(Params)] = {str(params)...};
|
Chris@50
|
444 return makeDescriptionInternal(macroArgs, arrayPtr(argValues, sizeof...(Params)));
|
Chris@50
|
445 }
|
Chris@50
|
446
|
Chris@50
|
447 template <>
|
Chris@50
|
448 inline String Debug::makeDescription<>(const char* macroArgs) {
|
Chris@50
|
449 return makeDescriptionInternal(macroArgs, nullptr);
|
Chris@50
|
450 }
|
Chris@50
|
451
|
Chris@50
|
452 } // namespace _ (private)
|
Chris@50
|
453 } // namespace kj
|
Chris@50
|
454
|
Chris@50
|
455 #endif // KJ_DEBUG_H_
|