xref: /aosp_15_r20/external/private-join-and-compute/third_party/abseil-cpp-20230125.2/absl/log/log.h (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
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/log.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header declares a family of LOG macros.
20 //
21 // Basic invocation looks like this:
22 //
23 //   LOG(INFO) << "Found " << num_cookies << " cookies";
24 //
25 // Most `LOG` macros take a severity level argument.  The severity levels are
26 // `INFO`, `WARNING`, `ERROR`, and `FATAL`.  They are defined
27 // in absl/base/log_severity.h.
28 // * The `FATAL` severity level terminates the program with a stack trace after
29 //   logging its message.  Error handlers registered with `RunOnFailure`
30 //   (process_state.h) are run, but exit handlers registered with `atexit(3)`
31 //   are not.
32 // * The `QFATAL` pseudo-severity level is equivalent to `FATAL` but triggers
33 //   quieter termination messages, e.g. without a full stack trace, and skips
34 //   running registered error handlers.
35 // Some preprocessor shenanigans are used to ensure that e.g. `LOG(INFO)` has
36 // the same meaning even if a local symbol or preprocessor macro named `INFO` is
37 // defined.  To specify a severity level using an expression instead of a
38 // literal, use `LEVEL(expr)`.
39 // Example:
40 //
41 //   LOG(LEVEL(stale ? absl::LogSeverity::kWarning : absl::LogSeverity::kInfo))
42 //       << "Cookies are " << days << " days old";
43 
44 // `LOG` macros evaluate to an unterminated statement.  The value at the end of
45 // the statement supports some chainable methods:
46 //
47 //   * .AtLocation(absl::string_view file, int line)
48 //     .AtLocation(absl::SourceLocation loc)
49 //     Overrides the location inferred from the callsite.  The string pointed to
50 //     by `file` must be valid until the end of the statement.
51 //   * .NoPrefix()
52 //     Omits the prefix from this line.  The prefix includes metadata about the
53 //     logged data such as source code location and timestamp.
54 //   * .WithTimestamp(absl::Time timestamp)
55 //     Uses the specified timestamp instead of one collected at the time of
56 //     execution.
57 //   * .WithThreadID(absl::LogEntry::tid_t tid)
58 //     Uses the specified thread ID instead of one collected at the time of
59 //     execution.
60 //   * .WithMetadataFrom(const absl::LogEntry &entry)
61 //     Copies all metadata (but no data) from the specified `absl::LogEntry`.
62 //     This can be used to change the severity of a message, but it has some
63 //     limitations:
64 //     * `ABSL_MIN_LOG_LEVEL` is evaluated against the severity passed into
65 //       `LOG` (or the implicit `FATAL` level of `CHECK`).
66 //     * `LOG(FATAL)` and `CHECK` terminate the process unconditionally, even if
67 //       the severity is changed later.
68 //     `.WithMetadataFrom(entry)` should almost always be used in combination
69 //     with `LOG(LEVEL(entry.log_severity()))`.
70 //   * .WithPerror()
71 //     Appends to the logged message a colon, a space, a textual description of
72 //     the current value of `errno` (as by `strerror(3)`), and the numerical
73 //     value of `errno`.
74 //   * .ToSinkAlso(absl::LogSink* sink)
75 //     Sends this message to `*sink` in addition to whatever other sinks it
76 //     would otherwise have been sent to.  `sink` must not be null.
77 //   * .ToSinkOnly(absl::LogSink* sink)
78 //     Sends this message to `*sink` and no others.  `sink` must not be null.
79 //
80 // No interfaces in this header are async-signal-safe; their use in signal
81 // handlers is unsupported and may deadlock your program or eat your lunch.
82 //
83 // Many logging statements are inherently conditional.  For example,
84 // `LOG_IF(INFO, !foo)` does nothing if `foo` is true.  Even seemingly
85 // unconditional statements like `LOG(INFO)` might be disabled at
86 // compile-time to minimize binary size or for security reasons.
87 //
88 // * Except for the condition in a `CHECK` or `QCHECK` statement, programs must
89 //   not rely on evaluation of expressions anywhere in logging statements for
90 //   correctness.  For example, this is ok:
91 //
92 //     CHECK((fp = fopen("config.ini", "r")) != nullptr);
93 //
94 //   But this is probably not ok:
95 //
96 //     LOG(INFO) << "Server status: " << StartServerAndReturnStatusString();
97 //
98 //   The example below is bad too; the `i++` in the `LOG_IF` condition might
99 //   not be evaluated, resulting in an infinite loop:
100 //
101 //     for (int i = 0; i < 1000000;)
102 //       LOG_IF(INFO, i++ % 1000 == 0) << "Still working...";
103 //
104 // * Except where otherwise noted, conditions which cause a statement not to log
105 //   also cause expressions not to be evaluated.  Programs may rely on this for
106 //   performance reasons, e.g. by streaming the result of an expensive function
107 //   call into a `DLOG` or `LOG_EVERY_N` statement.
108 // * Care has been taken to ensure that expressions are parsed by the compiler
109 //   even if they are never evaluated.  This means that syntax errors will be
110 //   caught and variables will be considered used for the purposes of
111 //   unused-variable diagnostics.  For example, this statement won't compile
112 //   even if `INFO`-level logging has been compiled out:
113 //
114 //     int number_of_cakes = 40;
115 //     LOG(INFO) << "Number of cakes: " << number_of_cake;  // Note the typo!
116 //
117 //   Similarly, this won't produce unused-variable compiler diagnostics even
118 //   if `INFO`-level logging is compiled out:
119 //
120 //     {
121 //       char fox_line1[] = "Hatee-hatee-hatee-ho!";
122 //       LOG_IF(ERROR, false) << "The fox says " << fox_line1;
123 //       char fox_line2[] = "A-oo-oo-oo-ooo!";
124 //       LOG(INFO) << "The fox also says " << fox_line2;
125 //     }
126 //
127 //   This error-checking is not perfect; for example, symbols that have been
128 //   declared but not defined may not produce link errors if used in logging
129 //   statements that compile away.
130 //
131 // Expressions streamed into these macros are formatted using `operator<<` just
132 // as they would be if streamed into a `std::ostream`, however it should be
133 // noted that their actual type is unspecified.
134 //
135 // To implement a custom formatting operator for a type you own, there are two
136 // options: `AbslStringify()` or `std::ostream& operator<<(std::ostream&, ...)`.
137 // It is recommended that users make their types loggable through
138 // `AbslStringify()` as it is a universal stringification extension that also
139 // enables `absl::StrFormat` and `absl::StrCat` support. If both
140 // `AbslStringify()` and `std::ostream& operator<<(std::ostream&, ...)` are
141 // defined, `AbslStringify()` will be used.
142 //
143 // To use the `AbslStringify()` API, define a friend function template in your
144 // type's namespace with the following signature:
145 //
146 //   template <typename Sink>
147 //   void AbslStringify(Sink& sink, const UserDefinedType& value);
148 //
149 // `Sink` has the same interface as `absl::FormatSink`, but without
150 // `PutPaddedString()`.
151 //
152 // Example:
153 //
154 //   struct Point {
155 //     template <typename Sink>
156 //     friend void AbslStringify(Sink& sink, const Point& p) {
157 //       absl::Format(&sink, "(%v, %v)", p.x, p.y);
158 //     }
159 //
160 //     int x;
161 //     int y;
162 //   };
163 //
164 // To use `std::ostream& operator<<(std::ostream&, ...)`, define
165 // `std::ostream& operator<<(std::ostream&, ...)` in your type's namespace (for
166 // ADL) just as you would to stream it to `std::cout`.
167 //
168 // Currently `AbslStringify()` ignores output manipulators but this is not
169 // guaranteed behavior and may be subject to change in the future. If you would
170 // like guaranteed behavior regarding output manipulators, please use
171 // `std::ostream& operator<<(std::ostream&, ...)` to make custom types loggable
172 // instead.
173 //
174 // Those macros that support streaming honor output manipulators and `fmtflag`
175 // changes that output data (e.g. `std::ends`) or control formatting of data
176 // (e.g. `std::hex` and `std::fixed`), however flushing such a stream is
177 // ignored.  The message produced by a log statement is sent to registered
178 // `absl::LogSink` instances at the end of the statement; those sinks are
179 // responsible for their own flushing (e.g. to disk) semantics.
180 //
181 // Flag settings are not carried over from one `LOG` statement to the next; this
182 // is a bit different than e.g. `std::cout`:
183 //
184 //   LOG(INFO) << std::hex << 0xdeadbeef;  // logs "0xdeadbeef"
185 //   LOG(INFO) << 0xdeadbeef;              // logs "3735928559"
186 
187 #ifndef ABSL_LOG_LOG_H_
188 #define ABSL_LOG_LOG_H_
189 
190 #include "absl/log/internal/log_impl.h"
191 
192 // LOG()
193 //
194 // `LOG` takes a single argument which is a severity level.  Data streamed in
195 // comprise the logged message.
196 // Example:
197 //
198 //   LOG(INFO) << "Found " << num_cookies << " cookies";
199 #define LOG(severity) ABSL_LOG_IMPL(_##severity)
200 
201 // PLOG()
202 //
203 // `PLOG` behaves like `LOG` except that a description of the current state of
204 // `errno` is appended to the streamed message.
205 #define PLOG(severity) ABSL_PLOG_IMPL(_##severity)
206 
207 // DLOG()
208 //
209 // `DLOG` behaves like `LOG` in debug mode (i.e. `#ifndef NDEBUG`).  Otherwise
210 // it compiles away and does nothing.  Note that `DLOG(FATAL)` does not
211 // terminate the program if `NDEBUG` is defined.
212 #define DLOG(severity) ABSL_DLOG_IMPL(_##severity)
213 
214 // `LOG_IF` and friends add a second argument which specifies a condition.  If
215 // the condition is false, nothing is logged.
216 // Example:
217 //
218 //   LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
219 #define LOG_IF(severity, condition) ABSL_LOG_IF_IMPL(_##severity, condition)
220 #define PLOG_IF(severity, condition) ABSL_PLOG_IF_IMPL(_##severity, condition)
221 #define DLOG_IF(severity, condition) ABSL_DLOG_IF_IMPL(_##severity, condition)
222 
223 // LOG_EVERY_N
224 //
225 // An instance of `LOG_EVERY_N` increments a hidden zero-initialized counter
226 // every time execution passes through it and logs the specified message when
227 // the counter's value is a multiple of `n`, doing nothing otherwise.  Each
228 // instance has its own counter.  The counter's value can be logged by streaming
229 // the symbol `COUNTER`.  `LOG_EVERY_N` is thread-safe.
230 // Example:
231 //
232 //   LOG_EVERY_N(WARNING, 1000) << "Got a packet with a bad CRC (" << COUNTER
233 //                              << " total)";
234 #define LOG_EVERY_N(severity, n) ABSL_LOG_EVERY_N_IMPL(_##severity, n)
235 
236 // LOG_FIRST_N
237 //
238 // `LOG_FIRST_N` behaves like `LOG_EVERY_N` except that the specified message is
239 // logged when the counter's value is less than `n`.  `LOG_FIRST_N` is
240 // thread-safe.
241 #define LOG_FIRST_N(severity, n) ABSL_LOG_FIRST_N_IMPL(_##severity, n)
242 
243 // LOG_EVERY_POW_2
244 //
245 // `LOG_EVERY_POW_2` behaves like `LOG_EVERY_N` except that the specified
246 // message is logged when the counter's value is a power of 2.
247 // `LOG_EVERY_POW_2` is thread-safe.
248 #define LOG_EVERY_POW_2(severity) ABSL_LOG_EVERY_POW_2_IMPL(_##severity)
249 
250 // LOG_EVERY_N_SEC
251 //
252 // An instance of `LOG_EVERY_N_SEC` uses a hidden state variable to log the
253 // specified message at most once every `n_seconds`.  A hidden counter of
254 // executions (whether a message is logged or not) is also maintained and can be
255 // logged by streaming the symbol `COUNTER`.  `LOG_EVERY_N_SEC` is thread-safe.
256 // Example:
257 //
258 //   LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far";
259 #define LOG_EVERY_N_SEC(severity, n_seconds) \
260   ABSL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
261 
262 #define PLOG_EVERY_N(severity, n) ABSL_PLOG_EVERY_N_IMPL(_##severity, n)
263 #define PLOG_FIRST_N(severity, n) ABSL_PLOG_FIRST_N_IMPL(_##severity, n)
264 #define PLOG_EVERY_POW_2(severity) ABSL_PLOG_EVERY_POW_2_IMPL(_##severity)
265 #define PLOG_EVERY_N_SEC(severity, n_seconds) \
266   ABSL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
267 
268 #define DLOG_EVERY_N(severity, n) ABSL_DLOG_EVERY_N_IMPL(_##severity, n)
269 #define DLOG_FIRST_N(severity, n) ABSL_DLOG_FIRST_N_IMPL(_##severity, n)
270 #define DLOG_EVERY_POW_2(severity) ABSL_DLOG_EVERY_POW_2_IMPL(_##severity)
271 #define DLOG_EVERY_N_SEC(severity, n_seconds) \
272   ABSL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
273 
274 // `LOG_IF_EVERY_N` and friends behave as the corresponding `LOG_EVERY_N`
275 // but neither increment a counter nor log a message if condition is false (as
276 // `LOG_IF`).
277 // Example:
278 //
279 //   LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER
280 //                                           << "th big cookie";
281 #define LOG_IF_EVERY_N(severity, condition, n) \
282   ABSL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n)
283 #define LOG_IF_FIRST_N(severity, condition, n) \
284   ABSL_LOG_IF_FIRST_N_IMPL(_##severity, condition, n)
285 #define LOG_IF_EVERY_POW_2(severity, condition) \
286   ABSL_LOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
287 #define LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
288   ABSL_LOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
289 
290 #define PLOG_IF_EVERY_N(severity, condition, n) \
291   ABSL_PLOG_IF_EVERY_N_IMPL(_##severity, condition, n)
292 #define PLOG_IF_FIRST_N(severity, condition, n) \
293   ABSL_PLOG_IF_FIRST_N_IMPL(_##severity, condition, n)
294 #define PLOG_IF_EVERY_POW_2(severity, condition) \
295   ABSL_PLOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
296 #define PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
297   ABSL_PLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
298 
299 #define DLOG_IF_EVERY_N(severity, condition, n) \
300   ABSL_DLOG_IF_EVERY_N_IMPL(_##severity, condition, n)
301 #define DLOG_IF_FIRST_N(severity, condition, n) \
302   ABSL_DLOG_IF_FIRST_N_IMPL(_##severity, condition, n)
303 #define DLOG_IF_EVERY_POW_2(severity, condition) \
304   ABSL_DLOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
305 #define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
306   ABSL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
307 
308 #endif  // ABSL_LOG_LOG_H_
309