annotate win64-msvc/include/kj/debug.h @ 59:cd4953afea46

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