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/internal/conditions.h
17 // -----------------------------------------------------------------------------
18 //
19 // This file contains implementation of conditional log statements, like LOG_IF
20 // including all the ABSL_LOG_INTERNAL_..._CONDITION_... macros and
21 // various condition classes like LogEveryNState.
22
23 #ifndef ABSL_LOG_INTERNAL_CONDITIONS_H_
24 #define ABSL_LOG_INTERNAL_CONDITIONS_H_
25
26 #ifdef _WIN32
27 #include <cstdlib>
28 #else
29 #include <unistd.h>
30 #endif
31 #include <stdlib.h>
32
33 #include <atomic>
34 #include <cstdint>
35
36 #include "absl/base/attributes.h"
37 #include "absl/base/config.h"
38 #include "absl/log/internal/voidify.h"
39
40 // `ABSL_LOG_INTERNAL_CONDITION` prefixes another macro that expands to a
41 // temporary `LogMessage` instantiation followed by zero or more streamed
42 // expressions. This definition is tricky to read correctly. It evaluates to
43 // either
44 //
45 // (void)0;
46 //
47 // or
48 //
49 // ::absl::log_internal::Voidify() &&
50 // ::absl::log_internal::LogMessage(...) << "the user's message";
51 //
52 // If the condition is evaluable at compile time, as is often the case, it
53 // compiles away to just one side or the other.
54 //
55 // Although this is not used anywhere a statement (e.g. `if`) could not go,
56 // the ternary expression does a better job avoiding spurious diagnostics
57 // (dangling else, missing switch case) and preserving noreturn semantics (e.g.
58 // on `LOG(FATAL)`) without requiring braces.
59 #define ABSL_LOG_INTERNAL_STATELESS_CONDITION(condition) \
60 switch (0) \
61 case 0: \
62 !(condition) ? (void)0 : ::absl::log_internal::Voidify()&&
63
64 // `ABSL_LOG_INTERNAL_STATEFUL_CONDITION` applies a condition like
65 // `ABSL_LOG_INTERNAL_CONDITION` but adds to that a series of variable
66 // declarations, including a local static object which stores the state needed
67 // to implement the stateful macros like `LOG_EVERY_N`.
68 //
69 // `for`-loops are used to declare scoped variables without braces (to permit
70 // streaming into the macro's expansion) and without the dangling-`else`
71 // problems/diagnostics that come with `if`.
72 //
73 // Two more variables are declared in separate `for`-loops:
74 //
75 // * `COUNTER` implements a streamable token whose value when streamed is the
76 // number of times execution has passed through the macro.
77 // * A boolean flag is used to prevent any of the `for`-loops from ever actually
78 // looping.
79 #define ABSL_LOG_INTERNAL_STATEFUL_CONDITION(condition) \
80 for (bool absl_log_internal_stateful_condition_do_log(condition); \
81 absl_log_internal_stateful_condition_do_log; \
82 absl_log_internal_stateful_condition_do_log = false) \
83 ABSL_LOG_INTERNAL_STATEFUL_CONDITION_IMPL
84 #define ABSL_LOG_INTERNAL_STATEFUL_CONDITION_IMPL(kind, ...) \
85 for (static ::absl::log_internal::Log##kind##State \
86 absl_log_internal_stateful_condition_state; \
87 absl_log_internal_stateful_condition_do_log && \
88 absl_log_internal_stateful_condition_state.ShouldLog(__VA_ARGS__); \
89 absl_log_internal_stateful_condition_do_log = false) \
90 for (const uint32_t COUNTER ABSL_ATTRIBUTE_UNUSED = \
91 absl_log_internal_stateful_condition_state.counter(); \
92 absl_log_internal_stateful_condition_do_log; \
93 absl_log_internal_stateful_condition_do_log = false)
94
95 // `ABSL_LOG_INTERNAL_CONDITION_*` serve to combine any conditions from the
96 // macro (e.g. `LOG_IF` or `VLOG`) with inherent conditions (e.g.
97 // `ABSL_MIN_LOG_LEVEL`) into a single boolean expression. We could chain
98 // ternary operators instead, however some versions of Clang sometimes issue
99 // spurious diagnostics after such expressions due to a control flow analysis
100 // bug.
101 #ifdef ABSL_MIN_LOG_LEVEL
102 #define ABSL_LOG_INTERNAL_CONDITION_INFO(type, condition) \
103 ABSL_LOG_INTERNAL_##type##_CONDITION( \
104 (condition) && ::absl::LogSeverity::kInfo >= \
105 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL))
106 #define ABSL_LOG_INTERNAL_CONDITION_WARNING(type, condition) \
107 ABSL_LOG_INTERNAL_##type##_CONDITION( \
108 (condition) && ::absl::LogSeverity::kWarning >= \
109 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL))
110 #define ABSL_LOG_INTERNAL_CONDITION_ERROR(type, condition) \
111 ABSL_LOG_INTERNAL_##type##_CONDITION( \
112 (condition) && ::absl::LogSeverity::kError >= \
113 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL))
114 // NOTE: Use ternary operators instead of short-circuiting to mitigate
115 // https://bugs.llvm.org/show_bug.cgi?id=51928.
116 #define ABSL_LOG_INTERNAL_CONDITION_FATAL(type, condition) \
117 ABSL_LOG_INTERNAL_##type##_CONDITION( \
118 ((condition) \
119 ? (::absl::LogSeverity::kFatal >= \
120 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) \
121 ? true \
122 : (::absl::log_internal::AbortQuietly(), false)) \
123 : false))
124 // NOTE: Use ternary operators instead of short-circuiting to mitigate
125 // https://bugs.llvm.org/show_bug.cgi?id=51928.
126 #define ABSL_LOG_INTERNAL_CONDITION_QFATAL(type, condition) \
127 ABSL_LOG_INTERNAL_##type##_CONDITION( \
128 ((condition) \
129 ? (::absl::LogSeverity::kFatal >= \
130 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) \
131 ? true \
132 : (::absl::log_internal::ExitQuietly(), false)) \
133 : false))
134
135 #define ABSL_LOG_INTERNAL_CONDITION_LEVEL(severity) \
136 for (int log_internal_severity_loop = 1; log_internal_severity_loop; \
137 log_internal_severity_loop = 0) \
138 for (const absl::LogSeverity log_internal_severity = \
139 ::absl::NormalizeLogSeverity(severity); \
140 log_internal_severity_loop; log_internal_severity_loop = 0) \
141 ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL
142 #define ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL(type, condition) \
143 ABSL_LOG_INTERNAL_##type##_CONDITION( \
144 (condition) && \
145 (log_internal_severity >= \
146 static_cast<::absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) || \
147 (log_internal_severity == ::absl::LogSeverity::kFatal && \
148 (::absl::log_internal::AbortQuietly(), false))))
149 #else // ndef ABSL_MIN_LOG_LEVEL
150 #define ABSL_LOG_INTERNAL_CONDITION_INFO(type, condition) \
151 ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
152 #define ABSL_LOG_INTERNAL_CONDITION_WARNING(type, condition) \
153 ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
154 #define ABSL_LOG_INTERNAL_CONDITION_ERROR(type, condition) \
155 ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
156 #define ABSL_LOG_INTERNAL_CONDITION_FATAL(type, condition) \
157 ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
158 #define ABSL_LOG_INTERNAL_CONDITION_QFATAL(type, condition) \
159 ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
160 #define ABSL_LOG_INTERNAL_CONDITION_LEVEL(severity) \
161 for (int log_internal_severity_loop = 1; log_internal_severity_loop; \
162 log_internal_severity_loop = 0) \
163 for (const absl::LogSeverity log_internal_severity = \
164 ::absl::NormalizeLogSeverity(severity); \
165 log_internal_severity_loop; log_internal_severity_loop = 0) \
166 ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL
167 #define ABSL_LOG_INTERNAL_CONDITION_LEVEL_IMPL(type, condition) \
168 ABSL_LOG_INTERNAL_##type##_CONDITION(condition)
169 #endif // ndef ABSL_MIN_LOG_LEVEL
170
171 namespace absl {
172 ABSL_NAMESPACE_BEGIN
173 namespace log_internal {
174
175 // Stateful condition class name should be "Log" + name + "State".
176 class LogEveryNState final {
177 public:
178 bool ShouldLog(int n);
counter()179 uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
180
181 private:
182 std::atomic<uint32_t> counter_{0};
183 };
184
185 class LogFirstNState final {
186 public:
187 bool ShouldLog(int n);
counter()188 uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
189
190 private:
191 std::atomic<uint32_t> counter_{0};
192 };
193
194 class LogEveryPow2State final {
195 public:
196 bool ShouldLog();
counter()197 uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
198
199 private:
200 std::atomic<uint32_t> counter_{0};
201 };
202
203 class LogEveryNSecState final {
204 public:
205 bool ShouldLog(double seconds);
counter()206 uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
207
208 private:
209 std::atomic<uint32_t> counter_{0};
210 // Cycle count according to CycleClock that we should next log at.
211 std::atomic<int64_t> next_log_time_cycles_{0};
212 };
213
214 // Helper routines to abort the application quietly
215
AbortQuietly()216 ABSL_ATTRIBUTE_NORETURN inline void AbortQuietly() { abort(); }
ExitQuietly()217 ABSL_ATTRIBUTE_NORETURN inline void ExitQuietly() { _exit(1); }
218 } // namespace log_internal
219 ABSL_NAMESPACE_END
220 } // namespace absl
221
222 #endif // ABSL_LOG_INTERNAL_CONDITIONS_H_
223