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