OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 |
| 10 // The following include must be first in this file. It ensures that |
| 11 // libjingle style logging is used. |
| 12 #define LOGGING_INSIDE_LIBJINGLE |
| 13 |
| 14 #include "third_party/libjingle/overrides/talk/base/logging.h" |
| 15 |
| 16 #include "base/command_line.h" |
| 17 #include "base/file_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 static const char log_file_name[] = "libjingle_logging.log"; |
| 21 static const int kDefaultVerbosity = 0; |
| 22 |
| 23 static const char* AsString(talk_base::LoggingSeverity severity) { |
| 24 switch (severity) { |
| 25 case talk_base::LS_ERROR: |
| 26 return "LS_ERROR"; |
| 27 case talk_base::LS_WARNING: |
| 28 return "LS_WARNING"; |
| 29 case talk_base::LS_INFO: |
| 30 return "LS_INFO"; |
| 31 case talk_base::LS_VERBOSE: |
| 32 return "LS_VERBOSE"; |
| 33 case talk_base::LS_SENSITIVE: |
| 34 return "LS_SENSITIVE"; |
| 35 default: |
| 36 return ""; |
| 37 } |
| 38 } |
| 39 |
| 40 bool ContainsString(const std::string& original, const char* string_to_match) { |
| 41 return original.find(string_to_match) != std::string::npos; |
| 42 } |
| 43 |
| 44 bool Initialize(int verbosity_level) { |
| 45 if (verbosity_level != kDefaultVerbosity) { |
| 46 // Update the command line with specified verbosity level for this file. |
| 47 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 48 std::ostringstream value_stream; |
| 49 value_stream << "logging_unittest=" << verbosity_level; |
| 50 const std::string& value = value_stream.str(); |
| 51 command_line->AppendSwitchASCII("vmodule", value); |
| 52 } |
| 53 |
| 54 // The command line flags are parsed here and the log file name is set. |
| 55 if (!InitLogging(log_file_name, logging::LOG_ONLY_TO_FILE, |
| 56 logging::DONT_LOCK_LOG_FILE, logging::DELETE_OLD_LOG_FILE, |
| 57 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) { |
| 58 return false; |
| 59 } |
| 60 EXPECT_TRUE(VLOG_IS_ON(verbosity_level)); |
| 61 EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1)); |
| 62 return true; |
| 63 } |
| 64 |
| 65 TEST(LibjingleLogTest, DefaultConfiguration) { |
| 66 ASSERT_TRUE(Initialize(kDefaultVerbosity)); |
| 67 |
| 68 // In the default configuration nothing should be logged. |
| 69 LOG_V(talk_base::LS_ERROR) << AsString(talk_base::LS_ERROR); |
| 70 LOG_V(talk_base::LS_WARNING) << AsString(talk_base::LS_WARNING); |
| 71 LOG_V(talk_base::LS_INFO) << AsString(talk_base::LS_INFO); |
| 72 LOG_V(talk_base::LS_VERBOSE) << AsString(talk_base::LS_VERBOSE); |
| 73 LOG_V(talk_base::LS_SENSITIVE) << AsString(talk_base::LS_SENSITIVE); |
| 74 |
| 75 // Read file to string. |
| 76 const std::string file_name_as_string = log_file_name; |
| 77 FilePath file_path(file_name_as_string); |
| 78 std::string contents_of_file; |
| 79 file_util::ReadFileToString(file_path, &contents_of_file); |
| 80 |
| 81 // Make sure string contains the expected values. |
| 82 EXPECT_FALSE(ContainsString(contents_of_file, AsString(talk_base::LS_ERROR))); |
| 83 EXPECT_FALSE(ContainsString(contents_of_file, |
| 84 AsString(talk_base::LS_WARNING))); |
| 85 EXPECT_FALSE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| 86 EXPECT_FALSE(ContainsString(contents_of_file, |
| 87 AsString(talk_base::LS_VERBOSE))); |
| 88 EXPECT_FALSE(ContainsString(contents_of_file, |
| 89 AsString(talk_base::LS_SENSITIVE))); |
| 90 } |
| 91 |
| 92 TEST(LibjingleLogTest, InfoConfiguration) { |
| 93 ASSERT_TRUE(Initialize(talk_base::LS_INFO)); |
| 94 |
| 95 // In this configuration everything lower or equal to LS_INFO should be |
| 96 // logged. |
| 97 LOG_V(talk_base::LS_ERROR) << AsString(talk_base::LS_ERROR); |
| 98 LOG_V(talk_base::LS_WARNING) << AsString(talk_base::LS_WARNING); |
| 99 LOG_V(talk_base::LS_INFO) << AsString(talk_base::LS_INFO); |
| 100 LOG_V(talk_base::LS_VERBOSE) << AsString(talk_base::LS_VERBOSE); |
| 101 LOG_V(talk_base::LS_SENSITIVE) << AsString(talk_base::LS_SENSITIVE); |
| 102 |
| 103 // Read file to string. |
| 104 const std::string file_name_as_string = log_file_name; |
| 105 FilePath file_path(file_name_as_string); |
| 106 std::string contents_of_file; |
| 107 file_util::ReadFileToString(file_path, &contents_of_file); |
| 108 |
| 109 // Make sure string contains the expected values. |
| 110 EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_ERROR))); |
| 111 EXPECT_TRUE(ContainsString(contents_of_file, |
| 112 AsString(talk_base::LS_WARNING))); |
| 113 EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| 114 EXPECT_FALSE(ContainsString(contents_of_file, |
| 115 AsString(talk_base::LS_VERBOSE))); |
| 116 EXPECT_FALSE(ContainsString(contents_of_file, |
| 117 AsString(talk_base::LS_SENSITIVE))); |
| 118 |
| 119 // Also check that the log is proper. |
| 120 EXPECT_TRUE(ContainsString(contents_of_file, "logging_unittest.cc")); |
| 121 EXPECT_FALSE(ContainsString(contents_of_file, "logging.h")); |
| 122 EXPECT_FALSE(ContainsString(contents_of_file, "logging.cc")); |
| 123 } |
| 124 |
| 125 TEST(LibjingleLogTest, LogEverythingConfiguration) { |
| 126 ASSERT_TRUE(Initialize(talk_base::LS_SENSITIVE)); |
| 127 |
| 128 // In this configuration everything should be logged. |
| 129 LOG_V(talk_base::LS_ERROR) << AsString(talk_base::LS_ERROR); |
| 130 LOG_V(talk_base::LS_WARNING) << AsString(talk_base::LS_WARNING); |
| 131 LOG(LS_INFO) << AsString(talk_base::LS_INFO); |
| 132 LOG_E(LS_INFO, EN, EINTR) << "LOG_E(" << AsString(talk_base::LS_INFO) << ")"; |
| 133 LOG_V(talk_base::LS_VERBOSE) << AsString(talk_base::LS_VERBOSE); |
| 134 LOG_V(talk_base::LS_SENSITIVE) << AsString(talk_base::LS_SENSITIVE); |
| 135 |
| 136 // Read file to string. |
| 137 const std::string file_name_as_string = log_file_name; |
| 138 FilePath file_path(file_name_as_string); |
| 139 std::string contents_of_file; |
| 140 file_util::ReadFileToString(file_path, &contents_of_file); |
| 141 |
| 142 // Make sure string contains the expected values. |
| 143 EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_ERROR))); |
| 144 EXPECT_TRUE(ContainsString(contents_of_file, |
| 145 AsString(talk_base::LS_WARNING))); |
| 146 EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| 147 // LOG_E |
| 148 EXPECT_TRUE(ContainsString(contents_of_file, "Interrupted system call")); |
| 149 EXPECT_TRUE(ContainsString(contents_of_file, |
| 150 AsString(talk_base::LS_VERBOSE))); |
| 151 EXPECT_TRUE(ContainsString(contents_of_file, |
| 152 AsString(talk_base::LS_SENSITIVE))); |
| 153 } |
OLD | NEW |