Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 1 | // 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 Stewart | dd374ca | 2013-04-15 13:59:57 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 11 | using std::string; |
Paul Stewart | dd374ca | 2013-04-15 13:59:57 -0700 | [diff] [blame] | 12 | using testing::_; |
| 13 | using testing::AnyNumber; |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 14 | |
| 15 | namespace shill { |
| 16 | |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 17 | ScopedMockLog *ScopedMockLog::instance_ = nullptr; |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 18 | |
| 19 | ScopedMockLog::ScopedMockLog() { |
| 20 | previous_handler_ = ::logging::GetLogMessageHandler(); |
| 21 | ::logging::SetLogMessageHandler(HandleLogMessages); |
| 22 | instance_ = this; |
| 23 | } |
| 24 | |
| 25 | ScopedMockLog::~ScopedMockLog() { |
| 26 | ::logging::SetLogMessageHandler(previous_handler_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 27 | instance_ = nullptr; |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // static |
| 31 | bool 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 Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 38 | // |full_message| looks like this if it came through MemoryLog: |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame] | 39 | // "[0514/165501:INFO:mock_log_unittest.cc(22)] Some message\n" |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 40 | // 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] | 41 | // extra stuff. |message_start| is the position where "Some message" begins. |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 42 | // |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 43 | // Note that the -1 is to remove the trailing return line. |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 44 | const string::size_type message_length = |
Christopher Wiley | 6e8c54b | 2012-08-15 16:12:30 -0700 | [diff] [blame] | 45 | full_message.length() - message_start - 1; |
| 46 | const string message(full_message, message_start, message_length); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 47 | |
| 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 Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 53 | if (instance_->previous_handler_) { |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 54 | 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 Stewart | dd374ca | 2013-04-15 13:59:57 -0700 | [diff] [blame] | 62 | NiceScopedMockLog::NiceScopedMockLog() : ScopedMockLog() { |
| 63 | EXPECT_CALL(*this, Log(_, _, _)).Times(AnyNumber()); |
| 64 | } |
| 65 | |
| 66 | NiceScopedMockLog::~NiceScopedMockLog() {} |
| 67 | |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 68 | } // namespace shill |