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.
5 // Note: this test tests LOG_V and LOG_E since all other logs are expressed
6 // in forms of them. LOG is also tested for good measure.
7 // Also note that we are only allowed to call InitLogging() twice so the test
8 // cases are more dense than normal.
10 // We must include Chromium headers before including the overrides header
11 // since webrtc's logging.h file may conflict with chromium.
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 // The following include come before including logging.h. It ensures that
17 // libjingle style logging is used.
18 #define LOGGING_INSIDE_WEBRTC
20 #include "third_party/webrtc/overrides/webrtc/base/logging.h"
23 static const wchar_t* const log_file_name
= L
"libjingle_logging.log";
25 static const char* const log_file_name
= "libjingle_logging.log";
28 static const int kDefaultVerbosity
= 0;
30 static const char* AsString(rtc::LoggingSeverity severity
) {
40 case rtc::LS_SENSITIVE
:
41 return "LS_SENSITIVE";
47 static bool ContainsString(const std::string
& original
,
48 const char* string_to_match
) {
49 return original
.find(string_to_match
) != std::string::npos
;
52 static 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
);
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
= log_file_name
;
66 settings
.lock_log
= logging::DONT_LOCK_LOG_FILE
;
67 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
68 if (!logging::InitLogging(settings
)) {
71 EXPECT_TRUE(VLOG_IS_ON(verbosity_level
));
72 EXPECT_FALSE(VLOG_IS_ON(verbosity_level
+ 1));
76 TEST(LibjingleLogTest
, DefaultConfiguration
) {
77 ASSERT_TRUE(Initialize(kDefaultVerbosity
));
79 // In the default configuration nothing should be logged.
80 LOG_V(rtc::LS_ERROR
) << AsString(rtc::LS_ERROR
);
81 LOG_V(rtc::LS_WARNING
) << AsString(rtc::LS_WARNING
);
82 LOG_V(rtc::LS_INFO
) << AsString(rtc::LS_INFO
);
83 LOG_V(rtc::LS_VERBOSE
) << AsString(rtc::LS_VERBOSE
);
84 LOG_V(rtc::LS_SENSITIVE
) << AsString(rtc::LS_SENSITIVE
);
86 // Read file to string.
87 base::FilePath
file_path(log_file_name
);
88 std::string contents_of_file
;
89 base::ReadFileToString(file_path
, &contents_of_file
);
91 // Make sure string contains the expected values.
92 EXPECT_FALSE(ContainsString(contents_of_file
, AsString(rtc::LS_ERROR
)));
93 EXPECT_FALSE(ContainsString(contents_of_file
,
94 AsString(rtc::LS_WARNING
)));
95 EXPECT_FALSE(ContainsString(contents_of_file
, AsString(rtc::LS_INFO
)));
96 EXPECT_FALSE(ContainsString(contents_of_file
,
97 AsString(rtc::LS_VERBOSE
)));
98 EXPECT_FALSE(ContainsString(contents_of_file
,
99 AsString(rtc::LS_SENSITIVE
)));
102 TEST(LibjingleLogTest
, InfoConfiguration
) {
103 ASSERT_TRUE(Initialize(rtc::LS_INFO
));
105 // In this configuration everything lower or equal to LS_INFO should be
107 LOG_V(rtc::LS_ERROR
) << AsString(rtc::LS_ERROR
);
108 LOG_V(rtc::LS_WARNING
) << AsString(rtc::LS_WARNING
);
109 LOG_V(rtc::LS_INFO
) << AsString(rtc::LS_INFO
);
110 LOG_V(rtc::LS_VERBOSE
) << AsString(rtc::LS_VERBOSE
);
111 LOG_V(rtc::LS_SENSITIVE
) << AsString(rtc::LS_SENSITIVE
);
113 // Read file to string.
114 base::FilePath
file_path(log_file_name
);
115 std::string contents_of_file
;
116 base::ReadFileToString(file_path
, &contents_of_file
);
118 // Make sure string contains the expected values.
119 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_ERROR
)));
120 EXPECT_TRUE(ContainsString(contents_of_file
,
121 AsString(rtc::LS_WARNING
)));
122 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_INFO
)));
123 EXPECT_FALSE(ContainsString(contents_of_file
,
124 AsString(rtc::LS_VERBOSE
)));
125 EXPECT_FALSE(ContainsString(contents_of_file
,
126 AsString(rtc::LS_SENSITIVE
)));
128 // Also check that the log is proper.
129 EXPECT_TRUE(ContainsString(contents_of_file
, "logging_unittest.cc"));
130 EXPECT_FALSE(ContainsString(contents_of_file
, "logging.h"));
131 EXPECT_FALSE(ContainsString(contents_of_file
, "logging.cc"));
134 TEST(LibjingleLogTest
, LogEverythingConfiguration
) {
135 ASSERT_TRUE(Initialize(rtc::LS_SENSITIVE
));
137 // In this configuration everything should be logged.
138 LOG_V(rtc::LS_ERROR
) << AsString(rtc::LS_ERROR
);
139 LOG_V(rtc::LS_WARNING
) << AsString(rtc::LS_WARNING
);
140 LOG(LS_INFO
) << AsString(rtc::LS_INFO
);
141 static const int kFakeError
= 1;
142 LOG_E(LS_INFO
, EN
, kFakeError
) << "LOG_E(" << AsString(rtc::LS_INFO
) <<
144 LOG_V(rtc::LS_VERBOSE
) << AsString(rtc::LS_VERBOSE
);
145 LOG_V(rtc::LS_SENSITIVE
) << AsString(rtc::LS_SENSITIVE
);
147 // Read file to string.
148 base::FilePath
file_path(log_file_name
);
149 std::string contents_of_file
;
150 base::ReadFileToString(file_path
, &contents_of_file
);
152 // Make sure string contains the expected values.
153 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_ERROR
)));
154 EXPECT_TRUE(ContainsString(contents_of_file
,
155 AsString(rtc::LS_WARNING
)));
156 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_INFO
)));
158 EXPECT_TRUE(ContainsString(contents_of_file
, strerror(kFakeError
)));
159 EXPECT_TRUE(ContainsString(contents_of_file
,
160 AsString(rtc::LS_VERBOSE
)));
161 EXPECT_TRUE(ContainsString(contents_of_file
,
162 AsString(rtc::LS_SENSITIVE
)));