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