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