| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
| 6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <cassert> | 10 #include <cassert> |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 // | 50 // |
| 51 // Make a bunch of macros for logging. The way to log things is to stream | 51 // Make a bunch of macros for logging. The way to log things is to stream |
| 52 // things to LOG(<a particular severity level>). E.g., | 52 // things to LOG(<a particular severity level>). E.g., |
| 53 // | 53 // |
| 54 // LOG(INFO) << "Found " << num_cookies << " cookies"; | 54 // LOG(INFO) << "Found " << num_cookies << " cookies"; |
| 55 // | 55 // |
| 56 // You can also do conditional logging: | 56 // You can also do conditional logging: |
| 57 // | 57 // |
| 58 // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; | 58 // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 59 // | 59 // |
| 60 // The CHECK(condition) macro is active in both debug and release builds and | 60 // LOG(FATAL) acts like a CHECK(condition) with additional logging info, though |
| 61 // effectively performs a LOG(FATAL) which terminates the process and | 61 // this comes at a cost to binary size and codegen. |
| 62 // generates a crashdump unless a debugger is attached. | |
| 63 // | 62 // |
| 64 // There are also "debug mode" logging macros like the ones above: | 63 // There are also "debug mode" logging macros like the ones above: |
| 65 // | 64 // |
| 66 // DLOG(INFO) << "Found cookies"; | 65 // DLOG(INFO) << "Found cookies"; |
| 67 // | 66 // |
| 68 // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; | 67 // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
| 69 // | 68 // |
| 70 // All "debug mode" logging is compiled away to nothing for non-debug mode | 69 // All "debug mode" logging is compiled away to nothing for non-debug mode |
| 71 // compiles. LOG_IF and development flags also work well together | 70 // compiles. LOG_IF and development flags also work well together |
| 72 // because the code can be compiled away sometimes. | 71 // because the code can be compiled away sometimes. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 // "program with --v=1 or more"; | 117 // "program with --v=1 or more"; |
| 119 // | 118 // |
| 120 // We also override the standard 'assert' to use 'DLOG_ASSERT'. | 119 // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
| 121 // | 120 // |
| 122 // Lastly, there is: | 121 // Lastly, there is: |
| 123 // | 122 // |
| 124 // PLOG(ERROR) << "Couldn't do foo"; | 123 // PLOG(ERROR) << "Couldn't do foo"; |
| 125 // DPLOG(ERROR) << "Couldn't do foo"; | 124 // DPLOG(ERROR) << "Couldn't do foo"; |
| 126 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; | 125 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 127 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; | 126 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
| 128 // PCHECK(condition) << "Couldn't do foo"; | |
| 129 // DPCHECK(condition) << "Couldn't do foo"; | 127 // DPCHECK(condition) << "Couldn't do foo"; |
| 130 // | 128 // |
| 131 // which append the last system error to the message in string form (taken from | 129 // which append the last system error to the message in string form (taken from |
| 132 // GetLastError() on Windows and errno on POSIX). | 130 // GetLastError() on Windows and errno on POSIX). |
| 133 // | 131 // |
| 134 // The supported severity levels for macros that allow you to specify one | 132 // The supported severity levels for macros that allow you to specify one |
| 135 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. | 133 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
| 136 // | 134 // |
| 137 // Very important: logging a message at the FATAL severity level causes | 135 // Very important: logging a message at the FATAL severity level causes |
| 138 // the program to terminate (after the message is logged). | 136 // the program to terminate (after the message is logged). |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 // the Windows SDK does for consistency. | 337 // the Windows SDK does for consistency. |
| 340 #define ERROR 0 | 338 #define ERROR 0 |
| 341 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ | 339 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ |
| 342 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) | 340 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) |
| 343 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR | 341 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
| 344 // Needed for LOG_IS_ON(ERROR). | 342 // Needed for LOG_IS_ON(ERROR). |
| 345 const LogSeverity LOG_0 = LOG_ERROR; | 343 const LogSeverity LOG_0 = LOG_ERROR; |
| 346 #endif | 344 #endif |
| 347 | 345 |
| 348 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, | 346 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
| 349 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will | 347 // LOG_IS_ON(DFATAL) always holds in debug mode. |
| 350 // always fire if they fail. | |
| 351 #define LOG_IS_ON(severity) \ | 348 #define LOG_IS_ON(severity) \ |
| 352 (::logging::ShouldCreateLogMessage(::logging::LOG_##severity)) | 349 (::logging::ShouldCreateLogMessage(::logging::LOG_##severity)) |
| 353 | 350 |
| 354 // We can't do any caching tricks with VLOG_IS_ON() like the | 351 // We can't do any caching tricks with VLOG_IS_ON() like the |
| 355 // google-glog version since it requires GCC extensions. This means | 352 // google-glog version since it requires GCC extensions. This means |
| 356 // that using the v-logging functions in conjunction with --vmodule | 353 // that using the v-logging functions in conjunction with --vmodule |
| 357 // may be slow. | 354 // may be slow. |
| 358 #define VLOG_IS_ON(verboselevel) \ | 355 #define VLOG_IS_ON(verboselevel) \ |
| 359 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) | 356 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
| 360 | 357 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 #if defined(OS_WIN) | 454 #if defined(OS_WIN) |
| 458 #define OOM_CRASH() \ | 455 #define OOM_CRASH() \ |
| 459 do { \ | 456 do { \ |
| 460 ::RaiseException(0xE0000008, EXCEPTION_NONCONTINUABLE, 0, nullptr); \ | 457 ::RaiseException(0xE0000008, EXCEPTION_NONCONTINUABLE, 0, nullptr); \ |
| 461 IMMEDIATE_CRASH(); \ | 458 IMMEDIATE_CRASH(); \ |
| 462 } while (0) | 459 } while (0) |
| 463 #else | 460 #else |
| 464 #define OOM_CRASH() IMMEDIATE_CRASH() | 461 #define OOM_CRASH() IMMEDIATE_CRASH() |
| 465 #endif | 462 #endif |
| 466 | 463 |
| 467 // CHECK dies with a fatal error if condition is not true. It is *not* | 464 // CHECK dies with a fatal error if condition is not true. It is always |
| 468 // controlled by NDEBUG, so the check will be executed regardless of | 465 // enabled in every compilation mode, unlike DCHECK which is conditionally |
| 469 // compilation mode. | 466 // disabled in release builds. |
| 470 // | 467 // |
| 471 // We make sure CHECK et al. always evaluates their arguments, as | 468 // We make sure CHECK et al. always evaluates their arguments, as |
| 472 // doing CHECK(FunctionWithSideEffect()) is a common idiom. | 469 // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
| 473 | 470 |
| 474 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) | 471 // _PREFAST_ is true when the code is being analyzed bythe PREfast analysis |
| 472 // tool. |
| 473 #if defined(_PREFAST_) && defined(OS_WIN) |
| 474 // Use __analysis_assume to tell the VC++ static analysis engine that |
| 475 // assert conditions are true, to suppress warnings. The !! before condition is |
| 476 // because __analysis_assume gets confused on some conditions: |
| 477 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl
y-part-5/ |
| 475 | 478 |
| 476 // Make all CHECK functions discard their log strings to reduce code bloat, and | 479 #define CHECK(condition) \ |
| 477 // improve performance, for official release builds. | 480 __analysis_assume(!!(condition)), ((condition ? (void)0 : (void)0) |
| 481 |
| 482 #define PCHECK(condition) \ |
| 483 __analysis_assume(!!(condition)), ((condition) ? (void)0 : (void)0) |
| 484 |
| 485 #else |
| 486 |
| 487 // Unlike DCHECK(), the CHECK() command does not accept a logging string, as |
| 488 // doing so causes poorer codegen on MSVC (see |
| 489 // https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/TL0NkNXIT1w/mb
MCKqymBQAJ |
| 490 // for more details). |
| 478 // | 491 // |
| 479 // This is not calling BreakDebugger since this is called frequently, and | 492 // This is not calling BreakDebugger since this is called frequently, and |
| 480 // calling an out-of-line function instead of a noreturn inline macro prevents | 493 // calling an out-of-line function instead of a noreturn inline macro prevents |
| 481 // compiler optimizations. | 494 // compiler optimizations. |
| 482 #define CHECK(condition) \ | 495 #define CHECK(condition) (!(condition) ? IMMEDIATE_CRASH() : (void)0) |
| 483 !(condition) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS | |
| 484 | 496 |
| 485 #define PCHECK(condition) CHECK(condition) | 497 #define PCHECK(condition) CHECK(condition) |
| 486 | 498 |
| 487 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) | 499 #endif // !(_PREFAST_ && OS_WIN) |
| 488 | 500 |
| 489 #else // !(OFFICIAL_BUILD && NDEBUG) | 501 // These exist for symmetry with DCHECK, though they also do not produce any |
| 490 | 502 // string output. |
| 491 #if defined(_PREFAST_) && defined(OS_WIN) | 503 #define CHECK_EQ(val1, val2) CHECK((val1) == (val2)) |
| 492 // Use __analysis_assume to tell the VC++ static analysis engine that | 504 #define CHECK_NE(val1, val2) CHECK((val1) != (val2)) |
| 493 // assert conditions are true, to suppress warnings. The LAZY_STREAM | 505 #define CHECK_LE(val1, val2) CHECK((val1) <= (val2)) |
| 494 // parameter doesn't reference 'condition' in /analyze builds because | 506 #define CHECK_LT(val1, val2) CHECK((val1) < (val2)) |
| 495 // this evaluation confuses /analyze. The !! before condition is because | 507 #define CHECK_GE(val1, val2) CHECK((val1) >= (val2)) |
| 496 // __analysis_assume gets confused on some conditions: | 508 #define CHECK_GT(val1, val2) CHECK((val1) > (val2)) |
| 497 // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugl
y-part-5/ | |
| 498 | |
| 499 #define CHECK(condition) \ | |
| 500 __analysis_assume(!!(condition)), \ | |
| 501 LAZY_STREAM(LOG_STREAM(FATAL), false) \ | |
| 502 << "Check failed: " #condition ". " | |
| 503 | |
| 504 #define PCHECK(condition) \ | |
| 505 __analysis_assume(!!(condition)), \ | |
| 506 LAZY_STREAM(PLOG_STREAM(FATAL), false) \ | |
| 507 << "Check failed: " #condition ". " | |
| 508 | |
| 509 #else // _PREFAST_ | |
| 510 | |
| 511 // Do as much work as possible out of line to reduce inline code size. | |
| 512 #define CHECK(condition) \ | |
| 513 LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ | |
| 514 !(condition)) | |
| 515 | |
| 516 #define PCHECK(condition) \ | |
| 517 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | |
| 518 << "Check failed: " #condition ". " | |
| 519 | |
| 520 #endif // _PREFAST_ | |
| 521 | |
| 522 // Helper macro for binary operators. | |
| 523 // Don't use this macro directly in your code, use CHECK_EQ et al below. | |
| 524 // The 'switch' is used to prevent the 'else' from being ambiguous when the | |
| 525 // macro is used in an 'if' clause such as: | |
| 526 // if (a == 1) | |
| 527 // CHECK_EQ(2, a); | |
| 528 #define CHECK_OP(name, op, val1, val2) \ | |
| 529 switch (0) case 0: default: \ | |
| 530 if (::logging::CheckOpResult true_if_passed = \ | |
| 531 ::logging::Check##name##Impl((val1), (val2), \ | |
| 532 #val1 " " #op " " #val2)) \ | |
| 533 ; \ | |
| 534 else \ | |
| 535 ::logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() | |
| 536 | |
| 537 #endif // !(OFFICIAL_BUILD && NDEBUG) | |
| 538 | |
| 539 // This formats a value for a failing CHECK_XX statement. Ordinarily, | |
| 540 // it uses the definition for operator<<, with a few special cases below. | |
| 541 template <typename T> | |
| 542 inline typename std::enable_if< | |
| 543 base::internal::SupportsOstreamOperator<const T&>::value && | |
| 544 !std::is_function<typename std::remove_pointer<T>::type>::value, | |
| 545 void>::type | |
| 546 MakeCheckOpValueString(std::ostream* os, const T& v) { | |
| 547 (*os) << v; | |
| 548 } | |
| 549 | |
| 550 // Provide an overload for functions and function pointers. Function pointers | |
| 551 // don't implicitly convert to void* but do implicitly convert to bool, so | |
| 552 // without this function pointers are always printed as 1 or 0. (MSVC isn't | |
| 553 // standards-conforming here and converts function pointers to regular | |
| 554 // pointers, so this is a no-op for MSVC.) | |
| 555 template <typename T> | |
| 556 inline typename std::enable_if< | |
| 557 std::is_function<typename std::remove_pointer<T>::type>::value, | |
| 558 void>::type | |
| 559 MakeCheckOpValueString(std::ostream* os, const T& v) { | |
| 560 (*os) << reinterpret_cast<const void*>(v); | |
| 561 } | |
| 562 | |
| 563 // We need overloads for enums that don't support operator<<. | |
| 564 // (i.e. scoped enums where no operator<< overload was declared). | |
| 565 template <typename T> | |
| 566 inline typename std::enable_if< | |
| 567 !base::internal::SupportsOstreamOperator<const T&>::value && | |
| 568 std::is_enum<T>::value, | |
| 569 void>::type | |
| 570 MakeCheckOpValueString(std::ostream* os, const T& v) { | |
| 571 (*os) << static_cast<typename base::underlying_type<T>::type>(v); | |
| 572 } | |
| 573 | |
| 574 // We need an explicit overload for std::nullptr_t. | |
| 575 BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p); | |
| 576 | |
| 577 // Build the error message string. This is separate from the "Impl" | |
| 578 // function template because it is not performance critical and so can | |
| 579 // be out of line, while the "Impl" code should be inline. Caller | |
| 580 // takes ownership of the returned string. | |
| 581 template<class t1, class t2> | |
| 582 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { | |
| 583 std::ostringstream ss; | |
| 584 ss << names << " ("; | |
| 585 MakeCheckOpValueString(&ss, v1); | |
| 586 ss << " vs. "; | |
| 587 MakeCheckOpValueString(&ss, v2); | |
| 588 ss << ")"; | |
| 589 std::string* msg = new std::string(ss.str()); | |
| 590 return msg; | |
| 591 } | |
| 592 | |
| 593 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated | |
| 594 // in logging.cc. | |
| 595 extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( | |
| 596 const int&, const int&, const char* names); | |
| 597 extern template BASE_EXPORT | |
| 598 std::string* MakeCheckOpString<unsigned long, unsigned long>( | |
| 599 const unsigned long&, const unsigned long&, const char* names); | |
| 600 extern template BASE_EXPORT | |
| 601 std::string* MakeCheckOpString<unsigned long, unsigned int>( | |
| 602 const unsigned long&, const unsigned int&, const char* names); | |
| 603 extern template BASE_EXPORT | |
| 604 std::string* MakeCheckOpString<unsigned int, unsigned long>( | |
| 605 const unsigned int&, const unsigned long&, const char* names); | |
| 606 extern template BASE_EXPORT | |
| 607 std::string* MakeCheckOpString<std::string, std::string>( | |
| 608 const std::string&, const std::string&, const char* name); | |
| 609 | |
| 610 // Helper functions for CHECK_OP macro. | |
| 611 // The (int, int) specialization works around the issue that the compiler | |
| 612 // will not instantiate the template version of the function on values of | |
| 613 // unnamed enum type - see comment below. | |
| 614 #define DEFINE_CHECK_OP_IMPL(name, op) \ | |
| 615 template <class t1, class t2> \ | |
| 616 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ | |
| 617 const char* names) { \ | |
| 618 if (v1 op v2) return NULL; \ | |
| 619 else return ::logging::MakeCheckOpString(v1, v2, names); \ | |
| 620 } \ | |
| 621 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ | |
| 622 if (v1 op v2) return NULL; \ | |
| 623 else return ::logging::MakeCheckOpString(v1, v2, names); \ | |
| 624 } | |
| 625 DEFINE_CHECK_OP_IMPL(EQ, ==) | |
| 626 DEFINE_CHECK_OP_IMPL(NE, !=) | |
| 627 DEFINE_CHECK_OP_IMPL(LE, <=) | |
| 628 DEFINE_CHECK_OP_IMPL(LT, < ) | |
| 629 DEFINE_CHECK_OP_IMPL(GE, >=) | |
| 630 DEFINE_CHECK_OP_IMPL(GT, > ) | |
| 631 #undef DEFINE_CHECK_OP_IMPL | |
| 632 | |
| 633 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) | |
| 634 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) | |
| 635 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) | |
| 636 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) | |
| 637 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) | |
| 638 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) | |
| 639 | 509 |
| 640 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 510 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 641 #define DCHECK_IS_ON() 0 | 511 #define DCHECK_IS_ON() 0 |
| 642 #else | 512 #else |
| 643 #define DCHECK_IS_ON() 1 | 513 #define DCHECK_IS_ON() 1 |
| 644 #endif | 514 #endif |
| 645 | 515 |
| 646 // Definitions for DLOG et al. | 516 // Definitions for DLOG et al. |
| 647 | 517 |
| 648 #if DCHECK_IS_ON() | 518 #if DCHECK_IS_ON() |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 #define DCHECK(condition) \ | 598 #define DCHECK(condition) \ |
| 729 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ | 599 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
| 730 << "Check failed: " #condition ". " | 600 << "Check failed: " #condition ". " |
| 731 | 601 |
| 732 #define DPCHECK(condition) \ | 602 #define DPCHECK(condition) \ |
| 733 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ | 603 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
| 734 << "Check failed: " #condition ". " | 604 << "Check failed: " #condition ". " |
| 735 | 605 |
| 736 #endif // _PREFAST_ | 606 #endif // _PREFAST_ |
| 737 | 607 |
| 608 // We need an explicit overload for std::nullptr_t. |
| 609 BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p); |
| 610 |
| 611 // This formats a value for a failing DCHECK_XX statement. Ordinarily, |
| 612 // it uses the definition for operator<<, with a few special cases below. |
| 613 template <typename T> |
| 614 inline typename std::enable_if< |
| 615 base::internal::SupportsOstreamOperator<const T&>::value && |
| 616 !std::is_function<typename std::remove_pointer<T>::type>::value, |
| 617 void>::type |
| 618 MakeCheckOpValueString(std::ostream* os, const T& v) { |
| 619 (*os) << v; |
| 620 } |
| 621 |
| 622 // Provide an overload for functions and function pointers. Function pointers |
| 623 // don't implicitly convert to void* but do implicitly convert to bool, so |
| 624 // without this function pointers are always printed as 1 or 0. (MSVC isn't |
| 625 // standards-conforming here and converts function pointers to regular |
| 626 // pointers, so this is a no-op for MSVC.) |
| 627 template <typename T> |
| 628 inline typename std::enable_if< |
| 629 std::is_function<typename std::remove_pointer<T>::type>::value, |
| 630 void>::type |
| 631 MakeCheckOpValueString(std::ostream* os, const T& v) { |
| 632 (*os) << reinterpret_cast<const void*>(v); |
| 633 } |
| 634 |
| 635 // We need overloads for enums that don't support operator<<. |
| 636 // (i.e. scoped enums where no operator<< overload was declared). |
| 637 template <typename T> |
| 638 inline typename std::enable_if< |
| 639 !base::internal::SupportsOstreamOperator<const T&>::value && |
| 640 std::is_enum<T>::value, |
| 641 void>::type |
| 642 MakeCheckOpValueString(std::ostream* os, const T& v) { |
| 643 (*os) << static_cast<typename base::underlying_type<T>::type>(v); |
| 644 } |
| 645 |
| 646 // We need an explicit overload for std::nullptr_t. |
| 647 BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p); |
| 648 |
| 649 // Build the error message string. This is separate from the "Impl" |
| 650 // function template because it is not performance critical and so can |
| 651 // be out of line, while the "Impl" code should be inline. Caller |
| 652 // takes ownership of the returned string. |
| 653 template <class t1, class t2> |
| 654 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 655 std::ostringstream ss; |
| 656 ss << names << " ("; |
| 657 MakeCheckOpValueString(&ss, v1); |
| 658 ss << " vs. "; |
| 659 MakeCheckOpValueString(&ss, v2); |
| 660 ss << ")"; |
| 661 std::string* msg = new std::string(ss.str()); |
| 662 return msg; |
| 663 } |
| 664 |
| 665 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
| 666 // in logging.cc. |
| 667 extern template BASE_EXPORT std::string* |
| 668 MakeCheckOpString<int, int>(const int&, const int&, const char* names); |
| 669 extern template BASE_EXPORT std::string* |
| 670 MakeCheckOpString<unsigned long, unsigned long>(const unsigned long&, |
| 671 const unsigned long&, |
| 672 const char* names); |
| 673 extern template BASE_EXPORT std::string* |
| 674 MakeCheckOpString<unsigned long, unsigned int>(const unsigned long&, |
| 675 const unsigned int&, |
| 676 const char* names); |
| 677 extern template BASE_EXPORT std::string* |
| 678 MakeCheckOpString<unsigned int, unsigned long>(const unsigned int&, |
| 679 const unsigned long&, |
| 680 const char* names); |
| 681 extern template BASE_EXPORT std::string* |
| 682 MakeCheckOpString<std::string, std::string>(const std::string&, |
| 683 const std::string&, |
| 684 const char* name); |
| 685 |
| 686 // Helper functions for DCHECK_OP macro. |
| 687 // The (int, int) specialization works around the issue that the compiler |
| 688 // will not instantiate the template version of the function on values of |
| 689 // unnamed enum type - see comment below. |
| 690 #define DEFINE_DCHECK_OP_IMPL(name, op) \ |
| 691 template <class t1, class t2> \ |
| 692 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 693 const char* names) { \ |
| 694 if (v1 op v2) \ |
| 695 return NULL; \ |
| 696 else \ |
| 697 return ::logging::MakeCheckOpString(v1, v2, names); \ |
| 698 } \ |
| 699 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
| 700 if (v1 op v2) \ |
| 701 return NULL; \ |
| 702 else \ |
| 703 return ::logging::MakeCheckOpString(v1, v2, names); \ |
| 704 } |
| 705 DEFINE_DCHECK_OP_IMPL(EQ, ==) |
| 706 DEFINE_DCHECK_OP_IMPL(NE, !=) |
| 707 DEFINE_DCHECK_OP_IMPL(LE, <=) |
| 708 DEFINE_DCHECK_OP_IMPL(LT, <) |
| 709 DEFINE_DCHECK_OP_IMPL(GE, >=) |
| 710 DEFINE_DCHECK_OP_IMPL(GT, >) |
| 711 #undef DEFINE_DCHECK_OP_IMPL |
| 712 |
| 738 // Helper macro for binary operators. | 713 // Helper macro for binary operators. |
| 739 // Don't use this macro directly in your code, use DCHECK_EQ et al below. | 714 // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
| 740 // The 'switch' is used to prevent the 'else' from being ambiguous when the | 715 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 741 // macro is used in an 'if' clause such as: | 716 // macro is used in an 'if' clause such as: |
| 742 // if (a == 1) | 717 // if (a == 1) |
| 743 // DCHECK_EQ(2, a); | 718 // DCHECK_EQ(2, a); |
| 744 #define DCHECK_OP(name, op, val1, val2) \ | 719 #define DCHECK_OP(name, op, val1, val2) \ |
| 745 switch (0) case 0: default: \ | 720 switch (0) case 0: default: \ |
| 746 if (::logging::CheckOpResult true_if_passed = \ | 721 if (::logging::CheckOpResult true_if_passed = \ |
| 747 DCHECK_IS_ON() ? \ | 722 DCHECK_IS_ON() ? \ |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 #elif NOTIMPLEMENTED_POLICY == 5 | 990 #elif NOTIMPLEMENTED_POLICY == 5 |
| 1016 #define NOTIMPLEMENTED() do {\ | 991 #define NOTIMPLEMENTED() do {\ |
| 1017 static bool logged_once = false;\ | 992 static bool logged_once = false;\ |
| 1018 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 993 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 1019 logged_once = true;\ | 994 logged_once = true;\ |
| 1020 } while(0);\ | 995 } while(0);\ |
| 1021 EAT_STREAM_PARAMETERS | 996 EAT_STREAM_PARAMETERS |
| 1022 #endif | 997 #endif |
| 1023 | 998 |
| 1024 #endif // BASE_LOGGING_H_ | 999 #endif // BASE_LOGGING_H_ |
| OLD | NEW |