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