annotate win32-mingw/include/kj/debug.h @ 140:59a8758c56b1

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