blob: 5bf78edc2666819aa843b3a76e7f68595866cb9e [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"
Tommifef05002018-02-27 13:51:08 +010012#include "rtc_base/arraysize.h"
13#include "rtc_base/checks.h"
14#include "rtc_base/event.h"
15#include "rtc_base/gunit.h"
16#include "rtc_base/platform_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/testsupport/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
Tommi0eefb4d2015-05-23 09:54:07 +020022template <typename Base>
23class LogSinkImpl
24 : public LogSink,
25 public Base {
26 public:
27 LogSinkImpl() {}
28
Tommi0eefb4d2015-05-23 09:54:07 +020029 template<typename P>
Tommi00aac5a2015-05-25 11:25:59 +020030 explicit LogSinkImpl(P* p) : Base(p) {}
Tommi0eefb4d2015-05-23 09:54:07 +020031
32 private:
33 void OnLogMessage(const std::string& message) override {
34 static_cast<Base*>(this)->WriteAll(
35 message.data(), message.size(), nullptr, nullptr);
36 }
37};
38
Tommifef05002018-02-27 13:51:08 +010039class LogMessageForTesting : public LogMessage {
40 public:
41 LogMessageForTesting(const char* file,
42 int line,
43 LoggingSeverity sev,
44 LogErrorContext err_ctx = ERRCTX_NONE,
45 int err = 0)
46 : LogMessage(file, line, sev, err_ctx, err) {}
47
48 const std::string& get_extra() const { return extra_; }
49 bool is_noop() const { return is_noop_; }
50
51 // Returns the contents of the internal log stream.
52 // Note that parts of the stream won't (as is) be available until *after* the
53 // dtor of the parent class has run. So, as is, this only represents a
54 // partially built stream.
55 std::string GetPrintStream() {
56 RTC_DCHECK(!is_finished_);
57 is_finished_ = true;
58 FinishPrintStream();
59 std::string ret = print_stream_.str();
60 // Just to make an error even more clear if the stream gets used after this.
61 print_stream_.clear();
62 return ret;
63 }
64
65 private:
66 bool is_finished_ = false;
67};
68
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069// Test basic logging operation. We should get the INFO log but not the VERBOSE.
70// We should restore the correct global state at the end.
71TEST(LogTest, SingleStream) {
deadbeef37f5ecf2017-02-27 14:06:41 -080072 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
74 std::string str;
Tommi00aac5a2015-05-25 11:25:59 +020075 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 LogMessage::AddLogToStream(&stream, LS_INFO);
77 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
78
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(LS_INFO) << "INFO";
80 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 EXPECT_NE(std::string::npos, str.find("INFO"));
82 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
83
84 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +020085 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086
deadbeef37f5ecf2017-02-27 14:06:41 -080087 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088}
89
Jonas Olssonae188862018-06-18 11:19:22 +020090/*
Jonas Olssonf8e5c112018-06-14 13:14:22 +020091#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
92TEST(LogTest, Checks) {
93 EXPECT_DEATH(FATAL() << "message",
94 "\n\n#\n"
95 "# Fatal error in: \\S+, line \\d+\n"
96 "# last system error: \\d+\n"
97 "# Check failed: FATAL\\(\\)\n"
98 "# message"
99 );
100
101 int a = 1, b = 2;
102 EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
103 "\n\n#\n"
104 "# Fatal error in: \\S+, line \\d+\n"
105 "# last system error: \\d+\n"
106 "# Check failed: a == b \\(1 vs. 2\\)\n"
107 "# 12"
108 );
109 RTC_CHECK_EQ(5, 5);
110
111 RTC_CHECK(true) << "Shouldn't crash" << 1;
112 EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
113 "\n\n#\n"
114 "# Fatal error in: \\S+, line \\d+\n"
115 "# last system error: \\d+\n"
116 "# Check failed: false\n"
117 "# Hi there!"
118 );
119}
120#endif
Jonas Olssonae188862018-06-18 11:19:22 +0200121*/
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122// Test using multiple log streams. The INFO stream should get the INFO message,
123// the VERBOSE stream should get the INFO and the VERBOSE.
124// We should restore the correct global state at the end.
125TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800126 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
128 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +0200129 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 LogMessage::AddLogToStream(&stream1, LS_INFO);
131 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
132 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
133 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
134
Mirko Bonadei675513b2017-11-09 11:09:25 +0100135 RTC_LOG(LS_INFO) << "INFO";
136 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137
138 EXPECT_NE(std::string::npos, str1.find("INFO"));
139 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
140 EXPECT_NE(std::string::npos, str2.find("INFO"));
141 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
142
143 LogMessage::RemoveLogToStream(&stream2);
144 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200145 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
146 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147
deadbeef37f5ecf2017-02-27 14:06:41 -0800148 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149}
150
Tommifef05002018-02-27 13:51:08 +0100151class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 public:
Tommifef05002018-02-27 13:51:08 +0100153 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
154 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700155
Tommifef05002018-02-27 13:51:08 +0100156 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157
158 private:
Tommifef05002018-02-27 13:51:08 +0100159 void Run() {
160 // LS_SENSITIVE by default to avoid cluttering up any real logging going on.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100161 RTC_LOG(LS_SENSITIVE) << "RTC_LOG";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162 }
Tommifef05002018-02-27 13:51:08 +0100163
164 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
165
166 PlatformThread thread_;
167 Event event_{false, false};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168};
169
Tommifef05002018-02-27 13:51:08 +0100170// Ensure we don't crash when adding/removing streams while threads are going.
171// We should restore the correct global state at the end.
172// This test also makes sure that the 'noop' stream() singleton object, can be
173// safely used from mutiple threads since the threads log at LS_SENSITIVE
174// (by default 'noop' entries).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000175TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800176 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177
178 LogThread thread1, thread2, thread3;
179 thread1.Start();
180 thread2.Start();
181 thread3.Start();
182
Tommi0eefb4d2015-05-23 09:54:07 +0200183 LogSinkImpl<NullStream> stream1, stream2, stream3;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 for (int i = 0; i < 1000; ++i) {
185 LogMessage::AddLogToStream(&stream1, LS_INFO);
186 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
187 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
188 LogMessage::RemoveLogToStream(&stream1);
189 LogMessage::RemoveLogToStream(&stream2);
190 LogMessage::RemoveLogToStream(&stream3);
191 }
192
deadbeef37f5ecf2017-02-27 14:06:41 -0800193 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194}
195
196
197TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
200 EXPECT_GT(time, 1325376000u);
201}
202
Tommifef05002018-02-27 13:51:08 +0100203TEST(LogTest, CheckExtraErrorField) {
204 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
205 ERRCTX_ERRNO, 0xD);
206 ASSERT_FALSE(log_msg.is_noop());
207 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700208
Tommifef05002018-02-27 13:51:08 +0100209 const std::string& extra = log_msg.get_extra();
210 const size_t length_to_check = arraysize("[0x12345678]") - 1;
211 ASSERT_GE(extra.length(), length_to_check);
212 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
213}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214
Tommifef05002018-02-27 13:51:08 +0100215TEST(LogTest, CheckFilePathParsed) {
216 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
217 ASSERT_FALSE(log_msg.is_noop());
218 log_msg.stream() << "<- Does this look right?";
219
220 const std::string stream = log_msg.GetPrintStream();
221 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
222}
223
224TEST(LogTest, CheckNoopLogEntry) {
225 if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) {
226 printf("CheckNoopLogEntry: skipping. Global severity is being overridden.");
227 return;
228 }
229
230 // Logging at LS_SENSITIVE severity, is by default turned off, so this should
231 // be treated as a noop message.
232 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_SENSITIVE);
233 log_msg.stream() << "Should be logged to nowhere.";
234 EXPECT_TRUE(log_msg.is_noop());
235 const std::string stream = log_msg.GetPrintStream();
236 EXPECT_TRUE(stream.empty());
237}
238
239// Test the time required to write 1000 80-character logs to a string.
240TEST(LogTest, Perf) {
241 std::string str;
242 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000243 LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
244
Tommifef05002018-02-27 13:51:08 +0100245 const std::string message(80, 'X');
246 {
247 // Just to be sure that we're not measuring the performance of logging
248 // noop log messages.
249 LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE);
250 ASSERT_FALSE(sanity_check_msg.is_noop());
251 }
252
253 // We now know how many bytes the logging framework will tag onto every msg.
254 const size_t logging_overhead = str.size();
255 // Reset the stream to 0 size.
256 str.clear();
257 str.reserve(120000);
258 static const int kRepetitions = 1000;
259
Honghai Zhang82d78622016-05-06 11:29:15 -0700260 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100261 for (int i = 0; i < kRepetitions; ++i) {
262 LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700264 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265
266 LogMessage::RemoveLogToStream(&stream);
267 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000268
Tommifef05002018-02-27 13:51:08 +0100269 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
270 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
271 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272}
273
274} // namespace rtc