| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/logging/logging.h" | 5 #include "components/proximity_auth/logging/logging.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "components/proximity_auth/logging/log_buffer.h" | 13 #include "components/proximity_auth/logging/log_buffer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace proximity_auth { | 16 namespace proximity_auth { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kLog1[] = "Mahogony destined to make a sturdy table"; | 20 const char kLog1[] = "Mahogony destined to make a sturdy table"; |
| 20 const char kLog2[] = "Construction grade cedar"; | 21 const char kLog2[] = "Construction grade cedar"; |
| 21 const char kLog3[] = "Pine infested by hungry beetles"; | 22 const char kLog3[] = "Pine infested by hungry beetles"; |
| 22 | 23 |
| 23 // Called for every log message added to the standard logging system. The new | 24 // Called for every log message added to the standard logging system. The new |
| 24 // log is saved in |g_standard_logs| and consumed so it does not flood stdout. | 25 // log is saved in |g_standard_logs| and consumed so it does not flood stdout. |
| 25 base::LazyInstance<std::vector<std::string>> g_standard_logs = | 26 base::LazyInstance<std::vector<std::string>> g_standard_logs = |
| 26 LAZY_INSTANCE_INITIALIZER; | 27 LAZY_INSTANCE_INITIALIZER; |
| 27 bool HandleStandardLogMessage(int severity, | 28 class StandardLogMessageHandler : logging::LogMessageHandler { |
| 28 const char* file, | 29 bool OnMessage(int severity, |
| 29 int line, | 30 const char* file, |
| 30 size_t message_start, | 31 int line, |
| 31 const std::string& str) { | 32 size_t message_start, |
| 32 g_standard_logs.Get().push_back(str); | 33 const std::string& str) override { |
| 33 return true; | 34 g_standard_logs.Get().push_back(str); |
| 34 } | 35 return true; |
| 36 } |
| 37 }; |
| 35 | 38 |
| 36 } // namespace | 39 } // namespace |
| 37 | 40 |
| 38 class ProximityAuthLoggingTest : public testing::Test { | 41 class ProximityAuthLoggingTest : public testing::Test { |
| 39 public: | 42 public: |
| 40 ProximityAuthLoggingTest() : previous_handler_(NULL) {} | |
| 41 | |
| 42 void SetUp() override { | 43 void SetUp() override { |
| 43 LogBuffer::GetInstance()->Clear(); | 44 LogBuffer::GetInstance()->Clear(); |
| 44 g_standard_logs.Get().clear(); | 45 g_standard_logs.Get().clear(); |
| 45 | 46 |
| 46 previous_handler_ = logging::GetLogMessageHandler(); | 47 log_handler_ = base::MakeUnique<StandardLogMessageHandler>(); |
| 47 logging::SetLogMessageHandler(&HandleStandardLogMessage); | |
| 48 } | 48 } |
| 49 | 49 |
| 50 void TearDown() override { logging::SetLogMessageHandler(previous_handler_); } | 50 void TearDown() override { log_handler_.reset(); } |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 logging::LogMessageHandlerFunction previous_handler_; | 53 std::unique_ptr<StandardLogMessageHandler> log_handler_; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 TEST_F(ProximityAuthLoggingTest, LogsSavedToBuffer) { | 56 TEST_F(ProximityAuthLoggingTest, LogsSavedToBuffer) { |
| 57 int base_line_number = __LINE__; | 57 int base_line_number = __LINE__; |
| 58 PA_LOG(INFO) << kLog1; | 58 PA_LOG(INFO) << kLog1; |
| 59 PA_LOG(WARNING) << kLog2; | 59 PA_LOG(WARNING) << kLog2; |
| 60 PA_LOG(ERROR) << kLog3; | 60 PA_LOG(ERROR) << kLog3; |
| 61 | 61 |
| 62 auto* logs = LogBuffer::GetInstance()->logs(); | 62 auto* logs = LogBuffer::GetInstance()->logs(); |
| 63 ASSERT_EQ(3u, logs->size()); | 63 ASSERT_EQ(3u, logs->size()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 PA_LOG(WARNING) << kLog2; | 110 PA_LOG(WARNING) << kLog2; |
| 111 PA_LOG(ERROR) << kLog3; | 111 PA_LOG(ERROR) << kLog3; |
| 112 | 112 |
| 113 ASSERT_EQ(3u, g_standard_logs.Get().size()); | 113 ASSERT_EQ(3u, g_standard_logs.Get().size()); |
| 114 EXPECT_NE(std::string::npos, g_standard_logs.Get()[0].find(kLog1)); | 114 EXPECT_NE(std::string::npos, g_standard_logs.Get()[0].find(kLog1)); |
| 115 EXPECT_NE(std::string::npos, g_standard_logs.Get()[1].find(kLog2)); | 115 EXPECT_NE(std::string::npos, g_standard_logs.Get()[1].find(kLog2)); |
| 116 EXPECT_NE(std::string::npos, g_standard_logs.Get()[2].find(kLog3)); | 116 EXPECT_NE(std::string::npos, g_standard_logs.Get()[2].find(kLog3)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 } // namespace proximity_auth | 119 } // namespace proximity_auth |
| OLD | NEW |