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/strings/string_view.h"
28
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31
32 //------------------------------------------------------------------------------
33 // Minimum Log Level
34 //------------------------------------------------------------------------------
35 //
36 // Messages logged at or above this severity are directed to all registered log
37 // sinks or skipped otherwise. This parameter can also be modified using
38 // command line flag --minloglevel.
39 // See absl/base/log_severity.h for descriptions of severity levels.
40
41 // MinLogLevel()
42 //
43 // Returns the value of the Minimum Log Level parameter.
44 // This function is async-signal-safe.
45 ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast MinLogLevel();
46
47 // SetMinLogLevel()
48 //
49 // Updates the value of Minimum Log Level parameter.
50 // This function is async-signal-safe.
51 void SetMinLogLevel(absl::LogSeverityAtLeast severity);
52
53 namespace log_internal {
54
55 // ScopedMinLogLevel
56 //
57 // RAII type used to temporarily update the Min Log Level parameter.
58 class ScopedMinLogLevel final {
59 public:
60 explicit ScopedMinLogLevel(absl::LogSeverityAtLeast severity);
61 ScopedMinLogLevel(const ScopedMinLogLevel&) = delete;
62 ScopedMinLogLevel& operator=(const ScopedMinLogLevel&) = delete;
63 ~ScopedMinLogLevel();
64
65 private:
66 absl::LogSeverityAtLeast saved_severity_;
67 };
68
69 } // namespace log_internal
70
71 //------------------------------------------------------------------------------
72 // Stderr Threshold
73 //------------------------------------------------------------------------------
74 //
75 // Messages logged at or above this level are directed to stderr in
76 // addition to other registered log sinks. This parameter can also be modified
77 // using command line flag --stderrthreshold.
78 // See absl/base/log_severity.h for descriptions of severity levels.
79
80 // StderrThreshold()
81 //
82 // Returns the value of the Stderr Threshold parameter.
83 // This function is async-signal-safe.
84 ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast StderrThreshold();
85
86 // SetStderrThreshold()
87 //
88 // Updates the Stderr Threshold parameter.
89 // This function is async-signal-safe.
90 void SetStderrThreshold(absl::LogSeverityAtLeast severity);
SetStderrThreshold(absl::LogSeverity severity)91 inline void SetStderrThreshold(absl::LogSeverity severity) {
92 absl::SetStderrThreshold(static_cast<absl::LogSeverityAtLeast>(severity));
93 }
94
95 // ScopedStderrThreshold
96 //
97 // RAII type used to temporarily update the Stderr Threshold parameter.
98 class ScopedStderrThreshold final {
99 public:
100 explicit ScopedStderrThreshold(absl::LogSeverityAtLeast severity);
101 ScopedStderrThreshold(const ScopedStderrThreshold&) = delete;
102 ScopedStderrThreshold& operator=(const ScopedStderrThreshold&) = delete;
103 ~ScopedStderrThreshold();
104
105 private:
106 absl::LogSeverityAtLeast saved_severity_;
107 };
108
109 //------------------------------------------------------------------------------
110 // Log Backtrace At
111 //------------------------------------------------------------------------------
112 //
113 // Users can request backtrace to be logged at specific locations, specified
114 // by file and line number.
115
116 // ShouldLogBacktraceAt()
117 //
118 // Returns true if we should log a backtrace at the specified location.
119 namespace log_internal {
120 ABSL_MUST_USE_RESULT bool ShouldLogBacktraceAt(absl::string_view file,
121 int line);
122 } // namespace log_internal
123
124 // SetLogBacktraceLocation()
125 //
126 // Sets the location the backtrace should be logged at.
127 void SetLogBacktraceLocation(absl::string_view file, int line);
128
129 //------------------------------------------------------------------------------
130 // Prepend Log Prefix
131 //------------------------------------------------------------------------------
132 //
133 // This option tells the logging library that every logged message
134 // should include the prefix (severity, date, time, PID, etc.)
135
136 // ShouldPrependLogPrefix()
137 //
138 // Returns the value of the Prepend Log Prefix option.
139 // This function is async-signal-safe.
140 ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
141
142 // EnableLogPrefix()
143 //
144 // Updates the value of the Prepend Log Prefix option.
145 // This function is async-signal-safe.
146 void EnableLogPrefix(bool on_off);
147
148 namespace log_internal {
149
150 using LoggingGlobalsListener = void (*)();
151 void SetLoggingGlobalsListener(LoggingGlobalsListener l);
152
153 // Internal implementation for the setter routines. These are used
154 // to break circular dependencies between flags and globals. Each "Raw"
155 // routine corresponds to the non-"Raw" counterpart and used to set the
156 // configuration parameter directly without calling back to the listener.
157 void RawSetMinLogLevel(absl::LogSeverityAtLeast severity);
158 void RawSetStderrThreshold(absl::LogSeverityAtLeast severity);
159 void RawEnableLogPrefix(bool on_off);
160
161 } // namespace log_internal
162 ABSL_NAMESPACE_END
163 } // namespace absl
164
165 #endif // ABSL_LOG_GLOBALS_H_
166