blob: 637d2e0a00a04055e274a267111c7a74e252fcdc [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
13#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <algorithm>
16
Tommifef05002018-02-27 13:51:08 +010017#include "rtc_base/arraysize.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/event.h"
Tommifef05002018-02-27 13:51:08 +010020#include "rtc_base/platform_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/stream.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
25namespace rtc {
26
Niels Möllerff3dd0c2018-06-20 10:27:53 +020027namespace {
28
29class StringStream : public StreamInterface {
30 public:
31 explicit StringStream(std::string* str);
32 explicit StringStream(const std::string& str);
33
34 StreamState GetState() const override;
35 StreamResult Read(void* buffer,
36 size_t buffer_len,
37 size_t* read,
38 int* error) override;
39 StreamResult Write(const void* data,
40 size_t data_len,
41 size_t* written,
42 int* error) override;
43 void Close() override;
Niels Möllerff3dd0c2018-06-20 10:27:53 +020044
45 private:
46 std::string& str_;
47 size_t read_pos_;
48 bool read_only_;
49};
50
51StringStream::StringStream(std::string* str)
52 : str_(*str), read_pos_(0), read_only_(false) {}
53
54StringStream::StringStream(const std::string& str)
55 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
56
57StreamState StringStream::GetState() const {
58 return SS_OPEN;
59}
60
61StreamResult StringStream::Read(void* buffer,
62 size_t buffer_len,
63 size_t* read,
64 int* error) {
65 size_t available = std::min(buffer_len, str_.size() - read_pos_);
66 if (!available)
67 return SR_EOS;
68 memcpy(buffer, str_.data() + read_pos_, available);
69 read_pos_ += available;
70 if (read)
71 *read = available;
72 return SR_SUCCESS;
73}
74
75StreamResult StringStream::Write(const void* data,
76 size_t data_len,
77 size_t* written,
78 int* error) {
79 if (read_only_) {
80 if (error) {
81 *error = -1;
82 }
83 return SR_ERROR;
84 }
85 str_.append(static_cast<const char*>(data),
86 static_cast<const char*>(data) + data_len);
87 if (written)
88 *written = data_len;
89 return SR_SUCCESS;
90}
91
92void StringStream::Close() {}
93
Niels Möllerff3dd0c2018-06-20 10:27:53 +020094} // namespace
95
Tommi0eefb4d2015-05-23 09:54:07 +020096template <typename Base>
Yves Gerey665174f2018-06-19 15:03:05 +020097class LogSinkImpl : public LogSink, public Base {
Tommi0eefb4d2015-05-23 09:54:07 +020098 public:
99 LogSinkImpl() {}
100
Yves Gerey665174f2018-06-19 15:03:05 +0200101 template <typename P>
Tommi00aac5a2015-05-25 11:25:59 +0200102 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +0200103
104 private:
105 void OnLogMessage(const std::string& message) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200106 static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
107 nullptr);
Tommi0eefb4d2015-05-23 09:54:07 +0200108 }
109};
110
Tommifef05002018-02-27 13:51:08 +0100111class LogMessageForTesting : public LogMessage {
112 public:
113 LogMessageForTesting(const char* file,
114 int line,
115 LoggingSeverity sev,
116 LogErrorContext err_ctx = ERRCTX_NONE,
117 int err = 0)
118 : LogMessage(file, line, sev, err_ctx, err) {}
119
120 const std::string& get_extra() const { return extra_; }
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200121#if defined(WEBRTC_ANDROID)
122 const char* get_tag() const { return tag_; }
123#endif
Tommifef05002018-02-27 13:51:08 +0100124
125 // Returns the contents of the internal log stream.
126 // Note that parts of the stream won't (as is) be available until *after* the
127 // dtor of the parent class has run. So, as is, this only represents a
128 // partially built stream.
129 std::string GetPrintStream() {
130 RTC_DCHECK(!is_finished_);
131 is_finished_ = true;
132 FinishPrintStream();
Jonas Olssond8c50782018-09-07 11:21:28 +0200133 return print_stream_.Release();
Tommifef05002018-02-27 13:51:08 +0100134 }
135
136 private:
137 bool is_finished_ = false;
138};
139
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140// Test basic logging operation. We should get the INFO log but not the VERBOSE.
141// We should restore the correct global state at the end.
142TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800143 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144
145 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +0200146 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147 LogMessage::AddLogToStream(&stream, LS_INFO);
148 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
149
Mirko Bonadei675513b2017-11-09 11:09:25 +0100150 RTC_LOG(LS_INFO) << "INFO";
151 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 EXPECT_NE(std::string::npos, str.find("INFO"));
153 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
154
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200155 int i = 1;
156 long l = 2l;
157 long long ll = 3ll;
158
159 unsigned int u = 4u;
160 unsigned long ul = 5ul;
161 unsigned long long ull = 6ull;
162
163 std::string s1 = "char*";
164 std::string s2 = "std::string";
165 std::string s3 = "absl::stringview";
Niels Möller65835be2019-02-04 19:23:58 +0100166 const char* null_string = nullptr;
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200167 void* p = reinterpret_cast<void*>(0xabcd);
168
169 // Log all suported types(except doubles/floats) as a sanity-check.
170 RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
171 << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
Niels Möller65835be2019-02-04 19:23:58 +0100172 << absl::string_view(s3) << "|" << p << "|" << null_string
173 << "|";
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200174
175 // Signed integers
176 EXPECT_NE(std::string::npos, str.find("|1|"));
177 EXPECT_NE(std::string::npos, str.find("|2|"));
178 EXPECT_NE(std::string::npos, str.find("|3|"));
179
180 // Unsigned integers
181 EXPECT_NE(std::string::npos, str.find("|4|"));
182 EXPECT_NE(std::string::npos, str.find("|5|"));
183 EXPECT_NE(std::string::npos, str.find("|6|"));
184
185 // Strings
186 EXPECT_NE(std::string::npos, str.find("|char*|"));
187 EXPECT_NE(std::string::npos, str.find("|std::string|"));
188 EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
189
190 // void*
191 EXPECT_NE(std::string::npos, str.find("|abcd|"));
192
Niels Möller65835be2019-02-04 19:23:58 +0100193 // null char*
194 EXPECT_NE(std::string::npos, str.find("|(null)|"));
195
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +0200197 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800198 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199}
200
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200201#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
202TEST(LogTest, Checks) {
203 EXPECT_DEATH(FATAL() << "message",
204 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200205 "# Fatal error in: \\S+, line \\w+\n"
206 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200207 "# Check failed: FATAL\\(\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200208 "# message");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200209
210 int a = 1, b = 2;
211 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
212 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200213 "# Fatal error in: \\S+, line \\w+\n"
214 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200215 "# Check failed: a == b \\(1 vs. 2\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200216 "# 12");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200217 RTC_CHECK_EQ(5, 5);
218
219 RTC_CHECK(true) << "Shouldn't crash" << 1;
220 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
221 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200222 "# Fatal error in: \\S+, line \\w+\n"
223 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200224 "# Check failed: false\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200225 "# Hi there!");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200226}
227#endif
Jonas Olssond000b0a2018-07-03 12:07:53 +0200228
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229// Test using multiple log streams. The INFO stream should get the INFO message,
230// the VERBOSE stream should get the INFO and the VERBOSE.
231// We should restore the correct global state at the end.
232TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800233 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234
235 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200236 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237 LogMessage::AddLogToStream(&stream1, LS_INFO);
238 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
239 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
240 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
241
Mirko Bonadei675513b2017-11-09 11:09:25 +0100242 RTC_LOG(LS_INFO) << "INFO";
243 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244
245 EXPECT_NE(std::string::npos, str1.find("INFO"));
246 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
247 EXPECT_NE(std::string::npos, str2.find("INFO"));
248 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
249
250 LogMessage::RemoveLogToStream(&stream2);
251 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200252 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
253 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254
deadbeef37f5ecf2017-02-27 14:06:41 -0800255 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256}
257
Tommifef05002018-02-27 13:51:08 +0100258class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 public:
Tommifef05002018-02-27 13:51:08 +0100260 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
261 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700262
Tommifef05002018-02-27 13:51:08 +0100263 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000264
265 private:
Niels Möller25aefd32018-11-21 09:34:11 +0100266 void Run() { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }
Tommifef05002018-02-27 13:51:08 +0100267
268 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
269
270 PlatformThread thread_;
Niels Möllerc572ff32018-11-07 08:43:50 +0100271 Event event_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272};
273
Tommifef05002018-02-27 13:51:08 +0100274// Ensure we don't crash when adding/removing streams while threads are going.
275// We should restore the correct global state at the end.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000276TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800277 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278
279 LogThread thread1, thread2, thread3;
280 thread1.Start();
281 thread2.Start();
282 thread3.Start();
283
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200284 std::string s1, s2, s3;
285 LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286 for (int i = 0; i < 1000; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100287 LogMessage::AddLogToStream(&stream1, LS_WARNING);
288 LogMessage::AddLogToStream(&stream2, LS_INFO);
289 LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 LogMessage::RemoveLogToStream(&stream1);
291 LogMessage::RemoveLogToStream(&stream2);
292 LogMessage::RemoveLogToStream(&stream3);
293 }
294
deadbeef37f5ecf2017-02-27 14:06:41 -0800295 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296}
297
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200299 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
301 EXPECT_GT(time, 1325376000u);
302}
303
Tommifef05002018-02-27 13:51:08 +0100304TEST(LogTest, CheckExtraErrorField) {
305 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
306 ERRCTX_ERRNO, 0xD);
Tommifef05002018-02-27 13:51:08 +0100307 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700308
Tommifef05002018-02-27 13:51:08 +0100309 const std::string& extra = log_msg.get_extra();
310 const size_t length_to_check = arraysize("[0x12345678]") - 1;
311 ASSERT_GE(extra.length(), length_to_check);
312 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
313}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314
Tommifef05002018-02-27 13:51:08 +0100315TEST(LogTest, CheckFilePathParsed) {
316 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
Tommifef05002018-02-27 13:51:08 +0100317 log_msg.stream() << "<- Does this look right?";
318
319 const std::string stream = log_msg.GetPrintStream();
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200320#if defined(WEBRTC_ANDROID)
321 const char* tag = log_msg.get_tag();
322 EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
323 EXPECT_NE(std::string::npos, stream.find("100"));
324#else
Tommifef05002018-02-27 13:51:08 +0100325 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200326#endif
Tommifef05002018-02-27 13:51:08 +0100327}
328
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200329#if defined(WEBRTC_ANDROID)
330TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
331 std::string str;
332 LogSinkImpl<StringStream> stream(&str);
333 LogMessage::AddLogToStream(&stream, LS_INFO);
334 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
335
336 RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
337 EXPECT_NE(std::string::npos, str.find("INFO"));
338 EXPECT_NE(std::string::npos, str.find("my_tag"));
339}
340#endif
341
Tommifef05002018-02-27 13:51:08 +0100342// Test the time required to write 1000 80-character logs to a string.
343TEST(LogTest, Perf) {
344 std::string str;
345 LogSinkImpl<StringStream> stream(&str);
Niels Möller25aefd32018-11-21 09:34:11 +0100346 LogMessage::AddLogToStream(&stream, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000347
Tommifef05002018-02-27 13:51:08 +0100348 const std::string message(80, 'X');
Niels Möller25aefd32018-11-21 09:34:11 +0100349 { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
Tommifef05002018-02-27 13:51:08 +0100350
351 // We now know how many bytes the logging framework will tag onto every msg.
352 const size_t logging_overhead = str.size();
353 // Reset the stream to 0 size.
354 str.clear();
355 str.reserve(120000);
356 static const int kRepetitions = 1000;
357
Honghai Zhang82d78622016-05-06 11:29:15 -0700358 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100359 for (int i = 0; i < kRepetitions; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100360 LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000361 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700362 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363
364 LogMessage::RemoveLogToStream(&stream);
365 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366
Tommifef05002018-02-27 13:51:08 +0100367 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
368 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
369 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000370}
371
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700372TEST(LogTest, EnumsAreSupported) {
373 enum class TestEnum { kValue0 = 0, kValue1 = 1 };
374 std::string str;
375 LogSinkImpl<StringStream> stream(&str);
376 LogMessage::AddLogToStream(&stream, LS_INFO);
377 RTC_LOG(LS_INFO) << "[" << TestEnum::kValue0 << "]";
378 EXPECT_NE(std::string::npos, str.find("[0]"));
379 EXPECT_EQ(std::string::npos, str.find("[1]"));
380 RTC_LOG(LS_INFO) << "[" << TestEnum::kValue1 << "]";
381 EXPECT_NE(std::string::npos, str.find("[1]"));
382 LogMessage::RemoveLogToStream(&stream);
383 stream.Close();
384}
385
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000386} // namespace rtc