blob: 2085eae96e2be064558dd02544c4e2bbd9575e94 [file] [log] [blame]
[email protected]48fe88f2012-01-17 18:08:491// 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 Bonadei4b719ba2017-11-10 18:27:455// 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]48fe88f2012-01-17 18:08:497// Also note that we are only allowed to call InitLogging() twice so the test
8// cases are more dense than normal.
9
tommid7c94e22015-03-20 10:56:5610// We must include Chromium headers before including the overrides header
11// since webrtc's logging.h file may conflict with chromium.
Xiaohan Wangc0823762022-01-08 01:27:3712#include "base/logging.h"
tommid7c94e22015-03-20 10:56:5613#include "base/command_line.h"
14#include "base/files/file_util.h"
Sergey Ulanov982aa9082022-01-21 19:22:3815#include "base/files/scoped_temp_dir.h"
Xiaohan Wangc0823762022-01-08 01:27:3716#include "build/build_config.h"
tommid7c94e22015-03-20 10:56:5617#include "testing/gtest/include/gtest/gtest.h"
Henrik Kjellander061fc99c2017-09-15 08:44:5018#include "third_party/webrtc_overrides/rtc_base/logging.h"
[email protected]48fe88f2012-01-17 18:08:4919
[email protected]48fe88f2012-01-17 18:08:4920static const int kDefaultVerbosity = 0;
21
[email protected]e758d4c2014-08-06 16:48:1622static const char* AsString(rtc::LoggingSeverity severity) {
[email protected]48fe88f2012-01-17 18:08:4923 switch (severity) {
[email protected]e758d4c2014-08-06 16:48:1624 case rtc::LS_ERROR:
[email protected]48fe88f2012-01-17 18:08:4925 return "LS_ERROR";
[email protected]e758d4c2014-08-06 16:48:1626 case rtc::LS_WARNING:
[email protected]48fe88f2012-01-17 18:08:4927 return "LS_WARNING";
[email protected]e758d4c2014-08-06 16:48:1628 case rtc::LS_INFO:
[email protected]48fe88f2012-01-17 18:08:4929 return "LS_INFO";
[email protected]e758d4c2014-08-06 16:48:1630 case rtc::LS_VERBOSE:
[email protected]48fe88f2012-01-17 18:08:4931 return "LS_VERBOSE";
[email protected]e758d4c2014-08-06 16:48:1632 case rtc::LS_SENSITIVE:
[email protected]48fe88f2012-01-17 18:08:4933 return "LS_SENSITIVE";
34 default:
35 return "";
36 }
37}
38
39static 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 Ulanov982aa9082022-01-21 19:22:3844class 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]48fe88f2012-01-17 18:08:4949 }
50
Sergey Ulanov982aa9082022-01-21 19:22:3851 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 Xiaa0559da2022-05-05 19:42:4571
72#if BUILDFLAG(USE_RUNTIME_VLOG)
Sergey Ulanov982aa9082022-01-21 19:22:3873 EXPECT_TRUE(VLOG_IS_ON(verbosity_level));
Xiyuan Xiaa0559da2022-05-05 19:42:4574#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 Ulanov982aa9082022-01-21 19:22:3879 EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1));
80 return true;
[email protected]48fe88f2012-01-17 18:08:4981 }
[email protected]48fe88f2012-01-17 18:08:4982
Sergey Ulanov982aa9082022-01-21 19:22:3883 base::ScopedTempDir log_dir_;
84 base::FilePath log_file_path_;
85};
Sergey Ulanov0e22a2e2022-01-19 09:47:3886
Sergey Ulanov982aa9082022-01-21 19:22:3887TEST_F(WebRtcTextLogTest, DefaultConfiguration) {
[email protected]48fe88f2012-01-17 18:08:4988 ASSERT_TRUE(Initialize(kDefaultVerbosity));
89
tommi557da2ea2015-05-14 13:19:3890 // In the default configuration only warnings and errors should be logged.
Mirko Bonadei4b719ba2017-11-10 18:27:4591 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]48fe88f2012-01-17 18:08:4996
97 // Read file to string.
[email protected]48fe88f2012-01-17 18:08:4998 std::string contents_of_file;
Sergey Ulanov982aa9082022-01-21 19:22:3899 base::ReadFileToString(log_file_path_, &contents_of_file);
[email protected]48fe88f2012-01-17 18:08:49100
101 // Make sure string contains the expected values.
tommi557da2ea2015-05-14 13:19:38102 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
103 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
[email protected]e758d4c2014-08-06 16:48:16104 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
Sergey Ulanov0e22a2e2022-01-19 09:47:38105 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE)));
106 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE)));
[email protected]48fe88f2012-01-17 18:08:49107}
108
Sergey Ulanov982aa9082022-01-21 19:22:38109TEST_F(WebRtcTextLogTest, InfoConfiguration) {
tommi557da2ea2015-05-14 13:19:38110 ASSERT_TRUE(Initialize(0)); // 0 == Chrome's 'info' level.
[email protected]48fe88f2012-01-17 18:08:49111
112 // In this configuration everything lower or equal to LS_INFO should be
113 // logged.
Mirko Bonadei4b719ba2017-11-10 18:27:45114 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]48fe88f2012-01-17 18:08:49119
120 // Read file to string.
[email protected]48fe88f2012-01-17 18:08:49121 std::string contents_of_file;
Sergey Ulanov982aa9082022-01-21 19:22:38122 base::ReadFileToString(log_file_path_, &contents_of_file);
[email protected]48fe88f2012-01-17 18:08:49123
124 // Make sure string contains the expected values.
[email protected]e758d4c2014-08-06 16:48:16125 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
Sergey Ulanov0e22a2e2022-01-19 09:47:38126 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
tommi557da2ea2015-05-14 13:19:38127 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
Sergey Ulanov0e22a2e2022-01-19 09:47:38128 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE)));
129 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE)));
[email protected]48fe88f2012-01-17 18:08:49130
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 Ulanov982aa9082022-01-21 19:22:38137TEST_F(WebRtcTextLogTest, LogEverythingConfiguration) {
tommi557da2ea2015-05-14 13:19:38138 ASSERT_TRUE(Initialize(2)); // verbosity at level 2 allows LS_SENSITIVE.
[email protected]48fe88f2012-01-17 18:08:49139
140 // In this configuration everything should be logged.
Mirko Bonadei4b719ba2017-11-10 18:27:45141 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]48fe88f2012-01-17 18:08:49144 static const int kFakeError = 1;
Sergey Ulanov0e22a2e2022-01-19 09:47:38145 RTC_LOG_E(LS_INFO, EN, kFakeError)
146 << "RTC_LOG_E(" << AsString(rtc::LS_INFO) << ")";
Mirko Bonadei4b719ba2017-11-10 18:27:45147 RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
148 RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
[email protected]48fe88f2012-01-17 18:08:49149
150 // Read file to string.
[email protected]48fe88f2012-01-17 18:08:49151 std::string contents_of_file;
Sergey Ulanov982aa9082022-01-21 19:22:38152 base::ReadFileToString(log_file_path_, &contents_of_file);
[email protected]48fe88f2012-01-17 18:08:49153
154 // Make sure string contains the expected values.
[email protected]e758d4c2014-08-06 16:48:16155 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
Sergey Ulanov0e22a2e2022-01-19 09:47:38156 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
Xiyuan Xiaa0559da2022-05-05 19:42:45157
158#if BUILDFLAG(USE_RUNTIME_VLOG)
[email protected]e758d4c2014-08-06 16:48:16159 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
Mirko Bonadei4b719ba2017-11-10 18:27:45160 // RTC_LOG_E
[email protected]48fe88f2012-01-17 18:08:49161 EXPECT_TRUE(ContainsString(contents_of_file, strerror(kFakeError)));
Sergey Ulanov0e22a2e2022-01-19 09:47:38162 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE)));
163 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE)));
Xiyuan Xiaa0559da2022-05-05 19:42:45164#endif // BUILDFLAG(USE_RUNTIME_VLOG)
[email protected]48fe88f2012-01-17 18:08:49165}