[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Mirko Bonadei | 4b719ba | 2017-11-10 18:27:45 | [diff] [blame] | 5 | // Note: this test tests RTC_LOG_V and RTC_LOG_E since all other logs are |
| 6 | // expressed in forms of them. RTC_LOG is also tested for good measure. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 7 | // Also note that we are only allowed to call InitLogging() twice so the test |
| 8 | // cases are more dense than normal. |
| 9 | |
tommi | d7c94e2 | 2015-03-20 10:56:56 | [diff] [blame] | 10 | // We must include Chromium headers before including the overrides header |
| 11 | // since webrtc's logging.h file may conflict with chromium. |
Xiaohan Wang | c082376 | 2022-01-08 01:27:37 | [diff] [blame] | 12 | #include "base/logging.h" |
tommi | d7c94e2 | 2015-03-20 10:56:56 | [diff] [blame] | 13 | #include "base/command_line.h" |
| 14 | #include "base/files/file_util.h" |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 15 | #include "base/files/scoped_temp_dir.h" |
Xiaohan Wang | c082376 | 2022-01-08 01:27:37 | [diff] [blame] | 16 | #include "build/build_config.h" |
tommi | d7c94e2 | 2015-03-20 10:56:56 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
Henrik Kjellander | 061fc99c | 2017-09-15 08:44:50 | [diff] [blame] | 18 | #include "third_party/webrtc_overrides/rtc_base/logging.h" |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 19 | |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 20 | static const int kDefaultVerbosity = 0; |
| 21 | |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 22 | static const char* AsString(rtc::LoggingSeverity severity) { |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 23 | switch (severity) { |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 24 | case rtc::LS_ERROR: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 25 | return "LS_ERROR"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 26 | case rtc::LS_WARNING: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 27 | return "LS_WARNING"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 28 | case rtc::LS_INFO: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 29 | return "LS_INFO"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 30 | case rtc::LS_VERBOSE: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 31 | return "LS_VERBOSE"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 32 | case rtc::LS_SENSITIVE: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 33 | return "LS_SENSITIVE"; |
| 34 | default: |
| 35 | return ""; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static bool ContainsString(const std::string& original, |
| 40 | const char* string_to_match) { |
| 41 | return original.find(string_to_match) != std::string::npos; |
| 42 | } |
| 43 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 44 | class WebRtcTextLogTest : public testing::Test { |
| 45 | public: |
| 46 | void SetUp() override { |
| 47 | ASSERT_TRUE(log_dir_.CreateUniqueTempDir()); |
| 48 | log_file_path_ = log_dir_.GetPath().AppendASCII("webrtc_log"); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 49 | } |
| 50 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 51 | protected: |
| 52 | bool Initialize(int verbosity_level) { |
| 53 | if (verbosity_level != kDefaultVerbosity) { |
| 54 | // Update the command line with specified verbosity level for this file. |
| 55 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 56 | std::ostringstream value_stream; |
| 57 | value_stream << "logging_unittest=" << verbosity_level; |
| 58 | const std::string& value = value_stream.str(); |
| 59 | command_line->AppendSwitchASCII("vmodule", value); |
| 60 | } |
| 61 | |
| 62 | // The command line flags are parsed here and the log file name is set. |
| 63 | logging::LoggingSettings settings; |
| 64 | settings.logging_dest = logging::LOG_TO_FILE; |
| 65 | settings.log_file_path = log_file_path_.value().data(); |
| 66 | settings.lock_log = logging::DONT_LOCK_LOG_FILE; |
| 67 | settings.delete_old = logging::DELETE_OLD_LOG_FILE; |
| 68 | if (!logging::InitLogging(settings)) { |
| 69 | return false; |
| 70 | } |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame] | 71 | |
| 72 | #if BUILDFLAG(USE_RUNTIME_VLOG) |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 73 | EXPECT_TRUE(VLOG_IS_ON(verbosity_level)); |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame] | 74 | #else |
| 75 | // VLOGs default to off when not using runtime vlog. |
| 76 | EXPECT_FALSE(VLOG_IS_ON(verbosity_level)); |
| 77 | #endif // BUILDFLAG(USE_RUNTIME_VLOG) |
| 78 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 79 | EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1)); |
| 80 | return true; |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 81 | } |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 82 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 83 | base::ScopedTempDir log_dir_; |
| 84 | base::FilePath log_file_path_; |
| 85 | }; |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 86 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 87 | TEST_F(WebRtcTextLogTest, DefaultConfiguration) { |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 88 | ASSERT_TRUE(Initialize(kDefaultVerbosity)); |
| 89 | |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 90 | // In the default configuration only warnings and errors should be logged. |
Mirko Bonadei | 4b719ba | 2017-11-10 18:27:45 | [diff] [blame] | 91 | RTC_LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR); |
| 92 | RTC_LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING); |
| 93 | RTC_LOG_V(rtc::LS_INFO) << AsString(rtc::LS_INFO); |
| 94 | RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE); |
| 95 | RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 96 | |
| 97 | // Read file to string. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 98 | std::string contents_of_file; |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 99 | base::ReadFileToString(log_file_path_, &contents_of_file); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 100 | |
| 101 | // Make sure string contains the expected values. |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 102 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR))); |
| 103 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING))); |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 104 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO))); |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 105 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE))); |
| 106 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 107 | } |
| 108 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 109 | TEST_F(WebRtcTextLogTest, InfoConfiguration) { |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 110 | ASSERT_TRUE(Initialize(0)); // 0 == Chrome's 'info' level. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 111 | |
| 112 | // In this configuration everything lower or equal to LS_INFO should be |
| 113 | // logged. |
Mirko Bonadei | 4b719ba | 2017-11-10 18:27:45 | [diff] [blame] | 114 | RTC_LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR); |
| 115 | RTC_LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING); |
| 116 | RTC_LOG_V(rtc::LS_INFO) << AsString(rtc::LS_INFO); |
| 117 | RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE); |
| 118 | RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 119 | |
| 120 | // Read file to string. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 121 | std::string contents_of_file; |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 122 | base::ReadFileToString(log_file_path_, &contents_of_file); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 123 | |
| 124 | // Make sure string contains the expected values. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 125 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR))); |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 126 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING))); |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 127 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO))); |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 128 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE))); |
| 129 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 130 | |
| 131 | // Also check that the log is proper. |
| 132 | EXPECT_TRUE(ContainsString(contents_of_file, "logging_unittest.cc")); |
| 133 | EXPECT_FALSE(ContainsString(contents_of_file, "logging.h")); |
| 134 | EXPECT_FALSE(ContainsString(contents_of_file, "logging.cc")); |
| 135 | } |
| 136 | |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 137 | TEST_F(WebRtcTextLogTest, LogEverythingConfiguration) { |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 138 | ASSERT_TRUE(Initialize(2)); // verbosity at level 2 allows LS_SENSITIVE. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 139 | |
| 140 | // In this configuration everything should be logged. |
Mirko Bonadei | 4b719ba | 2017-11-10 18:27:45 | [diff] [blame] | 141 | RTC_LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR); |
| 142 | RTC_LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING); |
| 143 | RTC_LOG(LS_INFO) << AsString(rtc::LS_INFO); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 144 | static const int kFakeError = 1; |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 145 | RTC_LOG_E(LS_INFO, EN, kFakeError) |
| 146 | << "RTC_LOG_E(" << AsString(rtc::LS_INFO) << ")"; |
Mirko Bonadei | 4b719ba | 2017-11-10 18:27:45 | [diff] [blame] | 147 | RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE); |
| 148 | RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 149 | |
| 150 | // Read file to string. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 151 | std::string contents_of_file; |
Sergey Ulanov | 982aa908 | 2022-01-21 19:22:38 | [diff] [blame] | 152 | base::ReadFileToString(log_file_path_, &contents_of_file); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 153 | |
| 154 | // Make sure string contains the expected values. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 155 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR))); |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 156 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING))); |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame] | 157 | |
| 158 | #if BUILDFLAG(USE_RUNTIME_VLOG) |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 159 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_INFO))); |
Mirko Bonadei | 4b719ba | 2017-11-10 18:27:45 | [diff] [blame] | 160 | // RTC_LOG_E |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 161 | EXPECT_TRUE(ContainsString(contents_of_file, strerror(kFakeError))); |
Sergey Ulanov | 0e22a2e | 2022-01-19 09:47:38 | [diff] [blame] | 162 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE))); |
| 163 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE))); |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame] | 164 | #endif // BUILDFLAG(USE_RUNTIME_VLOG) |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 165 | } |