logging_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // Note: this test tests RTC_LOG_V and RTC_LOG_E since all other logs are
  5. // expressed in forms of them. RTC_LOG is also tested for good measure.
  6. // Also note that we are only allowed to call InitLogging() twice so the test
  7. // cases are more dense than normal.
  8. // We must include Chromium headers before including the overrides header
  9. // since webrtc's logging.h file may conflict with chromium.
  10. #include "base/logging.h"
  11. #include "base/command_line.h"
  12. #include "base/files/file_util.h"
  13. #include "base/files/scoped_temp_dir.h"
  14. #include "build/build_config.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "third_party/webrtc_overrides/rtc_base/logging.h"
  17. static const int kDefaultVerbosity = 0;
  18. static const char* AsString(rtc::LoggingSeverity severity) {
  19. switch (severity) {
  20. case rtc::LS_ERROR:
  21. return "LS_ERROR";
  22. case rtc::LS_WARNING:
  23. return "LS_WARNING";
  24. case rtc::LS_INFO:
  25. return "LS_INFO";
  26. case rtc::LS_VERBOSE:
  27. return "LS_VERBOSE";
  28. case rtc::LS_SENSITIVE:
  29. return "LS_SENSITIVE";
  30. default:
  31. return "";
  32. }
  33. }
  34. static bool ContainsString(const std::string& original,
  35. const char* string_to_match) {
  36. return original.find(string_to_match) != std::string::npos;
  37. }
  38. class WebRtcTextLogTest : public testing::Test {
  39. public:
  40. void SetUp() override {
  41. ASSERT_TRUE(log_dir_.CreateUniqueTempDir());
  42. log_file_path_ = log_dir_.GetPath().AppendASCII("webrtc_log");
  43. }
  44. protected:
  45. bool Initialize(int verbosity_level) {
  46. if (verbosity_level != kDefaultVerbosity) {
  47. // Update the command line with specified verbosity level for this file.
  48. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  49. std::ostringstream value_stream;
  50. value_stream << "logging_unittest=" << verbosity_level;
  51. const std::string& value = value_stream.str();
  52. command_line->AppendSwitchASCII("vmodule", value);
  53. }
  54. // The command line flags are parsed here and the log file name is set.
  55. logging::LoggingSettings settings;
  56. settings.logging_dest = logging::LOG_TO_FILE;
  57. settings.log_file_path = log_file_path_.value().data();
  58. settings.lock_log = logging::DONT_LOCK_LOG_FILE;
  59. settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  60. if (!logging::InitLogging(settings)) {
  61. return false;
  62. }
  63. #if BUILDFLAG(USE_RUNTIME_VLOG)
  64. EXPECT_TRUE(VLOG_IS_ON(verbosity_level));
  65. #else
  66. // VLOGs default to off when not using runtime vlog.
  67. EXPECT_FALSE(VLOG_IS_ON(verbosity_level));
  68. #endif // BUILDFLAG(USE_RUNTIME_VLOG)
  69. EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1));
  70. return true;
  71. }
  72. base::ScopedTempDir log_dir_;
  73. base::FilePath log_file_path_;
  74. };
  75. TEST_F(WebRtcTextLogTest, DefaultConfiguration) {
  76. ASSERT_TRUE(Initialize(kDefaultVerbosity));
  77. // In the default configuration only warnings and errors should be logged.
  78. RTC_LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR);
  79. RTC_LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING);
  80. RTC_LOG_V(rtc::LS_INFO) << AsString(rtc::LS_INFO);
  81. RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
  82. RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
  83. // Read file to string.
  84. std::string contents_of_file;
  85. base::ReadFileToString(log_file_path_, &contents_of_file);
  86. // Make sure string contains the expected values.
  87. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
  88. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
  89. EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
  90. EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE)));
  91. EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE)));
  92. }
  93. TEST_F(WebRtcTextLogTest, InfoConfiguration) {
  94. ASSERT_TRUE(Initialize(0)); // 0 == Chrome's 'info' level.
  95. // In this configuration everything lower or equal to LS_INFO should be
  96. // logged.
  97. RTC_LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR);
  98. RTC_LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING);
  99. RTC_LOG_V(rtc::LS_INFO) << AsString(rtc::LS_INFO);
  100. RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
  101. RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
  102. // Read file to string.
  103. std::string contents_of_file;
  104. base::ReadFileToString(log_file_path_, &contents_of_file);
  105. // Make sure string contains the expected values.
  106. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
  107. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
  108. EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
  109. EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE)));
  110. EXPECT_FALSE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE)));
  111. // Also check that the log is proper.
  112. EXPECT_TRUE(ContainsString(contents_of_file, "logging_unittest.cc"));
  113. EXPECT_FALSE(ContainsString(contents_of_file, "logging.h"));
  114. EXPECT_FALSE(ContainsString(contents_of_file, "logging.cc"));
  115. }
  116. TEST_F(WebRtcTextLogTest, LogEverythingConfiguration) {
  117. ASSERT_TRUE(Initialize(2)); // verbosity at level 2 allows LS_SENSITIVE.
  118. // In this configuration everything should be logged.
  119. RTC_LOG_V(rtc::LS_ERROR) << AsString(rtc::LS_ERROR);
  120. RTC_LOG_V(rtc::LS_WARNING) << AsString(rtc::LS_WARNING);
  121. RTC_LOG(LS_INFO) << AsString(rtc::LS_INFO);
  122. static const int kFakeError = 1;
  123. RTC_LOG_E(LS_INFO, EN, kFakeError)
  124. << "RTC_LOG_E(" << AsString(rtc::LS_INFO) << ")";
  125. RTC_LOG_V(rtc::LS_VERBOSE) << AsString(rtc::LS_VERBOSE);
  126. RTC_LOG_V(rtc::LS_SENSITIVE) << AsString(rtc::LS_SENSITIVE);
  127. // Read file to string.
  128. std::string contents_of_file;
  129. base::ReadFileToString(log_file_path_, &contents_of_file);
  130. // Make sure string contains the expected values.
  131. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_ERROR)));
  132. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_WARNING)));
  133. #if BUILDFLAG(USE_RUNTIME_VLOG)
  134. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_INFO)));
  135. // RTC_LOG_E
  136. EXPECT_TRUE(ContainsString(contents_of_file, strerror(kFakeError)));
  137. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_VERBOSE)));
  138. EXPECT_TRUE(ContainsString(contents_of_file, AsString(rtc::LS_SENSITIVE)));
  139. #endif // BUILDFLAG(USE_RUNTIME_VLOG)
  140. }