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 // The following include must be first in this file. It ensures that
11 // libjingle style logging is used.
12 #define LOGGING_INSIDE_WEBRTC
14 #include "third_party/webrtc/overrides/webrtc/base/logging.h"
16 #include "base/command_line.h"
17 #include "base/file_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 static const wchar_t* const log_file_name
= L
"libjingle_logging.log";
23 static const char* const log_file_name
= "libjingle_logging.log";
26 static const int kDefaultVerbosity
= 0;
28 static const char* AsString(rtc::LoggingSeverity severity
) {
38 case rtc::LS_SENSITIVE
:
39 return "LS_SENSITIVE";
45 static bool ContainsString(const std::string
& original
,
46 const char* string_to_match
) {
47 return original
.find(string_to_match
) != std::string::npos
;
50 static bool Initialize(int verbosity_level
) {
51 if (verbosity_level
!= kDefaultVerbosity
) {
52 // Update the command line with specified verbosity level for this file.
53 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
54 std::ostringstream value_stream
;
55 value_stream
<< "logging_unittest=" << verbosity_level
;
56 const std::string
& value
= value_stream
.str();
57 command_line
->AppendSwitchASCII("vmodule", value
);
60 // The command line flags are parsed here and the log file name is set.
61 logging::LoggingSettings settings
;
62 settings
.logging_dest
= logging::LOG_TO_FILE
;
63 settings
.log_file
= log_file_name
;
64 settings
.lock_log
= logging::DONT_LOCK_LOG_FILE
;
65 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
66 if (!logging::InitLogging(settings
)) {
69 EXPECT_TRUE(VLOG_IS_ON(verbosity_level
));
70 EXPECT_FALSE(VLOG_IS_ON(verbosity_level
+ 1));
74 TEST(LibjingleLogTest
, DefaultConfiguration
) {
75 ASSERT_TRUE(Initialize(kDefaultVerbosity
));
77 // In the default configuration nothing should be logged.
78 LOG_V(rtc::LS_ERROR
) << AsString(rtc::LS_ERROR
);
79 LOG_V(rtc::LS_WARNING
) << AsString(rtc::LS_WARNING
);
80 LOG_V(rtc::LS_INFO
) << AsString(rtc::LS_INFO
);
81 LOG_V(rtc::LS_VERBOSE
) << AsString(rtc::LS_VERBOSE
);
82 LOG_V(rtc::LS_SENSITIVE
) << AsString(rtc::LS_SENSITIVE
);
84 // Read file to string.
85 base::FilePath
file_path(log_file_name
);
86 std::string contents_of_file
;
87 base::ReadFileToString(file_path
, &contents_of_file
);
89 // Make sure string contains the expected values.
90 EXPECT_FALSE(ContainsString(contents_of_file
, AsString(rtc::LS_ERROR
)));
91 EXPECT_FALSE(ContainsString(contents_of_file
,
92 AsString(rtc::LS_WARNING
)));
93 EXPECT_FALSE(ContainsString(contents_of_file
, AsString(rtc::LS_INFO
)));
94 EXPECT_FALSE(ContainsString(contents_of_file
,
95 AsString(rtc::LS_VERBOSE
)));
96 EXPECT_FALSE(ContainsString(contents_of_file
,
97 AsString(rtc::LS_SENSITIVE
)));
100 TEST(LibjingleLogTest
, InfoConfiguration
) {
101 ASSERT_TRUE(Initialize(rtc::LS_INFO
));
103 // In this configuration everything lower or equal to LS_INFO should be
105 LOG_V(rtc::LS_ERROR
) << AsString(rtc::LS_ERROR
);
106 LOG_V(rtc::LS_WARNING
) << AsString(rtc::LS_WARNING
);
107 LOG_V(rtc::LS_INFO
) << AsString(rtc::LS_INFO
);
108 LOG_V(rtc::LS_VERBOSE
) << AsString(rtc::LS_VERBOSE
);
109 LOG_V(rtc::LS_SENSITIVE
) << AsString(rtc::LS_SENSITIVE
);
111 // Read file to string.
112 base::FilePath
file_path(log_file_name
);
113 std::string contents_of_file
;
114 base::ReadFileToString(file_path
, &contents_of_file
);
116 // Make sure string contains the expected values.
117 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_ERROR
)));
118 EXPECT_TRUE(ContainsString(contents_of_file
,
119 AsString(rtc::LS_WARNING
)));
120 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_INFO
)));
121 EXPECT_FALSE(ContainsString(contents_of_file
,
122 AsString(rtc::LS_VERBOSE
)));
123 EXPECT_FALSE(ContainsString(contents_of_file
,
124 AsString(rtc::LS_SENSITIVE
)));
126 // Also check that the log is proper.
127 EXPECT_TRUE(ContainsString(contents_of_file
, "logging_unittest.cc"));
128 EXPECT_FALSE(ContainsString(contents_of_file
, "logging.h"));
129 EXPECT_FALSE(ContainsString(contents_of_file
, "logging.cc"));
132 TEST(LibjingleLogTest
, LogEverythingConfiguration
) {
133 ASSERT_TRUE(Initialize(rtc::LS_SENSITIVE
));
135 // In this configuration everything should be logged.
136 LOG_V(rtc::LS_ERROR
) << AsString(rtc::LS_ERROR
);
137 LOG_V(rtc::LS_WARNING
) << AsString(rtc::LS_WARNING
);
138 LOG(LS_INFO
) << AsString(rtc::LS_INFO
);
139 static const int kFakeError
= 1;
140 LOG_E(LS_INFO
, EN
, kFakeError
) << "LOG_E(" << AsString(rtc::LS_INFO
) <<
142 LOG_V(rtc::LS_VERBOSE
) << AsString(rtc::LS_VERBOSE
);
143 LOG_V(rtc::LS_SENSITIVE
) << AsString(rtc::LS_SENSITIVE
);
145 // Read file to string.
146 base::FilePath
file_path(log_file_name
);
147 std::string contents_of_file
;
148 base::ReadFileToString(file_path
, &contents_of_file
);
150 // Make sure string contains the expected values.
151 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_ERROR
)));
152 EXPECT_TRUE(ContainsString(contents_of_file
,
153 AsString(rtc::LS_WARNING
)));
154 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(rtc::LS_INFO
)));
156 EXPECT_TRUE(ContainsString(contents_of_file
, strerror(kFakeError
)));
157 EXPECT_TRUE(ContainsString(contents_of_file
,
158 AsString(rtc::LS_VERBOSE
)));
159 EXPECT_TRUE(ContainsString(contents_of_file
,
160 AsString(rtc::LS_SENSITIVE
)));