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