| 6 |
| // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 |
| 16 | #include "absl/log/globals.h" |
| 17 |
| 18 | #include "gmock/gmock.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | #include "absl/base/attributes.h" |
| 21 | #include "absl/base/log_severity.h" |
| 22 | #include "absl/log/internal/globals.h" |
| 23 | #include "absl/log/internal/test_helpers.h" |
| 24 | #include "absl/log/log.h" |
| 25 | #include "absl/log/scoped_mock_log.h" |
| 26 |
| 27 | namespace { |
| 28 | using ::testing::_; |
| 29 | using ::testing::StrEq; |
| 30 |
| 31 | auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( |
| 32 | new absl::log_internal::LogTestEnvironment); |
| 33 |
| 34 | constexpr static absl::LogSeverityAtLeast DefaultMinLogLevel() { |
| 35 | return absl::LogSeverityAtLeast::kInfo; |
| 36 | } |
| 37 | constexpr static absl::LogSeverityAtLeast DefaultStderrThreshold() { |
| 38 | return absl::LogSeverityAtLeast::kError; |
| 39 | } |
| 40 |
| 41 | TEST(TestGlobals, MinLogLevel) { |
| 42 | EXPECT_EQ(absl::MinLogLevel(), DefaultMinLogLevel()); |
| 43 | absl::SetMinLogLevel(absl::LogSeverityAtLeast::kError); |
| 44 | EXPECT_EQ(absl::MinLogLevel(), absl::LogSeverityAtLeast::kError); |
| 45 | absl::SetMinLogLevel(DefaultMinLogLevel()); |
| 46 | } |
| 47 |
| 48 | TEST(TestGlobals, ScopedMinLogLevel) { |
| 49 | EXPECT_EQ(absl::MinLogLevel(), DefaultMinLogLevel()); |
| 50 | { |
| 51 | absl::log_internal::ScopedMinLogLevel scoped_stderr_threshold( |
| 52 | absl::LogSeverityAtLeast::kError); |
| 53 | EXPECT_EQ(absl::MinLogLevel(), absl::LogSeverityAtLeast::kError); |
| 54 | } |
| 55 | EXPECT_EQ(absl::MinLogLevel(), DefaultMinLogLevel()); |
| 56 | } |
| 57 |
| 58 | TEST(TestGlobals, StderrThreshold) { |
| 59 | EXPECT_EQ(absl::StderrThreshold(), DefaultStderrThreshold()); |
| 60 | absl::SetStderrThreshold(absl::LogSeverityAtLeast::kError); |
| 61 | EXPECT_EQ(absl::StderrThreshold(), absl::LogSeverityAtLeast::kError); |
| 62 | absl::SetStderrThreshold(DefaultStderrThreshold()); |
| 63 | } |
| 64 |
| 65 | TEST(TestGlobals, ScopedStderrThreshold) { |
| 66 | EXPECT_EQ(absl::StderrThreshold(), DefaultStderrThreshold()); |
| 67 | { |
| 68 | absl::ScopedStderrThreshold scoped_stderr_threshold( |
| 69 | absl::LogSeverityAtLeast::kError); |
| 70 | EXPECT_EQ(absl::StderrThreshold(), absl::LogSeverityAtLeast::kError); |
| 71 | } |
| 72 | EXPECT_EQ(absl::StderrThreshold(), DefaultStderrThreshold()); |
| 73 | } |
| 74 |
| 75 | TEST(TestGlobals, LogBacktraceAt) { |
| 76 | EXPECT_FALSE(absl::log_internal::ShouldLogBacktraceAt("some_file.cc", 111)); |
| 77 | absl::SetLogBacktraceLocation("some_file.cc", 111); |
| 78 | EXPECT_TRUE(absl::log_internal::ShouldLogBacktraceAt("some_file.cc", 111)); |
| 79 | EXPECT_FALSE( |
| 80 | absl::log_internal::ShouldLogBacktraceAt("another_file.cc", 222)); |
| 81 | } |
| 82 |
| 83 | TEST(TestGlobals, LogPrefix) { |
| 84 | EXPECT_TRUE(absl::ShouldPrependLogPrefix()); |
| 85 | absl::EnableLogPrefix(false); |
| 86 | EXPECT_FALSE(absl::ShouldPrependLogPrefix()); |
| 87 | absl::EnableLogPrefix(true); |
| 88 | EXPECT_TRUE(absl::ShouldPrependLogPrefix()); |
| 89 | } |
| 90 |
| 91 | TEST(TestGlobals, SetGlobalVLogLevel) { |
| 92 | EXPECT_EQ(absl::SetGlobalVLogLevel(42), 0); |
| 93 | EXPECT_EQ(absl::SetGlobalVLogLevel(1337), 42); |
| 94 | // Restore the value since it affects the default unset module value for |
| 95 | // `SetVLogLevel()`. |
| 96 | EXPECT_EQ(absl::SetGlobalVLogLevel(0), 1337); |
| 97 | } |
| 98 |
| 99 | TEST(TestGlobals, SetVLogLevel) { |
| 100 | EXPECT_EQ(absl::SetVLogLevel("setvloglevel", 42), 0); |
| 101 | EXPECT_EQ(absl::SetVLogLevel("setvloglevel", 1337), 42); |
| 102 | EXPECT_EQ(absl::SetVLogLevel("othersetvloglevel", 50), 0); |
| 103 |
| 104 | EXPECT_EQ(absl::SetVLogLevel("*pattern*", 1), 0); |
| 105 | EXPECT_EQ(absl::SetVLogLevel("*less_generic_pattern*", 2), 1); |
| 106 | // "pattern_match" matches the pattern "*pattern*". Therefore, the previous |
| 107 | // level must be 1. |
| 108 | EXPECT_EQ(absl::SetVLogLevel("pattern_match", 3), 1); |
| 109 | // "less_generic_pattern_match" matches the pattern "*pattern*". Therefore, |
| 110 | // the previous level must be 2. |
| 111 | EXPECT_EQ(absl::SetVLogLevel("less_generic_pattern_match", 4), 2); |
| 112 | } |
| 113 |
| 114 | TEST(TestGlobals, AndroidLogTag) { |
| 115 | // Verify invalid tags result in a check failure. |
| 116 | EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag(nullptr), ".*"); |
| 117 |
| 118 | // Verify valid tags applied. |
| 119 | EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("native")); |
| 120 | absl::SetAndroidNativeTag("test_tag"); |
| 121 | EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("test_tag")); |
| 122 |
| 123 | // Verify that additional calls (more than 1) result in a check failure. |
| 124 | EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag("test_tag_fail"), ".*"); |
| 125 | } |
| 126 |
| 127 | TEST(TestExitOnDFatal, OffTest) { |
| 128 | // Turn off... |
| 129 | absl::log_internal::SetExitOnDFatal(false); |
| 130 | EXPECT_FALSE(absl::log_internal::ExitOnDFatal()); |
| 131 |
| 132 | // We don't die. |
| 133 | { |
| 134 | absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected); |
| 135 |
| 136 | // LOG(DFATAL) has severity FATAL if debugging, but is |
| 137 | // downgraded to ERROR if not debugging. |
| 138 | EXPECT_CALL(log, Log(absl::kLogDebugFatal, _, "This should not be fatal")); |
| 139 |
| 140 | log.StartCapturingLogs(); |
| 141 | LOG(DFATAL) << "This should not be fatal"; |
| 142 | } |
| 143 | } |
| 144 |
| 145 | #if GTEST_HAS_DEATH_TEST |
| 146 | TEST(TestDeathWhileExitOnDFatal, OnTest) { |
| 147 | absl::log_internal::SetExitOnDFatal(true); |
| 148 | EXPECT_TRUE(absl::log_internal::ExitOnDFatal()); |
| 149 |
| 150 | // Death comes on little cats' feet. |
| 151 | EXPECT_DEBUG_DEATH({ LOG(DFATAL) << "This should be fatal in debug mode"; }, |
| 152 | "This should be fatal in debug mode"); |
| 153 | } |
| 154 | #endif |
| 155 |
| 156 | } // namespace |
| 157 |