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

Side by Side Diff: base/test/mock_log.cc

Issue 988693005: Chromium roll (https://codereview.chromium.org/976353002) (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fixed bad android build patch Created 5 years, 9 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/test/test_mock_time_task_runner.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/test/mock_log.h"
6
7 namespace base {
8 namespace test {
9
10 // static
11 MockLog* MockLog::g_instance_ = nullptr;
12 Lock MockLog::g_lock;
13
14 MockLog::MockLog() : is_capturing_logs_(false) {
15 }
16
17 MockLog::~MockLog() {
18 if (is_capturing_logs_) {
19 StopCapturingLogs();
20 }
21 }
22
23 void MockLog::StartCapturingLogs() {
24 AutoLock scoped_lock(g_lock);
25
26 // We don't use CHECK(), which can generate a new LOG message, and
27 // thus can confuse MockLog objects or other registered
28 // LogSinks.
29 RAW_CHECK(!is_capturing_logs_);
30 RAW_CHECK(!g_instance_);
31
32 is_capturing_logs_ = true;
33 g_instance_ = this;
34 previous_handler_ = logging::GetLogMessageHandler();
35 logging::SetLogMessageHandler(LogMessageHandler);
36 }
37
38 void MockLog::StopCapturingLogs() {
39 AutoLock scoped_lock(g_lock);
40
41 // We don't use CHECK(), which can generate a new LOG message, and
42 // thus can confuse MockLog objects or other registered
43 // LogSinks.
44 RAW_CHECK(is_capturing_logs_);
45 RAW_CHECK(g_instance_ == this);
46
47 is_capturing_logs_ = false;
48 logging::SetLogMessageHandler(previous_handler_);
49 g_instance_ = nullptr;
50 }
51
52 // static
53 bool MockLog::LogMessageHandler(int severity,
54 const char* file,
55 int line,
56 size_t message_start,
57 const std::string& str) {
58 // gMock guarantees thread-safety for calling a mocked method
59 // (https://code.google.com/p/googlemock/wiki/CookBook#Using_Google_Mock_and_T hreads)
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);
65 }
66
67 } // namespace test
68 } // namespace base
OLDNEW
« no previous file with comments | « base/test/mock_log.h ('k') | base/test/test_mock_time_task_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698