blob: 4e64209cf2ec4964cc57dec7266fbc9a298b0302 [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;
Niels Möllerff3dd0c2018-06-20 10:27:53 +020043
44 private:
45 std::string& str_;
46 size_t read_pos_;
47 bool read_only_;
48};
49
50StringStream::StringStream(std::string* str)
51 : str_(*str), read_pos_(0), read_only_(false) {}
52
53StringStream::StringStream(const std::string& str)
54 : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
55
56StreamState StringStream::GetState() const {
57 return SS_OPEN;
58}
59
60StreamResult StringStream::Read(void* buffer,
61 size_t buffer_len,
62 size_t* read,
63 int* error) {
64 size_t available = std::min(buffer_len, str_.size() - read_pos_);
65 if (!available)
66 return SR_EOS;
67 memcpy(buffer, str_.data() + read_pos_, available);
68 read_pos_ += available;
69 if (read)
70 *read = available;
71 return SR_SUCCESS;
72}
73
74StreamResult StringStream::Write(const void* data,
75 size_t data_len,
76 size_t* written,
77 int* error) {
78 if (read_only_) {
79 if (error) {
80 *error = -1;
81 }
82 return SR_ERROR;
83 }
84 str_.append(static_cast<const char*>(data),
85 static_cast<const char*>(data) + data_len);
86 if (written)
87 *written = data_len;
88 return SR_SUCCESS;
89}
90
91void StringStream::Close() {}
92
Niels Möllerff3dd0c2018-06-20 10:27:53 +020093} // namespace
94
Tommi0eefb4d2015-05-23 09:54:07 +020095template <typename Base>
Yves Gerey665174f2018-06-19 15:03:05 +020096class LogSinkImpl : public LogSink, public Base {
Tommi0eefb4d2015-05-23 09:54:07 +020097 public:
98 LogSinkImpl() {}
99
Yves Gerey665174f2018-06-19 15:03:05 +0200100 template <typename P>
Tommi00aac5a2015-05-25 11:25:59 +0200101 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +0200102
103 private:
104 void OnLogMessage(const std::string& message) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200105 static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
106 nullptr);
Tommi0eefb4d2015-05-23 09:54:07 +0200107 }
108};
109
Tommifef05002018-02-27 13:51:08 +0100110class LogMessageForTesting : public LogMessage {
111 public:
112 LogMessageForTesting(const char* file,
113 int line,
114 LoggingSeverity sev,
115 LogErrorContext err_ctx = ERRCTX_NONE,
116 int err = 0)
117 : LogMessage(file, line, sev, err_ctx, err) {}
118
119 const std::string& get_extra() const { return extra_; }
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200120#if defined(WEBRTC_ANDROID)
121 const char* get_tag() const { return tag_; }
122#endif
Tommifef05002018-02-27 13:51:08 +0100123
124 // Returns the contents of the internal log stream.
125 // Note that parts of the stream won't (as is) be available until *after* the
126 // dtor of the parent class has run. So, as is, this only represents a
127 // partially built stream.
128 std::string GetPrintStream() {
129 RTC_DCHECK(!is_finished_);
130 is_finished_ = true;
131 FinishPrintStream();
Jonas Olssond8c50782018-09-07 11:21:28 +0200132 return print_stream_.Release();
Tommifef05002018-02-27 13:51:08 +0100133 }
134
135 private:
136 bool is_finished_ = false;
137};
138
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139// Test basic logging operation. We should get the INFO log but not the VERBOSE.
140// We should restore the correct global state at the end.
141TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800142 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
144 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +0200145 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 LogMessage::AddLogToStream(&stream, LS_INFO);
147 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
148
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_INFO) << "INFO";
150 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 EXPECT_NE(std::string::npos, str.find("INFO"));
152 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
153
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200154 int i = 1;
155 long l = 2l;
156 long long ll = 3ll;
157
158 unsigned int u = 4u;
159 unsigned long ul = 5ul;
160 unsigned long long ull = 6ull;
161
162 std::string s1 = "char*";
163 std::string s2 = "std::string";
164 std::string s3 = "absl::stringview";
165
166 void* p = reinterpret_cast<void*>(0xabcd);
167
168 // Log all suported types(except doubles/floats) as a sanity-check.
169 RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
170 << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
171 << absl::string_view(s3) << "|" << p << "|";
172
173 // Signed integers
174 EXPECT_NE(std::string::npos, str.find("|1|"));
175 EXPECT_NE(std::string::npos, str.find("|2|"));
176 EXPECT_NE(std::string::npos, str.find("|3|"));
177
178 // Unsigned integers
179 EXPECT_NE(std::string::npos, str.find("|4|"));
180 EXPECT_NE(std::string::npos, str.find("|5|"));
181 EXPECT_NE(std::string::npos, str.find("|6|"));
182
183 // Strings
184 EXPECT_NE(std::string::npos, str.find("|char*|"));
185 EXPECT_NE(std::string::npos, str.find("|std::string|"));
186 EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
187
188 // void*
189 EXPECT_NE(std::string::npos, str.find("|abcd|"));
190
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +0200192 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800193 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194}
195
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200196#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
197TEST(LogTest, Checks) {
198 EXPECT_DEATH(FATAL() << "message",
199 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200200 "# Fatal error in: \\S+, line \\w+\n"
201 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200202 "# Check failed: FATAL\\(\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200203 "# message");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200204
205 int a = 1, b = 2;
206 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
207 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200208 "# Fatal error in: \\S+, line \\w+\n"
209 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200210 "# Check failed: a == b \\(1 vs. 2\\)\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200211 "# 12");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200212 RTC_CHECK_EQ(5, 5);
213
214 RTC_CHECK(true) << "Shouldn't crash" << 1;
215 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
216 "\n\n#\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200217 "# Fatal error in: \\S+, line \\w+\n"
218 "# last system error: \\w+\n"
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200219 "# Check failed: false\n"
Jonas Olssond000b0a2018-07-03 12:07:53 +0200220 "# Hi there!");
Jonas Olssonf8e5c112018-06-14 13:14:22 +0200221}
222#endif
Jonas Olssond000b0a2018-07-03 12:07:53 +0200223
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224// Test using multiple log streams. The INFO stream should get the INFO message,
225// the VERBOSE stream should get the INFO and the VERBOSE.
226// We should restore the correct global state at the end.
227TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800228 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229
230 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200231 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232 LogMessage::AddLogToStream(&stream1, LS_INFO);
233 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
234 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
235 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
236
Mirko Bonadei675513b2017-11-09 11:09:25 +0100237 RTC_LOG(LS_INFO) << "INFO";
238 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239
240 EXPECT_NE(std::string::npos, str1.find("INFO"));
241 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
242 EXPECT_NE(std::string::npos, str2.find("INFO"));
243 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
244
245 LogMessage::RemoveLogToStream(&stream2);
246 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200247 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
248 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249
deadbeef37f5ecf2017-02-27 14:06:41 -0800250 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000251}
252
Tommifef05002018-02-27 13:51:08 +0100253class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 public:
Tommifef05002018-02-27 13:51:08 +0100255 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
256 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700257
Tommifef05002018-02-27 13:51:08 +0100258 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259
260 private:
Niels Möller25aefd32018-11-21 09:34:11 +0100261 void Run() { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }
Tommifef05002018-02-27 13:51:08 +0100262
263 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
264
265 PlatformThread thread_;
Niels Möllerc572ff32018-11-07 08:43:50 +0100266 Event event_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000267};
268
Tommifef05002018-02-27 13:51:08 +0100269// Ensure we don't crash when adding/removing streams while threads are going.
270// We should restore the correct global state at the end.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000271TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800272 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000273
274 LogThread thread1, thread2, thread3;
275 thread1.Start();
276 thread2.Start();
277 thread3.Start();
278
Niels Möllerff3dd0c2018-06-20 10:27:53 +0200279 std::string s1, s2, s3;
280 LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000281 for (int i = 0; i < 1000; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100282 LogMessage::AddLogToStream(&stream1, LS_WARNING);
283 LogMessage::AddLogToStream(&stream2, LS_INFO);
284 LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 LogMessage::RemoveLogToStream(&stream1);
286 LogMessage::RemoveLogToStream(&stream2);
287 LogMessage::RemoveLogToStream(&stream3);
288 }
289
deadbeef37f5ecf2017-02-27 14:06:41 -0800290 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291}
292
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200294 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
296 EXPECT_GT(time, 1325376000u);
297}
298
Tommifef05002018-02-27 13:51:08 +0100299TEST(LogTest, CheckExtraErrorField) {
300 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
301 ERRCTX_ERRNO, 0xD);
Tommifef05002018-02-27 13:51:08 +0100302 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700303
Tommifef05002018-02-27 13:51:08 +0100304 const std::string& extra = log_msg.get_extra();
305 const size_t length_to_check = arraysize("[0x12345678]") - 1;
306 ASSERT_GE(extra.length(), length_to_check);
307 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
308}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309
Tommifef05002018-02-27 13:51:08 +0100310TEST(LogTest, CheckFilePathParsed) {
311 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
Tommifef05002018-02-27 13:51:08 +0100312 log_msg.stream() << "<- Does this look right?";
313
314 const std::string stream = log_msg.GetPrintStream();
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200315#if defined(WEBRTC_ANDROID)
316 const char* tag = log_msg.get_tag();
317 EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
318 EXPECT_NE(std::string::npos, stream.find("100"));
319#else
Tommifef05002018-02-27 13:51:08 +0100320 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200321#endif
Tommifef05002018-02-27 13:51:08 +0100322}
323
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200324#if defined(WEBRTC_ANDROID)
325TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
326 std::string str;
327 LogSinkImpl<StringStream> stream(&str);
328 LogMessage::AddLogToStream(&stream, LS_INFO);
329 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
330
331 RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
332 EXPECT_NE(std::string::npos, str.find("INFO"));
333 EXPECT_NE(std::string::npos, str.find("my_tag"));
334}
335#endif
336
Tommifef05002018-02-27 13:51:08 +0100337// Test the time required to write 1000 80-character logs to a string.
338TEST(LogTest, Perf) {
339 std::string str;
340 LogSinkImpl<StringStream> stream(&str);
Niels Möller25aefd32018-11-21 09:34:11 +0100341 LogMessage::AddLogToStream(&stream, LS_VERBOSE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342
Tommifef05002018-02-27 13:51:08 +0100343 const std::string message(80, 'X');
Niels Möller25aefd32018-11-21 09:34:11 +0100344 { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
Tommifef05002018-02-27 13:51:08 +0100345
346 // We now know how many bytes the logging framework will tag onto every msg.
347 const size_t logging_overhead = str.size();
348 // Reset the stream to 0 size.
349 str.clear();
350 str.reserve(120000);
351 static const int kRepetitions = 1000;
352
Honghai Zhang82d78622016-05-06 11:29:15 -0700353 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100354 for (int i = 0; i < kRepetitions; ++i) {
Niels Möller25aefd32018-11-21 09:34:11 +0100355 LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700357 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358
359 LogMessage::RemoveLogToStream(&stream);
360 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000361
Tommifef05002018-02-27 13:51:08 +0100362 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
363 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
364 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000365}
366
367} // namespace rtc