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,AndroidLogTag)91 TEST(TestGlobals, AndroidLogTag) {
92 // Verify invalid tags result in a check failure.
93 EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag(nullptr), ".*");
94
95 // Verify valid tags applied.
96 EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("native"));
97 absl::SetAndroidNativeTag("test_tag");
98 EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("test_tag"));
99
100 // Verify that additional calls (more than 1) result in a check failure.
101 EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag("test_tag_fail"), ".*");
102 }
103
104 } // namespace
105