log.h 14 KB

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