1/*
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"
17#include "webrtc/test/testsupport/gtest_disable.h"
18
19namespace rtc {
20
21// Test basic logging operation. We should get the INFO log but not the VERBOSE.
22// We should restore the correct global state at the end.
23TEST(LogTest, SingleStream) {
24  int sev = LogMessage::GetLogToStream(NULL);
25
26  std::string str;
27  StringStream stream(str);
28  LogMessage::AddLogToStream(&stream, LS_INFO);
29  EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
30
31  LOG(LS_INFO) << "INFO";
32  LOG(LS_VERBOSE) << "VERBOSE";
33  EXPECT_NE(std::string::npos, str.find("INFO"));
34  EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
35
36  LogMessage::RemoveLogToStream(&stream);
37  EXPECT_EQ(LogMessage::NO_LOGGING, LogMessage::GetLogToStream(&stream));
38
39  EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
40}
41
42// Test using multiple log streams. The INFO stream should get the INFO message,
43// the VERBOSE stream should get the INFO and the VERBOSE.
44// We should restore the correct global state at the end.
45TEST(LogTest, MultipleStreams) {
46  int sev = LogMessage::GetLogToStream(NULL);
47
48  std::string str1, str2;
49  StringStream stream1(str1), stream2(str2);
50  LogMessage::AddLogToStream(&stream1, LS_INFO);
51  LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
52  EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
53  EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
54
55  LOG(LS_INFO) << "INFO";
56  LOG(LS_VERBOSE) << "VERBOSE";
57
58  EXPECT_NE(std::string::npos, str1.find("INFO"));
59  EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
60  EXPECT_NE(std::string::npos, str2.find("INFO"));
61  EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
62
63  LogMessage::RemoveLogToStream(&stream2);
64  LogMessage::RemoveLogToStream(&stream1);
65  EXPECT_EQ(LogMessage::NO_LOGGING, LogMessage::GetLogToStream(&stream2));
66  EXPECT_EQ(LogMessage::NO_LOGGING, LogMessage::GetLogToStream(&stream1));
67
68  EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
69}
70
71// Ensure we don't crash when adding/removing streams while threads are going.
72// We should restore the correct global state at the end.
73class LogThread : public Thread {
74 public:
75  virtual ~LogThread() {
76    Stop();
77  }
78
79 private:
80  void Run() {
81    // LS_SENSITIVE to avoid cluttering up any real logging going on
82    LOG(LS_SENSITIVE) << "LOG";
83  }
84};
85
86TEST(LogTest, DISABLED_ON_MAC(MultipleThreads)) {
87  int sev = LogMessage::GetLogToStream(NULL);
88
89  LogThread thread1, thread2, thread3;
90  thread1.Start();
91  thread2.Start();
92  thread3.Start();
93
94  NullStream stream1, stream2, stream3;
95  for (int i = 0; i < 1000; ++i) {
96    LogMessage::AddLogToStream(&stream1, LS_INFO);
97    LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
98    LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
99    LogMessage::RemoveLogToStream(&stream1);
100    LogMessage::RemoveLogToStream(&stream2);
101    LogMessage::RemoveLogToStream(&stream3);
102  }
103
104  EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
105}
106
107
108TEST(LogTest, WallClockStartTime) {
109  uint32 time = LogMessage::WallClockStartTime();
110  // Expect the time to be in a sensible range, e.g. > 2012-01-01.
111  EXPECT_GT(time, 1325376000u);
112}
113
114// Test the time required to write 1000 80-character logs to an unbuffered file.
115TEST(LogTest, Perf) {
116  Pathname path;
117  EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
118  path.SetPathname(Filesystem::TempFilename(path, "ut"));
119
120  FileStream stream;
121  EXPECT_TRUE(stream.Open(path.pathname(), "wb", NULL));
122  stream.DisableBuffering();
123  LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
124
125  uint32 start = Time(), finish;
126  std::string message('X', 80);
127  for (int i = 0; i < 1000; ++i) {
128    LOG(LS_SENSITIVE) << message;
129  }
130  finish = Time();
131
132  LogMessage::RemoveLogToStream(&stream);
133  stream.Close();
134  Filesystem::DeleteFile(path);
135
136  LOG(LS_INFO) << "Average log time: " << TimeDiff(finish, start) << " us";
137}
138
139}  // namespace rtc
140