summaryrefslogtreecommitdiff
path: root/absl/log/log_entry.h
blob: d1f500ca3347cd4a1684571f77c16ca44cf8ede9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: log/log_entry.h
// -----------------------------------------------------------------------------
//
// This header declares `class absl::LogEntry`, which represents a log record as
// passed to `LogSink::Send`. Data returned by pointer or by reference or by
// `absl::string_view` must be copied if they are needed after the lifetime of
// the `absl::LogEntry`.

#ifndef ABSL_LOG_LOG_ENTRY_H_
#define ABSL_LOG_LOG_ENTRY_H_

#include <cstddef>
#include <string>

#include "absl/base/config.h"
#include "absl/base/log_severity.h"
#include "absl/log/internal/config.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/span.h"

namespace absl {
ABSL_NAMESPACE_BEGIN

namespace log_internal {
// Test only friend.
class LogEntryTestPeer;
class LogMessage;
}  // namespace log_internal

// LogEntry
//
// Represents a single entry in a log, i.e., one log message.
//
// LogEntry is copyable and thread-compatible.
class LogEntry final {
 public:
  using tid_t = log_internal::Tid;

  // For non-verbose log entries, `verbosity()` returns `kNoVerbosityLevel`.
  static constexpr int kNoVerbosityLevel = -1;
  static constexpr int kNoVerboseLevel = -1;  // TO BE removed

  LogEntry(const LogEntry&) = default;
  LogEntry& operator=(const LogEntry&) = default;

  // Source file and line where the log message occurred.
  // Take special care not to dereference the pointers returned by
  // source_filename() and source_basename() after the lifetime of the
  // `LogEntry`. This will usually work, because these are usually backed by a
  // statically allocated char array obtained from the `__FILE__` macro, but
  // it is nevertheless incorrect and will be broken by statements like
  // `LOG(INFO).AtLocation(...)` (see above).  If you need the data later, you
  // must copy it.
  absl::string_view source_filename() const { return full_filename_; }
  absl::string_view source_basename() const { return base_filename_; }
  int source_line() const { return line_; }

  // LogEntry::prefix()
  //
  // True unless cleared by LOG(...).NoPrefix(), which indicates suppression of
  // the line prefix containing metadata like file, line, timestamp, etc.
  bool prefix() const { return prefix_; }

  // LogEntry::log_severity()
  //
  // Returns this LogEntry's severity.
  absl::LogSeverity log_severity() const { return severity_; }

  // LogEntry::verbosity()
  //
  // Returns this LogEntry's verbosity, or kNoVerbosityLevel for a non-verbose
  // LogEntry.
  int verbosity() const { return verbose_level_; }

  // LogEntry::timestamp()
  //
  // Returns the time at which this LogEntry was written.
  absl::Time timestamp() const { return timestamp_; }

  // LogEntry::tid()
  //
  // Returns the id of the thread that wrote this LogEntry.
  tid_t tid() const { return tid_; }

  // Text-formatted version of the log message.  An underlying buffer holds:
  //
  // * A prefix formed by formatting metadata (timestamp, filename, line number,
  //   etc.)
  // * The streamed data
  // * A newline
  // * A nul terminator
  //
  // These methods give access to the most commonly-used substrings of the
  // buffer's contents.  Other combinations can be obtained with substring
  // arithmetic.
  absl::string_view text_message_with_prefix_and_newline() const {
    return absl::string_view(
        text_message_with_prefix_and_newline_and_nul_.data(),
        text_message_with_prefix_and_newline_and_nul_.size() - 1);
  }
  absl::string_view text_message_with_prefix() const {
    return absl::string_view(
        text_message_with_prefix_and_newline_and_nul_.data(),
        text_message_with_prefix_and_newline_and_nul_.size() - 2);
  }
  absl::string_view text_message_with_newline() const {
    return absl::string_view(
        text_message_with_prefix_and_newline_and_nul_.data() + prefix_len_,
        text_message_with_prefix_and_newline_and_nul_.size() - prefix_len_ - 1);
  }
  absl::string_view text_message() const {
    return absl::string_view(
        text_message_with_prefix_and_newline_and_nul_.data() + prefix_len_,
        text_message_with_prefix_and_newline_and_nul_.size() - prefix_len_ - 2);
  }
  const char* text_message_with_prefix_and_newline_c_str() const {
    return text_message_with_prefix_and_newline_and_nul_.data();
  }

  // LogEntry::stacktrace()
  //
  // Optional stacktrace, e.g., for `FATAL` logs.
  absl::string_view stacktrace() const { return stacktrace_; }

 private:
  LogEntry() = default;

  absl::string_view full_filename_;
  absl::string_view base_filename_;
  int line_;
  bool prefix_;
  absl::LogSeverity severity_;
  int verbose_level_;  // >=0 for `VLOG`, etc.; otherwise `kNoVerbosityLevel`.
  absl::Time timestamp_;
  tid_t tid_;
  absl::Span<const char> text_message_with_prefix_and_newline_and_nul_;
  size_t prefix_len_;
  std::string stacktrace_;

  friend class log_internal::LogEntryTestPeer;
  friend class log_internal::LogMessage;
};

ABSL_NAMESPACE_END
}  // namespace absl

#endif  // ABSL_LOG_LOG_ENTRY_H_