annotate win64-msvc/include/kj/debug.h @ 135:38d1c0e7850b

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