vlog_is_on_test.cc (6972B)
1 // Copyright 2023 The Abseil Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "absl/log/vlog_is_on.h" 16 17 #include "gmock/gmock.h" 18 #include "gtest/gtest.h" 19 #include "absl/base/log_severity.h" 20 #include "absl/flags/flag.h" 21 #include "absl/log/flags.h" 22 #include "absl/log/globals.h" 23 #include "absl/log/log.h" 24 #include "absl/log/scoped_mock_log.h" 25 #include "absl/types/optional.h" 26 27 namespace { 28 29 using ::testing::_; 30 31 absl::optional<int> MaxLogVerbosity() { 32 #ifdef ABSL_MAX_VLOG_VERBOSITY 33 return ABSL_MAX_VLOG_VERBOSITY; 34 #else 35 return absl::nullopt; 36 #endif 37 } 38 39 absl::optional<int> MinLogLevel() { 40 #ifdef ABSL_MIN_LOG_LEVEL 41 return static_cast<int>(ABSL_MIN_LOG_LEVEL); 42 #else 43 return absl::nullopt; 44 #endif 45 } 46 47 // This fixture is used to reset the VLOG levels to their default values before 48 // each test. 49 class VLogIsOnTest : public ::testing::Test { 50 protected: 51 void SetUp() override { ResetVLogLevels(); } 52 53 private: 54 // Resets the VLOG levels to their default values. 55 // It is supposed to be called in the SetUp() method of the test fixture to 56 // eliminate any side effects from other tests. 57 static void ResetVLogLevels() { 58 absl::log_internal::UpdateVModule(""); 59 absl::SetGlobalVLogLevel(0); 60 } 61 }; 62 63 TEST_F(VLogIsOnTest, GlobalWorksWithoutMaxVerbosityAndMinLogLevel) { 64 if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) { 65 GTEST_SKIP(); 66 } 67 68 absl::SetGlobalVLogLevel(3); 69 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 70 71 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "important")); 72 73 log.StartCapturingLogs(); 74 VLOG(3) << "important"; 75 VLOG(4) << "spam"; 76 } 77 78 TEST_F(VLogIsOnTest, FileWorksWithoutMaxVerbosityAndMinLogLevel) { 79 if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) { 80 GTEST_SKIP(); 81 } 82 83 absl::SetVLogLevel("vlog_is_on_test", 3); 84 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 85 86 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "important")); 87 88 log.StartCapturingLogs(); 89 VLOG(3) << "important"; 90 VLOG(4) << "spam"; 91 } 92 93 TEST_F(VLogIsOnTest, PatternWorksWithoutMaxVerbosityAndMinLogLevel) { 94 if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) { 95 GTEST_SKIP(); 96 } 97 98 absl::SetVLogLevel("vlog_is_on*", 3); 99 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 100 101 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "important")); 102 103 log.StartCapturingLogs(); 104 VLOG(3) << "important"; 105 VLOG(4) << "spam"; 106 } 107 108 TEST_F(VLogIsOnTest, 109 PatternOverridesLessGenericOneWithoutMaxVerbosityAndMinLogLevel) { 110 if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) { 111 GTEST_SKIP(); 112 } 113 114 // This should disable logging in this file 115 absl::SetVLogLevel("vlog_is_on*", -1); 116 // This overrides the previous setting, because "vlog*" is more generic than 117 // "vlog_is_on*". This should enable VLOG level 3 in this file. 118 absl::SetVLogLevel("vlog*", 3); 119 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 120 121 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "important")); 122 123 log.StartCapturingLogs(); 124 VLOG(3) << "important"; 125 VLOG(4) << "spam"; 126 } 127 128 TEST_F(VLogIsOnTest, 129 PatternDoesNotOverridesMoreGenericOneWithoutMaxVerbosityAndMinLogLevel) { 130 if (MaxLogVerbosity().has_value() || MinLogLevel().has_value()) { 131 GTEST_SKIP(); 132 } 133 134 // This should enable VLOG level 3 in this file. 135 absl::SetVLogLevel("vlog*", 3); 136 // This should not change the VLOG level in this file. The pattern does not 137 // match this file and it is less generic than the previous patter "vlog*". 138 // Therefore, it does not disable VLOG level 3 in this file. 139 absl::SetVLogLevel("vlog_is_on_some_other_test*", -1); 140 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 141 142 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "important")); 143 144 log.StartCapturingLogs(); 145 VLOG(3) << "important"; 146 VLOG(5) << "spam"; 147 } 148 149 TEST_F(VLogIsOnTest, GlobalDoesNotFilterBelowMaxVerbosity) { 150 if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() < 2) { 151 GTEST_SKIP(); 152 } 153 154 // Set an arbitrary high value to avoid filtering VLOGs in tests by default. 155 absl::SetGlobalVLogLevel(1000); 156 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 157 158 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "asdf")); 159 160 log.StartCapturingLogs(); 161 VLOG(2) << "asdf"; 162 } 163 164 TEST_F(VLogIsOnTest, FileDoesNotFilterBelowMaxVerbosity) { 165 if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() < 2) { 166 GTEST_SKIP(); 167 } 168 169 // Set an arbitrary high value to avoid filtering VLOGs in tests by default. 170 absl::SetVLogLevel("vlog_is_on_test", 1000); 171 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 172 173 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "asdf")); 174 175 log.StartCapturingLogs(); 176 VLOG(2) << "asdf"; 177 } 178 179 TEST_F(VLogIsOnTest, PatternDoesNotFilterBelowMaxVerbosity) { 180 if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() < 2) { 181 GTEST_SKIP(); 182 } 183 184 // Set an arbitrary high value to avoid filtering VLOGs in tests by default. 185 absl::SetVLogLevel("vlog_is_on*", 1000); 186 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 187 188 EXPECT_CALL(log, Log(absl::LogSeverity::kInfo, _, "asdf")); 189 190 log.StartCapturingLogs(); 191 VLOG(2) << "asdf"; 192 } 193 194 TEST_F(VLogIsOnTest, GlobalFiltersAboveMaxVerbosity) { 195 if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() >= 4) { 196 GTEST_SKIP(); 197 } 198 199 // Set an arbitrary high value to avoid filtering VLOGs in tests by default. 200 absl::SetGlobalVLogLevel(1000); 201 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 202 203 log.StartCapturingLogs(); 204 VLOG(4) << "dfgh"; 205 } 206 207 TEST_F(VLogIsOnTest, FileFiltersAboveMaxVerbosity) { 208 if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() >= 4) { 209 GTEST_SKIP(); 210 } 211 212 // Set an arbitrary high value to avoid filtering VLOGs in tests by default. 213 absl::SetVLogLevel("vlog_is_on_test", 1000); 214 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 215 216 log.StartCapturingLogs(); 217 VLOG(4) << "dfgh"; 218 } 219 220 TEST_F(VLogIsOnTest, PatternFiltersAboveMaxVerbosity) { 221 if (!MaxLogVerbosity().has_value() || *MaxLogVerbosity() >= 4) { 222 GTEST_SKIP(); 223 } 224 225 // Set an arbitrary high value to avoid filtering VLOGs in tests by default. 226 absl::SetVLogLevel("vlog_is_on*", 1000); 227 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); 228 229 log.StartCapturingLogs(); 230 VLOG(4) << "dfgh"; 231 } 232 233 } // namespace