| 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 <cassert> | 8 #include <cassert> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <cstring> | 10 #include <cstring> |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 #define PLOG(severity) \ | 419 #define PLOG(severity) \ |
| 420 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) | 420 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 421 | 421 |
| 422 #define PLOG_IF(severity, condition) \ | 422 #define PLOG_IF(severity, condition) \ |
| 423 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) | 423 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 424 | 424 |
| 425 // The actual stream used isn't important. | 425 // The actual stream used isn't important. |
| 426 #define EAT_STREAM_PARAMETERS \ | 426 #define EAT_STREAM_PARAMETERS \ |
| 427 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) | 427 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
| 428 | 428 |
| 429 // Captures the result of a CHECK_EQ (for example) and facilitates testing as a |
| 430 // boolean. |
| 431 class CheckOpResult { |
| 432 public: |
| 433 // |message| must be null if and only if the check failed. |
| 434 CheckOpResult(std::string* message) : message_(message) {} |
| 435 // Returns true if the check succeeded. |
| 436 operator bool() const { return !message_; } |
| 437 // Returns the message. |
| 438 std::string* message() { return message_; } |
| 439 |
| 440 private: |
| 441 std::string* message_; |
| 442 }; |
| 443 |
| 429 // CHECK dies with a fatal error if condition is not true. It is *not* | 444 // CHECK dies with a fatal error if condition is not true. It is *not* |
| 430 // controlled by NDEBUG, so the check will be executed regardless of | 445 // controlled by NDEBUG, so the check will be executed regardless of |
| 431 // compilation mode. | 446 // compilation mode. |
| 432 // | 447 // |
| 433 // We make sure CHECK et al. always evaluates their arguments, as | 448 // We make sure CHECK et al. always evaluates their arguments, as |
| 434 // doing CHECK(FunctionWithSideEffect()) is a common idiom. | 449 // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
| 435 | 450 |
| 436 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) && !defined(OS_ANDROID) | 451 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) && !defined(OS_ANDROID) |
| 437 | 452 |
| 438 // Make all CHECK functions discard their log strings to reduce code | 453 // Make all CHECK functions discard their log strings to reduce code |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 << "Check failed: " #condition ". " | 490 << "Check failed: " #condition ". " |
| 476 | 491 |
| 477 #define PCHECK(condition) \ | 492 #define PCHECK(condition) \ |
| 478 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | 493 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ |
| 479 << "Check failed: " #condition ". " | 494 << "Check failed: " #condition ". " |
| 480 | 495 |
| 481 #endif // _PREFAST_ | 496 #endif // _PREFAST_ |
| 482 | 497 |
| 483 // Helper macro for binary operators. | 498 // Helper macro for binary operators. |
| 484 // Don't use this macro directly in your code, use CHECK_EQ et al below. | 499 // Don't use this macro directly in your code, use CHECK_EQ et al below. |
| 485 // | 500 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 486 // TODO(akalin): Rewrite this so that constructs like if (...) | 501 // macro is used in an 'if' clause such as: |
| 487 // CHECK_EQ(...) else { ... } work properly. | 502 // if (a == 1) |
| 488 #define CHECK_OP(name, op, val1, val2) \ | 503 // CHECK_EQ(2, a); |
| 489 if (std::string* _result = \ | 504 #define CHECK_OP(name, op, val1, val2) \ |
| 490 logging::Check##name##Impl((val1), (val2), \ | 505 switch (0) case 0: default: \ |
| 491 #val1 " " #op " " #val2)) \ | 506 if (logging::CheckOpResult true_if_passed = \ |
| 492 logging::LogMessage(__FILE__, __LINE__, _result).stream() | 507 logging::Check##name##Impl((val1), (val2), \ |
| 508 #val1 " " #op " " #val2)) \ |
| 509 ; \ |
| 510 else \ |
| 511 logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() |
| 493 | 512 |
| 494 #endif | 513 #endif |
| 495 | 514 |
| 496 // Build the error message string. This is separate from the "Impl" | 515 // Build the error message string. This is separate from the "Impl" |
| 497 // function template because it is not performance critical and so can | 516 // function template because it is not performance critical and so can |
| 498 // be out of line, while the "Impl" code should be inline. Caller | 517 // be out of line, while the "Impl" code should be inline. Caller |
| 499 // takes ownership of the returned string. | 518 // takes ownership of the returned string. |
| 500 template<class t1, class t2> | 519 template<class t1, class t2> |
| 501 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { | 520 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 502 std::ostringstream ss; | 521 std::ostringstream ss; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 << "Check failed: " #condition ". " | 677 << "Check failed: " #condition ". " |
| 659 | 678 |
| 660 #define DPCHECK(condition) \ | 679 #define DPCHECK(condition) \ |
| 661 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ | 680 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() ? !(condition) : false) \ |
| 662 << "Check failed: " #condition ". " | 681 << "Check failed: " #condition ". " |
| 663 | 682 |
| 664 #endif // _PREFAST_ | 683 #endif // _PREFAST_ |
| 665 | 684 |
| 666 // Helper macro for binary operators. | 685 // Helper macro for binary operators. |
| 667 // Don't use this macro directly in your code, use DCHECK_EQ et al below. | 686 // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
| 668 #define DCHECK_OP(name, op, val1, val2) \ | 687 // The 'switch' is used to prevent the 'else' from being ambiguous when the |
| 669 if (DCHECK_IS_ON()) \ | 688 // macro is used in an 'if' clause such as: |
| 670 if (std::string* _result = logging::Check##name##Impl( \ | 689 // if (a == 1) |
| 671 (val1), (val2), #val1 " " #op " " #val2)) \ | 690 // DCHECK_EQ(2, a); |
| 672 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, _result) \ | 691 #define DCHECK_OP(name, op, val1, val2) \ |
| 673 .stream() | 692 switch (0) case 0: default: \ |
| 693 if (logging::CheckOpResult true_if_passed = \ |
| 694 DCHECK_IS_ON() ? \ |
| 695 logging::Check##name##Impl((val1), (val2), \ |
| 696 #val1 " " #op " " #val2) : nullptr) \ |
| 697 ; \ |
| 698 else \ |
| 699 logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
| 700 true_if_passed.message()).stream() |
| 674 | 701 |
| 675 // Equality/Inequality checks - compare two values, and log a | 702 // Equality/Inequality checks - compare two values, and log a |
| 676 // LOG_DCHECK message including the two values when the result is not | 703 // LOG_DCHECK message including the two values when the result is not |
| 677 // as expected. The values must have operator<<(ostream, ...) | 704 // as expected. The values must have operator<<(ostream, ...) |
| 678 // defined. | 705 // defined. |
| 679 // | 706 // |
| 680 // You may append to the error message like so: | 707 // You may append to the error message like so: |
| 681 // DCHECK_NE(1, 2) << ": The world must be ending!"; | 708 // DCHECK_NE(1, 2) << ": The world must be ending!"; |
| 682 // | 709 // |
| 683 // We are very careful to ensure that each argument is evaluated exactly | 710 // We are very careful to ensure that each argument is evaluated exactly |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 930 #elif NOTIMPLEMENTED_POLICY == 5 | 957 #elif NOTIMPLEMENTED_POLICY == 5 |
| 931 #define NOTIMPLEMENTED() do {\ | 958 #define NOTIMPLEMENTED() do {\ |
| 932 static bool logged_once = false;\ | 959 static bool logged_once = false;\ |
| 933 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 960 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
| 934 logged_once = true;\ | 961 logged_once = true;\ |
| 935 } while(0);\ | 962 } while(0);\ |
| 936 EAT_STREAM_PARAMETERS | 963 EAT_STREAM_PARAMETERS |
| 937 #endif | 964 #endif |
| 938 | 965 |
| 939 #endif // BASE_LOGGING_H_ | 966 #endif // BASE_LOGGING_H_ |
| OLD | NEW |