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.
5 #include "base/test/mock_log.h"
11 MockLog
* MockLog::g_instance_
= nullptr;
14 MockLog::MockLog() : is_capturing_logs_(false) {
18 if (is_capturing_logs_
) {
23 void MockLog::StartCapturingLogs() {
24 AutoLock
scoped_lock(g_lock
);
26 // We don't use CHECK(), which can generate a new LOG message, and
27 // thus can confuse MockLog objects or other registered
29 RAW_CHECK(!is_capturing_logs_
);
30 RAW_CHECK(!g_instance_
);
32 is_capturing_logs_
= true;
34 previous_handler_
= logging::GetLogMessageHandler();
35 logging::SetLogMessageHandler(LogMessageHandler
);
38 void MockLog::StopCapturingLogs() {
39 AutoLock
scoped_lock(g_lock
);
41 // We don't use CHECK(), which can generate a new LOG message, and
42 // thus can confuse MockLog objects or other registered
44 RAW_CHECK(is_capturing_logs_
);
45 RAW_CHECK(g_instance_
== this);
47 is_capturing_logs_
= false;
48 logging::SetLogMessageHandler(previous_handler_
);
49 g_instance_
= nullptr;
53 bool MockLog::LogMessageHandler(int severity
,
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_Threads)
60 // but we also need to make sure that Start/StopCapturingLogs are synchronized
61 // with LogMessageHandler.
62 AutoLock
scoped_lock(g_lock
);
64 return g_instance_
->Log(severity
, file
, line
, message_start
, str
);