Infobar material design refresh: bg color
[chromium-blink-merge.git] / jingle / glue / logging_unittest.cc
blob96401784b9776cd1d3213cd11da9c54557fe748d
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"
22 #if defined(OS_WIN)
23 static const wchar_t* const log_file_name = L"libjingle_logging.log";
24 #else
25 static const char* const log_file_name = "libjingle_logging.log";
26 #endif
28 static const int kDefaultVerbosity = 0;
30 static const char* AsString(rtc::LoggingSeverity severity) {
31 switch (severity) {
32 case rtc::LS_ERROR:
33 return "LS_ERROR";
34 case rtc::LS_WARNING:
35 return "LS_WARNING";
36 case rtc::LS_INFO:
37 return "LS_INFO";
38 case rtc::LS_VERBOSE:
39 return "LS_VERBOSE";
40 case rtc::LS_SENSITIVE:
41 return "LS_SENSITIVE";
42 default:
43 return "";
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)) {
69 return false;
71 EXPECT_TRUE(VLOG_IS_ON(verbosity_level));
72 EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1));
73 return true;
76 TEST(LibjingleLogTest, DefaultConfiguration) {
77 ASSERT_TRUE(Initialize(kDefaultVerbosity));
79 // In the default configuration only warnings and errors 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_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
93 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
94 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
95 EXPECT_FALSE(ContainsString(contents_of_file,
96 AsString(rtc::LS_VERBOSE)));
97 EXPECT_FALSE(ContainsString(contents_of_file,
98 AsString(rtc::LS_SENSITIVE)));
101 TEST(LibjingleLogTest, InfoConfiguration) {
102 ASSERT_TRUE(Initialize(0)); // 0 == Chrome's 'info' level.
104 // In this configuration everything lower or equal to LS_INFO should be
105 // logged.
106 LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR);
107 LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING);
108 LOG_V(rtc::LS_INFO) << AsString(rtc::LS_INFO);
109 LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
110 LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
112 // Read file to string.
113 base::FilePath file_path(log_file_name);
114 std::string contents_of_file;
115 base::ReadFileToString(file_path, &contents_of_file);
117 // Make sure string contains the expected values.
118 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
119 EXPECT_TRUE(ContainsString(contents_of_file,
120 AsString(rtc::LS_WARNING)));
121 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
122 EXPECT_FALSE(ContainsString(contents_of_file,
123 AsString(rtc::LS_VERBOSE)));
124 EXPECT_FALSE(ContainsString(contents_of_file,
125 AsString(rtc::LS_SENSITIVE)));
127 // Also check that the log is proper.
128 EXPECT_TRUE(ContainsString(contents_of_file, "logging_unittest.cc"));
129 EXPECT_FALSE(ContainsString(contents_of_file, "logging.h"));
130 EXPECT_FALSE(ContainsString(contents_of_file, "logging.cc"));
133 TEST(LibjingleLogTest, LogEverythingConfiguration) {
134 ASSERT_TRUE(Initialize(2)); // verbosity at level 2 allows LS_SENSITIVE.
136 // In this configuration everything should be logged.
137 LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR);
138 LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING);
139 LOG(LS_INFO) << AsString(rtc::LS_INFO);
140 static const int kFakeError = 1;
141 LOG_E(LS_INFO, EN, kFakeError) << "LOG_E(" << AsString(rtc::LS_INFO) <<
142 ")";
143 LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
144 LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
146 // Read file to string.
147 base::FilePath file_path(log_file_name);
148 std::string contents_of_file;
149 base::ReadFileToString(file_path, &contents_of_file);
151 // Make sure string contains the expected values.
152 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
153 EXPECT_TRUE(ContainsString(contents_of_file,
154 AsString(rtc::LS_WARNING)));
155 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
156 // LOG_E
157 EXPECT_TRUE(ContainsString(contents_of_file, strerror(kFakeError)));
158 EXPECT_TRUE(ContainsString(contents_of_file,
159 AsString(rtc::LS_VERBOSE)));
160 EXPECT_TRUE(ContainsString(contents_of_file,
161 AsString(rtc::LS_SENSITIVE)));