xref: /aosp_15_r20/external/abseil-cpp/absl/log/internal/log_impl.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 #ifndef ABSL_LOG_INTERNAL_LOG_IMPL_H_
16 #define ABSL_LOG_INTERNAL_LOG_IMPL_H_
17 
18 #include "absl/log/absl_vlog_is_on.h"
19 #include "absl/log/internal/conditions.h"
20 #include "absl/log/internal/log_message.h"
21 #include "absl/log/internal/strip.h"
22 
23 // ABSL_LOG()
24 #define ABSL_LOG_INTERNAL_LOG_IMPL(severity)             \
25   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \
26       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
27 
28 // ABSL_PLOG()
29 #define ABSL_LOG_INTERNAL_PLOG_IMPL(severity)              \
30   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true)   \
31       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \
32           .WithPerror()
33 
34 // ABSL_DLOG()
35 #ifndef NDEBUG
36 #define ABSL_LOG_INTERNAL_DLOG_IMPL(severity)            \
37   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \
38       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
39 #else
40 #define ABSL_LOG_INTERNAL_DLOG_IMPL(severity)             \
41   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, false) \
42       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
43 #endif
44 
45 // The `switch` ensures that this expansion is the beginning of a statement (as
46 // opposed to an expression). The use of both `case 0` and `default` is to
47 // suppress a compiler warning.
48 #define ABSL_LOG_INTERNAL_VLOG_IMPL(verbose_level)                         \
49   switch (const int absl_logging_internal_verbose_level = (verbose_level)) \
50   case 0:                                                                  \
51   default:                                                                 \
52     ABSL_LOG_INTERNAL_LOG_IF_IMPL(                                         \
53         _INFO, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level))       \
54         .WithVerbosity(absl_logging_internal_verbose_level)
55 
56 #ifndef NDEBUG
57 #define ABSL_LOG_INTERNAL_DVLOG_IMPL(verbose_level)                        \
58   switch (const int absl_logging_internal_verbose_level = (verbose_level)) \
59   case 0:                                                                  \
60   default:                                                                 \
61     ABSL_LOG_INTERNAL_DLOG_IF_IMPL(                                         \
62         _INFO, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level))       \
63         .WithVerbosity(absl_logging_internal_verbose_level)
64 #else
65 #define ABSL_LOG_INTERNAL_DVLOG_IMPL(verbose_level)                           \
66   switch (const int absl_logging_internal_verbose_level = (verbose_level))    \
67   case 0:                                                                     \
68   default:                                                                    \
69     ABSL_LOG_INTERNAL_DLOG_IF_IMPL(                                            \
70         _INFO, false && ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
71         .WithVerbosity(absl_logging_internal_verbose_level)
72 #endif
73 
74 #define ABSL_LOG_INTERNAL_LOG_IF_IMPL(severity, condition)    \
75   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, condition) \
76       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
77 #define ABSL_LOG_INTERNAL_PLOG_IF_IMPL(severity, condition)   \
78   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, condition) \
79       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()    \
80           .WithPerror()
81 
82 #ifndef NDEBUG
83 #define ABSL_LOG_INTERNAL_DLOG_IF_IMPL(severity, condition)   \
84   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, condition) \
85       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
86 #else
87 #define ABSL_LOG_INTERNAL_DLOG_IF_IMPL(severity, condition)              \
88   ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, false && (condition)) \
89       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
90 #endif
91 
92 // ABSL_LOG_EVERY_N
93 #define ABSL_LOG_INTERNAL_LOG_EVERY_N_IMPL(severity, n)            \
94   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryN, n) \
95       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
96 
97 // ABSL_LOG_FIRST_N
98 #define ABSL_LOG_INTERNAL_LOG_FIRST_N_IMPL(severity, n)            \
99   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(FirstN, n) \
100       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
101 
102 // ABSL_LOG_EVERY_POW_2
103 #define ABSL_LOG_INTERNAL_LOG_EVERY_POW_2_IMPL(severity)           \
104   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryPow2) \
105       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
106 
107 // ABSL_LOG_EVERY_N_SEC
108 #define ABSL_LOG_INTERNAL_LOG_EVERY_N_SEC_IMPL(severity, n_seconds)           \
109   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryNSec, n_seconds) \
110       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
111 
112 #define ABSL_LOG_INTERNAL_PLOG_EVERY_N_IMPL(severity, n)           \
113   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryN, n) \
114       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()         \
115           .WithPerror()
116 
117 #define ABSL_LOG_INTERNAL_PLOG_FIRST_N_IMPL(severity, n)           \
118   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(FirstN, n) \
119       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()         \
120           .WithPerror()
121 
122 #define ABSL_LOG_INTERNAL_PLOG_EVERY_POW_2_IMPL(severity)          \
123   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryPow2) \
124       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()         \
125           .WithPerror()
126 
127 #define ABSL_LOG_INTERNAL_PLOG_EVERY_N_SEC_IMPL(severity, n_seconds)          \
128   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryNSec, n_seconds) \
129       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()                    \
130           .WithPerror()
131 
132 #ifndef NDEBUG
133 #define ABSL_LOG_INTERNAL_DLOG_EVERY_N_IMPL(severity, n) \
134   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true)       \
135   (EveryN, n) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
136 
137 #define ABSL_LOG_INTERNAL_DLOG_FIRST_N_IMPL(severity, n) \
138   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true)       \
139   (FirstN, n) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
140 
141 #define ABSL_LOG_INTERNAL_DLOG_EVERY_POW_2_IMPL(severity) \
142   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true)        \
143   (EveryPow2) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
144 
145 #define ABSL_LOG_INTERNAL_DLOG_EVERY_N_SEC_IMPL(severity, n_seconds) \
146   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true)                   \
147   (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
148 
149 #else  // def NDEBUG
150 #define ABSL_LOG_INTERNAL_DLOG_EVERY_N_IMPL(severity, n) \
151   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false)      \
152   (EveryN, n) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
153 
154 #define ABSL_LOG_INTERNAL_DLOG_FIRST_N_IMPL(severity, n) \
155   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false)      \
156   (FirstN, n) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
157 
158 #define ABSL_LOG_INTERNAL_DLOG_EVERY_POW_2_IMPL(severity) \
159   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false)       \
160   (EveryPow2) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
161 
162 #define ABSL_LOG_INTERNAL_DLOG_EVERY_N_SEC_IMPL(severity, n_seconds) \
163   ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false)                  \
164   (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
165 #endif  // def NDEBUG
166 
167 #define ABSL_LOG_INTERNAL_VLOG_EVERY_N_IMPL(verbose_level, n)                \
168   switch (const int absl_logging_internal_verbose_level = (verbose_level))   \
169   case 0:                                                                    \
170   default:                                                                   \
171     ABSL_LOG_INTERNAL_CONDITION_INFO(                                        \
172         STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level))      \
173   (EveryN, n) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream().WithVerbosity( \
174       absl_logging_internal_verbose_level)
175 
176 #define ABSL_LOG_INTERNAL_VLOG_FIRST_N_IMPL(verbose_level, n)                \
177   switch (const int absl_logging_internal_verbose_level = (verbose_level))   \
178   case 0:                                                                    \
179   default:                                                                   \
180     ABSL_LOG_INTERNAL_CONDITION_INFO(                                        \
181         STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level))      \
182   (FirstN, n) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream().WithVerbosity( \
183       absl_logging_internal_verbose_level)
184 
185 #define ABSL_LOG_INTERNAL_VLOG_EVERY_POW_2_IMPL(verbose_level)               \
186   switch (const int absl_logging_internal_verbose_level = (verbose_level))   \
187   case 0:                                                                    \
188   default:                                                                   \
189     ABSL_LOG_INTERNAL_CONDITION_INFO(                                        \
190         STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level))      \
191   (EveryPow2) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream().WithVerbosity( \
192       absl_logging_internal_verbose_level)
193 
194 #define ABSL_LOG_INTERNAL_VLOG_EVERY_N_SEC_IMPL(verbose_level, n_seconds)  \
195   switch (const int absl_logging_internal_verbose_level = (verbose_level)) \
196   case 0:                                                                  \
197   default:                                                                 \
198     ABSL_LOG_INTERNAL_CONDITION_INFO(                                      \
199         STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level))    \
200   (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream()   \
201       .WithVerbosity(absl_logging_internal_verbose_level)
202 
203 #define ABSL_LOG_INTERNAL_LOG_IF_EVERY_N_IMPL(severity, condition, n)   \
204   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryN, n) \
205       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
206 
207 #define ABSL_LOG_INTERNAL_LOG_IF_FIRST_N_IMPL(severity, condition, n)   \
208   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(FirstN, n) \
209       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
210 
211 #define ABSL_LOG_INTERNAL_LOG_IF_EVERY_POW_2_IMPL(severity, condition)  \
212   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryPow2) \
213       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
214 
215 #define ABSL_LOG_INTERNAL_LOG_IF_EVERY_N_SEC_IMPL(severity, condition,  \
216                                                   n_seconds)            \
217   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryNSec, \
218                                                              n_seconds) \
219       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()
220 
221 #define ABSL_LOG_INTERNAL_PLOG_IF_EVERY_N_IMPL(severity, condition, n)  \
222   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryN, n) \
223       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()              \
224           .WithPerror()
225 
226 #define ABSL_LOG_INTERNAL_PLOG_IF_FIRST_N_IMPL(severity, condition, n)  \
227   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(FirstN, n) \
228       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()              \
229           .WithPerror()
230 
231 #define ABSL_LOG_INTERNAL_PLOG_IF_EVERY_POW_2_IMPL(severity, condition) \
232   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryPow2) \
233       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()              \
234           .WithPerror()
235 
236 #define ABSL_LOG_INTERNAL_PLOG_IF_EVERY_N_SEC_IMPL(severity, condition, \
237                                                    n_seconds)           \
238   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryNSec, \
239                                                              n_seconds) \
240       ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream()              \
241           .WithPerror()
242 
243 #ifndef NDEBUG
244 #define ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_IMPL(severity, condition, n)  \
245   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryN, n) \
246       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
247 
248 #define ABSL_LOG_INTERNAL_DLOG_IF_FIRST_N_IMPL(severity, condition, n)  \
249   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(FirstN, n) \
250       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
251 
252 #define ABSL_LOG_INTERNAL_DLOG_IF_EVERY_POW_2_IMPL(severity, condition) \
253   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryPow2) \
254       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
255 
256 #define ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_SEC_IMPL(severity, condition, \
257                                                    n_seconds)           \
258   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryNSec, \
259                                                              n_seconds) \
260       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
261 
262 #else  // def NDEBUG
263 #define ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_IMPL(severity, condition, n)   \
264   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \
265       EveryN, n) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
266 
267 #define ABSL_LOG_INTERNAL_DLOG_IF_FIRST_N_IMPL(severity, condition, n)   \
268   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \
269       FirstN, n) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
270 
271 #define ABSL_LOG_INTERNAL_DLOG_IF_EVERY_POW_2_IMPL(severity, condition)  \
272   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \
273       EveryPow2) ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
274 
275 #define ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_SEC_IMPL(severity, condition,  \
276                                                    n_seconds)            \
277   ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \
278       EveryNSec, n_seconds)                                              \
279       ABSL_LOGGING_INTERNAL_DLOG##severity.InternalStream()
280 #endif  // def NDEBUG
281 
282 #endif  // ABSL_LOG_INTERNAL_LOG_IMPL_H_
283