xref: /aosp_15_r20/external/cronet/third_party/abseil-cpp/absl/log/globals.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 // -----------------------------------------------------------------------------
16 // File: log/globals.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header declares global logging library configuration knobs.
20 
21 #ifndef ABSL_LOG_GLOBALS_H_
22 #define ABSL_LOG_GLOBALS_H_
23 
24 #include "absl/base/attributes.h"
25 #include "absl/base/config.h"
26 #include "absl/base/log_severity.h"
27 #include "absl/log/internal/vlog_config.h"
28 #include "absl/strings/string_view.h"
29 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 
33 //------------------------------------------------------------------------------
34 //  Minimum Log Level
35 //------------------------------------------------------------------------------
36 //
37 // Messages logged at or above this severity are directed to all registered log
38 // sinks or skipped otherwise. This parameter can also be modified using
39 // command line flag --minloglevel.
40 // See absl/base/log_severity.h for descriptions of severity levels.
41 
42 // MinLogLevel()
43 //
44 // Returns the value of the Minimum Log Level parameter.
45 // This function is async-signal-safe.
46 ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast MinLogLevel();
47 
48 // SetMinLogLevel()
49 //
50 // Updates the value of Minimum Log Level parameter.
51 // This function is async-signal-safe.
52 void SetMinLogLevel(absl::LogSeverityAtLeast severity);
53 
54 namespace log_internal {
55 
56 // ScopedMinLogLevel
57 //
58 // RAII type used to temporarily update the Min Log Level parameter.
59 class ScopedMinLogLevel final {
60  public:
61   explicit ScopedMinLogLevel(absl::LogSeverityAtLeast severity);
62   ScopedMinLogLevel(const ScopedMinLogLevel&) = delete;
63   ScopedMinLogLevel& operator=(const ScopedMinLogLevel&) = delete;
64   ~ScopedMinLogLevel();
65 
66  private:
67   absl::LogSeverityAtLeast saved_severity_;
68 };
69 
70 }  // namespace log_internal
71 
72 //------------------------------------------------------------------------------
73 // Stderr Threshold
74 //------------------------------------------------------------------------------
75 //
76 // Messages logged at or above this level are directed to stderr in
77 // addition to other registered log sinks. This parameter can also be modified
78 // using command line flag --stderrthreshold.
79 // See absl/base/log_severity.h for descriptions of severity levels.
80 
81 // StderrThreshold()
82 //
83 // Returns the value of the Stderr Threshold parameter.
84 // This function is async-signal-safe.
85 ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast StderrThreshold();
86 
87 // SetStderrThreshold()
88 //
89 // Updates the Stderr Threshold parameter.
90 // This function is async-signal-safe.
91 void SetStderrThreshold(absl::LogSeverityAtLeast severity);
SetStderrThreshold(absl::LogSeverity severity)92 inline void SetStderrThreshold(absl::LogSeverity severity) {
93   absl::SetStderrThreshold(static_cast<absl::LogSeverityAtLeast>(severity));
94 }
95 
96 // ScopedStderrThreshold
97 //
98 // RAII type used to temporarily update the Stderr Threshold parameter.
99 class ScopedStderrThreshold final {
100  public:
101   explicit ScopedStderrThreshold(absl::LogSeverityAtLeast severity);
102   ScopedStderrThreshold(const ScopedStderrThreshold&) = delete;
103   ScopedStderrThreshold& operator=(const ScopedStderrThreshold&) = delete;
104   ~ScopedStderrThreshold();
105 
106  private:
107   absl::LogSeverityAtLeast saved_severity_;
108 };
109 
110 //------------------------------------------------------------------------------
111 // Log Backtrace At
112 //------------------------------------------------------------------------------
113 //
114 // Users can request an existing `LOG` statement, specified by file and line
115 // number, to also include a backtrace when logged.
116 
117 // ShouldLogBacktraceAt()
118 //
119 // Returns true if we should log a backtrace at the specified location.
120 namespace log_internal {
121 ABSL_MUST_USE_RESULT bool ShouldLogBacktraceAt(absl::string_view file,
122                                                int line);
123 }  // namespace log_internal
124 
125 // SetLogBacktraceLocation()
126 //
127 // Sets the location the backtrace should be logged at.  If the specified
128 // location isn't a `LOG` statement, the effect will be the same as
129 // `ClearLogBacktraceLocation` (but less efficient).
130 void SetLogBacktraceLocation(absl::string_view file, int line);
131 
132 // ClearLogBacktraceLocation()
133 //
134 // Clears the set location so that backtraces will no longer be logged at it.
135 void ClearLogBacktraceLocation();
136 
137 //------------------------------------------------------------------------------
138 // Prepend Log Prefix
139 //------------------------------------------------------------------------------
140 //
141 // This option tells the logging library that every logged message
142 // should include the prefix (severity, date, time, PID, etc.)
143 
144 // ShouldPrependLogPrefix()
145 //
146 // Returns the value of the Prepend Log Prefix option.
147 // This function is async-signal-safe.
148 ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
149 
150 // EnableLogPrefix()
151 //
152 // Updates the value of the Prepend Log Prefix option.
153 // This function is async-signal-safe.
154 void EnableLogPrefix(bool on_off);
155 
156 //------------------------------------------------------------------------------
157 // Set Global VLOG Level
158 //------------------------------------------------------------------------------
159 //
160 // Sets the global `(ABSL_)VLOG(_IS_ON)` level to `log_level`.  This level is
161 // applied to any sites whose filename doesn't match any `module_pattern`.
162 // Returns the prior value.
SetGlobalVLogLevel(int log_level)163 inline int SetGlobalVLogLevel(int log_level) {
164   return absl::log_internal::UpdateGlobalVLogLevel(log_level);
165 }
166 
167 //------------------------------------------------------------------------------
168 // Set VLOG Level
169 //------------------------------------------------------------------------------
170 //
171 // Sets `(ABSL_)VLOG(_IS_ON)` level for `module_pattern` to `log_level`.  This
172 // allows programmatic control of what is normally set by the --vmodule flag.
173 // Returns the level that previously applied to `module_pattern`.
SetVLogLevel(absl::string_view module_pattern,int log_level)174 inline int SetVLogLevel(absl::string_view module_pattern, int log_level) {
175   return absl::log_internal::PrependVModule(module_pattern, log_level);
176 }
177 
178 //------------------------------------------------------------------------------
179 // Configure Android Native Log Tag
180 //------------------------------------------------------------------------------
181 //
182 // The logging library forwards to the Android system log API when built for
183 // Android.  That API takes a string "tag" value in addition to a message and
184 // severity level.  The tag is used to identify the source of messages and to
185 // filter them.  This library uses the tag "native" by default.
186 
187 // SetAndroidNativeTag()
188 //
189 // Stores a copy of the string pointed to by `tag` and uses it as the Android
190 // logging tag thereafter. `tag` must not be null.
191 // This function must not be called more than once!
192 void SetAndroidNativeTag(const char* tag);
193 
194 namespace log_internal {
195 // GetAndroidNativeTag()
196 //
197 // Returns the configured Android logging tag.
198 const char* GetAndroidNativeTag();
199 }  // namespace log_internal
200 
201 namespace log_internal {
202 
203 using LoggingGlobalsListener = void (*)();
204 void SetLoggingGlobalsListener(LoggingGlobalsListener l);
205 
206 // Internal implementation for the setter routines. These are used
207 // to break circular dependencies between flags and globals. Each "Raw"
208 // routine corresponds to the non-"Raw" counterpart and used to set the
209 // configuration parameter directly without calling back to the listener.
210 void RawSetMinLogLevel(absl::LogSeverityAtLeast severity);
211 void RawSetStderrThreshold(absl::LogSeverityAtLeast severity);
212 void RawEnableLogPrefix(bool on_off);
213 
214 }  // namespace log_internal
215 ABSL_NAMESPACE_END
216 }  // namespace absl
217 
218 #endif  // ABSL_LOG_GLOBALS_H_
219