Index: base/test/mock_log.cc |
diff --git a/net/test/scoped_mock_log.cc b/base/test/mock_log.cc |
similarity index 43% |
rename from net/test/scoped_mock_log.cc |
rename to base/test/mock_log.cc |
index 3bc99cb3d42b005e91af466f10224cd018c080d5..fa511d44d08645e4f45b8cc33622205239441c1f 100644 |
--- a/net/test/scoped_mock_log.cc |
+++ b/base/test/mock_log.cc |
@@ -1,28 +1,30 @@ |
-// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "net/test/scoped_mock_log.h" |
+#include "base/test/mock_log.h" |
-#include "base/logging.h" |
- |
-namespace net { |
+namespace base { |
namespace test { |
// static |
-ScopedMockLog* ScopedMockLog::g_instance_ = NULL; |
+MockLog* MockLog::g_instance_ = nullptr; |
+Lock MockLog::g_lock; |
-ScopedMockLog::ScopedMockLog() : is_capturing_logs_(false) {} |
+MockLog::MockLog() : is_capturing_logs_(false) { |
+} |
-ScopedMockLog::~ScopedMockLog() { |
+MockLog::~MockLog() { |
if (is_capturing_logs_) { |
StopCapturingLogs(); |
} |
} |
-void ScopedMockLog::StartCapturingLogs() { |
+void MockLog::StartCapturingLogs() { |
+ AutoLock scoped_lock(g_lock); |
+ |
// We don't use CHECK(), which can generate a new LOG message, and |
- // thus can confuse ScopedMockLog objects or other registered |
+ // thus can confuse MockLog objects or other registered |
// LogSinks. |
RAW_CHECK(!is_capturing_logs_); |
RAW_CHECK(!g_instance_); |
@@ -33,26 +35,34 @@ void ScopedMockLog::StartCapturingLogs() { |
logging::SetLogMessageHandler(LogMessageHandler); |
} |
-void ScopedMockLog::StopCapturingLogs() { |
+void MockLog::StopCapturingLogs() { |
+ AutoLock scoped_lock(g_lock); |
+ |
// We don't use CHECK(), which can generate a new LOG message, and |
- // thus can confuse ScopedMockLog objects or other registered |
+ // thus can confuse MockLog objects or other registered |
// LogSinks. |
RAW_CHECK(is_capturing_logs_); |
RAW_CHECK(g_instance_ == this); |
is_capturing_logs_ = false; |
logging::SetLogMessageHandler(previous_handler_); |
- g_instance_ = NULL; |
+ g_instance_ = nullptr; |
} |
// static |
-bool ScopedMockLog::LogMessageHandler(int severity, |
- const char* file, |
- int line, |
- size_t message_start, |
- const std::string& str) { |
+bool MockLog::LogMessageHandler(int severity, |
+ const char* file, |
+ int line, |
+ size_t message_start, |
+ const std::string& str) { |
+ // gMock guarantees thread-safety for calling a mocked method |
+ // (https://code.google.com/p/googlemock/wiki/CookBook#Using_Google_Mock_and_Threads) |
+ // but we also need to make sure that Start/StopCapturingLogs are synchronized |
+ // with LogMessageHandler. |
+ AutoLock scoped_lock(g_lock); |
+ |
return g_instance_->Log(severity, file, line, message_start, str); |
} |
} // namespace test |
-} // namespace net |
+} // namespace base |