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