File: logging_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (210 lines) | stat: -rw-r--r-- 7,770 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Note: this test tests RTC_LOG_V and RTC_LOG_E since all other logs are
// expressed in forms of them. RTC_LOG is also tested for good measure.
// Also note that we are only allowed to call InitLogging() twice so the test
// cases are more dense than normal.

// We must include Chromium headers before including the overrides header
// since webrtc's logging.h file may conflict with chromium.

#include "base/logging.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc_overrides/rtc_base/logging.h"

namespace {

static const int kDefaultVerbosity = 0;

static const char* AsString(webrtc::LoggingSeverity severity) {
  switch (severity) {
    case webrtc::LS_ERROR:
      return "LS_ERROR";
    case webrtc::LS_WARNING:
      return "LS_WARNING";
    case webrtc::LS_INFO:
      return "LS_INFO";
    case webrtc::LS_VERBOSE:
      return "LS_VERBOSE";
    case webrtc::LS_SENSITIVE:
      return "LS_SENSITIVE";
    default:
      return "";
  }
}

class WebRtcTextLogTest : public testing::Test {
 public:
  void SetUp() override {
    // Some builds don't have runtime vlogging. See base/logging.h.
    if (!VLOG_IS_ON(0)) {
      GTEST_SKIP();
    }

    ASSERT_TRUE(log_dir_.CreateUniqueTempDir());
    log_file_path_ = log_dir_.GetPath().AppendASCII("webrtc_log");
  }

 protected:
  bool Initialize(int verbosity_level) {
    // The command line flags are parsed here and the log file name is set.
    logging::LoggingSettings settings;
    settings.logging_dest = logging::LOG_TO_FILE;
    settings.log_file_path = log_file_path_.value().data();
    settings.lock_log = logging::DONT_LOCK_LOG_FILE;
    settings.delete_old = logging::DELETE_OLD_LOG_FILE;
    if (!logging::InitLogging(settings)) {
      return false;
    }
    logging::SetMinLogLevel(-verbosity_level);

    EXPECT_TRUE(VLOG_IS_ON(verbosity_level));

    EXPECT_FALSE(VLOG_IS_ON(verbosity_level + 1));
    return true;
  }

  base::ScopedTempDir log_dir_;
  base::FilePath log_file_path_;
};

TEST_F(WebRtcTextLogTest, DefaultConfiguration) {
  ASSERT_TRUE(Initialize(kDefaultVerbosity));

  // In the default configuration only warnings and errors should be logged.
  RTC_LOG_V(webrtc::LS_ERROR) << AsString(webrtc::LS_ERROR);
  RTC_LOG_V(webrtc::LS_WARNING) << AsString(webrtc::LS_WARNING);
  RTC_LOG_V(webrtc::LS_INFO) << AsString(webrtc::LS_INFO);
  RTC_LOG_V(webrtc::LS_VERBOSE) << AsString(webrtc::LS_VERBOSE);
  RTC_LOG_V(webrtc::LS_SENSITIVE) << AsString(webrtc::LS_SENSITIVE);

  // Read file to string.
  std::string contents_of_file;
  base::ReadFileToString(log_file_path_, &contents_of_file);

  // Make sure string contains the expected values.
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_ERROR)));
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_WARNING)));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr(AsString(webrtc::LS_INFO))));
  EXPECT_THAT(
      contents_of_file,
      ::testing::Not(::testing::HasSubstr(AsString(webrtc::LS_VERBOSE))));
  EXPECT_THAT(
      contents_of_file,
      ::testing::Not(::testing::HasSubstr(AsString(webrtc::LS_SENSITIVE))));
}

