blob: f3f0ea56e5ff075451cbe9c46b31bfca9094fac9 [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>
23class LogSinkImpl
24 : public LogSink,
25 public Base {
26 public:
27 LogSinkImpl() {}
28
Tommi0eefb4d2015-05-23 09:54:07 +020029 template<typename P>
Tommi00aac5a2015-05-25 11:25:59 +020030 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +020031
32 private:
33 void OnLogMessage(const std::string& message) override {
34 static_cast<Base*>(this)->WriteAll(
35 message.data(), message.size(), nullptr, nullptr);
36 }
37};
38
Tommifef05002018-02-27 13:51:08 +010039class LogMessageForTesting : public LogMessage {
40 public:
41 LogMessageForTesting(const char* file,
42 int line,
43 LoggingSeverity sev,
44 LogErrorContext err_ctx = ERRCTX_NONE,
45 int err = 0)
46 : LogMessage(file, line, sev, err_ctx, err) {}
47
48 const std::string& get_extra() const { return extra_; }
49 bool is_noop() const { return is_noop_; }
50
51 // Returns the contents of the internal log stream.
52 // Note that parts of the stream won't (as is) be available until *after* the
53 // dtor of the parent class has run. So, as is, this only represents a
54 // partially built stream.
55 std::string GetPrintStream() {
56 RTC_DCHECK(!is_finished_);
57 is_finished_ = true;
58 FinishPrintStream();
59 std::string ret = print_stream_.str();
60 // Just to make an error even more clear if the stream gets used after this.
61 print_stream_.clear();
62 return ret;
63 }
64
65 private:
66 bool is_finished_ = false;
67};
68
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069// Test basic logging operation. We should get the INFO log but not the VERBOSE.
70// We should restore the correct global state at the end.
71TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -080072 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
74 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +020075 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 LogMessage::AddLogToStream(&stream, LS_INFO);
77 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
78
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(LS_INFO) << "INFO";
80 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 EXPECT_NE(std::string::npos, str.find("INFO"));
82 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
83
84 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +020085 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086
deadbeef37f5ecf2017-02-27 14:06:41 -080087 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088}
89
Jonas Olssonf8e5c112018-06-14 13:14:22 +020090#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
91TEST(LogTest, Checks) {
92 EXPECT_DEATH(FATAL() << "message",
93 "\n\n#\n"
94 "# Fatal error in: \\S+, line \\d+\n"
95 "# last system error: \\d+\n"
96 "# Check failed: FATAL\\(\\)\n"
97 "# message"
98 );
99
100 int a = 1, b = 2;
101 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
102 "\n\n#\n"
103 "# Fatal error in: \\S+, line \\d+\n"
104 "# last system error: \\d+\n"
105 "# Check failed: a == b \\(1 vs. 2\\)\n"
106 "# 12"
107 );
108 RTC_CHECK_EQ(5, 5);
109
110 RTC_CHECK(true) << "Shouldn't crash" << 1;
111 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
112 "\n\n#\n"
113 "# Fatal error in: \\S+, line \\d+\n"
114 "# last system error: \\d+\n"
115 "# Check failed: false\n"
116 "# Hi there!"
117 );
118}
119#endif
120
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121// Test using multiple log streams. The INFO stream should get the INFO message,
122// the VERBOSE stream should get the INFO and the VERBOSE.
123// We should restore the correct global state at the end.
124TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800125 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
127 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200128 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 LogMessage::AddLogToStream(&stream1, LS_INFO);
130 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
131 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
132 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
133
Mirko Bonadei675513b2017-11-09 11:09:25 +0100134 RTC_LOG(LS_INFO) << "INFO";
135 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136
137 EXPECT_NE(std::string::npos, str1.find("INFO"));
138 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
139 EXPECT_NE(std::string::npos, str2.find("INFO"));
140 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
141
142 LogMessage::RemoveLogToStream(&stream2);
143 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200144 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
145 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
deadbeef37f5ecf2017-02-27 14:06:41 -0800147 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148}
149
Tommifef05002018-02-27 13:51:08 +0100150class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 public:
Tommifef05002018-02-27 13:51:08 +0100152 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
153 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700154
Tommifef05002018-02-27 13:51:08 +0100155 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000156
157 private:
Tommifef05002018-02-27 13:51:08 +0100158 void Run() {
159 // LS_SENSITIVE by default to avoid cluttering up any real logging going on.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(LS_SENSITIVE) << "RTC_LOG";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 }
Tommifef05002018-02-27 13:51:08 +0100162
163 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
164
165 PlatformThread thread_;
166 Event event_{false, false};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167};
168
Tommifef05002018-02-27 13:51:08 +0100169// Ensure we don't crash when adding/removing streams while threads are going.
170// We should restore the correct global state at the end.
171// This test also makes sure that the 'noop' stream() singleton object, can be
172// safely used from mutiple threads since the threads log at LS_SENSITIVE
173// (by default 'noop' entries).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000174TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800175 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176
177 LogThread thread1, thread2, thread3;
178 thread1.Start();
179 thread2.Start();
180 thread3.Start();
181
Tommi0eefb4d2015-05-23 09:54:07 +0200182 LogSinkImpl<NullStream> stream1, stream2, stream3;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183 for (int i = 0; i < 1000; ++i) {
184 LogMessage::AddLogToStream(&stream1, LS_INFO);
185 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
186 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
187 LogMessage::RemoveLogToStream(&stream1);
188 LogMessage::RemoveLogToStream(&stream2);
189 LogMessage::RemoveLogToStream(&stream3);
190 }
191
deadbeef37f5ecf2017-02-27 14:06:41 -0800192 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193}
194
195
196TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200197 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
199 EXPECT_GT(time, 1325376000u);
200}
201
Tommifef05002018-02-27 13:51:08 +0100202TEST(LogTest, CheckExtraErrorField) {
203 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
204 ERRCTX_ERRNO, 0xD);
205 ASSERT_FALSE(log_msg.is_noop());
206 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700207
Tommifef05002018-02-27 13:51:08 +0100208 const std::string& extra = log_msg.get_extra();
209 const size_t length_to_check = arraysize("[0x12345678]") - 1;
210 ASSERT_GE(extra.length(), length_to_check);
211 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
212}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213
Tommifef05002018-02-27 13:51:08 +0100214TEST(LogTest, CheckFilePathParsed) {
215 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
216 ASSERT_FALSE(log_msg.is_noop());
217 log_msg.stream() << "<- Does this look right?";
218
219 const std::string stream = log_msg.GetPrintStream();
220 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
221}
222
223TEST(LogTest, CheckNoopLogEntry) {
224 if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) {
225 printf("CheckNoopLogEntry: skipping. Global severity is being overridden.");
226 return;
227 }
228
229 // Logging at LS_SENSITIVE severity, is by default turned off, so this should
230 // be treated as a noop message.
231 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_SENSITIVE);
232 log_msg.stream() << "Should be logged to nowhere.";
233 EXPECT_TRUE(log_msg.is_noop());
234 const std::string stream = log_msg.GetPrintStream();
235 EXPECT_TRUE(stream.empty());
236}
237
238// Test the time required to write 1000 80-character logs to a string.
239TEST(LogTest, Perf) {
240 std::string str;
241 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242 LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
243
Tommifef05002018-02-27 13:51:08 +0100244 const std::string message(80, 'X');
245 {
246 // Just to be sure that we're not measuring the performance of logging
247 // noop log messages.
248 LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE);
249 ASSERT_FALSE(sanity_check_msg.is_noop());
250 }
251
252 // We now know how many bytes the logging framework will tag onto every msg.
253 const size_t logging_overhead = str.size();
254 // Reset the stream to 0 size.
255 str.clear();
256 str.reserve(120000);
257 static const int kRepetitions = 1000;
258
Honghai Zhang82d78622016-05-06 11:29:15 -0700259 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100260 for (int i = 0; i < kRepetitions; ++i) {
261 LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700263 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000264
265 LogMessage::RemoveLogToStream(&stream);
266 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000267
Tommifef05002018-02-27 13:51:08 +0100268 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
269 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
270 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000271}
272
273} // namespace rtc