blob: 079cfb10402caee3ae880df2a267997274572f12 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/timeutils.h"
22#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;
45 bool GetSize(size_t* size) const override;
Niels Möllerff3dd0c2018-06-20 10:27:53 +020046 bool ReserveSize(size_t size) override;
47
48 private:
49 std::string& str_;
50 size_t read_pos_;
51 bool read_only_;
52};
53
54StringStream::StringStream(std::string* str)
55 : str_(*str), read_pos_(0), read_only_(false) {}
56
57StringStream::StringStream(const std::string& str)
58 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
59
60StreamState StringStream::GetState() const {
61 return SS_OPEN;
62}
63
64StreamResult StringStream::Read(void* buffer,
65 size_t buffer_len,
66 size_t* read,
67 int* error) {
68 size_t available = std::min(buffer_len, str_.size() - read_pos_);
69 if (!available)
70 return SR_EOS;
71 memcpy(buffer, str_.data() + read_pos_, available);
72 read_pos_ += available;
73 if (read)
74 *read = available;
75 return SR_SUCCESS;
76}
77
78StreamResult StringStream::Write(const void* data,
79 size_t data_len,
80 size_t* written,
81 int* error) {
82 if (read_only_) {
83 if (error) {
84 *error = -1;
85 }
86 return SR_ERROR;
87 }
88 str_.append(static_cast<const char*>(data),
89 static_cast<const char*>(data) + data_len);
90 if (written)
91 *written = data_len;
92 return SR_SUCCESS;
93}
94
95void StringStream::Close() {}
96
97bool StringStream::SetPosition(size_t position) {
98 if (position > str_.size())
99 return false;
100 read_pos_ = position;
101 return true;
102}
103
104bool StringStream::GetPosition(size_t* position) const {
105 if (position)
106 *position = read_pos_;
107 return true;
108}
109
110bool StringStream::GetSize(size_t* size) const {
111 if (size)
112 *size = str_.size();
113 return true;
114}
115
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200116bool StringStream::ReserveSize(size_t size) {
117 if (read_only_)
118 return false;
119 str_.reserve(size);
120 return true;
121}
122
123} // namespace
124
Tommi0eefb4d2015-05-23 09:54:07 +0200125template <typename Base>
Yves Gerey665174f2018-06-19 15:03:05 +0200126class LogSinkImpl : public LogSink, public Base {
Tommi0eefb4d2015-05-23 09:54:07 +0200127 public:
128 LogSinkImpl() {}
129
Yves Gerey665174f2018-06-19 15:03:05 +0200130 template <typename P>
Tommi00aac5a2015-05-25 11:25:59 +0200131 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +0200132
133 private:
134 void OnLogMessage(const std::string& message) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200135 static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
136 nullptr);
Tommi0eefb4d2015-05-23 09:54:07 +0200137 }
138};
139
Tommifef05002018-02-27 13:51:08 +0100140class LogMessageForTesting : public LogMessage {
141 public:
142 LogMessageForTesting(const char* file,
143 int line,
144 LoggingSeverity sev,
145 LogErrorContext err_ctx = ERRCTX_NONE,
146 int err = 0)
147 : LogMessage(file, line, sev, err_ctx, err) {}
148
149 const std::string& get_extra() const { return extra_; }
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200150#if defined(WEBRTC_ANDROID)
151 const char* get_tag() const { return tag_; }
152#endif
Tommifef05002018-02-27 13:51:08 +0100153
154 // Returns the contents of the internal log stream.
155 // Note that parts of the stream won't (as is) be available until *after* the
156 // dtor of the parent class has run. So, as is, this only represents a
157 // partially built stream.
158 std::string GetPrintStream() {
159 RTC_DCHECK(!is_finished_);
160 is_finished_ = true;
161 FinishPrintStream();
Jonas Olssond8c50782018-09-07 11:21:28 +0200162 return print_stream_.Release();
Tommifef05002018-02-27 13:51:08 +0100163 }
164
165 private:
166 bool is_finished_ = false;
167};
168
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169// Test basic logging operation. We should get the INFO log but not the VERBOSE.
170// We should restore the correct global state at the end.
171TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800172 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173
174 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +0200175 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 LogMessage::AddLogToStream(&stream, LS_INFO);
177 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
178
Mirko Bonadei675513b2017-11-09 11:09:25 +0100179 RTC_LOG(LS_INFO) << "INFO";
180 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000181 EXPECT_NE(std::string::npos, str.find("INFO"));
182 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
183
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200184 int i = 1;
185 long l = 2l;
186 long long ll = 3ll;
187
188 unsigned int u = 4u;
189 unsigned long ul = 5ul;
190 unsigned long long ull = 6ull;
191
192 std::string s1 = "char*";
193 std::string s2 = "std::string";
194 std::string s3 = "absl::stringview";
195
196 void* p = reinterpret_cast<void*>(0xabcd);
197
198 // Log all suported types(except doubles/floats) as a sanity-check.
199 RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
200 << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
201 << absl::string_view(s3) << "|" << p << "|";
202
203 // Signed integers
204 EXPECT_NE(std::string::npos, str.find("|1|"));
205 EXPECT_NE(std::string::npos, str.find("|2|"));
206 EXPECT_NE(std::string::npos, str.find("|3|"));
207
208 // Unsigned integers
209 EXPECT_NE(std::string::npos, str.find("|4|"));
210 EXPECT_NE(std::string::npos, str.find("|5|"));
211 EXPECT_NE(std::string::npos, str.find("|6|"));
212
213 // Strings
214 EXPECT_NE(std::string::npos, str.find("|char*|"));
215 EXPECT_NE(std::string::npos, str.find("|std::string|"));
216 EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
217
218 // void*
219 EXPECT_NE(std::string::npos, str.find("|abcd|"));
220
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +0200222 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800223 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224}
225
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200226#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
227TEST(LogTest, Checks) {
228 EXPECT_DEATH(FATAL() << "message",
229 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200230 "# Fatal error in: \\S+, line \\w+\n"
231 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200232 "# Check failed: FATAL\\(\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200233 "# message");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200234
235 int a = 1, b = 2;
236 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
237 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200238 "# Fatal error in: \\S+, line \\w+\n"
239 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200240 "# Check failed: a == b \\(1 vs. 2\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200241 "# 12");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200242 RTC_CHECK_EQ(5, 5);
243
244 RTC_CHECK(true) << "Shouldn't crash" << 1;
245 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
246 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200247 "# Fatal error in: \\S+, line \\w+\n"
248 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200249 "# Check failed: false\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200250 "# Hi there!");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200251}
252#endif
Jonas Olssond000b0a2018-07-03 12:07:53 +0200253
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254// Test using multiple log streams. The INFO stream should get the INFO message,
255// the VERBOSE stream should get the INFO and the VERBOSE.
256// We should restore the correct global state at the end.
257TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800258 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259
260 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200261 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 LogMessage::AddLogToStream(&stream1, LS_INFO);
263 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
264 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
265 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
266
Mirko Bonadei675513b2017-11-09 11:09:25 +0100267 RTC_LOG(LS_INFO) << "INFO";
268 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000269
270 EXPECT_NE(std::string::npos, str1.find("INFO"));
271 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
272 EXPECT_NE(std::string::npos, str2.find("INFO"));
273 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
274
275 LogMessage::RemoveLogToStream(&stream2);
276 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200277 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
278 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279
deadbeef37f5ecf2017-02-27 14:06:41 -0800280 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000281}
282
Tommifef05002018-02-27 13:51:08 +0100283class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 public:
Tommifef05002018-02-27 13:51:08 +0100285 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
286 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700287
Tommifef05002018-02-27 13:51:08 +0100288 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289
290 private:
Niels Möller25aefd32018-11-21 09:34:11 +0100291 void Run() { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }
Tommifef05002018-02-27 13:51:08 +0100292
293 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
294
295 PlatformThread thread_;
Niels Möllerc572ff32018-11-07 08:43:50 +0100296 Event event_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297};
298
Tommifef05002018-02-27 13:51:08 +0100299// Ensure we don't crash when adding/removing streams while threads are going.
300// We should restore the correct global state at the end.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000301TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800302 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303
304 LogThread thread1, thread2, thread3;
305 thread1.Start();
306 thread2.Start();
307 thread3.Start();
308
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200309 std::string s1, s2, s3;
310 LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000311 for (int i = 0; i < 1000; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100312 LogMessage::AddLogToStream(&stream1, LS_WARNING);
313 LogMessage::AddLogToStream(&stream2, LS_INFO);
314 LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 LogMessage::RemoveLogToStream(&stream1);
316 LogMessage::RemoveLogToStream(&stream2);
317 LogMessage::RemoveLogToStream(&stream3);
318 }
319
deadbeef37f5ecf2017-02-27 14:06:41 -0800320 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321}
322
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200324 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
326 EXPECT_GT(time, 1325376000u);
327}
328
Tommifef05002018-02-27 13:51:08 +0100329TEST(LogTest, CheckExtraErrorField) {
330 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
331 ERRCTX_ERRNO, 0xD);
Tommifef05002018-02-27 13:51:08 +0100332 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700333
Tommifef05002018-02-27 13:51:08 +0100334 const std::string& extra = log_msg.get_extra();
335 const size_t length_to_check = arraysize("[0x12345678]") - 1;
336 ASSERT_GE(extra.length(), length_to_check);
337 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
338}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339
Tommifef05002018-02-27 13:51:08 +0100340TEST(LogTest, CheckFilePathParsed) {
341 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
Tommifef05002018-02-27 13:51:08 +0100342 log_msg.stream() << "<- Does this look right?";
343
344 const std::string stream = log_msg.GetPrintStream();
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200345#if defined(WEBRTC_ANDROID)
346 const char* tag = log_msg.get_tag();
347 EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
348 EXPECT_NE(std::string::npos, stream.find("100"));
349#else
Tommifef05002018-02-27 13:51:08 +0100350 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200351#endif
Tommifef05002018-02-27 13:51:08 +0100352}
353
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200354#if defined(WEBRTC_ANDROID)
355TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
356 std::string str;
357 LogSinkImpl<StringStream> stream(&str);
358 LogMessage::AddLogToStream(&stream, LS_INFO);
359 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
360
361 RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
362 EXPECT_NE(std::string::npos, str.find("INFO"));
363 EXPECT_NE(std::string::npos, str.find("my_tag"));
364}
365#endif
366
Tommifef05002018-02-27 13:51:08 +0100367// Test the time required to write 1000 80-character logs to a string.
368TEST(LogTest, Perf) {
369 std::string str;
370 LogSinkImpl<StringStream> stream(&str);
Niels Möller25aefd32018-11-21 09:34:11 +0100371 LogMessage::AddLogToStream(&stream, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000372
Tommifef05002018-02-27 13:51:08 +0100373 const std::string message(80, 'X');
Niels Möller25aefd32018-11-21 09:34:11 +0100374 { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
Tommifef05002018-02-27 13:51:08 +0100375
376 // We now know how many bytes the logging framework will tag onto every msg.
377 const size_t logging_overhead = str.size();
378 // Reset the stream to 0 size.
379 str.clear();
380 str.reserve(120000);
381 static const int kRepetitions = 1000;
382
Honghai Zhang82d78622016-05-06 11:29:15 -0700383 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100384 for (int i = 0; i < kRepetitions; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100385 LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000386 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700387 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000388
389 LogMessage::RemoveLogToStream(&stream);
390 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000391
Tommifef05002018-02-27 13:51:08 +0100392 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
393 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
394 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395}
396
397} // namespace rtc