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/logging.h" |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 12 | #include "rtc_base/arraysize.h" |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/event.h" |
| 15 | #include "rtc_base/gunit.h" |
| 16 | #include "rtc_base/platform_thread.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/stream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "test/testsupport/fileutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 22 | template <typename Base> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 23 | class LogSinkImpl : public LogSink, public Base { |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 24 | public: |
| 25 | LogSinkImpl() {} |
| 26 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +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 { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 32 | static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr, |
| 33 | nullptr); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 34 | } |
| 35 | }; |
| 36 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 37 | class LogMessageForTesting : public LogMessage { |
| 38 | public: |
| 39 | LogMessageForTesting(const char* file, |
| 40 | int line, |
| 41 | LoggingSeverity sev, |
| 42 | LogErrorContext err_ctx = ERRCTX_NONE, |
| 43 | int err = 0) |
| 44 | : LogMessage(file, line, sev, err_ctx, err) {} |
| 45 | |
| 46 | const std::string& get_extra() const { return extra_; } |
| 47 | bool is_noop() const { return is_noop_; } |
| 48 | |
| 49 | // Returns the contents of the internal log stream. |
| 50 | // Note that parts of the stream won't (as is) be available until *after* the |
| 51 | // dtor of the parent class has run. So, as is, this only represents a |
| 52 | // partially built stream. |
| 53 | std::string GetPrintStream() { |
| 54 | RTC_DCHECK(!is_finished_); |
| 55 | is_finished_ = true; |
| 56 | FinishPrintStream(); |
| 57 | std::string ret = print_stream_.str(); |
| 58 | // Just to make an error even more clear if the stream gets used after this. |
| 59 | print_stream_.clear(); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | bool is_finished_ = false; |
| 65 | }; |
| 66 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | // Test basic logging operation. We should get the INFO log but not the VERBOSE. |
| 68 | // We should restore the correct global state at the end. |
| 69 | TEST(LogTest, SingleStream) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 70 | int sev = LogMessage::GetLogToStream(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | |
| 72 | std::string str; |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 73 | LogSinkImpl<StringStream> stream(&str); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | LogMessage::AddLogToStream(&stream, LS_INFO); |
| 75 | EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream)); |
| 76 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 77 | RTC_LOG(LS_INFO) << "INFO"; |
| 78 | RTC_LOG(LS_VERBOSE) << "VERBOSE"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | EXPECT_NE(std::string::npos, str.find("INFO")); |
| 80 | EXPECT_EQ(std::string::npos, str.find("VERBOSE")); |
| 81 | |
| 82 | LogMessage::RemoveLogToStream(&stream); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 83 | EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 85 | EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jonas Olsson | ae18886 | 2018-06-18 11:19:22 +0200 | [diff] [blame] | 88 | /* |
Jonas Olsson | f8e5c11 | 2018-06-14 13:14:22 +0200 | [diff] [blame] | 89 | #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 90 | TEST(LogTest, Checks) { |
| 91 | EXPECT_DEATH(FATAL() << "message", |
| 92 | "\n\n#\n" |
| 93 | "# Fatal error in: \\S+, line \\d+\n" |
| 94 | "# last system error: \\d+\n" |
| 95 | "# Check failed: FATAL\\(\\)\n" |
| 96 | "# message" |
| 97 | ); |
| 98 | |
| 99 | int a = 1, b = 2; |
| 100 | EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u, |
| 101 | "\n\n#\n" |
| 102 | "# Fatal error in: \\S+, line \\d+\n" |
| 103 | "# last system error: \\d+\n" |
| 104 | "# Check failed: a == b \\(1 vs. 2\\)\n" |
| 105 | "# 12" |
| 106 | ); |
| 107 | RTC_CHECK_EQ(5, 5); |
| 108 | |
| 109 | RTC_CHECK(true) << "Shouldn't crash" << 1; |
| 110 | EXPECT_DEATH(RTC_CHECK(false) << "Hi there!", |
| 111 | "\n\n#\n" |
| 112 | "# Fatal error in: \\S+, line \\d+\n" |
| 113 | "# last system error: \\d+\n" |
| 114 | "# Check failed: false\n" |
| 115 | "# Hi there!" |
| 116 | ); |
| 117 | } |
| 118 | #endif |
Jonas Olsson | ae18886 | 2018-06-18 11:19:22 +0200 | [diff] [blame] | 119 | */ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | // Test using multiple log streams. The INFO stream should get the INFO message, |
| 121 | // the VERBOSE stream should get the INFO and the VERBOSE. |
| 122 | // We should restore the correct global state at the end. |
| 123 | TEST(LogTest, MultipleStreams) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 124 | int sev = LogMessage::GetLogToStream(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | |
| 126 | std::string str1, str2; |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 127 | LogSinkImpl<StringStream> stream1(&str1), stream2(&str2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | LogMessage::AddLogToStream(&stream1, LS_INFO); |
| 129 | LogMessage::AddLogToStream(&stream2, LS_VERBOSE); |
| 130 | EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1)); |
| 131 | EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2)); |
| 132 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 133 | RTC_LOG(LS_INFO) << "INFO"; |
| 134 | RTC_LOG(LS_VERBOSE) << "VERBOSE"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | |
| 136 | EXPECT_NE(std::string::npos, str1.find("INFO")); |
| 137 | EXPECT_EQ(std::string::npos, str1.find("VERBOSE")); |
| 138 | EXPECT_NE(std::string::npos, str2.find("INFO")); |
| 139 | EXPECT_NE(std::string::npos, str2.find("VERBOSE")); |
| 140 | |
| 141 | LogMessage::RemoveLogToStream(&stream2); |
| 142 | LogMessage::RemoveLogToStream(&stream1); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 143 | EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2)); |
| 144 | EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 146 | EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 149 | class LogThread { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 150 | public: |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 151 | LogThread() : thread_(&ThreadEntry, this, "LogThread") {} |
| 152 | ~LogThread() { thread_.Stop(); } |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 153 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 154 | void Start() { thread_.Start(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 155 | |
| 156 | private: |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 157 | void Run() { |
| 158 | // LS_SENSITIVE by default to avoid cluttering up any real logging going on. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 159 | RTC_LOG(LS_SENSITIVE) << "RTC_LOG"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | } |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 161 | |
| 162 | static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); } |
| 163 | |
| 164 | PlatformThread thread_; |
| 165 | Event event_{false, false}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 168 | // Ensure we don't crash when adding/removing streams while threads are going. |
| 169 | // We should restore the correct global state at the end. |
| 170 | // This test also makes sure that the 'noop' stream() singleton object, can be |
| 171 | // safely used from mutiple threads since the threads log at LS_SENSITIVE |
| 172 | // (by default 'noop' entries). |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 173 | TEST(LogTest, MultipleThreads) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 174 | int sev = LogMessage::GetLogToStream(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | |
| 176 | LogThread thread1, thread2, thread3; |
| 177 | thread1.Start(); |
| 178 | thread2.Start(); |
| 179 | thread3.Start(); |
| 180 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 181 | LogSinkImpl<NullStream> stream1, stream2, stream3; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 182 | for (int i = 0; i < 1000; ++i) { |
| 183 | LogMessage::AddLogToStream(&stream1, LS_INFO); |
| 184 | LogMessage::AddLogToStream(&stream2, LS_VERBOSE); |
| 185 | LogMessage::AddLogToStream(&stream3, LS_SENSITIVE); |
| 186 | LogMessage::RemoveLogToStream(&stream1); |
| 187 | LogMessage::RemoveLogToStream(&stream2); |
| 188 | LogMessage::RemoveLogToStream(&stream3); |
| 189 | } |
| 190 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 191 | EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | } |
| 193 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 194 | TEST(LogTest, WallClockStartTime) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 195 | uint32_t time = LogMessage::WallClockStartTime(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 196 | // Expect the time to be in a sensible range, e.g. > 2012-01-01. |
| 197 | EXPECT_GT(time, 1325376000u); |
| 198 | } |
| 199 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 200 | TEST(LogTest, CheckExtraErrorField) { |
| 201 | LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING, |
| 202 | ERRCTX_ERRNO, 0xD); |
| 203 | ASSERT_FALSE(log_msg.is_noop()); |
| 204 | log_msg.stream() << "This gets added at dtor time"; |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 205 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 206 | const std::string& extra = log_msg.get_extra(); |
| 207 | const size_t length_to_check = arraysize("[0x12345678]") - 1; |
| 208 | ASSERT_GE(extra.length(), length_to_check); |
| 209 | EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check)); |
| 210 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 211 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 212 | TEST(LogTest, CheckFilePathParsed) { |
| 213 | LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO); |
| 214 | ASSERT_FALSE(log_msg.is_noop()); |
| 215 | log_msg.stream() << "<- Does this look right?"; |
| 216 | |
| 217 | const std::string stream = log_msg.GetPrintStream(); |
| 218 | EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)")); |
| 219 | } |
| 220 | |
| 221 | TEST(LogTest, CheckNoopLogEntry) { |
| 222 | if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) { |
| 223 | printf("CheckNoopLogEntry: skipping. Global severity is being overridden."); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Logging at LS_SENSITIVE severity, is by default turned off, so this should |
| 228 | // be treated as a noop message. |
| 229 | LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_SENSITIVE); |
| 230 | log_msg.stream() << "Should be logged to nowhere."; |
| 231 | EXPECT_TRUE(log_msg.is_noop()); |
| 232 | const std::string stream = log_msg.GetPrintStream(); |
| 233 | EXPECT_TRUE(stream.empty()); |
| 234 | } |
| 235 | |
| 236 | // Test the time required to write 1000 80-character logs to a string. |
| 237 | TEST(LogTest, Perf) { |
| 238 | std::string str; |
| 239 | LogSinkImpl<StringStream> stream(&str); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 240 | LogMessage::AddLogToStream(&stream, LS_SENSITIVE); |
| 241 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 242 | const std::string message(80, 'X'); |
| 243 | { |
| 244 | // Just to be sure that we're not measuring the performance of logging |
| 245 | // noop log messages. |
| 246 | LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE); |
| 247 | ASSERT_FALSE(sanity_check_msg.is_noop()); |
| 248 | } |
| 249 | |
| 250 | // We now know how many bytes the logging framework will tag onto every msg. |
| 251 | const size_t logging_overhead = str.size(); |
| 252 | // Reset the stream to 0 size. |
| 253 | str.clear(); |
| 254 | str.reserve(120000); |
| 255 | static const int kRepetitions = 1000; |
| 256 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 257 | int64_t start = TimeMillis(), finish; |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 258 | for (int i = 0; i < kRepetitions; ++i) { |
| 259 | LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | } |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 261 | finish = TimeMillis(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 262 | |
| 263 | LogMessage::RemoveLogToStream(&stream); |
| 264 | stream.Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 266 | EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions); |
| 267 | RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms " |
| 268 | << " total bytes logged: " << str.size(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | } // namespace rtc |