| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <cassert> | 9 #include <cassert> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <cstring> | 11 #include <cstring> |
| 12 #include <sstream> | 12 #include <sstream> |
| 13 | 13 |
| 14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/debug/debugger.h" |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 | 18 |
| 18 // | 19 // |
| 19 // Optional message capabilities | 20 // Optional message capabilities |
| 20 // ----------------------------- | 21 // ----------------------------- |
| 21 // Assertion failed messages and fatal errors are displayed in a dialog box | 22 // Assertion failed messages and fatal errors are displayed in a dialog box |
| 22 // before the application exits. However, running this UI creates a message | 23 // before the application exits. However, running this UI creates a message |
| 23 // loop, which causes application messages to be processed and potentially | 24 // loop, which causes application messages to be processed and potentially |
| 24 // dispatched to existing application windows. Since the application is in a | 25 // dispatched to existing application windows. Since the application is in a |
| 25 // bad state when this assertion dialog is displayed, these messages may not | 26 // bad state when this assertion dialog is displayed, these messages may not |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) | 436 #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity) |
| 436 // TODO(tschmelcher): Should we add OSStatus logging for Mac? | 437 // TODO(tschmelcher): Should we add OSStatus logging for Mac? |
| 437 #endif | 438 #endif |
| 438 | 439 |
| 439 #define PLOG(severity) \ | 440 #define PLOG(severity) \ |
| 440 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) | 441 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
| 441 | 442 |
| 442 #define PLOG_IF(severity, condition) \ | 443 #define PLOG_IF(severity, condition) \ |
| 443 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) | 444 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
| 444 | 445 |
| 446 // http://crbug.com/16512 is open for a real fix for this. For now, Windows |
| 447 // uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is |
| 448 // defined. |
| 449 #if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \ |
| 450 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD)) |
| 451 #define LOGGING_IS_OFFICIAL_BUILD 1 |
| 452 #else |
| 453 #define LOGGING_IS_OFFICIAL_BUILD 0 |
| 454 #endif |
| 455 |
| 456 // The actual stream used isn't important. |
| 457 #define EAT_STREAM_PARAMETERS \ |
| 458 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) |
| 459 |
| 445 // CHECK dies with a fatal error if condition is not true. It is *not* | 460 // CHECK dies with a fatal error if condition is not true. It is *not* |
| 446 // controlled by NDEBUG, so the check will be executed regardless of | 461 // controlled by NDEBUG, so the check will be executed regardless of |
| 447 // compilation mode. | 462 // compilation mode. |
| 448 // | 463 // |
| 449 // We make sure CHECK et al. always evaluates their arguments, as | 464 // We make sure CHECK et al. always evaluates their arguments, as |
| 450 // doing CHECK(FunctionWithSideEffect()) is a common idiom. | 465 // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
| 466 |
| 467 #if LOGGING_IS_OFFICIAL_BUILD |
| 468 |
| 469 // Make all CHECK functions discard their log strings to reduce code |
| 470 // bloat for official builds. |
| 471 |
| 472 // TODO(akalin): This would be more valuable if there were some way to |
| 473 // remove BreakDebugger() from the backtrace, perhaps by turning it |
| 474 // into a macro (like __debugbreak() on Windows). |
| 475 #define CHECK(condition) \ |
| 476 !(condition) ? ::base::debug::BreakDebugger() : EAT_STREAM_PARAMETERS |
| 477 |
| 478 #define PCHECK(condition) CHECK(condition) |
| 479 |
| 480 #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
| 481 |
| 482 #else |
| 483 |
| 451 #define CHECK(condition) \ | 484 #define CHECK(condition) \ |
| 452 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ | 485 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \ |
| 453 << "Check failed: " #condition ". " | 486 << "Check failed: " #condition ". " |
| 454 | 487 |
| 455 #define PCHECK(condition) \ | 488 #define PCHECK(condition) \ |
| 456 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ | 489 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ |
| 457 << "Check failed: " #condition ". " | 490 << "Check failed: " #condition ". " |
| 458 | 491 |
| 492 // Helper macro for binary operators. |
| 493 // Don't use this macro directly in your code, use CHECK_EQ et al below. |
| 494 // |
| 495 // TODO(akalin): Rewrite this so that constructs like if (...) |
| 496 // CHECK_EQ(...) else { ... } work properly. |
| 497 #define CHECK_OP(name, op, val1, val2) \ |
| 498 if (std::string* _result = \ |
| 499 logging::Check##name##Impl((val1), (val2), \ |
| 500 #val1 " " #op " " #val2)) \ |
| 501 logging::LogMessage(__FILE__, __LINE__, _result).stream() |
| 502 |
| 503 #endif |
| 504 |
| 459 // Build the error message string. This is separate from the "Impl" | 505 // Build the error message string. This is separate from the "Impl" |
| 460 // function template because it is not performance critical and so can | 506 // function template because it is not performance critical and so can |
| 461 // be out of line, while the "Impl" code should be inline. Caller | 507 // be out of line, while the "Impl" code should be inline. Caller |
| 462 // takes ownership of the returned string. | 508 // takes ownership of the returned string. |
| 463 template<class t1, class t2> | 509 template<class t1, class t2> |
| 464 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { | 510 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
| 465 std::ostringstream ss; | 511 std::ostringstream ss; |
| 466 ss << names << " (" << v1 << " vs. " << v2 << ")"; | 512 ss << names << " (" << v1 << " vs. " << v2 << ")"; |
| 467 std::string* msg = new std::string(ss.str()); | 513 std::string* msg = new std::string(ss.str()); |
| 468 return msg; | 514 return msg; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 481 std::string* MakeCheckOpString<unsigned long, unsigned int>( | 527 std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 482 const unsigned long&, const unsigned int&, const char* names); | 528 const unsigned long&, const unsigned int&, const char* names); |
| 483 extern template BASE_EXPORT | 529 extern template BASE_EXPORT |
| 484 std::string* MakeCheckOpString<unsigned int, unsigned long>( | 530 std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 485 const unsigned int&, const unsigned long&, const char* names); | 531 const unsigned int&, const unsigned long&, const char* names); |
| 486 extern template BASE_EXPORT | 532 extern template BASE_EXPORT |
| 487 std::string* MakeCheckOpString<std::string, std::string>( | 533 std::string* MakeCheckOpString<std::string, std::string>( |
| 488 const std::string&, const std::string&, const char* name); | 534 const std::string&, const std::string&, const char* name); |
| 489 #endif | 535 #endif |
| 490 | 536 |
| 491 // Helper macro for binary operators. | |
| 492 // Don't use this macro directly in your code, use CHECK_EQ et al below. | |
| 493 // | |
| 494 // TODO(akalin): Rewrite this so that constructs like if (...) | |
| 495 // CHECK_EQ(...) else { ... } work properly. | |
| 496 #define CHECK_OP(name, op, val1, val2) \ | |
| 497 if (std::string* _result = \ | |
| 498 logging::Check##name##Impl((val1), (val2), \ | |
| 499 #val1 " " #op " " #val2)) \ | |
| 500 logging::LogMessage(__FILE__, __LINE__, _result).stream() | |
| 501 | |
| 502 // Helper functions for CHECK_OP macro. | 537 // Helper functions for CHECK_OP macro. |
| 503 // The (int, int) specialization works around the issue that the compiler | 538 // The (int, int) specialization works around the issue that the compiler |
| 504 // will not instantiate the template version of the function on values of | 539 // will not instantiate the template version of the function on values of |
| 505 // unnamed enum type - see comment below. | 540 // unnamed enum type - see comment below. |
| 506 #define DEFINE_CHECK_OP_IMPL(name, op) \ | 541 #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 507 template <class t1, class t2> \ | 542 template <class t1, class t2> \ |
| 508 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ | 543 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
| 509 const char* names) { \ | 544 const char* names) { \ |
| 510 if (v1 op v2) return NULL; \ | 545 if (v1 op v2) return NULL; \ |
| 511 else return MakeCheckOpString(v1, v2, names); \ | 546 else return MakeCheckOpString(v1, v2, names); \ |
| (...skipping 10 matching lines...) Expand all Loading... |
| 522 DEFINE_CHECK_OP_IMPL(GT, > ) | 557 DEFINE_CHECK_OP_IMPL(GT, > ) |
| 523 #undef DEFINE_CHECK_OP_IMPL | 558 #undef DEFINE_CHECK_OP_IMPL |
| 524 | 559 |
| 525 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) | 560 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
| 526 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) | 561 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
| 527 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) | 562 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
| 528 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) | 563 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
| 529 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) | 564 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
| 530 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) | 565 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
| 531 | 566 |
| 532 // http://crbug.com/16512 is open for a real fix for this. For now, Windows | 567 #if LOGGING_IS_OFFICIAL_BUILD |
| 533 // uses OFFICIAL_BUILD and other platforms use the branding flag when NDEBUG is | |
| 534 // defined. | |
| 535 #if ( defined(OS_WIN) && defined(OFFICIAL_BUILD)) || \ | |
| 536 (!defined(OS_WIN) && defined(NDEBUG) && defined(GOOGLE_CHROME_BUILD)) | |
| 537 // Used by unit tests. | |
| 538 #define LOGGING_IS_OFFICIAL_BUILD | |
| 539 | |
| 540 // In order to have optimized code for official builds, remove DLOGs and | 568 // In order to have optimized code for official builds, remove DLOGs and |
| 541 // DCHECKs. | 569 // DCHECKs. |
| 542 #define ENABLE_DLOG 0 | 570 #define ENABLE_DLOG 0 |
| 543 #define ENABLE_DCHECK 0 | 571 #define ENABLE_DCHECK 0 |
| 544 | 572 |
| 545 #elif defined(NDEBUG) | 573 #elif defined(NDEBUG) |
| 546 // Otherwise, if we're a release build, remove DLOGs but not DCHECKs | 574 // Otherwise, if we're a release build, remove DLOGs but not DCHECKs |
| 547 // (since those can still be turned on via a command-line flag). | 575 // (since those can still be turned on via a command-line flag). |
| 548 #define ENABLE_DLOG 0 | 576 #define ENABLE_DLOG 0 |
| 549 #define ENABLE_DCHECK 1 | 577 #define ENABLE_DCHECK 1 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 565 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) | 593 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
| 566 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) | 594 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
| 567 | 595 |
| 568 #else // ENABLE_DLOG | 596 #else // ENABLE_DLOG |
| 569 | 597 |
| 570 // If ENABLE_DLOG is off, we want to avoid emitting any references to | 598 // If ENABLE_DLOG is off, we want to avoid emitting any references to |
| 571 // |condition| (which may reference a variable defined only if NDEBUG | 599 // |condition| (which may reference a variable defined only if NDEBUG |
| 572 // is not defined). Contrast this with DCHECK et al., which has | 600 // is not defined). Contrast this with DCHECK et al., which has |
| 573 // different behavior. | 601 // different behavior. |
| 574 | 602 |
| 575 #define DLOG_EAT_STREAM_PARAMETERS \ | |
| 576 true ? (void) 0 : ::logging::LogMessageVoidify() & LOG_STREAM(FATAL) | |
| 577 | |
| 578 #define DLOG_IS_ON(severity) false | 603 #define DLOG_IS_ON(severity) false |
| 579 #define DLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS | 604 #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 580 #define DLOG_ASSERT(condition) DLOG_EAT_STREAM_PARAMETERS | 605 #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
| 581 #define DPLOG_IF(severity, condition) DLOG_EAT_STREAM_PARAMETERS | 606 #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
| 582 #define DVLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS | 607 #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
| 583 #define DVPLOG_IF(verboselevel, condition) DLOG_EAT_STREAM_PARAMETERS | 608 #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
| 584 | 609 |
| 585 #endif // ENABLE_DLOG | 610 #endif // ENABLE_DLOG |
| 586 | 611 |
| 587 // DEBUG_MODE is for uses like | 612 // DEBUG_MODE is for uses like |
| 588 // if (DEBUG_MODE) foo.CheckThatFoo(); | 613 // if (DEBUG_MODE) foo.CheckThatFoo(); |
| 589 // instead of | 614 // instead of |
| 590 // #ifndef NDEBUG | 615 // #ifndef NDEBUG |
| 591 // foo.CheckThatFoo(); | 616 // foo.CheckThatFoo(); |
| 592 // #endif | 617 // #endif |
| 593 // | 618 // |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 namespace base { | 980 namespace base { |
| 956 | 981 |
| 957 class StringPiece; | 982 class StringPiece; |
| 958 | 983 |
| 959 // Allows StringPiece to be logged. | 984 // Allows StringPiece to be logged. |
| 960 BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece); | 985 BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
| 961 | 986 |
| 962 } // namespace base | 987 } // namespace base |
| 963 | 988 |
| 964 #endif // BASE_LOGGING_H_ | 989 #endif // BASE_LOGGING_H_ |
| OLD | NEW |