Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2149)

Unified Diff: base/test/mock_log.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: revert slow, try adding back handlers Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/test/mock_log.cc
diff --git a/base/test/mock_log.cc b/base/test/mock_log.cc
index a09000d8ed7f9e3080ff06a64cb060c76fa591f6..7e0531cfc4c37bce3597848dc5afe6ca9be7f280 100644
--- a/base/test/mock_log.cc
+++ b/base/test/mock_log.cc
@@ -2,14 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/memory/ptr_util.h"
#include "base/test/mock_log.h"
namespace base {
namespace test {
-// static
-MockLog* MockLog::g_instance_ = nullptr;
-Lock MockLog::g_lock;
+class MockLogMessageListener : logging::LogMessageListener {
grt (UTC plus 2) 2017/01/04 09:12:19 " : public logging::LogMessageListener"
+ public:
+ MockLogMessageListener(MockLog* mock_log) : mock_log_(mock_log) {}
+ void OnMessage(int severity,
+ const char* file,
+ int line,
+ size_t message_start,
+ const std::string& str) override {
+ return mock_log_->Log(severity, file, line, message_start, str);
+ }
+
+ private:
+ MockLog* mock_log_;
+};
grt (UTC plus 2) 2017/01/04 09:12:19 DISALLOW_COPY_AND_ASSIGN (and #include "base/macro
MockLog::MockLog() : is_capturing_logs_(false) {
}
@@ -21,47 +33,23 @@ MockLog::~MockLog() {
}
void MockLog::StartCapturingLogs() {
- AutoLock scoped_lock(g_lock);
-
// We don't use CHECK(), which can generate a new LOG message, and
// thus can confuse MockLog objects or other registered
// LogSinks.
RAW_CHECK(!is_capturing_logs_);
- RAW_CHECK(!g_instance_);
is_capturing_logs_ = true;
- g_instance_ = this;
- previous_handler_ = logging::GetLogMessageHandler();
- logging::SetLogMessageHandler(LogMessageHandler);
+ listener_ = base::MakeUnique<MockLogMessageListener>(this);
}
void MockLog::StopCapturingLogs() {
- AutoLock scoped_lock(g_lock);
-
// We don't use CHECK(), which can generate a new LOG message, and
// 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_ = nullptr;
-}
-
-// static
-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://github.com/google/googlemock/blob/master/googlemock/docs/CookBook.md#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);
+ listener_.reset();
}
} // namespace test

Powered by Google App Engine
This is Rietveld 408576698