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

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: address comments, format 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
« no previous file with comments | « base/test/mock_log.h ('k') | base/trace_event/trace_event_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
13 12
14 MockLog::MockLog() : is_capturing_logs_(false) { 13 MockLog::MockLog() : is_capturing_logs_(false) {
15 } 14 }
Dan Beam 2016/07/19 19:07:23 why can't the entirety of this file basically be:
Dan Beam 2016/07/19 19:07:49 Remove*
wychen 2016/07/19 20:56:22 I think the reason is that the {Add,Remove}LogMess
16 15
17 MockLog::~MockLog() { 16 MockLog::~MockLog() {
18 if (is_capturing_logs_) { 17 if (is_capturing_logs_) {
19 StopCapturingLogs(); 18 StopCapturingLogs();
20 } 19 }
21 } 20 }
22 21
23 void MockLog::StartCapturingLogs() { 22 void MockLog::StartCapturingLogs() {
24 AutoLock scoped_lock(g_lock);
25
26 // We don't use CHECK(), which can generate a new LOG message, and 23 // We don't use CHECK(), which can generate a new LOG message, and
27 // thus can confuse MockLog objects or other registered 24 // thus can confuse MockLog objects or other registered
28 // LogSinks. 25 // LogSinks.
29 RAW_CHECK(!is_capturing_logs_); 26 RAW_CHECK(!is_capturing_logs_);
30 RAW_CHECK(!g_instance_); 27 RAW_CHECK(!g_instance_);
31 28
32 is_capturing_logs_ = true; 29 is_capturing_logs_ = true;
33 g_instance_ = this; 30 g_instance_ = this;
34 previous_handler_ = logging::GetLogMessageHandler(); 31 logging::AddLogMessageListener(LogMessageListener);
35 logging::SetLogMessageHandler(LogMessageHandler);
36 } 32 }
37 33
38 void MockLog::StopCapturingLogs() { 34 void MockLog::StopCapturingLogs() {
39 AutoLock scoped_lock(g_lock);
40
41 // We don't use CHECK(), which can generate a new LOG message, and 35 // We don't use CHECK(), which can generate a new LOG message, and
42 // thus can confuse MockLog objects or other registered 36 // thus can confuse MockLog objects or other registered
43 // LogSinks. 37 // LogSinks.
44 RAW_CHECK(is_capturing_logs_); 38 RAW_CHECK(is_capturing_logs_);
45 RAW_CHECK(g_instance_ == this); 39 RAW_CHECK(g_instance_ == this);
46 40
47 is_capturing_logs_ = false; 41 is_capturing_logs_ = false;
48 logging::SetLogMessageHandler(previous_handler_); 42 logging::RemoveLogMessageListener(LogMessageListener);
49 g_instance_ = nullptr; 43 g_instance_ = nullptr;
50 } 44 }
51 45
52 // static 46 // static
53 bool MockLog::LogMessageHandler(int severity, 47 void MockLog::LogMessageListener(int severity,
54 const char* file, 48 const char* file,
55 int line, 49 int line,
56 size_t message_start, 50 size_t message_start,
57 const std::string& str) { 51 const std::string& str) {
58 // 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)
60 // but we also need to make sure that Start/StopCapturingLogs are synchronized
61 // with LogMessageHandler.
62 AutoLock scoped_lock(g_lock);
63
64 return g_instance_->Log(severity, file, line, message_start, str); 52 return g_instance_->Log(severity, file, line, message_start, str);
65 } 53 }
66 54
67 } // namespace test 55 } // namespace test
68 } // namespace base 56 } // namespace base
OLDNEW
« no previous file with comments | « base/test/mock_log.h ('k') | base/trace_event/trace_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698