blob: 1be0b24b87f3436d2397882db7ad7291d6981e8d [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
90// Test using multiple log streams. The INFO stream should get the INFO message,
91// the VERBOSE stream should get the INFO and the VERBOSE.
92// We should restore the correct global state at the end.
93TEST(LogTest, MultipleStreams) {
deadbeef37f5ecf2017-02-27 14:06:41 -080094 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095
96 std::string str1, str2;
Tommi00aac5a2015-05-25 11:25:59 +020097 LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 LogMessage::AddLogToStream(&stream1, LS_INFO);
99 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
100 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
101 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
102
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(LS_INFO) << "INFO";
104 RTC_LOG(LS_VERBOSE) << "VERBOSE";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105
106 EXPECT_NE(std::string::npos, str1.find("INFO"));
107 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
108 EXPECT_NE(std::string::npos, str2.find("INFO"));
109 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
110
111 LogMessage::RemoveLogToStream(&stream2);
112 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +0200113 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
114 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115
deadbeef37f5ecf2017-02-27 14:06:41 -0800116 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117}
118
Tommifef05002018-02-27 13:51:08 +0100119class LogThread {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 public:
Tommifef05002018-02-27 13:51:08 +0100121 LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
122 ~LogThread() { thread_.Stop(); }
tommie7251592017-07-14 14:44:46 -0700123
Tommifef05002018-02-27 13:51:08 +0100124 void Start() { thread_.Start(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
126 private:
Tommifef05002018-02-27 13:51:08 +0100127 void Run() {
128 // LS_SENSITIVE by default to avoid cluttering up any real logging going on.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100129 RTC_LOG(LS_SENSITIVE) << "RTC_LOG";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 }
Tommifef05002018-02-27 13:51:08 +0100131
132 static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
133
134 PlatformThread thread_;
135 Event event_{false, false};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136};
137
Tommifef05002018-02-27 13:51:08 +0100138// Ensure we don't crash when adding/removing streams while threads are going.
139// We should restore the correct global state at the end.
140// This test also makes sure that the 'noop' stream() singleton object, can be
141// safely used from mutiple threads since the threads log at LS_SENSITIVE
142// (by default 'noop' entries).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000143TEST(LogTest, MultipleThreads) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800144 int sev = LogMessage::GetLogToStream(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145
146 LogThread thread1, thread2, thread3;
147 thread1.Start();
148 thread2.Start();
149 thread3.Start();
150
Tommi0eefb4d2015-05-23 09:54:07 +0200151 LogSinkImpl<NullStream> stream1, stream2, stream3;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 for (int i = 0; i < 1000; ++i) {
153 LogMessage::AddLogToStream(&stream1, LS_INFO);
154 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
155 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
156 LogMessage::RemoveLogToStream(&stream1);
157 LogMessage::RemoveLogToStream(&stream2);
158 LogMessage::RemoveLogToStream(&stream3);
159 }
160
deadbeef37f5ecf2017-02-27 14:06:41 -0800161 EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162}
163
164
165TEST(LogTest, WallClockStartTime) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200166 uint32_t time = LogMessage::WallClockStartTime();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
168 EXPECT_GT(time, 1325376000u);
169}
170
Tommifef05002018-02-27 13:51:08 +0100171TEST(LogTest, CheckExtraErrorField) {
172 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
173 ERRCTX_ERRNO, 0xD);
174 ASSERT_FALSE(log_msg.is_noop());
175 log_msg.stream() << "This gets added at dtor time";
phoglundbb738732016-07-15 03:57:12 -0700176
Tommifef05002018-02-27 13:51:08 +0100177 const std::string& extra = log_msg.get_extra();
178 const size_t length_to_check = arraysize("[0x12345678]") - 1;
179 ASSERT_GE(extra.length(), length_to_check);
180 EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
181}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182
Tommifef05002018-02-27 13:51:08 +0100183TEST(LogTest, CheckFilePathParsed) {
184 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
185 ASSERT_FALSE(log_msg.is_noop());
186 log_msg.stream() << "<- Does this look right?";
187
188 const std::string stream = log_msg.GetPrintStream();
189 EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
190}
191
192TEST(LogTest, CheckNoopLogEntry) {
193 if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) {
194 printf("CheckNoopLogEntry: skipping. Global severity is being overridden.");
195 return;
196 }
197
198 // Logging at LS_SENSITIVE severity, is by default turned off, so this should
199 // be treated as a noop message.
200 LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_SENSITIVE);
201 log_msg.stream() << "Should be logged to nowhere.";
202 EXPECT_TRUE(log_msg.is_noop());
203 const std::string stream = log_msg.GetPrintStream();
204 EXPECT_TRUE(stream.empty());
205}
206
207// Test the time required to write 1000 80-character logs to a string.
208TEST(LogTest, Perf) {
209 std::string str;
210 LogSinkImpl<StringStream> stream(&str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
212
Tommifef05002018-02-27 13:51:08 +0100213 const std::string message(80, 'X');
214 {
215 // Just to be sure that we're not measuring the performance of logging
216 // noop log messages.
217 LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE);
218 ASSERT_FALSE(sanity_check_msg.is_noop());
219 }
220
221 // We now know how many bytes the logging framework will tag onto every msg.
222 const size_t logging_overhead = str.size();
223 // Reset the stream to 0 size.
224 str.clear();
225 str.reserve(120000);
226 static const int kRepetitions = 1000;
227
Honghai Zhang82d78622016-05-06 11:29:15 -0700228 int64_t start = TimeMillis(), finish;
Tommifef05002018-02-27 13:51:08 +0100229 for (int i = 0; i < kRepetitions; ++i) {
230 LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231 }
Honghai Zhang82d78622016-05-06 11:29:15 -0700232 finish = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233
234 LogMessage::RemoveLogToStream(&stream);
235 stream.Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236
Tommifef05002018-02-27 13:51:08 +0100237 EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
238 RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
239 << " total bytes logged: " << str.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240}
241
242} // namespace rtc