annotate win64-msvc/include/kj/debug.h @ 169:223a55898ab9 tip default

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