OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/memory/ptr_util.h" |
5 #include "base/test/mock_log.h" | 6 #include "base/test/mock_log.h" |
6 | 7 |
7 namespace base { | 8 namespace base { |
8 namespace test { | 9 namespace test { |
9 | 10 |
10 // static | 11 class MockLogMessageListener : logging::LogMessageListener { |
11 MockLog* MockLog::g_instance_ = nullptr; | 12 public: |
12 Lock MockLog::g_lock; | 13 MockLogMessageListener(MockLog* mock_log): mock_log_(mock_log) {} |
| 14 void OnMessage(int severity, const char* file, int line, |
| 15 size_t message_start, const std::string& str) override { |
| 16 return mock_log_->Log(severity, file, line, message_start, str); |
| 17 } |
| 18 private: |
| 19 MockLog* mock_log_; |
| 20 }; |
13 | 21 |
14 MockLog::MockLog() : is_capturing_logs_(false) { | 22 MockLog::MockLog() : is_capturing_logs_(false) { |
15 } | 23 } |
16 | 24 |
17 MockLog::~MockLog() { | 25 MockLog::~MockLog() { |
18 if (is_capturing_logs_) { | 26 if (is_capturing_logs_) { |
19 StopCapturingLogs(); | 27 StopCapturingLogs(); |
20 } | 28 } |
21 } | 29 } |
22 | 30 |
23 void MockLog::StartCapturingLogs() { | 31 void MockLog::StartCapturingLogs() { |
24 AutoLock scoped_lock(g_lock); | |
25 | |
26 // We don't use CHECK(), which can generate a new LOG message, and | 32 // We don't use CHECK(), which can generate a new LOG message, and |
27 // thus can confuse MockLog objects or other registered | 33 // thus can confuse MockLog objects or other registered |
28 // LogSinks. | 34 // LogSinks. |
29 RAW_CHECK(!is_capturing_logs_); | 35 RAW_CHECK(!is_capturing_logs_); |
30 RAW_CHECK(!g_instance_); | |
31 | 36 |
32 is_capturing_logs_ = true; | 37 is_capturing_logs_ = true; |
33 g_instance_ = this; | 38 listener_ = base::MakeUnique<MockLogMessageListener>(this); |
34 previous_handler_ = logging::GetLogMessageHandler(); | |
35 logging::SetLogMessageHandler(LogMessageHandler); | |
36 } | 39 } |
37 | 40 |
38 void MockLog::StopCapturingLogs() { | 41 void MockLog::StopCapturingLogs() { |
39 AutoLock scoped_lock(g_lock); | |
40 | |
41 // We don't use CHECK(), which can generate a new LOG message, and | 42 // We don't use CHECK(), which can generate a new LOG message, and |
42 // thus can confuse MockLog objects or other registered | 43 // thus can confuse MockLog objects or other registered |
43 // LogSinks. | 44 // LogSinks. |
44 RAW_CHECK(is_capturing_logs_); | 45 RAW_CHECK(is_capturing_logs_); |
45 RAW_CHECK(g_instance_ == this); | |
46 | 46 |
47 is_capturing_logs_ = false; | 47 is_capturing_logs_ = false; |
48 logging::SetLogMessageHandler(previous_handler_); | 48 listener_.reset(); |
49 g_instance_ = nullptr; | |
50 } | |
51 | |
52 // static | |
53 bool MockLog::LogMessageHandler(int severity, | |
54 const char* file, | |
55 int line, | |
56 size_t message_start, | |
57 const std::string& str) { | |
58 // gMock guarantees thread-safety for calling a mocked method | |
59 // (https://github.com/google/googlemock/blob/master/googlemock/docs/CookBook.
md#using-google-mock-and-threads) | |
60 // but we also need to make sure that Start/StopCapturingLogs are synchronized | |
61 // with LogMessageHandler. | |
62 AutoLock scoped_lock(g_lock); | |
63 | |
64 return g_instance_->Log(severity, file, line, message_start, str); | |
65 } | 49 } |
66 | 50 |
67 } // namespace test | 51 } // namespace test |
68 } // namespace base | 52 } // namespace base |
OLD | NEW |