blob: f17291fdc59cd921a39ea6ecfee6e817bcd91c07 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010012
Artem Titov6a4a1462019-11-26 16:24:46 +010013#if RTC_LOG_ENABLED()
14
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Yves Gerey3e707812018-11-28 16:47:49 +010017#include <algorithm>
18
Tommifef05002018-02-27 13:51:08 +010019#include "rtc_base/arraysize.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/event.h"
Tommifef05002018-02-27 13:51:08 +010022#include "rtc_base/platform_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/stream.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
27namespace rtc {
28
Niels Möllerff3dd0c2018-06-20 10:27:53 +020029namespace {
30
31class StringStream : public StreamInterface {
32 public:
33 explicit StringStream(std::string* str);
34 explicit StringStream(const std::string& str);
35
36 StreamState GetState() const override;
37 StreamResult Read(void* buffer,
38 size_t buffer_len,
39 size_t* read,
40 int* error) override;
41 StreamResult Write(const void* data,
42 size_t data_len,
43 size_t* written,
44 int* error) override;
45 void Close() override;
Niels Möllerff3dd0c2018-06-20 10:27:53 +020046
47 private:
48 std::string& str_;
49 size_t read_pos_;
50 bool read_only_;
51};
52
53StringStream::StringStream(std::string* str)
54 : str_(*str), read_pos_(0), read_only_(false) {}
55
56StringStream::StringStream(const std::string& str)
57 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
58
59StreamState StringStream::GetState() const {
60 return SS_OPEN;
61}
62
63StreamResult StringStream::Read(void* buffer,
64 size_t buffer_len,
65 size_t* read,
66 int* error) {
67 size_t available = std::min(buffer_len, str_.size() - read_pos_);
68 if (!available)
69 return SR_EOS;
70 memcpy(buffer, str_.data() + read_pos_, available);
71 read_pos_ += available;
72 if (read)
73 *read = available;
74 return SR_SUCCESS;
75}
76
77StreamResult StringStream::Write(const void* data,
78 size_t data_len,
79 size_t* written,
80 int* error) {
81 if (read_only_) {
82 if (error) {
83 *error = -1;
84 }
85 return SR_ERROR;
86 }
87 str_.append(static_cast<const char*>(data),
88 static_cast<const char*>(data) + data_len);
89 if (written)
90 *written = data_len;
91 return SR_SUCCESS;
92}
93
94void StringStream::Close() {}
95
Niels Möllerff3dd0c2018-06-20 10:27:53 +020096} // namespace
97
Tommi0eefb4d2015-05-23 09:54:07 +020098template <typename Base>
Yves Gerey665174f2018-06-19 15:03:05 +020099class LogSinkImpl : public LogSink, public Base {
Tommi0eefb4d2015-05-23 09:54:07 +0200100 public:
101 LogSinkImpl() {}
102
Yves Gerey665174f2018-06-19 15:03:05 +0200103 template <typename P>
Tommi00aac5a2015-05-25 11:25:59 +0200104 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +0200105
106 private:
107 void OnLogMessage(const std::string& message) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200108 static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
109 nullptr);
Tommi0eefb4d2015-05-23 09:54:07 +0200110 }
111};
112
Tommifef05002018-02-27 13:51:08 +0100113class LogMessageForTesting : public LogMessage {
114 public:
115 LogMessageForTesting(const char* file,
116 int line,
117 LoggingSeverity sev,
118 LogErrorContext err_ctx = ERRCTX_NONE,
119 int err = 0)
120 : LogMessage(file, line, sev, err_ctx, err) {}
121
122 const std::string& get_extra() const { return extra_; }
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200123#if defined(WEBRTC_ANDROID)
124 const char* get_tag() const { return tag_; }
125#endif
Tommifef05002018-02-27 13:51:08 +0100126
127 // Returns the contents of the internal log stream.
128 // Note that parts of the stream won't (as is) be available until *after* the
129 // dtor of the parent class has run. So, as is, this only represents a
130 // partially built stream.
131 std::string GetPrintStream() {
132 RTC_DCHECK(!is_finished_);
133 is_finished_ = true;
134 FinishPrintStream();
Jonas Olssond8c50782018-09-07 11:21:28 +0200135 return print_stream_.Release();
Tommifef05002018-02-27 13:51:08 +0100136 }
137
138 private:
139 bool is_finished_ = false;
140};
141
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142// Test basic logging operation. We should get the INFO log but not the VERBOSE.
143// We should restore the correct global state at the end.
144TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800145 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
147 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +0200148 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149 LogMessage::AddLogToStream(&stream, LS_INFO);
150 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
151
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_INFO) << "INFO";
153 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 EXPECT_NE(std::string::npos, str.find("INFO"));
155 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
156
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200157 int i = 1;
158 long l = 2l;
159 long long ll = 3ll;
160
161 unsigned int u = 4u;
162 unsigned long ul = 5ul;
163 unsigned long long ull = 6ull;
164
165 std::string s1 = "char*";
166 std::string s2 = "std::string";
167 std::string s3 = "absl::stringview";
Niels Möller65835be2019-02-04 19:23:58 +0100168 const char* null_string = nullptr;
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200169 void* p = reinterpret_cast<void*>(0xabcd);
170
171 // Log all suported types(except doubles/floats) as a sanity-check.
172 RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
173 << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
Niels Möller65835be2019-02-04 19:23:58 +0100174 << absl::string_view(s3) << "|" << p << "|" << null_string
175 << "|";
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200176
177 // Signed integers
178 EXPECT_NE(std::string::npos, str.find("|1|"));
179 EXPECT_NE(std::string::npos, str.find("|2|"));
180 EXPECT_NE(std::string::npos, str.find("|3|"));
181
182 // Unsigned integers
183 EXPECT_NE(std::string::npos, str.find("|4|"));
184 EXPECT_NE(std::string::npos, str.find("|5|"));
185 EXPECT_NE(std::string::npos, str.find("|6|"));
186
187 // Strings
188 EXPECT_NE(std::string::npos, str.find("|char*|"));
189 EXPECT_NE(std::string::npos, str.find("|std::string|"));
190 EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
191
192 // void*
193 EXPECT_NE(std::string::npos, str.find("|abcd|"));
194
Niels Möller65835be2019-02-04 19:23:58 +0100195 // null char*
196 EXPECT_NE(std::string::npos, str.find("|(null)|"));
197
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +0200199 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800200 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201}
202
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200203#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
204TEST(LogTest, Checks) {
205 EXPECT_DEATH(FATAL() << "message",
206 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200207 "# Fatal error in: \\S+, line \\w+\n"
208 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200209 "# Check failed: FATAL\\(\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200210 "# message");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200211
212 int a = 1, b = 2;
213 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
214 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200215 "# Fatal error in: \\S+, line \\w+\n"
216 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200217 "# Check failed: a == b \\(1 vs. 2\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200218 "# 12");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200219 RTC_CHECK_EQ(5, 5);
220
221 RTC_CHECK(true) << "Shouldn't crash" << 1;
222 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
223 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200224 "# Fatal error in: \\S+, line \\w+\n"
225 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200226 "# Check failed: false\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200227 "# Hi there!");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200228}
229#endif
Jonas Olssond000b0a2018-07-03 12:07:53 +0200230
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231// Test using multiple log streams. The INFO stream should get the INFO message,
232// the VERBOSE stream should get the INFO and the VERBOSE.
233// We should restore the correct global state at the end.
234TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800235 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236
237 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200238 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 LogMessage::AddLogToStream(&stream1, LS_INFO);
240 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
241 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
242 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
243
Mirko Bonadei675513b2017-11-09 11:09:25 +0100244 RTC_LOG(LS_INFO) << "INFO";
245 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000246
247 EXPECT_NE(std::string::npos, str1.find("INFO"));
248 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
249 EXPECT_NE(std::string::npos, str2.find("INFO"));
250 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
251
252 LogMessage::RemoveLogToStream(&stream2);
253 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200254 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
255 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256
deadbeef37f5ecf2017-02-27 14:06:41 -0800257 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000258}
259
Tommifef05002018-02-27 13:51:08 +0100260class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261 public:
Tommifef05002018-02-27 13:51:08 +0100262 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
263 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700264
Tommifef05002018-02-27 13:51:08 +0100265 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266
267 private:
Niels Möller25aefd32018-11-21 09:34:11 +0100268 void Run() { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }
Tommifef05002018-02-27 13:51:08 +0100269
270 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
271
272 PlatformThread thread_;
Niels Möllerc572ff32018-11-07 08:43:50 +0100273 Event event_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274};
275
Tommifef05002018-02-27 13:51:08 +0100276// Ensure we don't crash when adding/removing streams while threads are going.
277// We should restore the correct global state at the end.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000278TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800279 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280
281 LogThread thread1, thread2, thread3;
282 thread1.Start();
283 thread2.Start();
284 thread3.Start();
285
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200286 std::string s1, s2, s3;
287 LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 for (int i = 0; i < 1000; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100289 LogMessage::AddLogToStream(&stream1, LS_WARNING);
290 LogMessage::AddLogToStream(&stream2, LS_INFO);
291 LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000292 LogMessage::RemoveLogToStream(&stream1);
293 LogMessage::RemoveLogToStream(&stream2);
294 LogMessage::RemoveLogToStream(&stream3);
295 }
296
deadbeef37f5ecf2017-02-27 14:06:41 -0800297 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298}
299
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
303 EXPECT_GT(time, 1325376000u);
304}
305
Tommifef05002018-02-27 13:51:08 +0100306TEST(LogTest, CheckExtraErrorField) {
307 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
308 ERRCTX_ERRNO, 0xD);
Tommifef05002018-02-27 13:51:08 +0100309 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700310
Tommifef05002018-02-27 13:51:08 +0100311 const std::string& extra = log_msg.get_extra();
312 const size_t length_to_check = arraysize("[0x12345678]") - 1;
313 ASSERT_GE(extra.length(), length_to_check);
314 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
315}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316
Tommifef05002018-02-27 13:51:08 +0100317TEST(LogTest, CheckFilePathParsed) {
318 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
Tommifef05002018-02-27 13:51:08 +0100319 log_msg.stream() << "<- Does this look right?";
320
321 const std::string stream = log_msg.GetPrintStream();
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200322#if defined(WEBRTC_ANDROID)
323 const char* tag = log_msg.get_tag();
324 EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
325 EXPECT_NE(std::string::npos, stream.find("100"));
326#else
Tommifef05002018-02-27 13:51:08 +0100327 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200328#endif
Tommifef05002018-02-27 13:51:08 +0100329}
330
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200331#if defined(WEBRTC_ANDROID)
332TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
333 std::string str;
334 LogSinkImpl<StringStream> stream(&str);
335 LogMessage::AddLogToStream(&stream, LS_INFO);
336 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
337
338 RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
339 EXPECT_NE(std::string::npos, str.find("INFO"));
340 EXPECT_NE(std::string::npos, str.find("my_tag"));
341}
342#endif
343
Tommifef05002018-02-27 13:51:08 +0100344// Test the time required to write 1000 80-character logs to a string.
345TEST(LogTest, Perf) {
346 std::string str;
347 LogSinkImpl<StringStream> stream(&str);
Niels Möller25aefd32018-11-21 09:34:11 +0100348 LogMessage::AddLogToStream(&stream, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349
Tommifef05002018-02-27 13:51:08 +0100350 const std::string message(80, 'X');
Niels Möller25aefd32018-11-21 09:34:11 +0100351 { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
Tommifef05002018-02-27 13:51:08 +0100352
353 // We now know how many bytes the logging framework will tag onto every msg.
354 const size_t logging_overhead = str.size();
355 // Reset the stream to 0 size.
356 str.clear();
357 str.reserve(120000);
358 static const int kRepetitions = 1000;
359
Honghai Zhang82d78622016-05-06 11:29:15 -0700360 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100361 for (int i = 0; i < kRepetitions; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100362 LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700364 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000365
366 LogMessage::RemoveLogToStream(&stream);
367 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368
Tommifef05002018-02-27 13:51:08 +0100369 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
370 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
371 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000372}
373
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700374TEST(LogTest, EnumsAreSupported) {
375 enum class TestEnum { kValue0 = 0, kValue1 = 1 };
376 std::string str;
377 LogSinkImpl<StringStream> stream(&str);
378 LogMessage::AddLogToStream(&stream, LS_INFO);
379 RTC_LOG(LS_INFO) << "[" << TestEnum::kValue0 << "]";
380 EXPECT_NE(std::string::npos, str.find("[0]"));
381 EXPECT_EQ(std::string::npos, str.find("[1]"));
382 RTC_LOG(LS_INFO) << "[" << TestEnum::kValue1 << "]";
383 EXPECT_NE(std::string::npos, str.find("[1]"));
384 LogMessage::RemoveLogToStream(&stream);
385 stream.Close();
386}
387
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000388} // namespace rtc
Artem Titov6a4a1462019-11-26 16:24:46 +0100389#endif