40 #ifndef _ASSERTIONS_H_ 41 #define _ASSERTIONS_H_ 43 #ifndef TURI_LOGGER_THROW_ON_FAILURE 44 #define TURI_LOGGER_THROW_ON_FAILURE 61 #include <core/logging/fail_method.hpp> 62 #include <core/logging/backtrace.hpp> 64 #include <boost/typeof/typeof.hpp> 66 #include <core/util/code_optimization.hpp> 67 #include <core/util/sys_util.hpp> 69 extern void __print_back_trace();
77 #pragma clang diagnostic push 78 #pragma clang diagnostic ignored "-Wconversion-null" 81 #pragma GCC diagnostic push 82 #pragma GCC diagnostic ignored "-Wconversion-null" 89 #define WRITE_TO_STDERR(buf, len) (logbuf(LOG_FATAL, buf, len)) 199 #define __CHECK(condition) \ 201 if (UNLIKELY(!(condition))) { \ 202 auto throw_error = [&]() GL_GCC_ONLY(GL_COLD_NOINLINE_ERROR) { \ 203 std::ostringstream ss; \ 204 ss << "Check failed (" << __FILE__ << ":" << __LINE__ \ 205 << "): " << #condition << std::endl; \ 206 logstream(LOG_ERROR) << ss.str(); \ 207 __print_back_trace(); \ 208 LOGGED_TURI_LOGGER_FAIL_METHOD(ss.str()); \ 211 TURI_BUILTIN_UNREACHABLE(); \ 217 #define __PCHECK(condition) \ 219 if (UNLIKELY(!(condition))) { \ 220 auto throw_error = [&]() GL_GCC_ONLY(GL_COLD_NOINLINE_ERROR) { \ 221 const int _PCHECK_err_no_ = errno; \ 222 std::ostringstream ss; \ 223 ss << "Assertion failed (" << __FILE__ << ":" << __LINE__ \ 224 << "): " << #condition << ": " << strerror(err_no) << std::endl; \ 225 logstream(LOG_ERROR) << ss.str(); \ 226 __print_back_trace(); \ 227 LOGGED_TURI_LOGGER_FAIL_METHOD(ss.str()); \ 230 TURI_BUILTIN_UNREACHABLE(); \ 240 #define __CHECK_OP(op, val1, val2) \ 242 const auto _CHECK_OP_v1_ = val1; \ 243 const auto _CHECK_OP_v2_ = val2; \ 244 if (__builtin_expect(!((_CHECK_OP_v1_)op(decltype(val1))(_CHECK_OP_v2_)), \ 246 auto throw_error = [&]() GL_GCC_ONLY(GL_COLD_NOINLINE_ERROR) { \ 247 std::ostringstream ss; \ 248 ss << "Assertion failed: (" << __FILE__ << ":" << __LINE__ \ 249 << "): " << #val1 << #op << #val2 << " [" << _CHECK_OP_v1_ << ' ' \ 250 << #op << ' ' << _CHECK_OP_v2_ << "]" << std::endl; \ 251 logstream(LOG_ERROR) << ss.str(); \ 252 __print_back_trace(); \ 253 LOGGED_TURI_LOGGER_FAIL_METHOD(ss.str()); \ 256 TURI_BUILTIN_UNREACHABLE(); \ 260 #define __CHECK_DELTA(val1, val2, delta) \ 262 const double _CHECK_OP_v1_ = val1; \ 263 const double _CHECK_OP_v2_ = val2; \ 264 const double _CHECK_OP_delta_ = delta; \ 265 if (__builtin_expect( \ 266 !(std::abs((_CHECK_OP_v1_) - (_CHECK_OP_v2_)) <= _CHECK_OP_delta_),\ 268 auto throw_error = [&]() GL_GCC_ONLY(GL_COLD_NOINLINE_ERROR) { \ 269 std::ostringstream ss; \ 270 ss << "Assertion failed: (" << __FILE__ << ":" << __LINE__ << "): " \ 271 << "abs(" << #val1 << " - " << #val2 << ") <= " << #delta << ". [" \ 272 << "abs(" << _CHECK_OP_v2_ << " - " << _CHECK_OP_v1_ << ") > " \ 273 << _CHECK_OP_delta_ << "]" << std::endl; \ 274 logstream(LOG_ERROR) << ss.str(); \ 275 __print_back_trace(); \ 276 LOGGED_TURI_LOGGER_FAIL_METHOD(ss.str()); \ 279 TURI_BUILTIN_UNREACHABLE(); \ 283 #define __CHECK_EQ(val1, val2) __CHECK_OP(==, val1, val2) 284 #define __CHECK_NE(val1, val2) __CHECK_OP(!=, val1, val2) 285 #define __CHECK_LE(val1, val2) __CHECK_OP(<=, val1, val2) 286 #define __CHECK_LT(val1, val2) __CHECK_OP(< , val1, val2) 287 #define __CHECK_GE(val1, val2) __CHECK_OP(>=, val1, val2) 288 #define __CHECK_GT(val1, val2) __CHECK_OP(> , val1, val2) 291 #define EXPECT_EQ(val1, val2) __CHECK_EQ(val1, val2) 292 #define EXPECT_DELTA(val1, val2, delta) __CHECK_DELTA(val1, val2, delta) 293 #define EXPECT_NE(val1, val2) __CHECK_NE(val1, val2) 294 #define EXPECT_LE(val1, val2) __CHECK_LE(val1, val2) 295 #define EXPECT_LT(val1, val2) __CHECK_LT(val1, val2) 296 #define EXPECT_GE(val1, val2) __CHECK_GE(val1, val2) 297 #define EXPECT_GT(val1, val2) __CHECK_GT(val1, val2) 298 #define ASSERT_EQ(val1, val2) EXPECT_EQ(val1, val2) 299 #define ASSERT_DELTA(val1, val2, delta) __CHECK_DELTA(val1, val2, delta) 300 #define ASSERT_NE(val1, val2) EXPECT_NE(val1, val2) 301 #define ASSERT_LE(val1, val2) EXPECT_LE(val1, val2) 302 #define ASSERT_LT(val1, val2) EXPECT_LT(val1, val2) 303 #define ASSERT_GE(val1, val2) EXPECT_GE(val1, val2) 304 #define ASSERT_GT(val1, val2) EXPECT_GT(val1, val2) 306 #define EXPECT_TRUE(cond) __CHECK(cond) 307 #define EXPECT_FALSE(cond) __CHECK(!(cond)) 308 #define EXPECT_STREQ(a, b) __CHECK(strcmp(a, b) == 0) 309 #define ASSERT_TRUE(cond) EXPECT_TRUE(cond) 310 #define ASSERT_FALSE(cond) EXPECT_FALSE(cond) 311 #define ASSERT_STREQ(a, b) EXPECT_STREQ(a, b) 313 #define ASSERT_UNREACHABLE() { EXPECT_TRUE(false); assert(false); TURI_BUILTIN_UNREACHABLE(); } 315 #define ASSERT_MSG(condition, fmt, ...) \ 317 if (__builtin_expect(!(condition), 0)) { \ 318 auto throw_error = [&]() GL_GCC_ONLY(GL_COLD_NOINLINE_ERROR) { \ 319 logstream(LOG_ERROR) << "Check failed: " << #condition << ":\n"; \ 320 std::ostringstream ss; \ 321 ss << "Assertion Failure: " << #condition << ": " << fmt; \ 322 logger(LOG_ERROR, fmt, ##__VA_ARGS__, "\n"); \ 323 __print_back_trace(); \ 324 TURI_LOGGER_FAIL_METHOD(ss.str().c_str()); \ 325 ASSERT_UNREACHABLE(); \ 328 TURI_BUILTIN_UNREACHABLE(); \ 333 #define __CHECK_ERR(invocation) __PCHECK((invocation) != -1) 337 #define __DCHECK_EQ(val1, val2) 338 #define __DCHECK_DELTA(val1, val2, delta) 339 #define __DCHECK_NE(val1, val2) 340 #define __DCHECK_LE(val1, val2) 341 #define __DCHECK_LT(val1, val2) 342 #define __DCHECK_GE(val1, val2) 343 #define __DCHECK_GT(val1, val2) 344 #define DASSERT_TRUE(cond) 345 #define DASSERT_FALSE(cond) 346 #define DASSERT_EQ(val1, val2) 347 #define DASSERT_DELTA(val1, val2, delta) 348 #define DASSERT_NE(val1, val2) 349 #define DASSERT_LE(val1, val2) 350 #define DASSERT_LT(val1, val2) 351 #define DASSERT_GE(val1, val2) 352 #define DASSERT_GT(val1, val2) 354 #define DASSERT_MSG(condition, fmt, ...) 357 #define __DCHECK_EQ(val1, val2) __CHECK_EQ(val1, val2) 358 #define __DCHECK_DELTA(val1, val2, delta) __CHECK_DELTA(val1, val2, delta) 359 #define __DCHECK_NE(val1, val2) __CHECK_NE(val1, val2) 360 #define __DCHECK_LE(val1, val2) __CHECK_LE(val1, val2) 361 #define __DCHECK_LT(val1, val2) __CHECK_LT(val1, val2) 362 #define __DCHECK_GE(val1, val2) __CHECK_GE(val1, val2) 363 #define __DCHECK_GT(val1, val2) __CHECK_GT(val1, val2) 364 #define DASSERT_TRUE(cond) ASSERT_TRUE(cond) 365 #define DASSERT_FALSE(cond) ASSERT_FALSE(cond) 366 #define DASSERT_EQ(val1, val2) ASSERT_EQ(val1, val2) 367 #define DASSERT_DELTA(val1, val2, delta) ASSERT_DELTA(val1, val2, delta) 368 #define DASSERT_NE(val1, val2) ASSERT_NE(val1, val2) 369 #define DASSERT_LE(val1, val2) ASSERT_LE(val1, val2) 370 #define DASSERT_LT(val1, val2) ASSERT_LT(val1, val2) 371 #define DASSERT_GE(val1, val2) ASSERT_GE(val1, val2) 372 #define DASSERT_GT(val1, val2) ASSERT_GT(val1, val2) 375 #define DASSERT_MSG(condition, fmt, ...) \ 376 ASSERT_MSG(condition, fmt, ##__VA_ARGS__) 382 #undef ERROR // may conflict with ERROR macro on windows 385 #endif // _LOGGING_H_