blob: db744b57bc03e847e79dde105165deb1926051f8 [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
11#include "webrtc/base/fileutils.h"
12#include "webrtc/base/gunit.h"
13#include "webrtc/base/logging.h"
14#include "webrtc/base/pathutils.h"
15#include "webrtc/base/stream.h"
16#include "webrtc/base/thread.h"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000017#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19namespace rtc {
20
Tommi0eefb4d2015-05-23 09:54:07 +020021template <typename Base>
22class LogSinkImpl
23 : public LogSink,
24 public Base {
25 public:
26 LogSinkImpl() {}
27
28 // The non-const reference constructor is required because of StringStream.
29 // TODO(tommi): Fix StringStream to accept a pointer for non-const.
30 template<typename P>
31 explicit LogSinkImpl(P& p) : Base(p) {}
32
33 template<typename P>
34 explicit LogSinkImpl(const P& p) : Base(p) {}
35
36 private:
37 void OnLogMessage(const std::string& message) override {
38 static_cast<Base*>(this)->WriteAll(
39 message.data(), message.size(), nullptr, nullptr);
40 }
41};
42
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043// Test basic logging operation. We should get the INFO log but not the VERBOSE.
44// We should restore the correct global state at the end.
45TEST(LogTest, SingleStream) {
46 int sev = LogMessage::GetLogToStream(NULL);
47
48 std::string str;
Tommi0eefb4d2015-05-23 09:54:07 +020049 LogSinkImpl<StringStream> stream(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050 LogMessage::AddLogToStream(&stream, LS_INFO);
51 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
52
53 LOG(LS_INFO) << "INFO";
54 LOG(LS_VERBOSE) << "VERBOSE";
55 EXPECT_NE(std::string::npos, str.find("INFO"));
56 EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
57
58 LogMessage::RemoveLogToStream(&stream);
Tommi0eefb4d2015-05-23 09:54:07 +020059 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060
61 EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
62}
63
64// Test using multiple log streams. The INFO stream should get the INFO message,
65// the VERBOSE stream should get the INFO and the VERBOSE.
66// We should restore the correct global state at the end.
67TEST(LogTest, MultipleStreams) {
68 int sev = LogMessage::GetLogToStream(NULL);
69
70 std::string str1, str2;
Tommi0eefb4d2015-05-23 09:54:07 +020071 LogSinkImpl<StringStream> stream1(str1), stream2(str2);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 LogMessage::AddLogToStream(&stream1, LS_INFO);
73 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
74 EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
75 EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
76
77 LOG(LS_INFO) << "INFO";
78 LOG(LS_VERBOSE) << "VERBOSE";
79
80 EXPECT_NE(std::string::npos, str1.find("INFO"));
81 EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
82 EXPECT_NE(std::string::npos, str2.find("INFO"));
83 EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
84
85 LogMessage::RemoveLogToStream(&stream2);
86 LogMessage::RemoveLogToStream(&stream1);
Tommi0eefb4d2015-05-23 09:54:07 +020087 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
88 EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089
90 EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
91}
92
93// Ensure we don't crash when adding/removing streams while threads are going.
94// We should restore the correct global state at the end.
95class LogThread : public Thread {
96 public:
97 virtual ~LogThread() {
98 Stop();
99 }
100
101 private:
102 void Run() {
103 // LS_SENSITIVE to avoid cluttering up any real logging going on
104 LOG(LS_SENSITIVE) << "LOG";
105 }
106};
107
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000108TEST(LogTest, MultipleThreads) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109 int sev = LogMessage::GetLogToStream(NULL);
110
111 LogThread thread1, thread2, thread3;
112 thread1.Start();
113 thread2.Start();
114 thread3.Start();
115
Tommi0eefb4d2015-05-23 09:54:07 +0200116 LogSinkImpl<NullStream> stream1, stream2, stream3;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 for (int i = 0; i < 1000; ++i) {
118 LogMessage::AddLogToStream(&stream1, LS_INFO);
119 LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
120 LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
121 LogMessage::RemoveLogToStream(&stream1);
122 LogMessage::RemoveLogToStream(&stream2);
123 LogMessage::RemoveLogToStream(&stream3);
124 }
125
126 EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
127}
128
129
130TEST(LogTest, WallClockStartTime) {
131 uint32 time = LogMessage::WallClockStartTime();
132 // Expect the time to be in a sensible range, e.g. > 2012-01-01.
133 EXPECT_GT(time, 1325376000u);
134}
135
136// Test the time required to write 1000 80-character logs to an unbuffered file.
137TEST(LogTest, Perf) {
138 Pathname path;
139 EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
140 path.SetPathname(Filesystem::TempFilename(path, "ut"));
141
Tommi0eefb4d2015-05-23 09:54:07 +0200142 LogSinkImpl<FileStream> stream;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143 EXPECT_TRUE(stream.Open(path.pathname(), "wb", NULL));
144 stream.DisableBuffering();
145 LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
146
147 uint32 start = Time(), finish;
148 std::string message('X', 80);
149 for (int i = 0; i < 1000; ++i) {
150 LOG(LS_SENSITIVE) << message;
151 }
152 finish = Time();
153
154 LogMessage::RemoveLogToStream(&stream);
155 stream.Close();
156 Filesystem::DeleteFile(path);
157
158 LOG(LS_INFO) << "Average log time: " << TimeDiff(finish, start) << " us";
159}
160
161} // namespace rtc