Chromium Code Reviews| Index: jingle/glue/logging_unittest.cc |
| =================================================================== |
| --- jingle/glue/logging_unittest.cc (revision 0) |
| +++ jingle/glue/logging_unittest.cc (revision 0) |
| @@ -0,0 +1,153 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// 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
|
| +// expressed in forms of them. Also note that we are only allowed to |
| +// call InitLogging() twice so the test cases are more dense than |
| +// normal. |
| + |
| +// 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.
|
| +// used. |
| +#include "third_party/libjingle/overrides/talk/base/logging.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/file_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +static const char log_file_name[] = "libjingle_logging.log"; |
| +static const int kDefaultVerbosity = 0; |
| + |
| +static const char* AsString(talk_base::LoggingSeverity severity) { |
| + switch (severity) { |
| + case talk_base::LS_ERROR: |
| + return "LS_ERROR"; |
| + case talk_base::LS_WARNING: |
| + return "LS_WARNING"; |
| + case talk_base::LS_INFO: |
| + return "LS_INFO"; |
| + case talk_base::LS_VERBOSE: |
| + return "LS_VERBOSE"; |
| + case talk_base::LS_SENSITIVE: |
| + return "LS_SENSITIVE"; |
| + default: |
| + return ""; |
| + } |
| +} |
| + |
| +bool ContainsString(const std::string& original, const char* string_to_match) { |
| + return original.find(string_to_match) != std::string::npos; |
| +} |
| + |
| +bool Initialize(int verbosity_level) { |
| + if (verbosity_level != kDefaultVerbosity) { |
| + // Update the command line with specified verbosity level for this file. |
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + std::ostringstream value_stream; |
| + value_stream << "logging_unittest=" << verbosity_level; |
| + const std::string& value = value_stream.str(); |
| + command_line->AppendSwitchASCII("vmodule", value); |
| + } |
| + |
| + // The command line flags are parsed here and the log file name is set. |
| + if (!InitLogging(log_file_name, logging::LOG_ONLY_TO_FILE, |
| + logging::DONT_LOCK_LOG_FILE, logging::DELETE_OLD_LOG_FILE, |
| + logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) { |
| + return false; |
| + } |
| + EXPECT_TRUE(VLOG_IS_ON(verbosity_level)); |
| + EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1)); |
| + return true; |
| +} |
| + |
| +TEST(LibjingleLogTest, DefaultConfiguration) { |
| + ASSERT_TRUE(Initialize(kDefaultVerbosity)); |
| + |
| + // In the default configuration nothing should be logged. |
| + LOG_V(talk_base::LS_ERROR) << AsString(talk_base::LS_ERROR); |
| + LOG_V(talk_base::LS_WARNING) << AsString(talk_base::LS_WARNING); |
| + LOG_V(talk_base::LS_INFO) << AsString(talk_base::LS_INFO); |
| + LOG_V(talk_base::LS_VERBOSE) << AsString(talk_base::LS_VERBOSE); |
| + LOG_V(talk_base::LS_SENSITIVE) << AsString(talk_base::LS_SENSITIVE); |
| + |
| + // Read file to string. |
| + const std::string file_name_as_string = log_file_name; |
| + FilePath file_path(file_name_as_string); |
| + std::string contents_of_file; |
| + file_util::ReadFileToString(file_path, &contents_of_file); |
| + |
| + // Make sure string contains the expected values. |
| + EXPECT_FALSE(ContainsString(contents_of_file, AsString(talk_base::LS_ERROR))); |
| + EXPECT_FALSE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_WARNING))); |
| + EXPECT_FALSE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| + EXPECT_FALSE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_VERBOSE))); |
| + EXPECT_FALSE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_SENSITIVE))); |
| +} |
| + |
| +TEST(LibjingleLogTest, InfoConfiguration) { |
| + ASSERT_TRUE(Initialize(talk_base::LS_INFO)); |
| + |
| + // In this configuration everything lower or equal to LS_INFO should be |
| + // logged. |
| + LOG_V(talk_base::LS_ERROR) << AsString(talk_base::LS_ERROR); |
| + LOG_V(talk_base::LS_WARNING) << AsString(talk_base::LS_WARNING); |
| + LOG_V(talk_base::LS_INFO) << AsString(talk_base::LS_INFO); |
| + LOG_V(talk_base::LS_VERBOSE) << AsString(talk_base::LS_VERBOSE); |
| + LOG_V(talk_base::LS_SENSITIVE) << AsString(talk_base::LS_SENSITIVE); |
| + |
| + // Read file to string. |
| + const std::string file_name_as_string = log_file_name; |
| + FilePath file_path(file_name_as_string); |
| + std::string contents_of_file; |
| + file_util::ReadFileToString(file_path, &contents_of_file); |
| + |
| + // Make sure string contains the expected values. |
| + EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_ERROR))); |
| + EXPECT_TRUE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_WARNING))); |
| + EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| + EXPECT_FALSE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_VERBOSE))); |
| + EXPECT_FALSE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_SENSITIVE))); |
| + |
| + // Also check that the log is proper. |
| + EXPECT_TRUE(ContainsString(contents_of_file, "logging_unittest.cc")); |
| + EXPECT_FALSE(ContainsString(contents_of_file, "logging.h")); |
| + EXPECT_FALSE(ContainsString(contents_of_file, "logging.cc")); |
| +} |
| + |
| +TEST(LibjingleLogTest, LogEverythingConfiguration) { |
| + ASSERT_TRUE(Initialize(talk_base::LS_SENSITIVE)); |
| + |
| + // In this configuration everything should be logged. |
| + LOG_V(talk_base::LS_ERROR) << AsString(talk_base::LS_ERROR); |
| + LOG_V(talk_base::LS_WARNING) << AsString(talk_base::LS_WARNING); |
| + LOG_V(talk_base::LS_INFO) << AsString(talk_base::LS_INFO); |
| + LOG_E(LS_INFO, EN, EINTR) << "LOG_E(" << AsString(talk_base::LS_INFO) << ")"; |
| + LOG_V(talk_base::LS_VERBOSE) << AsString(talk_base::LS_VERBOSE); |
| + LOG_V(talk_base::LS_SENSITIVE) << AsString(talk_base::LS_SENSITIVE); |
| + |
| + // Read file to string. |
| + const std::string file_name_as_string = log_file_name; |
| + FilePath file_path(file_name_as_string); |
| + std::string contents_of_file; |
| + file_util::ReadFileToString(file_path, &contents_of_file); |
| + |
| + // Make sure string contains the expected values. |
| + EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_ERROR))); |
| + EXPECT_TRUE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_WARNING))); |
| + EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| + // Begin: LOG_E |
| + EXPECT_TRUE(ContainsString(contents_of_file, AsString(talk_base::LS_INFO))); |
| + EXPECT_TRUE(ContainsString(contents_of_file, "Interrupted system call")); |
| + // End: LOG_E |
| + EXPECT_TRUE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_VERBOSE))); |
| + EXPECT_TRUE(ContainsString(contents_of_file, |
| + AsString(talk_base::LS_SENSITIVE))); |
| +} |
| Property changes on: jingle/glue/logging_unittest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |