log_entry.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_entry.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header declares `class absl::LogEntry`, which represents a log record as
  20. // passed to `LogSink::Send`. Data returned by pointer or by reference or by
  21. // `absl::string_view` must be copied if they are needed after the lifetime of
  22. // the `absl::LogEntry`.
  23. #ifndef ABSL_LOG_LOG_ENTRY_H_
  24. #define ABSL_LOG_LOG_ENTRY_H_
  25. #include <cstddef>
  26. #include <string>
  27. #include "absl/base/attributes.h"
  28. #include "absl/base/config.h"
  29. #include "absl/base/log_severity.h"
  30. #include "absl/log/internal/config.h"
  31. #include "absl/strings/string_view.h"
  32. #include "absl/time/time.h"
  33. #include "absl/types/span.h"
  34. namespace absl {
  35. ABSL_NAMESPACE_BEGIN
  36. namespace log_internal {
  37. // Test only friend.
  38. class LogEntryTestPeer;
  39. class LogMessage;
  40. } // namespace log_internal
  41. // LogEntry
  42. //
  43. // Represents a single entry in a log, i.e., one `LOG` statement or failed
  44. // `CHECK`.
  45. //
  46. // `LogEntry` is thread-compatible.
  47. class LogEntry final {
  48. public:
  49. using tid_t = log_internal::Tid;
  50. // For non-verbose log entries, `verbosity()` returns `kNoVerbosityLevel`.
  51. static constexpr int kNoVerbosityLevel = -1;
  52. static constexpr int kNoVerboseLevel = -1; // TO BE removed
  53. // Pass `LogEntry` by reference, and do not store it as its state does not
  54. // outlive the call to `LogSink::Send()`.
  55. LogEntry(const LogEntry&) = delete;
  56. LogEntry& operator=(const LogEntry&) = delete;
  57. // Source file and line where the log message occurred. Taken from `__FILE__`
  58. // and `__LINE__` unless overridden by `LOG(...).AtLocation(...)`.
  59. //
  60. // Take special care not to use the values returned by `source_filename()` and
  61. // `source_basename()` after the lifetime of the entry. This is always
  62. // incorrect, but it will often work in practice because they usually point
  63. // into a statically allocated character array obtained from `__FILE__`.
  64. // Statements like `LOG(INFO).AtLocation(std::string(...), ...)` will expose
  65. // the bug. If you need the data later, you must copy them.
  66. absl::string_view source_filename() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
  67. return full_filename_;
  68. }
  69. absl::string_view source_basename() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
  70. return base_filename_;
  71. }
  72. int source_line() const { return line_; }
  73. // LogEntry::prefix()
  74. //
  75. // True unless the metadata prefix was suppressed once by
  76. // `LOG(...).NoPrefix()` or globally by `absl::EnableLogPrefix(false)`.
  77. // Implies `text_message_with_prefix() == text_message()`.
  78. bool prefix() const { return prefix_; }
  79. // LogEntry::log_severity()
  80. //
  81. // Returns this entry's severity. For `LOG`, taken from the first argument;
  82. // for `CHECK`, always `absl::LogSeverity::kFatal`.
  83. absl::LogSeverity log_severity() const { return severity_; }
  84. // LogEntry::verbosity()
  85. //
  86. // Returns this entry's verbosity, or `kNoVerbosityLevel` for a non-verbose
  87. // entry. Taken from the argument to `VLOG` or from
  88. // `LOG(...).WithVerbosity(...)`.
  89. int verbosity() const { return verbose_level_; }
  90. // LogEntry::timestamp()
  91. //
  92. // Returns the time at which this entry was written. Captured during
  93. // evaluation of `LOG`, but can be overridden by
  94. // `LOG(...).WithTimestamp(...)`.
  95. //
  96. // Take care not to rely on timestamps increasing monotonically, or even to
  97. // rely on timestamps having any particular relationship with reality (since
  98. // they can be overridden).
  99. absl::Time timestamp() const { return timestamp_; }
  100. // LogEntry::tid()
  101. //
  102. // Returns the ID of the thread that wrote this entry. Captured during
  103. // evaluation of `LOG`, but can be overridden by `LOG(...).WithThreadID(...)`.
  104. //
  105. // Take care not to *rely* on reported thread IDs as they can be overridden as
  106. // specified above.
  107. tid_t tid() const { return tid_; }
  108. // Text-formatted version of the log message. An underlying buffer holds
  109. // these contiguous data:
  110. //
  111. // * A prefix formed by formatting metadata (timestamp, filename, line number,
  112. // etc.)
  113. // The prefix may be empty - see `LogEntry::prefix()` - and may rarely be
  114. // truncated if the metadata are very long.
  115. // * The streamed data
  116. // The data may be empty if nothing was streamed, or may be truncated to fit
  117. // the buffer.
  118. // * A newline
  119. // * A nul terminator
  120. //
  121. // The newline and nul terminator will be present even if the prefix and/or
  122. // data are truncated.
  123. //
  124. // These methods give access to the most commonly useful substrings of the
  125. // buffer's contents. Other combinations can be obtained with substring
  126. // arithmetic.
  127. //
  128. // The buffer does not outlive the entry; if you need the data later, you must
  129. // copy them.
  130. absl::string_view text_message_with_prefix_and_newline() const
  131. ABSL_ATTRIBUTE_LIFETIME_BOUND {
  132. return absl::string_view(
  133. text_message_with_prefix_and_newline_and_nul_.data(),
  134. text_message_with_prefix_and_newline_and_nul_.size() - 1);
  135. }
  136. absl::string_view text_message_with_prefix() const
  137. ABSL_ATTRIBUTE_LIFETIME_BOUND {
  138. return absl::string_view(
  139. text_message_with_prefix_and_newline_and_nul_.data(),
  140. text_message_with_prefix_and_newline_and_nul_.size() - 2);
  141. }
  142. absl::string_view text_message_with_newline() const
  143. ABSL_ATTRIBUTE_LIFETIME_BOUND {
  144. return absl::string_view(
  145. text_message_with_prefix_and_newline_and_nul_.data() + prefix_len_,
  146. text_message_with_prefix_and_newline_and_nul_.size() - prefix_len_ - 1);
  147. }
  148. absl::string_view text_message() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
  149. return absl::string_view(
  150. text_message_with_prefix_and_newline_and_nul_.data() + prefix_len_,
  151. text_message_with_prefix_and_newline_and_nul_.size() - prefix_len_ - 2);
  152. }
  153. const char* text_message_with_prefix_and_newline_c_str() const
  154. ABSL_ATTRIBUTE_LIFETIME_BOUND {
  155. return text_message_with_prefix_and_newline_and_nul_.data();
  156. }
  157. // Returns a serialized protobuf holding the operands streamed into this
  158. // log message. The message definition is not yet published.
  159. //
  160. // The buffer does not outlive the entry; if you need the data later, you must
  161. // copy them.
  162. absl::string_view encoded_message() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
  163. return encoding_;
  164. }
  165. // LogEntry::stacktrace()
  166. //
  167. // Optional stacktrace, e.g. for `FATAL` logs and failed `CHECK`s.
  168. //
  169. // Fatal entries are dispatched to each sink twice: first with all data and
  170. // metadata but no stacktrace, and then with the stacktrace. This is done
  171. // because stacktrace collection is sometimes slow and fallible, and it's
  172. // critical to log enough information to diagnose the failure even if the
  173. // stacktrace collection hangs.
  174. //
  175. // The buffer does not outlive the entry; if you need the data later, you must
  176. // copy them.
  177. absl::string_view stacktrace() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
  178. return stacktrace_;
  179. }
  180. private:
  181. LogEntry() = default;
  182. absl::string_view full_filename_;
  183. absl::string_view base_filename_;
  184. int line_;
  185. bool prefix_;
  186. absl::LogSeverity severity_;
  187. int verbose_level_; // >=0 for `VLOG`, etc.; otherwise `kNoVerbosityLevel`.
  188. absl::Time timestamp_;
  189. tid_t tid_;
  190. absl::Span<const char> text_message_with_prefix_and_newline_and_nul_;
  191. size_t prefix_len_;
  192. absl::string_view encoding_;
  193. std::string stacktrace_;
  194. friend class log_internal::LogEntryTestPeer;
  195. friend class log_internal::LogMessage;
  196. };
  197. ABSL_NAMESPACE_END
  198. } // namespace absl
  199. #endif // ABSL_LOG_LOG_ENTRY_H_