Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:53

0001 // Copyright 2022 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: log/log.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This header declares a family of LOG macros.
0020 //
0021 // Basic invocation looks like this:
0022 //
0023 //   LOG(INFO) << "Found " << num_cookies << " cookies";
0024 //
0025 // Most `LOG` macros take a severity level argument.  The severity levels are
0026 // `INFO`, `WARNING`, `ERROR`, and `FATAL`.  They are defined
0027 // in absl/base/log_severity.h.
0028 // * The `FATAL` severity level terminates the program with a stack trace after
0029 //   logging its message.  Error handlers registered with `RunOnFailure`
0030 //   (process_state.h) are run, but exit handlers registered with `atexit(3)`
0031 //   are not.
0032 // * The `QFATAL` pseudo-severity level is equivalent to `FATAL` but triggers
0033 //   quieter termination messages, e.g. without a full stack trace, and skips
0034 //   running registered error handlers.
0035 // * The `DFATAL` pseudo-severity level is defined as `FATAL` in debug mode and
0036 //   as `ERROR` otherwise.
0037 // Some preprocessor shenanigans are used to ensure that e.g. `LOG(INFO)` has
0038 // the same meaning even if a local symbol or preprocessor macro named `INFO` is
0039 // defined.  To specify a severity level using an expression instead of a
0040 // literal, use `LEVEL(expr)`.
0041 // Example:
0042 //
0043 //   LOG(LEVEL(stale ? absl::LogSeverity::kWarning : absl::LogSeverity::kInfo))
0044 //       << "Cookies are " << days << " days old";
0045 
0046 // `LOG` macros evaluate to an unterminated statement.  The value at the end of
0047 // the statement supports some chainable methods:
0048 //
0049 //   * .AtLocation(absl::string_view file, int line)
0050 //     .AtLocation(absl::SourceLocation loc)
0051 //     Overrides the location inferred from the callsite.  The string pointed to
0052 //     by `file` must be valid until the end of the statement.
0053 //   * .NoPrefix()
0054 //     Omits the prefix from this line.  The prefix includes metadata about the
0055 //     logged data such as source code location and timestamp.
0056 //   * .WithVerbosity(int verbose_level)
0057 //     Sets the verbosity field of the logged message as if it was logged by
0058 //     `VLOG(verbose_level)`.  Unlike `VLOG`, this method does not affect
0059 //     evaluation of the statement when the specified `verbose_level` has been
0060 //     disabled.  The only effect is on `LogSink` implementations which make use
0061 //     of the `absl::LogSink::verbosity()` value.  The value
0062 //     `absl::LogEntry::kNoVerbosityLevel` can be specified to mark the message
0063 //     not verbose.
0064 //   * .WithTimestamp(absl::Time timestamp)
0065 //     Uses the specified timestamp instead of one collected at the time of
0066 //     execution.
0067 //   * .WithThreadID(absl::LogEntry::tid_t tid)
0068 //     Uses the specified thread ID instead of one collected at the time of
0069 //     execution.
0070 //   * .WithMetadataFrom(const absl::LogEntry &entry)
0071 //     Copies all metadata (but no data) from the specified `absl::LogEntry`.
0072 //     This can be used to change the severity of a message, but it has some
0073 //     limitations:
0074 //     * `ABSL_MIN_LOG_LEVEL` is evaluated against the severity passed into
0075 //       `LOG` (or the implicit `FATAL` level of `CHECK`).
0076 //     * `LOG(FATAL)` and `CHECK` terminate the process unconditionally, even if
0077 //       the severity is changed later.
0078 //     `.WithMetadataFrom(entry)` should almost always be used in combination
0079 //     with `LOG(LEVEL(entry.log_severity()))`.
0080 //   * .WithPerror()
0081 //     Appends to the logged message a colon, a space, a textual description of
0082 //     the current value of `errno` (as by `strerror(3)`), and the numerical
0083 //     value of `errno`.
0084 //   * .ToSinkAlso(absl::LogSink* sink)
0085 //     Sends this message to `*sink` in addition to whatever other sinks it
0086 //     would otherwise have been sent to.  `sink` must not be null.
0087 //   * .ToSinkOnly(absl::LogSink* sink)
0088 //     Sends this message to `*sink` and no others.  `sink` must not be null.
0089 //
0090 // No interfaces in this header are async-signal-safe; their use in signal
0091 // handlers is unsupported and may deadlock your program or eat your lunch.
0092 //
0093 // Many logging statements are inherently conditional.  For example,
0094 // `LOG_IF(INFO, !foo)` does nothing if `foo` is true.  Even seemingly
0095 // unconditional statements like `LOG(INFO)` might be disabled at
0096 // compile-time to minimize binary size or for security reasons.
0097 //
0098 // * Except for the condition in a `CHECK` or `QCHECK` statement, programs must
0099 //   not rely on evaluation of expressions anywhere in logging statements for
0100 //   correctness.  For example, this is ok:
0101 //
0102 //     CHECK((fp = fopen("config.ini", "r")) != nullptr);
0103 //
0104 //   But this is probably not ok:
0105 //
0106 //     LOG(INFO) << "Server status: " << StartServerAndReturnStatusString();
0107 //
0108 //   The example below is bad too; the `i++` in the `LOG_IF` condition might
0109 //   not be evaluated, resulting in an infinite loop:
0110 //
0111 //     for (int i = 0; i < 1000000;)
0112 //       LOG_IF(INFO, i++ % 1000 == 0) << "Still working...";
0113 //
0114 // * Except where otherwise noted, conditions which cause a statement not to log
0115 //   also cause expressions not to be evaluated.  Programs may rely on this for
0116 //   performance reasons, e.g. by streaming the result of an expensive function
0117 //   call into a `DLOG` or `LOG_EVERY_N` statement.
0118 // * Care has been taken to ensure that expressions are parsed by the compiler
0119 //   even if they are never evaluated.  This means that syntax errors will be
0120 //   caught and variables will be considered used for the purposes of
0121 //   unused-variable diagnostics.  For example, this statement won't compile
0122 //   even if `INFO`-level logging has been compiled out:
0123 //
0124 //     int number_of_cakes = 40;
0125 //     LOG(INFO) << "Number of cakes: " << number_of_cake;  // Note the typo!
0126 //
0127 //   Similarly, this won't produce unused-variable compiler diagnostics even
0128 //   if `INFO`-level logging is compiled out:
0129 //
0130 //     {
0131 //       char fox_line1[] = "Hatee-hatee-hatee-ho!";
0132 //       LOG_IF(ERROR, false) << "The fox says " << fox_line1;
0133 //       char fox_line2[] = "A-oo-oo-oo-ooo!";
0134 //       LOG(INFO) << "The fox also says " << fox_line2;
0135 //     }
0136 //
0137 //   This error-checking is not perfect; for example, symbols that have been
0138 //   declared but not defined may not produce link errors if used in logging
0139 //   statements that compile away.
0140 //
0141 // Expressions streamed into these macros are formatted using `operator<<` just
0142 // as they would be if streamed into a `std::ostream`, however it should be
0143 // noted that their actual type is unspecified.
0144 //
0145 // To implement a custom formatting operator for a type you own, there are two
0146 // options: `AbslStringify()` or `std::ostream& operator<<(std::ostream&, ...)`.
0147 // It is recommended that users make their types loggable through
0148 // `AbslStringify()` as it is a universal stringification extension that also
0149 // enables `absl::StrFormat` and `absl::StrCat` support. If both
0150 // `AbslStringify()` and `std::ostream& operator<<(std::ostream&, ...)` are
0151 // defined, `AbslStringify()` will be used.
0152 //
0153 // To use the `AbslStringify()` API, define a friend function template in your
0154 // type's namespace with the following signature:
0155 //
0156 //   template <typename Sink>
0157 //   void AbslStringify(Sink& sink, const UserDefinedType& value);
0158 //
0159 // `Sink` has the same interface as `absl::FormatSink`, but without
0160 // `PutPaddedString()`.
0161 //
0162 // Example:
0163 //
0164 //   struct Point {
0165 //     template <typename Sink>
0166 //     friend void AbslStringify(Sink& sink, const Point& p) {
0167 //       absl::Format(&sink, "(%v, %v)", p.x, p.y);
0168 //     }
0169 //
0170 //     int x;
0171 //     int y;
0172 //   };
0173 //
0174 // To use `std::ostream& operator<<(std::ostream&, ...)`, define
0175 // `std::ostream& operator<<(std::ostream&, ...)` in your type's namespace (for
0176 // ADL) just as you would to stream it to `std::cout`.
0177 //
0178 // Currently `AbslStringify()` ignores output manipulators but this is not
0179 // guaranteed behavior and may be subject to change in the future. If you would
0180 // like guaranteed behavior regarding output manipulators, please use
0181 // `std::ostream& operator<<(std::ostream&, ...)` to make custom types loggable
0182 // instead.
0183 //
0184 // Those macros that support streaming honor output manipulators and `fmtflag`
0185 // changes that output data (e.g. `std::ends`) or control formatting of data
0186 // (e.g. `std::hex` and `std::fixed`), however flushing such a stream is
0187 // ignored.  The message produced by a log statement is sent to registered
0188 // `absl::LogSink` instances at the end of the statement; those sinks are
0189 // responsible for their own flushing (e.g. to disk) semantics.
0190 //
0191 // Flag settings are not carried over from one `LOG` statement to the next; this
0192 // is a bit different than e.g. `std::cout`:
0193 //
0194 //   LOG(INFO) << std::hex << 0xdeadbeef;  // logs "0xdeadbeef"
0195 //   LOG(INFO) << 0xdeadbeef;              // logs "3735928559"
0196 
0197 #ifndef ABSL_LOG_LOG_H_
0198 #define ABSL_LOG_LOG_H_
0199 
0200 #include "absl/log/internal/log_impl.h"
0201 
0202 // LOG()
0203 //
0204 // `LOG` takes a single argument which is a severity level.  Data streamed in
0205 // comprise the logged message.
0206 // Example:
0207 //
0208 //   LOG(INFO) << "Found " << num_cookies << " cookies";
0209 #define LOG(severity) ABSL_LOG_INTERNAL_LOG_IMPL(_##severity)
0210 
0211 // PLOG()
0212 //
0213 // `PLOG` behaves like `LOG` except that a description of the current state of
0214 // `errno` is appended to the streamed message.
0215 #define PLOG(severity) ABSL_LOG_INTERNAL_PLOG_IMPL(_##severity)
0216 
0217 // DLOG()
0218 //
0219 // `DLOG` behaves like `LOG` in debug mode (i.e. `#ifndef NDEBUG`).  Otherwise
0220 // it compiles away and does nothing.  Note that `DLOG(FATAL)` does not
0221 // terminate the program if `NDEBUG` is defined.
0222 #define DLOG(severity) ABSL_LOG_INTERNAL_DLOG_IMPL(_##severity)
0223 
0224 // `VLOG` uses numeric levels to provide verbose logging that can configured at
0225 // runtime, including at a per-module level.  `VLOG` statements are logged at
0226 // `INFO` severity if they are logged at all; the numeric levels are on a
0227 // different scale than the proper severity levels.  Positive levels are
0228 // disabled by default.  Negative levels should not be used.
0229 // Example:
0230 //
0231 //   VLOG(1) << "I print when you run the program with --v=1 or higher";
0232 //   VLOG(2) << "I print when you run the program with --v=2 or higher";
0233 //
0234 // See vlog_is_on.h for further documentation, including the usage of the
0235 // --vmodule flag to log at different levels in different source files.
0236 //
0237 // `VLOG` does not produce any output when verbose logging is not enabled.
0238 // However, simply testing whether verbose logging is enabled can be expensive.
0239 // If you don't intend to enable verbose logging in non-debug builds, consider
0240 // using `DVLOG` instead.
0241 #define VLOG(severity) ABSL_LOG_INTERNAL_VLOG_IMPL(severity)
0242 
0243 // `DVLOG` behaves like `VLOG` in debug mode (i.e. `#ifndef NDEBUG`).
0244 // Otherwise, it compiles away and does nothing.
0245 #define DVLOG(severity) ABSL_LOG_INTERNAL_DVLOG_IMPL(severity)
0246 
0247 // `LOG_IF` and friends add a second argument which specifies a condition.  If
0248 // the condition is false, nothing is logged.
0249 // Example:
0250 //
0251 //   LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
0252 //
0253 // There is no `VLOG_IF` because the order of evaluation of the arguments is
0254 // ambiguous and the alternate spelling with an `if`-statement is trivial.
0255 #define LOG_IF(severity, condition) \
0256   ABSL_LOG_INTERNAL_LOG_IF_IMPL(_##severity, condition)
0257 #define PLOG_IF(severity, condition) \
0258   ABSL_LOG_INTERNAL_PLOG_IF_IMPL(_##severity, condition)
0259 #define DLOG_IF(severity, condition) \
0260   ABSL_LOG_INTERNAL_DLOG_IF_IMPL(_##severity, condition)
0261 
0262 // LOG_EVERY_N
0263 //
0264 // An instance of `LOG_EVERY_N` increments a hidden zero-initialized counter
0265 // every time execution passes through it and logs the specified message when
0266 // the counter's value is a multiple of `n`, doing nothing otherwise.  Each
0267 // instance has its own counter.  The counter's value can be logged by streaming
0268 // the symbol `COUNTER`.  `LOG_EVERY_N` is thread-safe.
0269 // Example:
0270 //
0271 //   LOG_EVERY_N(WARNING, 1000) << "Got a packet with a bad CRC (" << COUNTER
0272 //                              << " total)";
0273 #define LOG_EVERY_N(severity, n) \
0274   ABSL_LOG_INTERNAL_LOG_EVERY_N_IMPL(_##severity, n)
0275 
0276 // LOG_FIRST_N
0277 //
0278 // `LOG_FIRST_N` behaves like `LOG_EVERY_N` except that the specified message is
0279 // logged when the counter's value is less than `n`.  `LOG_FIRST_N` is
0280 // thread-safe.
0281 #define LOG_FIRST_N(severity, n) \
0282   ABSL_LOG_INTERNAL_LOG_FIRST_N_IMPL(_##severity, n)
0283 
0284 // LOG_EVERY_POW_2
0285 //
0286 // `LOG_EVERY_POW_2` behaves like `LOG_EVERY_N` except that the specified
0287 // message is logged when the counter's value is a power of 2.
0288 // `LOG_EVERY_POW_2` is thread-safe.
0289 #define LOG_EVERY_POW_2(severity) \
0290   ABSL_LOG_INTERNAL_LOG_EVERY_POW_2_IMPL(_##severity)
0291 
0292 // LOG_EVERY_N_SEC
0293 //
0294 // An instance of `LOG_EVERY_N_SEC` uses a hidden state variable to log the
0295 // specified message at most once every `n_seconds`.  A hidden counter of
0296 // executions (whether a message is logged or not) is also maintained and can be
0297 // logged by streaming the symbol `COUNTER`.  `LOG_EVERY_N_SEC` is thread-safe.
0298 // Example:
0299 //
0300 //   LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far";
0301 #define LOG_EVERY_N_SEC(severity, n_seconds) \
0302   ABSL_LOG_INTERNAL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
0303 
0304 #define PLOG_EVERY_N(severity, n) \
0305   ABSL_LOG_INTERNAL_PLOG_EVERY_N_IMPL(_##severity, n)
0306 #define PLOG_FIRST_N(severity, n) \
0307   ABSL_LOG_INTERNAL_PLOG_FIRST_N_IMPL(_##severity, n)
0308 #define PLOG_EVERY_POW_2(severity) \
0309   ABSL_LOG_INTERNAL_PLOG_EVERY_POW_2_IMPL(_##severity)
0310 #define PLOG_EVERY_N_SEC(severity, n_seconds) \
0311   ABSL_LOG_INTERNAL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
0312 
0313 #define DLOG_EVERY_N(severity, n) \
0314   ABSL_LOG_INTERNAL_DLOG_EVERY_N_IMPL(_##severity, n)
0315 #define DLOG_FIRST_N(severity, n) \
0316   ABSL_LOG_INTERNAL_DLOG_FIRST_N_IMPL(_##severity, n)
0317 #define DLOG_EVERY_POW_2(severity) \
0318   ABSL_LOG_INTERNAL_DLOG_EVERY_POW_2_IMPL(_##severity)
0319 #define DLOG_EVERY_N_SEC(severity, n_seconds) \
0320   ABSL_LOG_INTERNAL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds)
0321 
0322 #define VLOG_EVERY_N(severity, n) \
0323   ABSL_LOG_INTERNAL_VLOG_EVERY_N_IMPL(severity, n)
0324 #define VLOG_FIRST_N(severity, n) \
0325   ABSL_LOG_INTERNAL_VLOG_FIRST_N_IMPL(severity, n)
0326 #define VLOG_EVERY_POW_2(severity) \
0327   ABSL_LOG_INTERNAL_VLOG_EVERY_POW_2_IMPL(severity)
0328 #define VLOG_EVERY_N_SEC(severity, n_seconds) \
0329   ABSL_LOG_INTERNAL_VLOG_EVERY_N_SEC_IMPL(severity, n_seconds)
0330 
0331 // `LOG_IF_EVERY_N` and friends behave as the corresponding `LOG_EVERY_N`
0332 // but neither increment a counter nor log a message if condition is false (as
0333 // `LOG_IF`).
0334 // Example:
0335 //
0336 //   LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER
0337 //                                           << "th big cookie";
0338 #define LOG_IF_EVERY_N(severity, condition, n) \
0339   ABSL_LOG_INTERNAL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n)
0340 #define LOG_IF_FIRST_N(severity, condition, n) \
0341   ABSL_LOG_INTERNAL_LOG_IF_FIRST_N_IMPL(_##severity, condition, n)
0342 #define LOG_IF_EVERY_POW_2(severity, condition) \
0343   ABSL_LOG_INTERNAL_LOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
0344 #define LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
0345   ABSL_LOG_INTERNAL_LOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
0346 
0347 #define PLOG_IF_EVERY_N(severity, condition, n) \
0348   ABSL_LOG_INTERNAL_PLOG_IF_EVERY_N_IMPL(_##severity, condition, n)
0349 #define PLOG_IF_FIRST_N(severity, condition, n) \
0350   ABSL_LOG_INTERNAL_PLOG_IF_FIRST_N_IMPL(_##severity, condition, n)
0351 #define PLOG_IF_EVERY_POW_2(severity, condition) \
0352   ABSL_LOG_INTERNAL_PLOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
0353 #define PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
0354   ABSL_LOG_INTERNAL_PLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
0355 
0356 #define DLOG_IF_EVERY_N(severity, condition, n) \
0357   ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_IMPL(_##severity, condition, n)
0358 #define DLOG_IF_FIRST_N(severity, condition, n) \
0359   ABSL_LOG_INTERNAL_DLOG_IF_FIRST_N_IMPL(_##severity, condition, n)
0360 #define DLOG_IF_EVERY_POW_2(severity, condition) \
0361   ABSL_LOG_INTERNAL_DLOG_IF_EVERY_POW_2_IMPL(_##severity, condition)
0362 #define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \
0363   ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds)
0364 
0365 #endif  // ABSL_LOG_LOG_H_