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