Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame^] | 1 | // |
| 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 Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 16 | |
| 17 | #include "shill/mock_log.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
Paul Stewart | dd374ca | 2013-04-15 13:59:57 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 23 | using std::string; |
Paul Stewart | dd374ca | 2013-04-15 13:59:57 -0700 | [diff] [blame] | 24 | using testing::_; |
| 25 | using testing::AnyNumber; |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 26 | |
| 27 | namespace shill { |
| 28 | |
Paul Stewart | 1e006c6 | 2015-06-16 12:29:06 -0700 | [diff] [blame] | 29 | ScopedMockLog* ScopedMockLog::instance_ = nullptr; |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 30 | |
| 31 | ScopedMockLog::ScopedMockLog() { |
| 32 | previous_handler_ = ::logging::GetLogMessageHandler(); |
| 33 | ::logging::SetLogMessageHandler(HandleLogMessages); |
| 34 | instance_ = this; |
| 35 | } |
| 36 | |
| 37 | ScopedMockLog::~ScopedMockLog() { |
| 38 | ::logging::SetLogMessageHandler(previous_handler_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 39 | instance_ = nullptr; |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // static |
| 43 | bool ScopedMockLog::HandleLogMessages(int severity, |
Paul Stewart | 1e006c6 | 2015-06-16 12:29:06 -0700 | [diff] [blame] | 44 | const char* file, |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 45 | int line, |
| 46 | size_t message_start, |
Paul Stewart | 1e006c6 | 2015-06-16 12:29:06 -0700 | [diff] [blame] | 47 | const string& full_message) { |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 48 | CHECK(instance_); |
| 49 | |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 50 | // |full_message| looks like this if it came through MemoryLog: |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame] | 51 | // "[0514/165501:INFO:mock_log_unittest.cc(22)] Some message\n" |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 52 | // The user wants to match just the substring "Some message". Strip off the |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame] | 53 | // extra stuff. |message_start| is the position where "Some message" begins. |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 54 | // |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 55 | // Note that the -1 is to remove the trailing return line. |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 56 | const string::size_type message_length = |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame] | 57 | full_message.length() - message_start - 1; |
| 58 | const string message(full_message, message_start, message_length); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 59 | |
| 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 Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 65 | if (instance_->previous_handler_) { |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 66 | 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 Stewart | dd374ca | 2013-04-15 13:59:57 -0700 | [diff] [blame] | 74 | NiceScopedMockLog::NiceScopedMockLog() : ScopedMockLog() { |
| 75 | EXPECT_CALL(*this, Log(_, _, _)).Times(AnyNumber()); |
| 76 | } |
| 77 | |
| 78 | NiceScopedMockLog::~NiceScopedMockLog() {} |
| 79 | |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 80 | } // namespace shill |