tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

log.h (17785B)


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