blob: 4b008b042ee19b190cd12502dffcf443a6581302 [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
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.
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.
12#include "base/command_line.h"
13#include "base/files/file_util.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16// The following include come before including logging.h. It ensures that
[email protected]48fe88f2012-01-17 18:08:4917// libjingle style logging is used.
[email protected]e758d4c2014-08-06 16:48:1618#define LOGGING_INSIDE_WEBRTC
[email protected]48fe88f2012-01-17 18:08:4919
avia2a6db22015-12-22 02:05:1620#include "build/build_config.h"
Henrik Kjellander061fc99c2017-09-15 08:44:5021#include "third_party/webrtc_overrides/rtc_base/logging.h"
[email protected]48fe88f2012-01-17 18:08:4922
[email protected]48fe88f2012-01-17 18:08:4923#if defined(OS_WIN)
24static const wchar_t* const log_file_name = L"libjingle_logging.log";
25#else
26static const char* const log_file_name = "libjingle_logging.log";
27#endif
28
29static const int kDefaultVerbosity = 0;
30
[email protected]e758d4c2014-08-06 16:48:1631static const char* AsString(rtc::LoggingSeverity severity) {
[email protected]48fe88f2012-01-17 18:08:4932 switch (severity) {
[email protected]e758d4c2014-08-06 16:48:1633 case rtc::LS_ERROR:
[email protected]48fe88f2012-01-17 18:08:4934 return "LS_ERROR";
[email protected]e758d4c2014-08-06 16:48:1635 case rtc::LS_WARNING:
[email protected]48fe88f2012-01-17 18:08:4936 return "LS_WARNING";
[email protected]e758d4c2014-08-06 16:48:1637 case rtc::LS_INFO:
[email protected]48fe88f2012-01-17 18:08:4938 return "LS_INFO";
[email protected]e758d4c2014-08-06 16:48:1639 case rtc::LS_VERBOSE:
[email protected]48fe88f2012-01-17 18:08:4940 return "LS_VERBOSE";
[email protected]e758d4c2014-08-06 16:48:1641 case rtc::LS_SENSITIVE:
[email protected]48fe88f2012-01-17 18:08:4942 return "LS_SENSITIVE";
43 default:
44 return "";
45 }
46}
47
48static bool ContainsString(const std::string& original,
49 const char* string_to_match) {
50 return original.find(string_to_match) != std::string::npos;
51}
52
53static bool Initialize(int verbosity_level) {
54 if (verbosity_level != kDefaultVerbosity) {
55 // Update the command line with specified verbosity level for this file.
avi429bbdd2014-12-23 00:27:2756 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
[email protected]48fe88f2012-01-17 18:08:4957 std::ostringstream value_stream;
58 value_stream << "logging_unittest=" << verbosity_level;
59 const std::string& value = value_stream.str();
60 command_line->AppendSwitchASCII("vmodule", value);
61 }
62
63 // The command line flags are parsed here and the log file name is set.
[email protected]5e3f7c22013-06-21 21:15:3364 logging::LoggingSettings settings;
65 settings.logging_dest = logging::LOG_TO_FILE;
66 settings.log_file = log_file_name;
67 settings.lock_log = logging::DONT_LOCK_LOG_FILE;
68 settings.delete_old = logging::DELETE_OLD_LOG_FILE;
69 if (!logging::InitLogging(settings)) {
[email protected]48fe88f2012-01-17 18:08:4970 return false;
71 }
72 EXPECT_TRUE(VLOG_IS_ON(verbosity_level));
73 EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1));
74 return true;
75}
76
77TEST(LibjingleLogTest, DefaultConfiguration) {
78 ASSERT_TRUE(Initialize(kDefaultVerbosity));
79
tommi557da2ea2015-05-14 13:19:3880 // In the default configuration only warnings and errors should be logged.
[email protected]e758d4c2014-08-06 16:48:1681 LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR);
82 LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING);
83 LOG_V(rtc::LS_INFO) << AsString(rtc::LS_INFO);
84 LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
85 LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
[email protected]48fe88f2012-01-17 18:08:4986
87 // Read file to string.
[email protected]9e275712013-02-10 19:20:1488 base::FilePath file_path(log_file_name);
[email protected]48fe88f2012-01-17 18:08:4989 std::string contents_of_file;
[email protected]82f84b92013-08-30 18:23:5090 base::ReadFileToString(file_path, &contents_of_file);
[email protected]48fe88f2012-01-17 18:08:4991
92 // Make sure string contains the expected values.
tommi557da2ea2015-05-14 13:19:3893 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
94 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
[email protected]e758d4c2014-08-06 16:48:1695 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
[email protected]48fe88f2012-01-17 18:08:4996 EXPECT_FALSE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:1697 AsString(rtc::LS_VERBOSE)));
[email protected]48fe88f2012-01-17 18:08:4998 EXPECT_FALSE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:1699 AsString(rtc::LS_SENSITIVE)));
[email protected]48fe88f2012-01-17 18:08:49100}
101
102TEST(LibjingleLogTest, InfoConfiguration) {
tommi557da2ea2015-05-14 13:19:38103 ASSERT_TRUE(Initialize(0)); // 0 == Chrome's 'info' level.
[email protected]48fe88f2012-01-17 18:08:49104
105 // In this configuration everything lower or equal to LS_INFO should be
106 // logged.
[email protected]e758d4c2014-08-06 16:48:16107 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);
[email protected]48fe88f2012-01-17 18:08:49112
113 // Read file to string.
[email protected]9e275712013-02-10 19:20:14114 base::FilePath file_path(log_file_name);
[email protected]48fe88f2012-01-17 18:08:49115 std::string contents_of_file;
[email protected]82f84b92013-08-30 18:23:50116 base::ReadFileToString(file_path, &contents_of_file);
[email protected]48fe88f2012-01-17 18:08:49117
118 // Make sure string contains the expected values.
[email protected]e758d4c2014-08-06 16:48:16119 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
[email protected]48fe88f2012-01-17 18:08:49120 EXPECT_TRUE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:16121 AsString(rtc::LS_WARNING)));
tommi557da2ea2015-05-14 13:19:38122 EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
[email protected]48fe88f2012-01-17 18:08:49123 EXPECT_FALSE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:16124 AsString(rtc::LS_VERBOSE)));
[email protected]48fe88f2012-01-17 18:08:49125 EXPECT_FALSE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:16126 AsString(rtc::LS_SENSITIVE)));
[email protected]48fe88f2012-01-17 18:08:49127
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"));
132}
133
134TEST(LibjingleLogTest, LogEverythingConfiguration) {
tommi557da2ea2015-05-14 13:19:38135 ASSERT_TRUE(Initialize(2)); // verbosity at level 2 allows LS_SENSITIVE.
[email protected]48fe88f2012-01-17 18:08:49136
137 // In this configuration everything should be logged.
[email protected]e758d4c2014-08-06 16:48:16138 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);
[email protected]48fe88f2012-01-17 18:08:49141 static const int kFakeError = 1;
[email protected]e758d4c2014-08-06 16:48:16142 LOG_E(LS_INFO, EN, kFakeError) << "LOG_E(" << AsString(rtc::LS_INFO) <<
[email protected]48fe88f2012-01-17 18:08:49143 ")";
[email protected]e758d4c2014-08-06 16:48:16144 LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
145 LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
[email protected]48fe88f2012-01-17 18:08:49146
147 // Read file to string.
[email protected]9e275712013-02-10 19:20:14148 base::FilePath file_path(log_file_name);
[email protected]48fe88f2012-01-17 18:08:49149 std::string contents_of_file;
[email protected]82f84b92013-08-30 18:23:50150 base::ReadFileToString(file_path, &contents_of_file);
[email protected]48fe88f2012-01-17 18:08:49151
152 // Make sure string contains the expected values.
[email protected]e758d4c2014-08-06 16:48:16153 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
[email protected]48fe88f2012-01-17 18:08:49154 EXPECT_TRUE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:16155 AsString(rtc::LS_WARNING)));
156 EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
[email protected]48fe88f2012-01-17 18:08:49157 // LOG_E
158 EXPECT_TRUE(ContainsString(contents_of_file, strerror(kFakeError)));
159 EXPECT_TRUE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:16160 AsString(rtc::LS_VERBOSE)));
[email protected]48fe88f2012-01-17 18:08:49161 EXPECT_TRUE(ContainsString(contents_of_file,
[email protected]e758d4c2014-08-06 16:48:16162 AsString(rtc::LS_SENSITIVE)));
[email protected]48fe88f2012-01-17 18:08:49163}