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