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