blob: 44d5805b2ffcfce3b001cbbf48a998d1f6a66582 [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_; }
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020048#if defined(WEBRTC_ANDROID)
49 const char* get_tag() const { return tag_; }
50#endif
Tommifef05002018-02-27 13:51:08 +010051
52 // Returns the contents of the internal log stream.
53 // Note that parts of the stream won't (as is) be available until *after* the
54 // dtor of the parent class has run. So, as is, this only represents a
55 // partially built stream.
56 std::string GetPrintStream() {
57 RTC_DCHECK(!is_finished_);
58 is_finished_ = true;
59 FinishPrintStream();
60 std::string ret = print_stream_.str();
61 // Just to make an error even more clear if the stream gets used after this.
62 print_stream_.clear();
63 return ret;
64 }
65
66 private:
67 bool is_finished_ = false;
68};
69
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070// Test basic logging operation. We should get the INFO log but not the VERBOSE.
71// We should restore the correct global state at the end.
72TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -080073 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074
75 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +020076 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 LogMessage::AddLogToStream(&stream, LS_INFO);
78 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
79
Mirko Bonadei675513b2017-11-09 11:09:25 +010080 RTC_LOG(LS_INFO) << "INFO";
81 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 EXPECT_NE(std::string::npos, str.find("INFO"));
83 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
84
85 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +020086 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
deadbeef37f5ecf2017-02-27 14:06:41 -080088 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089}
90
Jonas Olssonae188862018-06-18 11:19:22 +020091/*
Jonas Olssonf8e5c112018-06-14 13:14:22 +020092#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
93TEST(LogTest, Checks) {
94 EXPECT_DEATH(FATAL() << "message",
95 "\n\n#\n"
96 "# Fatal error in: \\S+, line \\d+\n"
97 "# last system error: \\d+\n"
98 "# Check failed: FATAL\\(\\)\n"
99 "# message"
100 );
101
102 int a = 1, b = 2;
103 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
104 "\n\n#\n"
105 "# Fatal error in: \\S+, line \\d+\n"
106 "# last system error: \\d+\n"
107 "# Check failed: a == b \\(1 vs. 2\\)\n"
108 "# 12"
109 );
110 RTC_CHECK_EQ(5, 5);
111
112 RTC_CHECK(true) << "Shouldn't crash" << 1;
113 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
114 "\n\n#\n"
115 "# Fatal error in: \\S+, line \\d+\n"
116 "# last system error: \\d+\n"
117 "# Check failed: false\n"
118 "# Hi there!"
119 );
120}
121#endif
Jonas Olssonae188862018-06-18 11:19:22 +0200122*/
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123// Test using multiple log streams. The INFO stream should get the INFO message,
124// the VERBOSE stream should get the INFO and the VERBOSE.
125// We should restore the correct global state at the end.
126TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800127 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128
129 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200130 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131 LogMessage::AddLogToStream(&stream1, LS_INFO);
132 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
133 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
134 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
135
Mirko Bonadei675513b2017-11-09 11:09:25 +0100136 RTC_LOG(LS_INFO) << "INFO";
137 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138
139 EXPECT_NE(std::string::npos, str1.find("INFO"));
140 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
141 EXPECT_NE(std::string::npos, str2.find("INFO"));
142 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
143
144 LogMessage::RemoveLogToStream(&stream2);
145 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200146 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
147 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148
deadbeef37f5ecf2017-02-27 14:06:41 -0800149 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150}
151
Tommifef05002018-02-27 13:51:08 +0100152class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153 public:
Tommifef05002018-02-27 13:51:08 +0100154 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
155 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700156
Tommifef05002018-02-27 13:51:08 +0100157 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158
159 private:
Tommifef05002018-02-27 13:51:08 +0100160 void Run() {
161 // LS_SENSITIVE by default to avoid cluttering up any real logging going on.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_SENSITIVE) << "RTC_LOG";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163 }
Tommifef05002018-02-27 13:51:08 +0100164
165 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
166
167 PlatformThread thread_;
168 Event event_{false, false};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169};
170
Tommifef05002018-02-27 13:51:08 +0100171// Ensure we don't crash when adding/removing streams while threads are going.
172// We should restore the correct global state at the end.
173// This test also makes sure that the 'noop' stream() singleton object, can be
174// safely used from mutiple threads since the threads log at LS_SENSITIVE
175// (by default 'noop' entries).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000176TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800177 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178
179 LogThread thread1, thread2, thread3;
180 thread1.Start();
181 thread2.Start();
182 thread3.Start();
183
Tommi0eefb4d2015-05-23 09:54:07 +0200184 LogSinkImpl<NullStream> stream1, stream2, stream3;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185 for (int i = 0; i < 1000; ++i) {
186 LogMessage::AddLogToStream(&stream1, LS_INFO);
187 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
188 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
189 LogMessage::RemoveLogToStream(&stream1);
190 LogMessage::RemoveLogToStream(&stream2);
191 LogMessage::RemoveLogToStream(&stream3);
192 }
193
deadbeef37f5ecf2017-02-27 14:06:41 -0800194 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195}
196
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
200 EXPECT_GT(time, 1325376000u);
201}
202
Tommifef05002018-02-27 13:51:08 +0100203TEST(LogTest, CheckExtraErrorField) {
204 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
205 ERRCTX_ERRNO, 0xD);
206 ASSERT_FALSE(log_msg.is_noop());
207 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700208
Tommifef05002018-02-27 13:51:08 +0100209 const std::string& extra = log_msg.get_extra();
210 const size_t length_to_check = arraysize("[0x12345678]") - 1;
211 ASSERT_GE(extra.length(), length_to_check);
212 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
213}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214
Tommifef05002018-02-27 13:51:08 +0100215TEST(LogTest, CheckFilePathParsed) {
216 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
217 ASSERT_FALSE(log_msg.is_noop());
218 log_msg.stream() << "<- Does this look right?";
219
220 const std::string stream = log_msg.GetPrintStream();
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200221#if defined(WEBRTC_ANDROID)
222 const char* tag = log_msg.get_tag();
223 EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
224 EXPECT_NE(std::string::npos, stream.find("100"));
225#else
Tommifef05002018-02-27 13:51:08 +0100226 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200227#endif
Tommifef05002018-02-27 13:51:08 +0100228}
229
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200230#if defined(WEBRTC_ANDROID)
231TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
232 std::string str;
233 LogSinkImpl<StringStream> stream(&str);
234 LogMessage::AddLogToStream(&stream, LS_INFO);
235 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
236
237 RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
238 EXPECT_NE(std::string::npos, str.find("INFO"));
239 EXPECT_NE(std::string::npos, str.find("my_tag"));
240}
241#endif
242
Tommifef05002018-02-27 13:51:08 +0100243TEST(LogTest, CheckNoopLogEntry) {
244 if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) {
245 printf("CheckNoopLogEntry: skipping. Global severity is being overridden.");
246 return;
247 }
248
249 // Logging at LS_SENSITIVE severity, is by default turned off, so this should
250 // be treated as a noop message.
251 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_SENSITIVE);
252 log_msg.stream() << "Should be logged to nowhere.";
253 EXPECT_TRUE(log_msg.is_noop());
254 const std::string stream = log_msg.GetPrintStream();
255 EXPECT_TRUE(stream.empty());
256}
257
258// Test the time required to write 1000 80-character logs to a string.
259TEST(LogTest, Perf) {
260 std::string str;
261 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
263
Tommifef05002018-02-27 13:51:08 +0100264 const std::string message(80, 'X');
265 {
266 // Just to be sure that we're not measuring the performance of logging
267 // noop log messages.
268 LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE);
269 ASSERT_FALSE(sanity_check_msg.is_noop());
270 }
271
272 // We now know how many bytes the logging framework will tag onto every msg.
273 const size_t logging_overhead = str.size();
274 // Reset the stream to 0 size.
275 str.clear();
276 str.reserve(120000);
277 static const int kRepetitions = 1000;
278
Honghai Zhang82d78622016-05-06 11:29:15 -0700279 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100280 for (int i = 0; i < kRepetitions; ++i) {
281 LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700283 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284
285 LogMessage::RemoveLogToStream(&stream);
286 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287
Tommifef05002018-02-27 13:51:08 +0100288 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
289 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
290 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291}
292
293} // namespace rtc