log.h 16 KB

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