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_LIBJINGLE
14 #include "third_party/libjingle/overrides/talk/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(talk_base::LoggingSeverity severity
) {
30 case talk_base::LS_ERROR
:
32 case talk_base::LS_WARNING
:
34 case talk_base::LS_INFO
:
36 case talk_base::LS_VERBOSE
:
38 case talk_base::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 if (!InitLogging(log_file_name
, logging::LOG_ONLY_TO_FILE
,
62 logging::DONT_LOCK_LOG_FILE
, logging::DELETE_OLD_LOG_FILE
,
63 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS
)) {
66 EXPECT_TRUE(VLOG_IS_ON(verbosity_level
));
67 EXPECT_FALSE(VLOG_IS_ON(verbosity_level
+ 1));
71 TEST(LibjingleLogTest
, DefaultConfiguration
) {
72 ASSERT_TRUE(Initialize(kDefaultVerbosity
));
74 // In the default configuration nothing should be logged.
75 LOG_V(talk_base::LS_ERROR
) << AsString(talk_base::LS_ERROR
);
76 LOG_V(talk_base::LS_WARNING
) << AsString(talk_base::LS_WARNING
);
77 LOG_V(talk_base::LS_INFO
) << AsString(talk_base::LS_INFO
);
78 LOG_V(talk_base::LS_VERBOSE
) << AsString(talk_base::LS_VERBOSE
);
79 LOG_V(talk_base::LS_SENSITIVE
) << AsString(talk_base::LS_SENSITIVE
);
81 // Read file to string.
82 base::FilePath
file_path(log_file_name
);
83 std::string contents_of_file
;
84 file_util::ReadFileToString(file_path
, &contents_of_file
);
86 // Make sure string contains the expected values.
87 EXPECT_FALSE(ContainsString(contents_of_file
, AsString(talk_base::LS_ERROR
)));
88 EXPECT_FALSE(ContainsString(contents_of_file
,
89 AsString(talk_base::LS_WARNING
)));
90 EXPECT_FALSE(ContainsString(contents_of_file
, AsString(talk_base::LS_INFO
)));
91 EXPECT_FALSE(ContainsString(contents_of_file
,
92 AsString(talk_base::LS_VERBOSE
)));
93 EXPECT_FALSE(ContainsString(contents_of_file
,
94 AsString(talk_base::LS_SENSITIVE
)));
97 TEST(LibjingleLogTest
, InfoConfiguration
) {
98 ASSERT_TRUE(Initialize(talk_base::LS_INFO
));
100 // In this configuration everything lower or equal to LS_INFO should be
102 LOG_V(talk_base::LS_ERROR
) << AsString(talk_base::LS_ERROR
);
103 LOG_V(talk_base::LS_WARNING
) << AsString(talk_base::LS_WARNING
);
104 LOG_V(talk_base::LS_INFO
) << AsString(talk_base::LS_INFO
);
105 LOG_V(talk_base::LS_VERBOSE
) << AsString(talk_base::LS_VERBOSE
);
106 LOG_V(talk_base::LS_SENSITIVE
) << AsString(talk_base::LS_SENSITIVE
);
108 // Read file to string.
109 base::FilePath
file_path(log_file_name
);
110 std::string contents_of_file
;
111 file_util::ReadFileToString(file_path
, &contents_of_file
);
113 // Make sure string contains the expected values.
114 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(talk_base::LS_ERROR
)));
115 EXPECT_TRUE(ContainsString(contents_of_file
,
116 AsString(talk_base::LS_WARNING
)));
117 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(talk_base::LS_INFO
)));
118 EXPECT_FALSE(ContainsString(contents_of_file
,
119 AsString(talk_base::LS_VERBOSE
)));
120 EXPECT_FALSE(ContainsString(contents_of_file
,
121 AsString(talk_base::LS_SENSITIVE
)));
123 // Also check that the log is proper.
124 EXPECT_TRUE(ContainsString(contents_of_file
, "logging_unittest.cc"));
125 EXPECT_FALSE(ContainsString(contents_of_file
, "logging.h"));
126 EXPECT_FALSE(ContainsString(contents_of_file
, "logging.cc"));
129 TEST(LibjingleLogTest
, LogEverythingConfiguration
) {
130 ASSERT_TRUE(Initialize(talk_base::LS_SENSITIVE
));
132 // In this configuration everything should be logged.
133 LOG_V(talk_base::LS_ERROR
) << AsString(talk_base::LS_ERROR
);
134 LOG_V(talk_base::LS_WARNING
) << AsString(talk_base::LS_WARNING
);
135 LOG(LS_INFO
) << AsString(talk_base::LS_INFO
);
136 static const int kFakeError
= 1;
137 LOG_E(LS_INFO
, EN
, kFakeError
) << "LOG_E(" << AsString(talk_base::LS_INFO
) <<
139 LOG_V(talk_base::LS_VERBOSE
) << AsString(talk_base::LS_VERBOSE
);
140 LOG_V(talk_base::LS_SENSITIVE
) << AsString(talk_base::LS_SENSITIVE
);
142 // Read file to string.
143 base::FilePath
file_path(log_file_name
);
144 std::string contents_of_file
;
145 file_util::ReadFileToString(file_path
, &contents_of_file
);
147 // Make sure string contains the expected values.
148 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(talk_base::LS_ERROR
)));
149 EXPECT_TRUE(ContainsString(contents_of_file
,
150 AsString(talk_base::LS_WARNING
)));
151 EXPECT_TRUE(ContainsString(contents_of_file
, AsString(talk_base::LS_INFO
)));
153 EXPECT_TRUE(ContainsString(contents_of_file
, strerror(kFakeError
)));
154 EXPECT_TRUE(ContainsString(contents_of_file
,
155 AsString(talk_base::LS_VERBOSE
)));
156 EXPECT_TRUE(ContainsString(contents_of_file
,
157 AsString(talk_base::LS_SENSITIVE
)));