| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 private: | 30 private: |
| 31 void OnLogMessage(const std::string& message) override { | 31 void OnLogMessage(const std::string& message) override { |
| 32 static_cast<Base*>(this)->WriteAll( | 32 static_cast<Base*>(this)->WriteAll( |
| 33 message.data(), message.size(), nullptr, nullptr); | 33 message.data(), message.size(), nullptr, nullptr); |
| 34 } | 34 } |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 // Test basic logging operation. We should get the INFO log but not the VERBOSE. | 37 // Test basic logging operation. We should get the INFO log but not the VERBOSE. |
| 38 // We should restore the correct global state at the end. | 38 // We should restore the correct global state at the end. |
| 39 TEST(LogTest, SingleStream) { | 39 TEST(LogTest, SingleStream) { |
| 40 int sev = LogMessage::GetLogToStream(NULL); | 40 int sev = LogMessage::GetLogToStream(nullptr); |
| 41 | 41 |
| 42 std::string str; | 42 std::string str; |
| 43 LogSinkImpl<StringStream> stream(&str); | 43 LogSinkImpl<StringStream> stream(&str); |
| 44 LogMessage::AddLogToStream(&stream, LS_INFO); | 44 LogMessage::AddLogToStream(&stream, LS_INFO); |
| 45 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream)); | 45 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream)); |
| 46 | 46 |
| 47 LOG(LS_INFO) << "INFO"; | 47 LOG(LS_INFO) << "INFO"; |
| 48 LOG(LS_VERBOSE) << "VERBOSE"; | 48 LOG(LS_VERBOSE) << "VERBOSE"; |
| 49 EXPECT_NE(std::string::npos, str.find("INFO")); | 49 EXPECT_NE(std::string::npos, str.find("INFO")); |
| 50 EXPECT_EQ(std::string::npos, str.find("VERBOSE")); | 50 EXPECT_EQ(std::string::npos, str.find("VERBOSE")); |
| 51 | 51 |
| 52 LogMessage::RemoveLogToStream(&stream); | 52 LogMessage::RemoveLogToStream(&stream); |
| 53 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream)); | 53 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream)); |
| 54 | 54 |
| 55 EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL)); | 55 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Test using multiple log streams. The INFO stream should get the INFO message, | 58 // Test using multiple log streams. The INFO stream should get the INFO message, |
| 59 // the VERBOSE stream should get the INFO and the VERBOSE. | 59 // the VERBOSE stream should get the INFO and the VERBOSE. |
| 60 // We should restore the correct global state at the end. | 60 // We should restore the correct global state at the end. |
| 61 TEST(LogTest, MultipleStreams) { | 61 TEST(LogTest, MultipleStreams) { |
| 62 int sev = LogMessage::GetLogToStream(NULL); | 62 int sev = LogMessage::GetLogToStream(nullptr); |
| 63 | 63 |
| 64 std::string str1, str2; | 64 std::string str1, str2; |
| 65 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2); | 65 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2); |
| 66 LogMessage::AddLogToStream(&stream1, LS_INFO); | 66 LogMessage::AddLogToStream(&stream1, LS_INFO); |
| 67 LogMessage::AddLogToStream(&stream2, LS_VERBOSE); | 67 LogMessage::AddLogToStream(&stream2, LS_VERBOSE); |
| 68 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1)); | 68 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1)); |
| 69 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2)); | 69 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2)); |
| 70 | 70 |
| 71 LOG(LS_INFO) << "INFO"; | 71 LOG(LS_INFO) << "INFO"; |
| 72 LOG(LS_VERBOSE) << "VERBOSE"; | 72 LOG(LS_VERBOSE) << "VERBOSE"; |
| 73 | 73 |
| 74 EXPECT_NE(std::string::npos, str1.find("INFO")); | 74 EXPECT_NE(std::string::npos, str1.find("INFO")); |
| 75 EXPECT_EQ(std::string::npos, str1.find("VERBOSE")); | 75 EXPECT_EQ(std::string::npos, str1.find("VERBOSE")); |
| 76 EXPECT_NE(std::string::npos, str2.find("INFO")); | 76 EXPECT_NE(std::string::npos, str2.find("INFO")); |
| 77 EXPECT_NE(std::string::npos, str2.find("VERBOSE")); | 77 EXPECT_NE(std::string::npos, str2.find("VERBOSE")); |
| 78 | 78 |
| 79 LogMessage::RemoveLogToStream(&stream2); | 79 LogMessage::RemoveLogToStream(&stream2); |
| 80 LogMessage::RemoveLogToStream(&stream1); | 80 LogMessage::RemoveLogToStream(&stream1); |
| 81 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2)); | 81 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2)); |
| 82 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1)); | 82 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1)); |
| 83 | 83 |
| 84 EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL)); | 84 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Ensure we don't crash when adding/removing streams while threads are going. | 87 // Ensure we don't crash when adding/removing streams while threads are going. |
| 88 // We should restore the correct global state at the end. | 88 // We should restore the correct global state at the end. |
| 89 class LogThread : public Thread { | 89 class LogThread : public Thread { |
| 90 public: | 90 public: |
| 91 ~LogThread() override { | 91 ~LogThread() override { |
| 92 Stop(); | 92 Stop(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 void Run() override { | 96 void Run() override { |
| 97 // LS_SENSITIVE to avoid cluttering up any real logging going on | 97 // LS_SENSITIVE to avoid cluttering up any real logging going on |
| 98 LOG(LS_SENSITIVE) << "LOG"; | 98 LOG(LS_SENSITIVE) << "LOG"; |
| 99 } | 99 } |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 TEST(LogTest, MultipleThreads) { | 102 TEST(LogTest, MultipleThreads) { |
| 103 int sev = LogMessage::GetLogToStream(NULL); | 103 int sev = LogMessage::GetLogToStream(nullptr); |
| 104 | 104 |
| 105 LogThread thread1, thread2, thread3; | 105 LogThread thread1, thread2, thread3; |
| 106 thread1.Start(); | 106 thread1.Start(); |
| 107 thread2.Start(); | 107 thread2.Start(); |
| 108 thread3.Start(); | 108 thread3.Start(); |
| 109 | 109 |
| 110 LogSinkImpl<NullStream> stream1, stream2, stream3; | 110 LogSinkImpl<NullStream> stream1, stream2, stream3; |
| 111 for (int i = 0; i < 1000; ++i) { | 111 for (int i = 0; i < 1000; ++i) { |
| 112 LogMessage::AddLogToStream(&stream1, LS_INFO); | 112 LogMessage::AddLogToStream(&stream1, LS_INFO); |
| 113 LogMessage::AddLogToStream(&stream2, LS_VERBOSE); | 113 LogMessage::AddLogToStream(&stream2, LS_VERBOSE); |
| 114 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE); | 114 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE); |
| 115 LogMessage::RemoveLogToStream(&stream1); | 115 LogMessage::RemoveLogToStream(&stream1); |
| 116 LogMessage::RemoveLogToStream(&stream2); | 116 LogMessage::RemoveLogToStream(&stream2); |
| 117 LogMessage::RemoveLogToStream(&stream3); | 117 LogMessage::RemoveLogToStream(&stream3); |
| 118 } | 118 } |
| 119 | 119 |
| 120 EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL)); | 120 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 | 123 |
| 124 TEST(LogTest, WallClockStartTime) { | 124 TEST(LogTest, WallClockStartTime) { |
| 125 uint32_t time = LogMessage::WallClockStartTime(); | 125 uint32_t time = LogMessage::WallClockStartTime(); |
| 126 // Expect the time to be in a sensible range, e.g. > 2012-01-01. | 126 // Expect the time to be in a sensible range, e.g. > 2012-01-01. |
| 127 EXPECT_GT(time, 1325376000u); | 127 EXPECT_GT(time, 1325376000u); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Test the time required to write 1000 80-character logs to an unbuffered file. | 130 // Test the time required to write 1000 80-character logs to an unbuffered file. |
| 131 #if defined (WEBRTC_ANDROID) | 131 #if defined (WEBRTC_ANDROID) |
| 132 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. | 132 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 133 #define MAYBE_Perf DISABLED_Perf | 133 #define MAYBE_Perf DISABLED_Perf |
| 134 #else | 134 #else |
| 135 #define MAYBE_Perf Perf | 135 #define MAYBE_Perf Perf |
| 136 #endif | 136 #endif |
| 137 | 137 |
| 138 TEST(LogTest, MAYBE_Perf) { | 138 TEST(LogTest, MAYBE_Perf) { |
| 139 Pathname path; | 139 Pathname path; |
| 140 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); | 140 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, nullptr)); |
| 141 path.SetPathname(Filesystem::TempFilename(path, "ut")); | 141 path.SetPathname(Filesystem::TempFilename(path, "ut")); |
| 142 | 142 |
| 143 LogSinkImpl<FileStream> stream; | 143 LogSinkImpl<FileStream> stream; |
| 144 EXPECT_TRUE(stream.Open(path.pathname(), "wb", NULL)); | 144 EXPECT_TRUE(stream.Open(path.pathname(), "wb", nullptr)); |
| 145 stream.DisableBuffering(); | 145 stream.DisableBuffering(); |
| 146 LogMessage::AddLogToStream(&stream, LS_SENSITIVE); | 146 LogMessage::AddLogToStream(&stream, LS_SENSITIVE); |
| 147 | 147 |
| 148 int64_t start = TimeMillis(), finish; | 148 int64_t start = TimeMillis(), finish; |
| 149 std::string message('X', 80); | 149 std::string message('X', 80); |
| 150 for (int i = 0; i < 1000; ++i) { | 150 for (int i = 0; i < 1000; ++i) { |
| 151 LOG(LS_SENSITIVE) << message; | 151 LOG(LS_SENSITIVE) << message; |
| 152 } | 152 } |
| 153 finish = TimeMillis(); | 153 finish = TimeMillis(); |
| 154 | 154 |
| 155 LogMessage::RemoveLogToStream(&stream); | 155 LogMessage::RemoveLogToStream(&stream); |
| 156 stream.Close(); | 156 stream.Close(); |
| 157 Filesystem::DeleteFile(path); | 157 Filesystem::DeleteFile(path); |
| 158 | 158 |
| 159 LOG(LS_INFO) << "Average log time: " << TimeDiff(finish, start) << " ms"; | 159 LOG(LS_INFO) << "Average log time: " << TimeDiff(finish, start) << " ms"; |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace rtc | 162 } // namespace rtc |
| OLD | NEW |