blob: bbeac444ed214de98f1f5c00f6aec75049d8cd25 [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>
14#include <algorithm>
15
Tommifef05002018-02-27 13:51:08 +010016#include "rtc_base/arraysize.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/event.h"
Tommifef05002018-02-27 13:51:08 +010019#include "rtc_base/platform_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/stream.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24namespace rtc {
25
Niels Möllerff3dd0c2018-06-20 10:27:53 +020026namespace {
27
28class StringStream : public StreamInterface {
29 public:
30 explicit StringStream(std::string* str);
31 explicit StringStream(const std::string& str);
32
33 StreamState GetState() const override;
34 StreamResult Read(void* buffer,
35 size_t buffer_len,
36 size_t* read,
37 int* error) override;
38 StreamResult Write(const void* data,
39 size_t data_len,
40 size_t* written,
41 int* error) override;
42 void Close() override;
43 bool SetPosition(size_t position) override;
44 bool GetPosition(size_t* position) const override;
Niels Möllerff3dd0c2018-06-20 10:27:53 +020045
46 private:
47 std::string& str_;
48 size_t read_pos_;
49 bool read_only_;
50};
51
52StringStream::StringStream(std::string* str)
53 : str_(*str), read_pos_(0), read_only_(false) {}
54
55StringStream::StringStream(const std::string& str)
56 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
57
58StreamState StringStream::GetState() const {
59 return SS_OPEN;
60}
61
62StreamResult StringStream::Read(void* buffer,
63 size_t buffer_len,
64 size_t* read,
65 int* error) {
66 size_t available = std::min(buffer_len, str_.size() - read_pos_);
67 if (!available)
68 return SR_EOS;
69 memcpy(buffer, str_.data() + read_pos_, available);
70 read_pos_ += available;
71 if (read)
72 *read = available;
73 return SR_SUCCESS;
74}
75
76StreamResult StringStream::Write(const void* data,
77 size_t data_len,
78 size_t* written,
79 int* error) {
80 if (read_only_) {
81 if (error) {
82 *error = -1;
83 }
84 return SR_ERROR;
85 }
86 str_.append(static_cast<const char*>(data),
87 static_cast<const char*>(data) + data_len);
88 if (written)
89 *written = data_len;
90 return SR_SUCCESS;
91}
92
93void StringStream::Close() {}
94
95bool StringStream::SetPosition(size_t position) {
96 if (position > str_.size())
97 return false;
98 read_pos_ = position;
99 return true;
100}
101
102bool StringStream::GetPosition(size_t* position) const {
103 if (position)
104 *position = read_pos_;
105 return true;
106}
107
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200108} // namespace
109
Tommi0eefb4d2015-05-23 09:54:07 +0200110template <typename Base>
Yves Gerey665174f2018-06-19 15:03:05 +0200111class LogSinkImpl : public LogSink, public Base {
Tommi0eefb4d2015-05-23 09:54:07 +0200112 public:
113 LogSinkImpl() {}
114
Yves Gerey665174f2018-06-19 15:03:05 +0200115 template <typename P>
Tommi00aac5a2015-05-25 11:25:59 +0200116 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +0200117
118 private:
119 void OnLogMessage(const std::string& message) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200120 static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
121 nullptr);
Tommi0eefb4d2015-05-23 09:54:07 +0200122 }
123};
124
Tommifef05002018-02-27 13:51:08 +0100125class LogMessageForTesting : public LogMessage {
126 public:
127 LogMessageForTesting(const char* file,
128 int line,
129 LoggingSeverity sev,
130 LogErrorContext err_ctx = ERRCTX_NONE,
131 int err = 0)
132 : LogMessage(file, line, sev, err_ctx, err) {}
133
134 const std::string& get_extra() const { return extra_; }
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200135#if defined(WEBRTC_ANDROID)
136 const char* get_tag() const { return tag_; }
137#endif
Tommifef05002018-02-27 13:51:08 +0100138
139 // Returns the contents of the internal log stream.
140 // Note that parts of the stream won't (as is) be available until *after* the
141 // dtor of the parent class has run. So, as is, this only represents a
142 // partially built stream.
143 std::string GetPrintStream() {
144 RTC_DCHECK(!is_finished_);
145 is_finished_ = true;
146 FinishPrintStream();
Jonas Olssond8c50782018-09-07 11:21:28 +0200147 return print_stream_.Release();
Tommifef05002018-02-27 13:51:08 +0100148 }
149
150 private:
151 bool is_finished_ = false;
152};
153
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154// Test basic logging operation. We should get the INFO log but not the VERBOSE.
155// We should restore the correct global state at the end.
156TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800157 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158
159 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +0200160 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 LogMessage::AddLogToStream(&stream, LS_INFO);
162 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
163
Mirko Bonadei675513b2017-11-09 11:09:25 +0100164 RTC_LOG(LS_INFO) << "INFO";
165 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166 EXPECT_NE(std::string::npos, str.find("INFO"));
167 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
168
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200169 int i = 1;
170 long l = 2l;
171 long long ll = 3ll;
172
173 unsigned int u = 4u;
174 unsigned long ul = 5ul;
175 unsigned long long ull = 6ull;
176
177 std::string s1 = "char*";
178 std::string s2 = "std::string";
179 std::string s3 = "absl::stringview";
180
181 void* p = reinterpret_cast<void*>(0xabcd);
182
183 // Log all suported types(except doubles/floats) as a sanity-check.
184 RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
185 << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
186 << absl::string_view(s3) << "|" << p << "|";
187
188 // Signed integers
189 EXPECT_NE(std::string::npos, str.find("|1|"));
190 EXPECT_NE(std::string::npos, str.find("|2|"));
191 EXPECT_NE(std::string::npos, str.find("|3|"));
192
193 // Unsigned integers
194 EXPECT_NE(std::string::npos, str.find("|4|"));
195 EXPECT_NE(std::string::npos, str.find("|5|"));
196 EXPECT_NE(std::string::npos, str.find("|6|"));
197
198 // Strings
199 EXPECT_NE(std::string::npos, str.find("|char*|"));
200 EXPECT_NE(std::string::npos, str.find("|std::string|"));
201 EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
202
203 // void*
204 EXPECT_NE(std::string::npos, str.find("|abcd|"));
205
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +0200207 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800208 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209}
210
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200211#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
212TEST(LogTest, Checks) {
213 EXPECT_DEATH(FATAL() << "message",
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: FATAL\\(\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200218 "# message");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200219
220 int a = 1, b = 2;
221 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
222 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200223 "# Fatal error in: \\S+, line \\w+\n"
224 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200225 "# Check failed: a == b \\(1 vs. 2\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200226 "# 12");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200227 RTC_CHECK_EQ(5, 5);
228
229 RTC_CHECK(true) << "Shouldn't crash" << 1;
230 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
231 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200232 "# Fatal error in: \\S+, line \\w+\n"
233 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200234 "# Check failed: false\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200235 "# Hi there!");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200236}
237#endif
Jonas Olssond000b0a2018-07-03 12:07:53 +0200238
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239// Test using multiple log streams. The INFO stream should get the INFO message,
240// the VERBOSE stream should get the INFO and the VERBOSE.
241// We should restore the correct global state at the end.
242TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800243 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244
245 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200246 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 LogMessage::AddLogToStream(&stream1, LS_INFO);
248 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
249 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
250 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
251
Mirko Bonadei675513b2017-11-09 11:09:25 +0100252 RTC_LOG(LS_INFO) << "INFO";
253 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254
255 EXPECT_NE(std::string::npos, str1.find("INFO"));
256 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
257 EXPECT_NE(std::string::npos, str2.find("INFO"));
258 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
259
260 LogMessage::RemoveLogToStream(&stream2);
261 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200262 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
263 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000264
deadbeef37f5ecf2017-02-27 14:06:41 -0800265 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266}
267
Tommifef05002018-02-27 13:51:08 +0100268class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000269 public:
Tommifef05002018-02-27 13:51:08 +0100270 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
271 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700272
Tommifef05002018-02-27 13:51:08 +0100273 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274
275 private:
Niels Möller25aefd32018-11-21 09:34:11 +0100276 void Run() { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }
Tommifef05002018-02-27 13:51:08 +0100277
278 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
279
280 PlatformThread thread_;
Niels Möllerc572ff32018-11-07 08:43:50 +0100281 Event event_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282};
283
Tommifef05002018-02-27 13:51:08 +0100284// Ensure we don't crash when adding/removing streams while threads are going.
285// We should restore the correct global state at the end.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000286TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800287 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288
289 LogThread thread1, thread2, thread3;
290 thread1.Start();
291 thread2.Start();
292 thread3.Start();
293
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200294 std::string s1, s2, s3;
295 LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296 for (int i = 0; i < 1000; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100297 LogMessage::AddLogToStream(&stream1, LS_WARNING);
298 LogMessage::AddLogToStream(&stream2, LS_INFO);
299 LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300 LogMessage::RemoveLogToStream(&stream1);
301 LogMessage::RemoveLogToStream(&stream2);
302 LogMessage::RemoveLogToStream(&stream3);
303 }
304
deadbeef37f5ecf2017-02-27 14:06:41 -0800305 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000306}
307
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200309 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000310 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
311 EXPECT_GT(time, 1325376000u);
312}
313
Tommifef05002018-02-27 13:51:08 +0100314TEST(LogTest, CheckExtraErrorField) {
315 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
316 ERRCTX_ERRNO, 0xD);
Tommifef05002018-02-27 13:51:08 +0100317 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700318
Tommifef05002018-02-27 13:51:08 +0100319 const std::string& extra = log_msg.get_extra();
320 const size_t length_to_check = arraysize("[0x12345678]") - 1;
321 ASSERT_GE(extra.length(), length_to_check);
322 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
323}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324
Tommifef05002018-02-27 13:51:08 +0100325TEST(LogTest, CheckFilePathParsed) {
326 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
Tommifef05002018-02-27 13:51:08 +0100327 log_msg.stream() << "<- Does this look right?";
328
329 const std::string stream = log_msg.GetPrintStream();
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200330#if defined(WEBRTC_ANDROID)
331 const char* tag = log_msg.get_tag();
332 EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
333 EXPECT_NE(std::string::npos, stream.find("100"));
334#else
Tommifef05002018-02-27 13:51:08 +0100335 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200336#endif
Tommifef05002018-02-27 13:51:08 +0100337}
338
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200339#if defined(WEBRTC_ANDROID)
340TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
341 std::string str;
342 LogSinkImpl<StringStream> stream(&str);
343 LogMessage::AddLogToStream(&stream, LS_INFO);
344 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
345
346 RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
347 EXPECT_NE(std::string::npos, str.find("INFO"));
348 EXPECT_NE(std::string::npos, str.find("my_tag"));
349}
350#endif
351
Tommifef05002018-02-27 13:51:08 +0100352// Test the time required to write 1000 80-character logs to a string.
353TEST(LogTest, Perf) {
354 std::string str;
355 LogSinkImpl<StringStream> stream(&str);
Niels Möller25aefd32018-11-21 09:34:11 +0100356 LogMessage::AddLogToStream(&stream, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000357
Tommifef05002018-02-27 13:51:08 +0100358 const std::string message(80, 'X');
Niels Möller25aefd32018-11-21 09:34:11 +0100359 { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
Tommifef05002018-02-27 13:51:08 +0100360
361 // We now know how many bytes the logging framework will tag onto every msg.
362 const size_t logging_overhead = str.size();
363 // Reset the stream to 0 size.
364 str.clear();
365 str.reserve(120000);
366 static const int kRepetitions = 1000;
367
Honghai Zhang82d78622016-05-06 11:29:15 -0700368 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100369 for (int i = 0; i < kRepetitions; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100370 LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700372 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000373
374 LogMessage::RemoveLogToStream(&stream);
375 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000376
Tommifef05002018-02-27 13:51:08 +0100377 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
378 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
379 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000380}
381
382} // namespace rtc