[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 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. |
| 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 | |
tommi | d7c94e2 | 2015-03-20 10:56:56 | [diff] [blame] | 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" |
| 15 | |
| 16 | // The following include come before including logging.h. It ensures that |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 17 | // libjingle style logging is used. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 18 | #define LOGGING_INSIDE_WEBRTC |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 19 | |
avi | a2a6db2 | 2015-12-22 02:05:16 | [diff] [blame] | 20 | #include "build/build_config.h" |
Henrik Kjellander | 061fc99c | 2017-09-15 08:44:50 | [diff] [blame^] | 21 | #include "third_party/webrtc_overrides/rtc_base/logging.h" |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 22 | |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 23 | #if defined(OS_WIN) |
| 24 | static const wchar_t* const log_file_name = L"libjingle_logging.log"; |
| 25 | #else |
| 26 | static const char* const log_file_name = "libjingle_logging.log"; |
| 27 | #endif |
| 28 | |
| 29 | static const int kDefaultVerbosity = 0; |
| 30 | |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 31 | static const char* AsString(rtc::LoggingSeverity severity) { |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 32 | switch (severity) { |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 33 | case rtc::LS_ERROR: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 34 | return "LS_ERROR"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 35 | case rtc::LS_WARNING: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 36 | return "LS_WARNING"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 37 | case rtc::LS_INFO: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 38 | return "LS_INFO"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 39 | case rtc::LS_VERBOSE: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 40 | return "LS_VERBOSE"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 41 | case rtc::LS_SENSITIVE: |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 42 | return "LS_SENSITIVE"; |
| 43 | default: |
| 44 | return ""; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static bool ContainsString(const std::string& original, |
| 49 | const char* string_to_match) { |
| 50 | return original.find(string_to_match) != std::string::npos; |
| 51 | } |
| 52 | |
| 53 | static bool Initialize(int verbosity_level) { |
| 54 | if (verbosity_level != kDefaultVerbosity) { |
| 55 | // Update the command line with specified verbosity level for this file. |
avi | 429bbdd | 2014-12-23 00:27:27 | [diff] [blame] | 56 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 57 | 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] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 64 | 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] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 70 | 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 | |
| 77 | TEST(LibjingleLogTest, DefaultConfiguration) { |
| 78 | ASSERT_TRUE(Initialize(kDefaultVerbosity)); |
| 79 | |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 80 | // In the default configuration only warnings and errors should be logged. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 81 | 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] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 86 | |
| 87 | // Read file to string. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 88 | base::FilePath file_path(log_file_name); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 89 | std::string contents_of_file; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 90 | base::ReadFileToString(file_path, &contents_of_file); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 91 | |
| 92 | // Make sure string contains the expected values. |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 93 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR))); |
| 94 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING))); |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 95 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 96 | EXPECT_FALSE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 97 | AsString(rtc::LS_VERBOSE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 98 | EXPECT_FALSE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 99 | AsString(rtc::LS_SENSITIVE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(LibjingleLogTest, InfoConfiguration) { |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 103 | ASSERT_TRUE(Initialize(0)); // 0 == Chrome's 'info' level. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 104 | |
| 105 | // In this configuration everything lower or equal to LS_INFO should be |
| 106 | // logged. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 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); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 112 | |
| 113 | // Read file to string. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 114 | base::FilePath file_path(log_file_name); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 115 | std::string contents_of_file; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 116 | base::ReadFileToString(file_path, &contents_of_file); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 117 | |
| 118 | // Make sure string contains the expected values. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 119 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 120 | EXPECT_TRUE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 121 | AsString(rtc::LS_WARNING))); |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 122 | EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 123 | EXPECT_FALSE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 124 | AsString(rtc::LS_VERBOSE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 125 | EXPECT_FALSE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 126 | AsString(rtc::LS_SENSITIVE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 127 | |
| 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 | |
| 134 | TEST(LibjingleLogTest, LogEverythingConfiguration) { |
tommi | 557da2ea | 2015-05-14 13:19:38 | [diff] [blame] | 135 | ASSERT_TRUE(Initialize(2)); // verbosity at level 2 allows LS_SENSITIVE. |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 136 | |
| 137 | // In this configuration everything should be logged. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 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); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 141 | static const int kFakeError = 1; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 142 | LOG_E(LS_INFO, EN, kFakeError) << "LOG_E(" << AsString(rtc::LS_INFO) << |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 143 | ")"; |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 144 | LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE); |
| 145 | LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 146 | |
| 147 | // Read file to string. |
[email protected] | 9e27571 | 2013-02-10 19:20:14 | [diff] [blame] | 148 | base::FilePath file_path(log_file_name); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 149 | std::string contents_of_file; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 150 | base::ReadFileToString(file_path, &contents_of_file); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 151 | |
| 152 | // Make sure string contains the expected values. |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 153 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 154 | EXPECT_TRUE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 155 | AsString(rtc::LS_WARNING))); |
| 156 | EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_INFO))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 157 | // LOG_E |
| 158 | EXPECT_TRUE(ContainsString(contents_of_file, strerror(kFakeError))); |
| 159 | EXPECT_TRUE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 160 | AsString(rtc::LS_VERBOSE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 161 | EXPECT_TRUE(ContainsString(contents_of_file, |
[email protected] | e758d4c | 2014-08-06 16:48:16 | [diff] [blame] | 162 | AsString(rtc::LS_SENSITIVE))); |
[email protected] | 48fe88f | 2012-01-17 18:08:49 | [diff] [blame] | 163 | } |