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/bind.h" | 6 #include "base/bind.h" |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #endif // OS_WIN | 28 #endif // OS_WIN |
29 | 29 |
30 namespace logging { | 30 namespace logging { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 using ::testing::Return; | 34 using ::testing::Return; |
35 using ::testing::_; | 35 using ::testing::_; |
36 | 36 |
37 // Needs to be global since log assert handlers can't maintain state. | 37 // Needs to be global since log assert handlers can't maintain state. |
38 int log_sink_call_count = 0; | 38 int g_log_sink_call_count = 0; |
39 | 39 |
40 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 40 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
41 void LogSink(const char* file, | 41 void LogSink(const char* file, |
42 int line, | 42 int line, |
43 const base::StringPiece message, | 43 const base::StringPiece message, |
44 const base::StringPiece stack_trace) { | 44 const base::StringPiece stack_trace) { |
45 ++log_sink_call_count; | 45 ++g_log_sink_call_count; |
46 } | 46 } |
47 #endif | 47 #endif |
48 | 48 |
49 // Class to make sure any manipulations we do to the min log level are | 49 // Class to make sure any manipulations we do to the min log level are |
50 // contained (i.e., do not affect other unit tests). | 50 // contained (i.e., do not affect other unit tests). |
51 class LogStateSaver { | 51 class LogStateSaver { |
52 public: | 52 public: |
53 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} | 53 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} |
54 | 54 |
55 ~LogStateSaver() { | 55 ~LogStateSaver() { |
56 SetMinLogLevel(old_min_log_level_); | 56 SetMinLogLevel(old_min_log_level_); |
57 log_sink_call_count = 0; | 57 g_log_sink_call_count = 0; |
58 } | 58 } |
59 | 59 |
60 private: | 60 private: |
61 int old_min_log_level_; | 61 int old_min_log_level_; |
62 | 62 |
63 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | 63 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
64 }; | 64 }; |
65 | 65 |
66 class LoggingTest : public testing::Test { | 66 class LoggingTest : public testing::Test { |
67 private: | 67 private: |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 EXPECT_TRUE(LOG_IS_ON(ERROR)); | 138 EXPECT_TRUE(LOG_IS_ON(ERROR)); |
139 EXPECT_TRUE(LOG_IS_ON(FATAL)); | 139 EXPECT_TRUE(LOG_IS_ON(FATAL)); |
140 EXPECT_TRUE(LOG_IS_ON(DFATAL)); | 140 EXPECT_TRUE(LOG_IS_ON(DFATAL)); |
141 | 141 |
142 // LOG_IS_ON(FATAL) should always be true. | 142 // LOG_IS_ON(FATAL) should always be true. |
143 SetMinLogLevel(LOG_FATAL + 1); | 143 SetMinLogLevel(LOG_FATAL + 1); |
144 EXPECT_FALSE(LOG_IS_ON(INFO)); | 144 EXPECT_FALSE(LOG_IS_ON(INFO)); |
145 EXPECT_FALSE(LOG_IS_ON(WARNING)); | 145 EXPECT_FALSE(LOG_IS_ON(WARNING)); |
146 EXPECT_FALSE(LOG_IS_ON(ERROR)); | 146 EXPECT_FALSE(LOG_IS_ON(ERROR)); |
147 EXPECT_TRUE(LOG_IS_ON(FATAL)); | 147 EXPECT_TRUE(LOG_IS_ON(FATAL)); |
148 EXPECT_TRUE(kDfatalIsFatal == LOG_IS_ON(DFATAL)); | 148 EXPECT_EQ(kDfatalIsFatal, LOG_IS_ON(DFATAL)); |
149 } | 149 } |
150 | 150 |
151 TEST_F(LoggingTest, LoggingIsLazyBySeverity) { | 151 TEST_F(LoggingTest, LoggingIsLazyBySeverity) { |
152 MockLogSource mock_log_source; | 152 MockLogSource mock_log_source; |
153 EXPECT_CALL(mock_log_source, Log()).Times(0); | 153 EXPECT_CALL(mock_log_source, Log()).Times(0); |
154 | 154 |
155 SetMinLogLevel(LOG_WARNING); | 155 SetMinLogLevel(LOG_WARNING); |
156 | 156 |
157 EXPECT_FALSE(LOG_IS_ON(INFO)); | 157 EXPECT_FALSE(LOG_IS_ON(INFO)); |
158 EXPECT_FALSE(DLOG_IS_ON(INFO)); | 158 EXPECT_FALSE(DLOG_IS_ON(INFO)); |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 ASSERT_NE(0u, child_crash_addr_1); | 364 ASSERT_NE(0u, child_crash_addr_1); |
365 ASSERT_NE(0u, child_crash_addr_2); | 365 ASSERT_NE(0u, child_crash_addr_2); |
366 ASSERT_NE(0u, child_crash_addr_3); | 366 ASSERT_NE(0u, child_crash_addr_3); |
367 ASSERT_NE(child_crash_addr_1, child_crash_addr_2); | 367 ASSERT_NE(child_crash_addr_1, child_crash_addr_2); |
368 ASSERT_NE(child_crash_addr_1, child_crash_addr_3); | 368 ASSERT_NE(child_crash_addr_1, child_crash_addr_3); |
369 ASSERT_NE(child_crash_addr_2, child_crash_addr_3); | 369 ASSERT_NE(child_crash_addr_2, child_crash_addr_3); |
370 } | 370 } |
371 #endif // OS_POSIX | 371 #endif // OS_POSIX |
372 | 372 |
373 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { | 373 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { |
374 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 374 #if DCHECK_IS_ON() |
375 int debug_only_variable = 1; | 375 int debug_only_variable = 1; |
376 #endif | 376 #endif |
377 // These should avoid emitting references to |debug_only_variable| | 377 // These should avoid emitting references to |debug_only_variable| |
378 // in release mode. | 378 // in release mode. |
379 DLOG_IF(INFO, debug_only_variable) << "test"; | 379 DLOG_IF(INFO, debug_only_variable) << "test"; |
380 DLOG_ASSERT(debug_only_variable) << "test"; | 380 DLOG_ASSERT(debug_only_variable) << "test"; |
381 DPLOG_IF(INFO, debug_only_variable) << "test"; | 381 DPLOG_IF(INFO, debug_only_variable) << "test"; |
382 DVLOG_IF(1, debug_only_variable) << "test"; | 382 DVLOG_IF(1, debug_only_variable) << "test"; |
383 } | 383 } |
384 | 384 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); | 421 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
422 EXPECT_TRUE(DCHECK_IS_ON()); | 422 EXPECT_TRUE(DCHECK_IS_ON()); |
423 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 423 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
424 #else | 424 #else |
425 // Debug build. | 425 // Debug build. |
426 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); | 426 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
427 EXPECT_TRUE(DCHECK_IS_ON()); | 427 EXPECT_TRUE(DCHECK_IS_ON()); |
428 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 428 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
429 #endif | 429 #endif |
430 | 430 |
431 EXPECT_EQ(0, log_sink_call_count); | 431 EXPECT_EQ(0, g_log_sink_call_count); |
432 DCHECK(false); | 432 DCHECK(false); |
433 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 433 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, g_log_sink_call_count); |
434 DPCHECK(false); | 434 DPCHECK(false); |
435 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 435 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, g_log_sink_call_count); |
436 DCHECK_EQ(0, 1); | 436 DCHECK_EQ(0, 1); |
437 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count); | 437 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, g_log_sink_call_count); |
438 | 438 |
439 // Test DCHECK on std::nullptr_t | 439 // Test DCHECK on std::nullptr_t |
440 log_sink_call_count = 0; | 440 g_log_sink_call_count = 0; |
441 const void* p_null = nullptr; | 441 const void* p_null = nullptr; |
442 const void* p_not_null = &p_null; | 442 const void* p_not_null = &p_null; |
443 DCHECK_EQ(p_null, nullptr); | 443 DCHECK_EQ(p_null, nullptr); |
444 DCHECK_EQ(nullptr, p_null); | 444 DCHECK_EQ(nullptr, p_null); |
445 DCHECK_NE(p_not_null, nullptr); | 445 DCHECK_NE(p_not_null, nullptr); |
446 DCHECK_NE(nullptr, p_not_null); | 446 DCHECK_NE(nullptr, p_not_null); |
447 EXPECT_EQ(0, log_sink_call_count); | 447 EXPECT_EQ(0, g_log_sink_call_count); |
448 | 448 |
449 // Test DCHECK on a scoped enum. | 449 // Test DCHECK on a scoped enum. |
450 enum class Animal { DOG, CAT }; | 450 enum class Animal { DOG, CAT }; |
451 DCHECK_EQ(Animal::DOG, Animal::DOG); | 451 DCHECK_EQ(Animal::DOG, Animal::DOG); |
452 EXPECT_EQ(0, log_sink_call_count); | 452 EXPECT_EQ(0, g_log_sink_call_count); |
453 DCHECK_EQ(Animal::DOG, Animal::CAT); | 453 DCHECK_EQ(Animal::DOG, Animal::CAT); |
454 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 454 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, g_log_sink_call_count); |
455 | 455 |
456 // Test DCHECK on functions and function pointers. | 456 // Test DCHECK on functions and function pointers. |
457 log_sink_call_count = 0; | 457 g_log_sink_call_count = 0; |
458 struct MemberFunctions { | 458 struct MemberFunctions { |
459 void MemberFunction1() { | 459 void MemberFunction1() { |
460 // See the comment in DcheckEmptyFunction1(). | 460 // See the comment in DcheckEmptyFunction1(). |
461 LOG(INFO) << "Do not merge with MemberFunction2."; | 461 LOG(INFO) << "Do not merge with MemberFunction2."; |
462 } | 462 } |
463 void MemberFunction2() {} | 463 void MemberFunction2() {} |
464 }; | 464 }; |
465 void (MemberFunctions::*mp1)() = &MemberFunctions::MemberFunction1; | 465 void (MemberFunctions::*mp1)() = &MemberFunctions::MemberFunction1; |
466 void (MemberFunctions::*mp2)() = &MemberFunctions::MemberFunction2; | 466 void (MemberFunctions::*mp2)() = &MemberFunctions::MemberFunction2; |
467 void (*fp1)() = DcheckEmptyFunction1; | 467 void (*fp1)() = DcheckEmptyFunction1; |
468 void (*fp2)() = DcheckEmptyFunction2; | 468 void (*fp2)() = DcheckEmptyFunction2; |
469 void (*fp3)() = DcheckEmptyFunction1; | 469 void (*fp3)() = DcheckEmptyFunction1; |
470 DCHECK_EQ(fp1, fp3); | 470 DCHECK_EQ(fp1, fp3); |
471 EXPECT_EQ(0, log_sink_call_count); | 471 EXPECT_EQ(0, g_log_sink_call_count); |
472 DCHECK_EQ(mp1, &MemberFunctions::MemberFunction1); | 472 DCHECK_EQ(mp1, &MemberFunctions::MemberFunction1); |
473 EXPECT_EQ(0, log_sink_call_count); | 473 EXPECT_EQ(0, g_log_sink_call_count); |
474 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction2); | 474 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction2); |
475 EXPECT_EQ(0, log_sink_call_count); | 475 EXPECT_EQ(0, g_log_sink_call_count); |
476 DCHECK_EQ(fp1, fp2); | 476 DCHECK_EQ(fp1, fp2); |
477 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 477 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, g_log_sink_call_count); |
478 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction1); | 478 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction1); |
479 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 479 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, g_log_sink_call_count); |
480 } | 480 } |
481 | 481 |
482 TEST_F(LoggingTest, DcheckReleaseBehavior) { | 482 TEST_F(LoggingTest, DcheckReleaseBehavior) { |
483 int some_variable = 1; | 483 int some_variable = 1; |
484 // These should still reference |some_variable| so we don't get | 484 // These should still reference |some_variable| so we don't get |
485 // unused variable warnings. | 485 // unused variable warnings. |
486 DCHECK(some_variable) << "test"; | 486 DCHECK(some_variable) << "test"; |
487 DPCHECK(some_variable) << "test"; | 487 DPCHECK(some_variable) << "test"; |
488 DCHECK_EQ(some_variable, 1) << "test"; | 488 DCHECK_EQ(some_variable, 1) << "test"; |
489 } | 489 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 std::wstring wstr = L"Hello World"; | 564 std::wstring wstr = L"Hello World"; |
565 std::ostringstream ostr; | 565 std::ostringstream ostr; |
566 ostr << wstr; | 566 ostr << wstr; |
567 EXPECT_EQ("Hello World", ostr.str()); | 567 EXPECT_EQ("Hello World", ostr.str()); |
568 } | 568 } |
569 } // namespace nested_test | 569 } // namespace nested_test |
570 | 570 |
571 } // namespace | 571 } // namespace |
572 | 572 |
573 } // namespace logging | 573 } // namespace logging |
OLD | NEW |