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|. |
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 StandardLogMessageListener : logging::LogMessageListener { |
28 const char* file, | 29 void 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 } |
| 36 }; |
35 | 37 |
36 } // namespace | 38 } // namespace |
37 | 39 |
38 class ProximityAuthLoggingTest : public testing::Test { | 40 class ProximityAuthLoggingTest : public testing::Test { |
39 public: | 41 public: |
40 ProximityAuthLoggingTest() : previous_handler_(NULL) {} | |
41 | |
42 void SetUp() override { | 42 void SetUp() override { |
43 LogBuffer::GetInstance()->Clear(); | 43 LogBuffer::GetInstance()->Clear(); |
44 g_standard_logs.Get().clear(); | 44 g_standard_logs.Get().clear(); |
45 | 45 |
46 previous_handler_ = logging::GetLogMessageHandler(); | 46 log_listener_ = base::MakeUnique<StandardLogMessageListener>(); |
47 logging::SetLogMessageHandler(&HandleStandardLogMessage); | |
48 } | 47 } |
49 | 48 |
50 void TearDown() override { logging::SetLogMessageHandler(previous_handler_); } | 49 void TearDown() override { log_listener_.reset(); } |
51 | 50 |
52 private: | 51 private: |
53 logging::LogMessageHandlerFunction previous_handler_; | 52 std::unique_ptr<StandardLogMessageListener> log_listener_; |
54 }; | 53 }; |
55 | 54 |
56 TEST_F(ProximityAuthLoggingTest, LogsSavedToBuffer) { | 55 TEST_F(ProximityAuthLoggingTest, LogsSavedToBuffer) { |
57 int base_line_number = __LINE__; | 56 int base_line_number = __LINE__; |
58 PA_LOG(INFO) << kLog1; | 57 PA_LOG(INFO) << kLog1; |
59 PA_LOG(WARNING) << kLog2; | 58 PA_LOG(WARNING) << kLog2; |
60 PA_LOG(ERROR) << kLog3; | 59 PA_LOG(ERROR) << kLog3; |
61 | 60 |
62 auto* logs = LogBuffer::GetInstance()->logs(); | 61 auto* logs = LogBuffer::GetInstance()->logs(); |
63 ASSERT_EQ(3u, logs->size()); | 62 ASSERT_EQ(3u, logs->size()); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 PA_LOG(WARNING) << kLog2; | 109 PA_LOG(WARNING) << kLog2; |
111 PA_LOG(ERROR) << kLog3; | 110 PA_LOG(ERROR) << kLog3; |
112 | 111 |
113 ASSERT_EQ(3u, g_standard_logs.Get().size()); | 112 ASSERT_EQ(3u, g_standard_logs.Get().size()); |
114 EXPECT_NE(std::string::npos, g_standard_logs.Get()[0].find(kLog1)); | 113 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)); | 114 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)); | 115 EXPECT_NE(std::string::npos, g_standard_logs.Get()[2].find(kLog3)); |
117 } | 116 } |
118 | 117 |
119 } // namespace proximity_auth | 118 } // namespace proximity_auth |
OLD | NEW |