TEST_F(WebRtcTextLogTest, InfoConfiguration) {
  ASSERT_TRUE(Initialize(0));  // 0 == Chrome's 'info' level.

  // In this configuration everything lower or equal to LS_INFO should be
  // logged.
  RTC_LOG_V(webrtc::LS_ERROR) << AsString(webrtc::LS_ERROR);
  RTC_LOG_V(webrtc::LS_WARNING) << AsString(webrtc::LS_WARNING);
  RTC_LOG_V(webrtc::LS_INFO) << AsString(webrtc::LS_INFO);
  RTC_LOG_V(webrtc::LS_VERBOSE) << AsString(webrtc::LS_VERBOSE);
  RTC_LOG_V(webrtc::LS_SENSITIVE) << AsString(webrtc::LS_SENSITIVE);

  // Read file to string.
  std::string contents_of_file;
  base::ReadFileToString(log_file_path_, &contents_of_file);

  // Make sure string contains the expected values.
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_ERROR)));
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_WARNING)));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr(AsString(webrtc::LS_INFO))));
  EXPECT_THAT(
      contents_of_file,
      ::testing::Not(::testing::HasSubstr(AsString(webrtc::LS_VERBOSE))));
  EXPECT_THAT(
      contents_of_file,
      ::testing::Not(::testing::HasSubstr(AsString(webrtc::LS_SENSITIVE))));

  // Also check that the log is proper.
  EXPECT_THAT(contents_of_file, ::testing::HasSubstr("logging_unittest.cc"));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr("logging.h")));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr("logging.cc")));
}

TEST_F(WebRtcTextLogTest, LogEverythingConfiguration) {
  ASSERT_TRUE(Initialize(2));  // verbosity at level 2 allows LS_SENSITIVE.

  // In this configuration everything should be logged.
  RTC_LOG_V(webrtc::LS_ERROR) << AsString(webrtc::LS_ERROR);
  RTC_LOG_V(webrtc::LS_WARNING) << AsString(webrtc::LS_WARNING);
  RTC_LOG(LS_INFO) << AsString(webrtc::LS_INFO);
  static const int kFakeError = 1;
  RTC_LOG_E(LS_INFO, EN, kFakeError)
      << "RTC_LOG_E(" << AsString(webrtc::LS_INFO) << ")";
  RTC_LOG_V(webrtc::LS_VERBOSE) << AsString(webrtc::LS_VERBOSE);
  RTC_LOG_V(webrtc::LS_SENSITIVE) << AsString(webrtc::LS_SENSITIVE);

  // Read file to string.
  std::string contents_of_file;
  base::ReadFileToString(log_file_path_, &contents_of_file);

  // Make sure string contains the expected values.
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_ERROR)));
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_WARNING)));

  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_INFO)));
  // RTC_LOG_E
  EXPECT_THAT(contents_of_file, ::testing::HasSubstr(strerror(kFakeError)));
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_VERBOSE)));
  EXPECT_THAT(contents_of_file,
              ::testing::HasSubstr(AsString(webrtc::LS_SENSITIVE)));
}

TEST_F(WebRtcTextLogTest, LogIf) {
  ASSERT_TRUE(Initialize(2));

  RTC_LOG_IF(LS_INFO, true) << "IfTrue";
  RTC_LOG_IF(LS_INFO, false) << "IfFalse";
  RTC_LOG_IF_F(LS_INFO, true) << "LogF";
  RTC_LOG_IF_F(LS_INFO, false) << "NoLogF";

  RTC_DLOG_IF(LS_INFO, true) << "DebugIfTrue";
  RTC_DLOG_IF(LS_INFO, false) << "DebugIfFalse";
  RTC_DLOG_IF_F(LS_INFO, true) << "DebugLogF";
  RTC_DLOG_IF_F(LS_INFO, false) << "DebugNotLogF";

  // Read file to string.
  std::string contents_of_file;
  base::ReadFileToString(log_file_path_, &contents_of_file);

  EXPECT_THAT(contents_of_file, ::testing::HasSubstr("IfTrue"));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr("IfFalse")));
  EXPECT_THAT(contents_of_file, ::testing::HasSubstr(__FUNCTION__));
  EXPECT_THAT(contents_of_file, ::testing::HasSubstr("LogF"));
  EXPECT_THAT(contents_of_file, ::testing::Not(::testing::HasSubstr("NoLogF")));

#if RTC_DLOG_IS_ON
  EXPECT_THAT(contents_of_file, ::testing::HasSubstr("DebugIfTrue"));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr("DebugIfFalse")));
  EXPECT_THAT(contents_of_file, ::testing::HasSubstr("DebugLogF"));
  EXPECT_THAT(contents_of_file,
              ::testing::Not(::testing::HasSubstr("DebugNoLogF")));
#endif  // RTC_DLOG_IF_ON
}

}  // namespace