blob: 8f96095a792b50c24e53fc9707507f1d468d14ac [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/logging.h"
Tommifef05002018-02-27 13:51:08 +010012#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 Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/testsupport/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
Tommi0eefb4d2015-05-23 09:54:07 +020022template <typename Base>
Yves Gerey665174f2018-06-19 15:03:05 +020023class LogSinkImpl : public LogSink, public Base {
Tommi0eefb4d2015-05-23 09:54:07 +020024 public:
25 LogSinkImpl() {}
26
Yves Gerey665174f2018-06-19 15:03:05 +020027 template <typename P>
Tommi00aac5a2015-05-25 11:25:59 +020028 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +020029
30 private:
31 void OnLogMessage(const std::string& message) override {
Yves Gerey665174f2018-06-19 15:03:05 +020032 static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
33 nullptr);
Tommi0eefb4d2015-05-23 09:54:07 +020034 }
35};
36
Tommifef05002018-02-27 13:51:08 +010037class 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.orgf0488722014-05-13 18:00:26 +000067// 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.
69TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -080070 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071
72 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +020073 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 LogMessage::AddLogToStream(&stream, LS_INFO);
75 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
76
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_INFO) << "INFO";
78 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 EXPECT_NE(std::string::npos, str.find("INFO"));
80 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
81
82 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +020083 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084
deadbeef37f5ecf2017-02-27 14:06:41 -080085 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086}
87
Jonas Olssonae188862018-06-18 11:19:22 +020088/*
Jonas Olssonf8e5c112018-06-14 13:14:22 +020089#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
90TEST(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 Olssonae188862018-06-18 11:19:22 +0200119*/
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120// 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.
123TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800124 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
126 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200127 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 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 Bonadei675513b2017-11-09 11:09:25 +0100133 RTC_LOG(LS_INFO) << "INFO";
134 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135
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);
Tommi0eefb4d2015-05-23 09:54:07 +0200143 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
144 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145
deadbeef37f5ecf2017-02-27 14:06:41 -0800146 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147}
148
Tommifef05002018-02-27 13:51:08 +0100149class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150 public:
Tommifef05002018-02-27 13:51:08 +0100151 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
152 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700153
Tommifef05002018-02-27 13:51:08 +0100154 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155
156 private:
Tommifef05002018-02-27 13:51:08 +0100157 void Run() {
158 // LS_SENSITIVE by default to avoid cluttering up any real logging going on.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100159 RTC_LOG(LS_SENSITIVE) << "RTC_LOG";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000160 }
Tommifef05002018-02-27 13:51:08 +0100161
162 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
163
164 PlatformThread thread_;
165 Event event_{false, false};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166};
167
Tommifef05002018-02-27 13:51:08 +0100168// 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.orgc732a3e2014-10-09 22:08:15 +0000173TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800174 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175
176 LogThread thread1, thread2, thread3;
177 thread1.Start();
178 thread2.Start();
179 thread3.Start();
180
Tommi0eefb4d2015-05-23 09:54:07 +0200181 LogSinkImpl<NullStream> stream1, stream2, stream3;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182 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
deadbeef37f5ecf2017-02-27 14:06:41 -0800191 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192}
193
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200195 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
197 EXPECT_GT(time, 1325376000u);
198}
199
Tommifef05002018-02-27 13:51:08 +0100200TEST(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";
phoglundbb738732016-07-15 03:57:12 -0700205
Tommifef05002018-02-27 13:51:08 +0100206 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.orgf0488722014-05-13 18:00:26 +0000211
Tommifef05002018-02-27 13:51:08 +0100212TEST(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
221TEST(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.
237TEST(LogTest, Perf) {
238 std::string str;
239 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
241
Tommifef05002018-02-27 13:51:08 +0100242 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 Zhang82d78622016-05-06 11:29:15 -0700257 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100258 for (int i = 0; i < kRepetitions; ++i) {
259 LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700261 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262
263 LogMessage::RemoveLogToStream(&stream);
264 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265
Tommifef05002018-02-27 13:51:08 +0100266 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.orgf0488722014-05-13 18:00:26 +0000269}
270
271} // namespace rtc