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" |
| 6 #include "base/bind.h" |
| 7 #include "base/callback.h" |
5 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
6 #include "base/logging.h" | |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string_piece.h" |
8 | 11 |
9 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 #if defined(OS_POSIX) | 15 #if defined(OS_POSIX) |
13 #include <signal.h> | 16 #include <signal.h> |
14 #include <unistd.h> | 17 #include <unistd.h> |
15 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
16 #endif // OS_POSIX | 19 #endif // OS_POSIX |
17 | 20 |
18 #if defined(OS_LINUX) || defined(OS_ANDROID) | 21 #if defined(OS_LINUX) || defined(OS_ANDROID) |
19 #include <ucontext.h> | 22 #include <ucontext.h> |
20 #endif | 23 #endif |
21 | 24 |
22 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
23 #include <excpt.h> | 26 #include <excpt.h> |
24 #include <windows.h> | 27 #include <windows.h> |
25 #endif // OS_WIN | 28 #endif // OS_WIN |
26 | 29 |
27 namespace logging { | 30 namespace logging { |
28 | 31 |
29 namespace { | 32 namespace { |
30 | 33 |
31 using ::testing::Return; | 34 using ::testing::Return; |
| 35 using ::testing::_; |
32 | 36 |
33 // 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. |
34 int log_sink_call_count = 0; | 38 int log_sink_call_count = 0; |
35 | 39 |
36 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 40 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
37 void LogSink(const std::string& str) { | 41 void LogSink(const char* file, |
| 42 int line, |
| 43 const base::StringPiece message, |
| 44 const base::StringPiece stack_trace) { |
38 ++log_sink_call_count; | 45 ++log_sink_call_count; |
39 } | 46 } |
40 #endif | 47 #endif |
41 | 48 |
42 // 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 |
43 // contained (i.e., do not affect other unit tests). | 50 // contained (i.e., do not affect other unit tests). |
44 class LogStateSaver { | 51 class LogStateSaver { |
45 public: | 52 public: |
46 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} | 53 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} |
47 | 54 |
48 ~LogStateSaver() { | 55 ~LogStateSaver() { |
49 SetMinLogLevel(old_min_log_level_); | 56 SetMinLogLevel(old_min_log_level_); |
50 SetLogAssertHandler(NULL); | |
51 log_sink_call_count = 0; | 57 log_sink_call_count = 0; |
52 } | 58 } |
53 | 59 |
54 private: | 60 private: |
55 int old_min_log_level_; | 61 int old_min_log_level_; |
56 | 62 |
57 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | 63 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
58 }; | 64 }; |
59 | 65 |
60 class LoggingTest : public testing::Test { | 66 class LoggingTest : public testing::Test { |
61 private: | 67 private: |
62 LogStateSaver log_state_saver_; | 68 LogStateSaver log_state_saver_; |
63 }; | 69 }; |
64 | 70 |
65 class MockLogSource { | 71 class MockLogSource { |
66 public: | 72 public: |
67 MOCK_METHOD0(Log, const char*()); | 73 MOCK_METHOD0(Log, const char*()); |
68 }; | 74 }; |
69 | 75 |
| 76 class MockLogAssertHandler { |
| 77 public: |
| 78 MOCK_METHOD4( |
| 79 HandleLogAssert, |
| 80 void(const char*, int, const base::StringPiece, const base::StringPiece)); |
| 81 }; |
| 82 |
70 TEST_F(LoggingTest, BasicLogging) { | 83 TEST_F(LoggingTest, BasicLogging) { |
71 MockLogSource mock_log_source; | 84 MockLogSource mock_log_source; |
72 EXPECT_CALL(mock_log_source, Log()) | 85 EXPECT_CALL(mock_log_source, Log()) |
73 .Times(DCHECK_IS_ON() ? 16 : 8) | 86 .Times(DCHECK_IS_ON() ? 16 : 8) |
74 .WillRepeatedly(Return("log message")); | 87 .WillRepeatedly(Return("log message")); |
75 | 88 |
76 SetMinLogLevel(LOG_INFO); | 89 SetMinLogLevel(LOG_INFO); |
77 | 90 |
78 EXPECT_TRUE(LOG_IS_ON(INFO)); | 91 EXPECT_TRUE(LOG_IS_ON(INFO)); |
79 EXPECT_TRUE((DCHECK_IS_ON() != 0) == DLOG_IS_ON(INFO)); | 92 EXPECT_TRUE((DCHECK_IS_ON() != 0) == DLOG_IS_ON(INFO)); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 #define MAYBE_CheckStreamsAreLazy DISABLED_CheckStreamsAreLazy | 203 #define MAYBE_CheckStreamsAreLazy DISABLED_CheckStreamsAreLazy |
191 #else | 204 #else |
192 #define MAYBE_CheckStreamsAreLazy CheckStreamsAreLazy | 205 #define MAYBE_CheckStreamsAreLazy CheckStreamsAreLazy |
193 #endif | 206 #endif |
194 TEST_F(LoggingTest, MAYBE_CheckStreamsAreLazy) { | 207 TEST_F(LoggingTest, MAYBE_CheckStreamsAreLazy) { |
195 MockLogSource mock_log_source, uncalled_mock_log_source; | 208 MockLogSource mock_log_source, uncalled_mock_log_source; |
196 EXPECT_CALL(mock_log_source, Log()).Times(8). | 209 EXPECT_CALL(mock_log_source, Log()).Times(8). |
197 WillRepeatedly(Return("check message")); | 210 WillRepeatedly(Return("check message")); |
198 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); | 211 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); |
199 | 212 |
200 SetLogAssertHandler(&LogSink); | 213 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
201 | 214 |
202 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); | 215 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); |
203 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); | 216 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); |
204 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) | 217 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) |
205 << uncalled_mock_log_source.Log(); | 218 << uncalled_mock_log_source.Log(); |
206 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) | 219 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) |
207 << mock_log_source.Log(); | 220 << mock_log_source.Log(); |
208 } | 221 } |
209 | 222 |
210 #endif | 223 #endif |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 #else | 411 #else |
399 #define MAYBE_Dcheck Dcheck | 412 #define MAYBE_Dcheck Dcheck |
400 #endif | 413 #endif |
401 TEST_F(LoggingTest, MAYBE_Dcheck) { | 414 TEST_F(LoggingTest, MAYBE_Dcheck) { |
402 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 415 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
403 // Release build. | 416 // Release build. |
404 EXPECT_FALSE(DCHECK_IS_ON()); | 417 EXPECT_FALSE(DCHECK_IS_ON()); |
405 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 418 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
406 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) | 419 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
407 // Release build with real DCHECKS. | 420 // Release build with real DCHECKS. |
408 SetLogAssertHandler(&LogSink); | 421 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
409 EXPECT_TRUE(DCHECK_IS_ON()); | 422 EXPECT_TRUE(DCHECK_IS_ON()); |
410 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 423 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
411 #else | 424 #else |
412 // Debug build. | 425 // Debug build. |
413 SetLogAssertHandler(&LogSink); | 426 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
414 EXPECT_TRUE(DCHECK_IS_ON()); | 427 EXPECT_TRUE(DCHECK_IS_ON()); |
415 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 428 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
416 #endif | 429 #endif |
417 | 430 |
418 EXPECT_EQ(0, log_sink_call_count); | 431 EXPECT_EQ(0, log_sink_call_count); |
419 DCHECK(false); | 432 DCHECK(false); |
420 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 433 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
421 DPCHECK(false); | 434 DPCHECK(false); |
422 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | 435 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); |
423 DCHECK_EQ(0, 1); | 436 DCHECK_EQ(0, 1); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 if (false) | 505 if (false) |
493 CHECK_EQ(false, true); // Unreached. | 506 CHECK_EQ(false, true); // Unreached. |
494 else | 507 else |
495 CHECK_EQ(true, reached = true); // Reached, passed. | 508 CHECK_EQ(true, reached = true); // Reached, passed. |
496 ASSERT_TRUE(reached); | 509 ASSERT_TRUE(reached); |
497 | 510 |
498 if (false) | 511 if (false) |
499 CHECK_EQ(false, true); // Unreached. | 512 CHECK_EQ(false, true); // Unreached. |
500 } | 513 } |
501 | 514 |
| 515 TEST_F(LoggingTest, NestedLogAssertHandlers) { |
| 516 ::testing::InSequence dummy; |
| 517 ::testing::StrictMock<MockLogAssertHandler> handler_a, handler_b; |
| 518 |
| 519 EXPECT_CALL( |
| 520 handler_a, |
| 521 HandleLogAssert( |
| 522 _, _, |
| 523 base::StringPiece( |
| 524 "Check failed: false. First assert must be catched by handler_a"), |
| 525 _)); |
| 526 EXPECT_CALL( |
| 527 handler_b, |
| 528 HandleLogAssert(_, _, |
| 529 base::StringPiece("Check failed: false. Second assert " |
| 530 "must be catched by handler_b"), |
| 531 _)); |
| 532 EXPECT_CALL( |
| 533 handler_a, |
| 534 HandleLogAssert(_, _, |
| 535 base::StringPiece("Check failed: false. Last assert " |
| 536 "must be catched by handler_a again"), |
| 537 _)); |
| 538 |
| 539 logging::ScopedLogAssertHandler scoped_handler_a(base::Bind( |
| 540 &MockLogAssertHandler::HandleLogAssert, base::Unretained(&handler_a))); |
| 541 |
| 542 CHECK(false) << "First assert must be catched by handler_a"; |
| 543 |
| 544 { |
| 545 logging::ScopedLogAssertHandler scoped_handler_b(base::Bind( |
| 546 &MockLogAssertHandler::HandleLogAssert, base::Unretained(&handler_b))); |
| 547 CHECK(false) << "Second assert must be catched by handler_b"; |
| 548 } |
| 549 |
| 550 CHECK(false) << "Last assert must be catched by handler_a again"; |
| 551 } |
| 552 |
502 // Test that defining an operator<< for a type in a namespace doesn't prevent | 553 // Test that defining an operator<< for a type in a namespace doesn't prevent |
503 // other code in that namespace from calling the operator<<(ostream, wstring) | 554 // other code in that namespace from calling the operator<<(ostream, wstring) |
504 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be | 555 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be |
505 // found by ADL, since defining another operator<< prevents name lookup from | 556 // found by ADL, since defining another operator<< prevents name lookup from |
506 // looking in the global namespace. | 557 // looking in the global namespace. |
507 namespace nested_test { | 558 namespace nested_test { |
508 class Streamable {}; | 559 class Streamable {}; |
509 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, | 560 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, |
510 const Streamable&) { | 561 const Streamable&) { |
511 return out << "Streamable"; | 562 return out << "Streamable"; |
512 } | 563 } |
513 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { | 564 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { |
514 std::wstring wstr = L"Hello World"; | 565 std::wstring wstr = L"Hello World"; |
515 std::ostringstream ostr; | 566 std::ostringstream ostr; |
516 ostr << wstr; | 567 ostr << wstr; |
517 EXPECT_EQ("Hello World", ostr.str()); | 568 EXPECT_EQ("Hello World", ostr.str()); |
518 } | 569 } |
519 } // namespace nested_test | 570 } // namespace nested_test |
520 | 571 |
521 } // namespace | 572 } // namespace |
522 | 573 |
523 } // namespace logging | 574 } // namespace logging |
OLD | NEW |