1 // Copyright 2022 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/globals.h"
16
17 #include <stddef.h>
18 #include <stdint.h>
19
20 #include <atomic>
21
22 #include "absl/base/attributes.h"
23 #include "absl/base/config.h"
24 #include "absl/base/internal/atomic_hook.h"
25 #include "absl/base/log_severity.h"
26 #include "absl/hash/hash.h"
27 #include "absl/strings/string_view.h"
28
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31 namespace {
32
33 // These atomics represent logging library configuration.
34 // Integer types are used instead of absl::LogSeverity to ensure that a
35 // lock-free std::atomic is used when possible.
36 ABSL_CONST_INIT std::atomic<int> min_log_level{
37 static_cast<int>(absl::LogSeverityAtLeast::kInfo)};
38 ABSL_CONST_INIT std::atomic<int> stderrthreshold{
39 static_cast<int>(absl::LogSeverityAtLeast::kError)};
40 // We evaluate this value as a hash comparison to avoid having to
41 // hold a mutex or make a copy (to access the value of a string-typed flag) in
42 // very hot codepath.
43 ABSL_CONST_INIT std::atomic<size_t> log_backtrace_at_hash{0};
44 ABSL_CONST_INIT std::atomic<bool> prepend_log_prefix{true};
45
46 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
47 absl::base_internal::AtomicHook<log_internal::LoggingGlobalsListener>
48 logging_globals_listener;
49
HashSiteForLogBacktraceAt(absl::string_view file,int line)50 size_t HashSiteForLogBacktraceAt(absl::string_view file, int line) {
51 return absl::HashOf(file, line);
52 }
53
TriggerLoggingGlobalsListener()54 void TriggerLoggingGlobalsListener() {
55 auto* listener = logging_globals_listener.Load();
56 if (listener != nullptr) listener();
57 }
58
59 } // namespace
60
61 namespace log_internal {
62
RawSetMinLogLevel(absl::LogSeverityAtLeast severity)63 void RawSetMinLogLevel(absl::LogSeverityAtLeast severity) {
64 min_log_level.store(static_cast<int>(severity), std::memory_order_release);
65 }
66
RawSetStderrThreshold(absl::LogSeverityAtLeast severity)67 void RawSetStderrThreshold(absl::LogSeverityAtLeast severity) {
68 stderrthreshold.store(static_cast<int>(severity), std::memory_order_release);
69 }
70
RawEnableLogPrefix(bool on_off)71 void RawEnableLogPrefix(bool on_off) {
72 prepend_log_prefix.store(on_off, std::memory_order_release);
73 }
74
SetLoggingGlobalsListener(LoggingGlobalsListener l)75 void SetLoggingGlobalsListener(LoggingGlobalsListener l) {
76 logging_globals_listener.Store(l);
77 }
78
79 } // namespace log_internal
80
MinLogLevel()81 absl::LogSeverityAtLeast MinLogLevel() {
82 return static_cast<absl::LogSeverityAtLeast>(
83 min_log_level.load(std::memory_order_acquire));
84 }
85
SetMinLogLevel(absl::LogSeverityAtLeast severity)86 void SetMinLogLevel(absl::LogSeverityAtLeast severity) {
87 log_internal::RawSetMinLogLevel(severity);
88 TriggerLoggingGlobalsListener();
89 }
90
91 namespace log_internal {
92
ScopedMinLogLevel(absl::LogSeverityAtLeast severity)93 ScopedMinLogLevel::ScopedMinLogLevel(absl::LogSeverityAtLeast severity)
94 : saved_severity_(absl::MinLogLevel()) {
95 absl::SetMinLogLevel(severity);
96 }
~ScopedMinLogLevel()97 ScopedMinLogLevel::~ScopedMinLogLevel() {
98 absl::SetMinLogLevel(saved_severity_);
99 }
100
101 } // namespace log_internal
102
StderrThreshold()103 absl::LogSeverityAtLeast StderrThreshold() {
104 return static_cast<absl::LogSeverityAtLeast>(
105 stderrthreshold.load(std::memory_order_acquire));
106 }
107
SetStderrThreshold(absl::LogSeverityAtLeast severity)108 void SetStderrThreshold(absl::LogSeverityAtLeast severity) {
109 log_internal::RawSetStderrThreshold(severity);
110 TriggerLoggingGlobalsListener();
111 }
112
ScopedStderrThreshold(absl::LogSeverityAtLeast severity)113 ScopedStderrThreshold::ScopedStderrThreshold(absl::LogSeverityAtLeast severity)
114 : saved_severity_(absl::StderrThreshold()) {
115 absl::SetStderrThreshold(severity);
116 }
117
~ScopedStderrThreshold()118 ScopedStderrThreshold::~ScopedStderrThreshold() {
119 absl::SetStderrThreshold(saved_severity_);
120 }
121
122 namespace log_internal {
123
ShouldLogBacktraceAt(absl::string_view file,int line)124 bool ShouldLogBacktraceAt(absl::string_view file, int line) {
125 const size_t flag_hash =
126 log_backtrace_at_hash.load(std::memory_order_acquire);
127
128 return flag_hash != 0 && flag_hash == HashSiteForLogBacktraceAt(file, line);
129 }
130
131 } // namespace log_internal
132
SetLogBacktraceLocation(absl::string_view file,int line)133 void SetLogBacktraceLocation(absl::string_view file, int line) {
134 log_backtrace_at_hash.store(HashSiteForLogBacktraceAt(file, line),
135 std::memory_order_release);
136 }
137
ShouldPrependLogPrefix()138 bool ShouldPrependLogPrefix() {
139 return prepend_log_prefix.load(std::memory_order_acquire);
140 }
141
EnableLogPrefix(bool on_off)142 void EnableLogPrefix(bool on_off) {
143 log_internal::RawEnableLogPrefix(on_off);
144 TriggerLoggingGlobalsListener();
145 }
146
147 ABSL_NAMESPACE_END
148 } // namespace absl
149