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