henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/gunit.h" |
| 12 | #include "rtc_base/logging.h" |
| 13 | #include "rtc_base/nullsocketserver.h" |
| 14 | #include "rtc_base/stream.h" |
| 15 | #include "rtc_base/thread.h" |
| 16 | #include "test/testsupport/fileutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 20 | template <typename Base> |
| 21 | class LogSinkImpl |
| 22 | : public LogSink, |
| 23 | public Base { |
| 24 | public: |
| 25 | LogSinkImpl() {} |
| 26 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 27 | template<typename P> |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 28 | explicit LogSinkImpl(P* p) : Base(p) {} |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | void OnLogMessage(const std::string& message) override { |
| 32 | static_cast<Base*>(this)->WriteAll( |
| 33 | message.data(), message.size(), nullptr, nullptr); |
| 34 | } |
| 35 | }; |
| 36 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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. |
| 39 | TEST(LogTest, SingleStream) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 40 | int sev = LogMessage::GetLogToStream(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | |
| 42 | std::string str; |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 43 | LogSinkImpl<StringStream> stream(&str); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | LogMessage::AddLogToStream(&stream, LS_INFO); |
| 45 | EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream)); |
| 46 | |
| 47 | LOG(LS_INFO) << "INFO"; |
| 48 | LOG(LS_VERBOSE) << "VERBOSE"; |
| 49 | EXPECT_NE(std::string::npos, str.find("INFO")); |
| 50 | EXPECT_EQ(std::string::npos, str.find("VERBOSE")); |
| 51 | |
| 52 | LogMessage::RemoveLogToStream(&stream); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 53 | EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 55 | EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 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. |
| 60 | // We should restore the correct global state at the end. |
| 61 | TEST(LogTest, MultipleStreams) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 62 | int sev = LogMessage::GetLogToStream(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | |
| 64 | std::string str1, str2; |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 65 | LogSinkImpl<StringStream> stream1(&str1), stream2(&str2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | LogMessage::AddLogToStream(&stream1, LS_INFO); |
| 67 | LogMessage::AddLogToStream(&stream2, LS_VERBOSE); |
| 68 | EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1)); |
| 69 | EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2)); |
| 70 | |
| 71 | LOG(LS_INFO) << "INFO"; |
| 72 | LOG(LS_VERBOSE) << "VERBOSE"; |
| 73 | |
| 74 | EXPECT_NE(std::string::npos, str1.find("INFO")); |
| 75 | EXPECT_EQ(std::string::npos, str1.find("VERBOSE")); |
| 76 | EXPECT_NE(std::string::npos, str2.find("INFO")); |
| 77 | EXPECT_NE(std::string::npos, str2.find("VERBOSE")); |
| 78 | |
| 79 | LogMessage::RemoveLogToStream(&stream2); |
| 80 | LogMessage::RemoveLogToStream(&stream1); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 81 | EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2)); |
| 82 | EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 84 | EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 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. |
| 89 | class LogThread : public Thread { |
| 90 | public: |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 91 | LogThread() : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())) {} |
| 92 | |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 93 | ~LogThread() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | Stop(); |
| 95 | } |
| 96 | |
| 97 | private: |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 98 | void Run() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | // LS_SENSITIVE to avoid cluttering up any real logging going on |
| 100 | LOG(LS_SENSITIVE) << "LOG"; |
| 101 | } |
| 102 | }; |
| 103 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 104 | TEST(LogTest, MultipleThreads) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 105 | int sev = LogMessage::GetLogToStream(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | |
| 107 | LogThread thread1, thread2, thread3; |
| 108 | thread1.Start(); |
| 109 | thread2.Start(); |
| 110 | thread3.Start(); |
| 111 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 112 | LogSinkImpl<NullStream> stream1, stream2, stream3; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | for (int i = 0; i < 1000; ++i) { |
| 114 | LogMessage::AddLogToStream(&stream1, LS_INFO); |
| 115 | LogMessage::AddLogToStream(&stream2, LS_VERBOSE); |
| 116 | LogMessage::AddLogToStream(&stream3, LS_SENSITIVE); |
| 117 | LogMessage::RemoveLogToStream(&stream1); |
| 118 | LogMessage::RemoveLogToStream(&stream2); |
| 119 | LogMessage::RemoveLogToStream(&stream3); |
| 120 | } |
| 121 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 122 | EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | TEST(LogTest, WallClockStartTime) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 127 | uint32_t time = LogMessage::WallClockStartTime(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | // Expect the time to be in a sensible range, e.g. > 2012-01-01. |
| 129 | EXPECT_GT(time, 1325376000u); |
| 130 | } |
| 131 | |
| 132 | // Test the time required to write 1000 80-character logs to an unbuffered file. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 133 | #if defined (WEBRTC_ANDROID) |
| 134 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 135 | #define MAYBE_Perf DISABLED_Perf |
| 136 | #else |
| 137 | #define MAYBE_Perf Perf |
| 138 | #endif |
| 139 | |
| 140 | TEST(LogTest, MAYBE_Perf) { |
nisse | a0c8887 | 2017-08-24 02:20:46 -0700 | [diff] [blame] | 141 | std::string path = |
| 142 | webrtc::test::TempFilename(webrtc::test::OutputPath(), "ut"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 144 | LogSinkImpl<FileStream> stream; |
nisse | a0c8887 | 2017-08-24 02:20:46 -0700 | [diff] [blame] | 145 | EXPECT_TRUE(stream.Open(path, "wb", nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | stream.DisableBuffering(); |
| 147 | LogMessage::AddLogToStream(&stream, LS_SENSITIVE); |
| 148 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 149 | int64_t start = TimeMillis(), finish; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 150 | std::string message('X', 80); |
| 151 | for (int i = 0; i < 1000; ++i) { |
| 152 | LOG(LS_SENSITIVE) << message; |
| 153 | } |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 154 | finish = TimeMillis(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 155 | |
| 156 | LogMessage::RemoveLogToStream(&stream); |
| 157 | stream.Close(); |
nisse | a0c8887 | 2017-08-24 02:20:46 -0700 | [diff] [blame] | 158 | webrtc::test::RemoveFile(path); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 160 | LOG(LS_INFO) << "Average log time: " << TimeDiff(finish, start) << " ms"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace rtc |