xref: /aosp_15_r20/external/abseil-cpp/absl/log/globals_test.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 //
2 // Copyright 2022 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
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 
DefaultMinLogLevel()34 constexpr static absl::LogSeverityAtLeast DefaultMinLogLevel() {
35   return absl::LogSeverityAtLeast::kInfo;
36 }
DefaultStderrThreshold()37 constexpr static absl::LogSeverityAtLeast DefaultStderrThreshold() {
38   return absl::LogSeverityAtLeast::kError;
39 }
40 
TEST(TestGlobals,MinLogLevel)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 
TEST(TestGlobals,ScopedMinLogLevel)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 
TEST(TestGlobals,StderrThreshold)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 
TEST(TestGlobals,ScopedStderrThreshold)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 
TEST(TestGlobals,LogBacktraceAt)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 
TEST(TestGlobals,LogPrefix)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 
TEST(TestGlobals,SetGlobalVLogLevel)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 
TEST(TestGlobals,SetVLogLevel)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 
TEST(TestGlobals,AndroidLogTag)105 TEST(TestGlobals, AndroidLogTag) {
106   // Verify invalid tags result in a check failure.
107   EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag(nullptr), ".*");
108 
109   // Verify valid tags applied.
110   EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("native"));
111   absl::SetAndroidNativeTag("test_tag");
112   EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("test_tag"));
113 
114   // Verify that additional calls (more than 1) result in a check failure.
115   EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag("test_tag_fail"), ".*");
116 }
117 
TEST(TestExitOnDFatal,OffTest)118 TEST(TestExitOnDFatal, OffTest) {
119   // Turn off...
120   absl::log_internal::SetExitOnDFatal(false);
121   EXPECT_FALSE(absl::log_internal::ExitOnDFatal());
122 
123   // We don't die.
124   {
125     absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
126 
127     // LOG(DFATAL) has severity FATAL if debugging, but is
128     // downgraded to ERROR if not debugging.
129     EXPECT_CALL(log, Log(absl::kLogDebugFatal, _, "This should not be fatal"));
130 
131     log.StartCapturingLogs();
132     LOG(DFATAL) << "This should not be fatal";
133   }
134 }
135 
136 #if GTEST_HAS_DEATH_TEST
TEST(TestDeathWhileExitOnDFatal,OnTest)137 TEST(TestDeathWhileExitOnDFatal, OnTest) {
138   absl::log_internal::SetExitOnDFatal(true);
139   EXPECT_TRUE(absl::log_internal::ExitOnDFatal());
140 
141   // Death comes on little cats' feet.
142   EXPECT_DEBUG_DEATH({ LOG(DFATAL) << "This should be fatal in debug mode"; },
143                      "This should be fatal in debug mode");
144 }
145 #endif
146 
147 }  // namespace
148