blob: 7cf1488a7ebaacc3866bb086e778c8ad33d57884 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Gary Morain8a5726a2012-05-15 10:56:49 -070016
17#include "shill/mock_log.h"
18
19#include <string>
20
Paul Stewartdd374ca2013-04-15 13:59:57 -070021#include <gtest/gtest.h>
22
Gary Morain8a5726a2012-05-15 10:56:49 -070023using std::string;
Paul Stewartdd374ca2013-04-15 13:59:57 -070024using testing::_;
25using testing::AnyNumber;
Gary Morain8a5726a2012-05-15 10:56:49 -070026
27namespace shill {
28
Paul Stewart1e006c62015-06-16 12:29:06 -070029ScopedMockLog* ScopedMockLog::instance_ = nullptr;
Gary Morain8a5726a2012-05-15 10:56:49 -070030
31ScopedMockLog::ScopedMockLog() {
32 previous_handler_ = ::logging::GetLogMessageHandler();
33 ::logging::SetLogMessageHandler(HandleLogMessages);
34 instance_ = this;
35}
36
37ScopedMockLog::~ScopedMockLog() {
38 ::logging::SetLogMessageHandler(previous_handler_);
Ben Chancc225ef2014-09-30 13:26:51 -070039 instance_ = nullptr;
Gary Morain8a5726a2012-05-15 10:56:49 -070040}
41
42// static
43bool ScopedMockLog::HandleLogMessages(int severity,
Paul Stewart1e006c62015-06-16 12:29:06 -070044 const char* file,
Gary Morain8a5726a2012-05-15 10:56:49 -070045 int line,
46 size_t message_start,
Paul Stewart1e006c62015-06-16 12:29:06 -070047 const string& full_message) {
Gary Morain8a5726a2012-05-15 10:56:49 -070048 CHECK(instance_);
49
Christopher Wileyd7783522012-08-10 14:21:47 -070050 // |full_message| looks like this if it came through MemoryLog:
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070051 // "[0514/165501:INFO:mock_log_unittest.cc(22)] Some message\n"
Gary Morain8a5726a2012-05-15 10:56:49 -070052 // The user wants to match just the substring "Some message". Strip off the
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070053 // extra stuff. |message_start| is the position where "Some message" begins.
Christopher Wileyd7783522012-08-10 14:21:47 -070054 //
Christopher Wileyd7783522012-08-10 14:21:47 -070055 // Note that the -1 is to remove the trailing return line.
Gary Morain8a5726a2012-05-15 10:56:49 -070056 const string::size_type message_length =
Christopher Wiley6e8c54b2012-08-15 16:12:30 -070057 full_message.length() - message_start - 1;
58 const string message(full_message, message_start, message_length);
Gary Morain8a5726a2012-05-15 10:56:49 -070059
60 // Call Log. Because Log is a mock method, this sets in motion the mocking
61 // magic.
62 instance_->Log(severity, file, message);
63
64 // Invoke the previously installed message handler if there was one.
Ben Chancc225ef2014-09-30 13:26:51 -070065 if (instance_->previous_handler_) {
Gary Morain8a5726a2012-05-15 10:56:49 -070066 return (*instance_->previous_handler_)(severity, file, line,
67 message_start, full_message);
68 }
69
70 // Return false so that messages show up on stderr.
71 return false;
72}
73
Paul Stewartdd374ca2013-04-15 13:59:57 -070074NiceScopedMockLog::NiceScopedMockLog() : ScopedMockLog() {
75 EXPECT_CALL(*this, Log(_, _, _)).Times(AnyNumber());
76}
77
78NiceScopedMockLog::~NiceScopedMockLog() {}
79
Gary Morain8a5726a2012-05-15 10:56:49 -070080} // namespace shill