| 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 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/debug/dump_without_crashing.h" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 | 9 |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 13 #include <signal.h> | 14 #include <signal.h> |
| 14 #include <unistd.h> | 15 #include <unistd.h> |
| 15 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
| 16 #endif // OS_POSIX | 17 #endif // OS_POSIX |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 DLOG_IF(INFO, true) << mock_log_source.Log(); | 95 DLOG_IF(INFO, true) << mock_log_source.Log(); |
| 95 DPLOG(INFO) << mock_log_source.Log(); | 96 DPLOG(INFO) << mock_log_source.Log(); |
| 96 DPLOG_IF(INFO, true) << mock_log_source.Log(); | 97 DPLOG_IF(INFO, true) << mock_log_source.Log(); |
| 97 DVLOG(0) << mock_log_source.Log(); | 98 DVLOG(0) << mock_log_source.Log(); |
| 98 DVLOG_IF(0, true) << mock_log_source.Log(); | 99 DVLOG_IF(0, true) << mock_log_source.Log(); |
| 99 DVPLOG(0) << mock_log_source.Log(); | 100 DVPLOG(0) << mock_log_source.Log(); |
| 100 DVPLOG_IF(0, true) << mock_log_source.Log(); | 101 DVPLOG_IF(0, true) << mock_log_source.Log(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 TEST_F(LoggingTest, LogIsOn) { | 104 TEST_F(LoggingTest, LogIsOn) { |
| 104 #if defined(NDEBUG) | 105 #if defined(NDEBUG) || defined(DCHECK_IS_DUMP_WITHOUT_CRASH) |
| 105 const bool kDfatalIsFatal = false; | 106 const bool kDfatalIsFatal = false; |
| 106 #else // defined(NDEBUG) | 107 #else // defined(NDEBUG) |
| 107 const bool kDfatalIsFatal = true; | 108 const bool kDfatalIsFatal = true; |
| 108 #endif // defined(NDEBUG) | 109 #endif // defined(NDEBUG) |
| 109 | 110 |
| 110 SetMinLogLevel(LOG_INFO); | 111 SetMinLogLevel(LOG_INFO); |
| 111 EXPECT_TRUE(LOG_IS_ON(INFO)); | 112 EXPECT_TRUE(LOG_IS_ON(INFO)); |
| 112 EXPECT_TRUE(LOG_IS_ON(WARNING)); | 113 EXPECT_TRUE(LOG_IS_ON(WARNING)); |
| 113 EXPECT_TRUE(LOG_IS_ON(ERROR)); | 114 EXPECT_TRUE(LOG_IS_ON(ERROR)); |
| 114 EXPECT_TRUE(LOG_IS_ON(FATAL)); | 115 EXPECT_TRUE(LOG_IS_ON(FATAL)); |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 DCHECK_EQ(0, 0) << mock_log_source.Log(); | 374 DCHECK_EQ(0, 0) << mock_log_source.Log(); |
| 374 #else | 375 #else |
| 375 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); | 376 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); |
| 376 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); | 377 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); |
| 377 DCHECK_EQ(0, 0) << mock_log_source.Log(); | 378 DCHECK_EQ(0, 0) << mock_log_source.Log(); |
| 378 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) | 379 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) |
| 379 << mock_log_source.Log(); | 380 << mock_log_source.Log(); |
| 380 #endif | 381 #endif |
| 381 } | 382 } |
| 382 | 383 |
| 384 #if !defined(DCHECK_IS_DUMP_WITHOUT_CRASH) |
| 383 void DcheckEmptyFunction1() { | 385 void DcheckEmptyFunction1() { |
| 384 // Provide a body so that Release builds do not cause the compiler to | 386 // Provide a body so that Release builds do not cause the compiler to |
| 385 // optimize DcheckEmptyFunction1 and DcheckEmptyFunction2 as a single | 387 // optimize DcheckEmptyFunction1 and DcheckEmptyFunction2 as a single |
| 386 // function, which breaks the Dcheck tests below. | 388 // function, which breaks the Dcheck tests below. |
| 387 LOG(INFO) << "DcheckEmptyFunction1"; | 389 LOG(INFO) << "DcheckEmptyFunction1"; |
| 388 } | 390 } |
| 389 void DcheckEmptyFunction2() {} | 391 void DcheckEmptyFunction2() {} |
| 390 | 392 |
| 391 TEST_F(LoggingTest, Dcheck) { | 393 TEST_F(LoggingTest, Dcheck) { |
| 392 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 394 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 EXPECT_EQ(0, g_log_sink_call_count); | 450 EXPECT_EQ(0, g_log_sink_call_count); |
| 449 DCHECK_EQ(mp1, &MemberFunctions::MemberFunction1); | 451 DCHECK_EQ(mp1, &MemberFunctions::MemberFunction1); |
| 450 EXPECT_EQ(0, g_log_sink_call_count); | 452 EXPECT_EQ(0, g_log_sink_call_count); |
| 451 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction2); | 453 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction2); |
| 452 EXPECT_EQ(0, g_log_sink_call_count); | 454 EXPECT_EQ(0, g_log_sink_call_count); |
| 453 DCHECK_EQ(fp1, fp2); | 455 DCHECK_EQ(fp1, fp2); |
| 454 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, g_log_sink_call_count); | 456 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, g_log_sink_call_count); |
| 455 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction1); | 457 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction1); |
| 456 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, g_log_sink_call_count); | 458 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, g_log_sink_call_count); |
| 457 } | 459 } |
| 460 #endif // !defined(DCHECK_IS_DUMP_WITHOUT_CRASH) |
| 458 | 461 |
| 459 TEST_F(LoggingTest, DcheckReleaseBehavior) { | 462 TEST_F(LoggingTest, DcheckReleaseBehavior) { |
| 460 int some_variable = 1; | 463 int some_variable = 1; |
| 461 // These should still reference |some_variable| so we don't get | 464 // These should still reference |some_variable| so we don't get |
| 462 // unused variable warnings. | 465 // unused variable warnings. |
| 463 DCHECK(some_variable) << "test"; | 466 DCHECK(some_variable) << "test"; |
| 464 DPCHECK(some_variable) << "test"; | 467 DPCHECK(some_variable) << "test"; |
| 465 DCHECK_EQ(some_variable, 1) << "test"; | 468 DCHECK_EQ(some_variable, 1) << "test"; |
| 466 } | 469 } |
| 467 | 470 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 482 if (false) | 485 if (false) |
| 483 CHECK_EQ(false, true); // Unreached. | 486 CHECK_EQ(false, true); // Unreached. |
| 484 else | 487 else |
| 485 CHECK_EQ(true, reached = true); // Reached, passed. | 488 CHECK_EQ(true, reached = true); // Reached, passed. |
| 486 ASSERT_TRUE(reached); | 489 ASSERT_TRUE(reached); |
| 487 | 490 |
| 488 if (false) | 491 if (false) |
| 489 CHECK_EQ(false, true); // Unreached. | 492 CHECK_EQ(false, true); // Unreached. |
| 490 } | 493 } |
| 491 | 494 |
| 495 #if defined(DCHECK_IS_DUMP_WITHOUT_CRASH) |
| 496 namespace { |
| 497 |
| 498 int g_fake_dump_without_crashing_count = 0; |
| 499 void FakeDumpWithoutCrashing() { |
| 500 g_fake_dump_without_crashing_count++; |
| 501 } |
| 502 |
| 503 } // namespace |
| 504 |
| 505 TEST_F(LoggingTest, DCheckIsDumpWithoutCrash) { |
| 506 // Replace the dump-without-crashing function, to test. |
| 507 base::debug::SetDumpWithoutCrashingFunction(&FakeDumpWithoutCrashing); |
| 508 |
| 509 // Invoke DCHECK(false) twice, and verify that only one dump call is made. |
| 510 DCHECK(false); |
| 511 EXPECT_EQ(1, g_fake_dump_without_crashing_count); |
| 512 DCHECK(false); |
| 513 EXPECT_EQ(1, g_fake_dump_without_crashing_count); |
| 514 |
| 515 EXPECT_EQ(LOG_ERROR, LOG_DFATAL); |
| 516 |
| 517 base::debug::SetDumpWithoutCrashingFunction(nullptr); |
| 518 } |
| 519 #endif // defined(DCHECK_IS_DUMP_WITHOUT_CRASH) |
| 520 |
| 492 // Test that defining an operator<< for a type in a namespace doesn't prevent | 521 // Test that defining an operator<< for a type in a namespace doesn't prevent |
| 493 // other code in that namespace from calling the operator<<(ostream, wstring) | 522 // other code in that namespace from calling the operator<<(ostream, wstring) |
| 494 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be | 523 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be |
| 495 // found by ADL, since defining another operator<< prevents name lookup from | 524 // found by ADL, since defining another operator<< prevents name lookup from |
| 496 // looking in the global namespace. | 525 // looking in the global namespace. |
| 497 namespace nested_test { | 526 namespace nested_test { |
| 498 class Streamable {}; | 527 class Streamable {}; |
| 499 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, | 528 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, |
| 500 const Streamable&) { | 529 const Streamable&) { |
| 501 return out << "Streamable"; | 530 return out << "Streamable"; |
| 502 } | 531 } |
| 503 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { | 532 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { |
| 504 std::wstring wstr = L"Hello World"; | 533 std::wstring wstr = L"Hello World"; |
| 505 std::ostringstream ostr; | 534 std::ostringstream ostr; |
| 506 ostr << wstr; | 535 ostr << wstr; |
| 507 EXPECT_EQ("Hello World", ostr.str()); | 536 EXPECT_EQ("Hello World", ostr.str()); |
| 508 } | 537 } |
| 509 } // namespace nested_test | 538 } // namespace nested_test |
| 510 | 539 |
| 511 } // namespace | 540 } // namespace |
| 512 | 541 |
| 513 } // namespace logging | 542 } // namespace logging |
| OLD | NEW |