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

Side by Side 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: clean up, MockLog uses listener Created 4 years, 5 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 unified diff | Download patch
OLDNEW
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/test/mock_log.h" 5 #include "base/test/mock_log.h"
6 6
7 namespace base { 7 namespace base {
8 namespace test { 8 namespace test {
9 9
10 // static 10 // static
11 MockLog* MockLog::g_instance_ = nullptr; 11 MockLog* MockLog::g_instance_ = nullptr;
12 Lock MockLog::g_lock; 12 Lock MockLog::g_lock;
Dan Beam 2016/07/15 03:11:19 does this class still need to stay global in natur
wychen 2016/07/18 15:44:12 Since LazyInstance is thread safe, I think we don'
Dan Beam 2016/07/18 20:00:35 right, but this is a raw pointer, not a LazyInstan
wychen 2016/07/19 18:13:06 You are right. g_instance_ is still a raw pointer.
13 13
14 MockLog::MockLog() : is_capturing_logs_(false) { 14 MockLog::MockLog() : is_capturing_logs_(false) {
15 } 15 }
16 16
17 MockLog::~MockLog() { 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 MockLog::StartCapturingLogs() { 23 void MockLog::StartCapturingLogs() {
24 AutoLock scoped_lock(g_lock); 24 AutoLock scoped_lock(g_lock);
25 25
26 // 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
27 // thus can confuse MockLog objects or other registered 27 // thus can confuse MockLog objects or other registered
28 // LogSinks. 28 // LogSinks.
29 RAW_CHECK(!is_capturing_logs_); 29 RAW_CHECK(!is_capturing_logs_);
30 RAW_CHECK(!g_instance_); 30 RAW_CHECK(!g_instance_);
31 31
32 is_capturing_logs_ = true; 32 is_capturing_logs_ = true;
33 g_instance_ = this; 33 g_instance_ = this;
34 previous_handler_ = logging::GetLogMessageHandler(); 34 logging::AddLogMessageListener(LogMessageListener);
35 logging::SetLogMessageHandler(LogMessageHandler);
36 } 35 }
37 36
38 void MockLog::StopCapturingLogs() { 37 void MockLog::StopCapturingLogs() {
39 AutoLock scoped_lock(g_lock); 38 AutoLock scoped_lock(g_lock);
40 39
41 // 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
42 // thus can confuse MockLog objects or other registered 41 // thus can confuse MockLog objects or other registered
43 // LogSinks. 42 // LogSinks.
44 RAW_CHECK(is_capturing_logs_); 43 RAW_CHECK(is_capturing_logs_);
45 RAW_CHECK(g_instance_ == this); 44 RAW_CHECK(g_instance_ == this);
46 45
47 is_capturing_logs_ = false; 46 is_capturing_logs_ = false;
48 logging::SetLogMessageHandler(previous_handler_); 47 logging::RemoveLogMessageListener(LogMessageListener);
49 g_instance_ = nullptr; 48 g_instance_ = nullptr;
50 } 49 }
51 50
52 // static 51 // static
53 bool MockLog::LogMessageHandler(int severity, 52 void MockLog::LogMessageListener(int severity,
54 const char* file, 53 const char* file,
55 int line, 54 int line,
56 size_t message_start, 55 size_t message_start,
57 const std::string& str) { 56 const std::string& str) {
58 // gMock guarantees thread-safety for calling a mocked method 57 // 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) 58 // (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 59 // but we also need to make sure that Start/StopCapturingLogs are synchronized
61 // with LogMessageHandler. 60 // with LogMessageHandler.
62 AutoLock scoped_lock(g_lock); 61 AutoLock scoped_lock(g_lock);
63 62
64 return g_instance_->Log(severity, file, line, message_start, str); 63 return g_instance_->Log(severity, file, line, message_start, str);
65 } 64 }
66 65
67 } // namespace test 66 } // namespace test
68 } // namespace base 67 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698