blob: 3679c1ea89d11c90b16c588ece2ebef2d9a0f0ca [file] [log] [blame]
Gary Morain8a5726a2012-05-15 10:56:49 -07001// Copyright (c) 2012 The Chromium OS 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 "shill/mock_log.h"
6
7#include <string>
8
Paul Stewartdd374ca2013-04-15 13:59:57 -07009#include <gtest/gtest.h>
10
Gary Morain8a5726a2012-05-15 10:56:49 -070011using std::string;
Paul Stewartdd374ca2013-04-15 13:59:57 -070012using testing::_;
13using testing::AnyNumber;
Gary Morain8a5726a2012-05-15 10:56:49 -070014
15namespace shill {
16
Ben Chancc225ef2014-09-30 13:26:51 -070017ScopedMockLog *ScopedMockLog::instance_ = nullptr;
Gary Morain8a5726a2012-05-15 10:56:49 -070018
19ScopedMockLog::ScopedMockLog() {
20 previous_handler_ = ::logging::GetLogMessageHandler();
21 ::logging::SetLogMessageHandler(HandleLogMessages);
22 instance_ = this;
23}
24
25ScopedMockLog::~ScopedMockLog() {
26 ::logging::SetLogMessageHandler(previous_handler_);
Ben Chancc225ef2014-09-30 13:26:51 -070027 instance_ = nullptr;
Gary Morain8a5726a2012-05-15 10:56:49 -070028}
29
30// static
31bool ScopedMockLog::HandleLogMessages(int severity,
32 const char *file,
33 int line,
34 size_t message_start,
35 const string &full_message) {
36 CHECK(instance_);
37
Christopher Wileyd7783522012-08-10 14:21:47 -070038 // |full_message| looks like this if it came through MemoryLog:
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070039 // "[0514/165501:INFO:mock_log_unittest.cc(22)] Some message\n"
Gary Morain8a5726a2012-05-15 10:56:49 -070040 // The user wants to match just the substring "Some message". Strip off the
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070041 // extra stuff. |message_start| is the position where "Some message" begins.
Christopher Wileyd7783522012-08-10 14:21:47 -070042 //
Christopher Wileyd7783522012-08-10 14:21:47 -070043 // Note that the -1 is to remove the trailing return line.
Gary Morain8a5726a2012-05-15 10:56:49 -070044 const string::size_type message_length =
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070045 full_message.length() - message_start - 1;
46 const string message(full_message, message_start, message_length);
Gary Morain8a5726a2012-05-15 10:56:49 -070047
48 // Call Log. Because Log is a mock method, this sets in motion the mocking
49 // magic.
50 instance_->Log(severity, file, message);
51
52 // Invoke the previously installed message handler if there was one.
Ben Chancc225ef2014-09-30 13:26:51 -070053 if (instance_->previous_handler_) {
Gary Morain8a5726a2012-05-15 10:56:49 -070054 return (*instance_->previous_handler_)(severity, file, line,
55 message_start, full_message);
56 }
57
58 // Return false so that messages show up on stderr.
59 return false;
60}
61
Paul Stewartdd374ca2013-04-15 13:59:57 -070062NiceScopedMockLog::NiceScopedMockLog() : ScopedMockLog() {
63 EXPECT_CALL(*this, Log(_, _, _)).Times(AnyNumber());
64}
65
66NiceScopedMockLog::~NiceScopedMockLog() {}
67
Gary Morain8a5726a2012-05-15 10:56:49 -070068} // namespace shill