Coverage Report

Created: 2026-05-31 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/logging/event_logger.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
6
#pragma once
7
8
#include <chrono>
9
#include <memory>
10
#include <sstream>
11
#include <string>
12
13
#include "logging/log_buffer.h"
14
#include "rocksdb/env.h"
15
16
namespace ROCKSDB_NAMESPACE {
17
18
class JSONWriter {
19
 public:
20
131k
  JSONWriter() : state_(kExpectKey), first_element_(true), in_array_(false) {
21
131k
    stream_ << "{";
22
131k
  }
23
24
1.70M
  void AddKey(const std::string& key) {
25
1.70M
    assert(state_ == kExpectKey);
26
1.70M
    if (!first_element_) {
27
1.54M
      stream_ << ", ";
28
1.54M
    }
29
1.70M
    stream_ << "\"" << key << "\": ";
30
1.70M
    state_ = kExpectValue;
31
1.70M
    first_element_ = false;
32
1.70M
  }
33
34
526k
  void AddValue(const char* value) {
35
526k
    assert(state_ == kExpectValue || state_ == kInArray);
36
526k
    if (state_ == kInArray && !first_element_) {
37
0
      stream_ << ", ";
38
0
    }
39
526k
    stream_ << "\"" << value << "\"";
40
526k
    if (state_ != kInArray) {
41
526k
      state_ = kExpectKey;
42
526k
    }
43
526k
    first_element_ = false;
44
526k
  }
45
46
  template <typename T>
47
1.21M
  void AddValue(const T& value) {
48
1.21M
    assert(state_ == kExpectValue || state_ == kInArray);
49
1.21M
    if (state_ == kInArray && !first_element_) {
50
56.5k
      stream_ << ", ";
51
56.5k
    }
52
1.21M
    stream_ << value;
53
1.21M
    if (state_ != kInArray) {
54
1.09M
      state_ = kExpectKey;
55
1.09M
    }
56
1.21M
    first_element_ = false;
57
1.21M
  }
void rocksdb::JSONWriter::AddValue<long long>(long long const&)
Line
Count
Source
47
131k
  void AddValue(const T& value) {
48
131k
    assert(state_ == kExpectValue || state_ == kInArray);
49
131k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
131k
    stream_ << value;
53
131k
    if (state_ != kInArray) {
54
131k
      state_ = kExpectKey;
55
131k
    }
56
131k
    first_element_ = false;
57
131k
  }
void rocksdb::JSONWriter::AddValue<int>(int const&)
Line
Count
Source
47
185k
  void AddValue(const T& value) {
48
185k
    assert(state_ == kExpectValue || state_ == kInArray);
49
185k
    if (state_ == kInArray && !first_element_) {
50
41.6k
      stream_ << ", ";
51
41.6k
    }
52
185k
    stream_ << value;
53
185k
    if (state_ != kInArray) {
54
137k
      state_ = kExpectKey;
55
137k
    }
56
185k
    first_element_ = false;
57
185k
  }
void rocksdb::JSONWriter::AddValue<unsigned long>(unsigned long const&)
Line
Count
Source
47
874k
  void AddValue(const T& value) {
48
874k
    assert(state_ == kExpectValue || state_ == kInArray);
49
874k
    if (state_ == kInArray && !first_element_) {
50
14.8k
      stream_ << ", ";
51
14.8k
    }
52
874k
    stream_ << value;
53
874k
    if (state_ != kInArray) {
54
809k
      state_ = kExpectKey;
55
809k
    }
56
874k
    first_element_ = false;
57
874k
  }
void rocksdb::JSONWriter::AddValue<unsigned int>(unsigned int const&)
Line
Count
Source
47
9.70k
  void AddValue(const T& value) {
48
9.70k
    assert(state_ == kExpectValue || state_ == kInArray);
49
9.70k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
9.70k
    stream_ << value;
53
9.70k
    if (state_ != kInArray) {
54
9.70k
      state_ = kExpectKey;
55
9.70k
    }
56
9.70k
    first_element_ = false;
57
9.70k
  }
void rocksdb::JSONWriter::AddValue<double>(double const&)
Line
Count
Source
47
4.85k
  void AddValue(const T& value) {
48
4.85k
    assert(state_ == kExpectValue || state_ == kInArray);
49
4.85k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
4.85k
    stream_ << value;
53
4.85k
    if (state_ != kInArray) {
54
4.85k
      state_ = kExpectKey;
55
4.85k
    }
56
4.85k
    first_element_ = false;
57
4.85k
  }
void rocksdb::JSONWriter::AddValue<long>(long const&)
Line
Count
Source
47
4.85k
  void AddValue(const T& value) {
48
4.85k
    assert(state_ == kExpectValue || state_ == kInArray);
49
4.85k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
4.85k
    stream_ << value;
53
4.85k
    if (state_ != kInArray) {
54
4.85k
      state_ = kExpectKey;
55
4.85k
    }
56
4.85k
    first_element_ = false;
57
4.85k
  }
Unexecuted instantiation: void rocksdb::JSONWriter::AddValue<bool>(bool const&)
58
59
56.6k
  void StartArray() {
60
56.6k
    assert(state_ == kExpectValue);
61
56.6k
    state_ = kInArray;
62
56.6k
    in_array_ = true;
63
56.6k
    stream_ << "[";
64
56.6k
    first_element_ = true;
65
56.6k
  }
66
67
56.6k
  void EndArray() {
68
56.6k
    assert(state_ == kInArray);
69
56.6k
    state_ = kExpectKey;
70
56.6k
    in_array_ = false;
71
56.6k
    stream_ << "]";
72
56.6k
    first_element_ = false;
73
56.6k
  }
74
75
21.7k
  void StartObject() {
76
21.7k
    assert(state_ == kExpectValue);
77
21.7k
    state_ = kExpectKey;
78
21.7k
    stream_ << "{";
79
21.7k
    first_element_ = true;
80
21.7k
  }
81
82
153k
  void EndObject() {
83
153k
    assert(state_ == kExpectKey);
84
153k
    stream_ << "}";
85
153k
    first_element_ = false;
86
153k
  }
87
88
0
  void StartArrayedObject() {
89
0
    assert(state_ == kInArray && in_array_);
90
0
    state_ = kExpectValue;
91
0
    if (!first_element_) {
92
0
      stream_ << ", ";
93
0
    }
94
0
    StartObject();
95
0
  }
96
97
0
  void EndArrayedObject() {
98
0
    assert(in_array_);
99
0
    EndObject();
100
0
    state_ = kInArray;
101
0
  }
102
103
131k
  std::string Get() const { return stream_.str(); }
104
105
2.22M
  JSONWriter& operator<<(const char* val) {
106
2.22M
    if (state_ == kExpectKey) {
107
1.70M
      AddKey(val);
108
1.70M
    } else {
109
526k
      AddValue(val);
110
526k
    }
111
2.22M
    return *this;
112
2.22M
  }
113
114
373k
  JSONWriter& operator<<(const std::string& val) {
115
373k
    return *this << val.c_str();
116
373k
  }
117
118
  template <typename T>
119
1.21M
  JSONWriter& operator<<(const T& val) {
120
1.21M
    assert(state_ != kExpectKey);
121
1.21M
    AddValue(val);
122
1.21M
    return *this;
123
1.21M
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long long>(long long const&)
Line
Count
Source
119
131k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
131k
    AddValue(val);
122
131k
    return *this;
123
131k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <int>(int const&)
Line
Count
Source
119
185k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
185k
    AddValue(val);
122
185k
    return *this;
123
185k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
119
874k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
874k
    AddValue(val);
122
874k
    return *this;
123
874k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
119
9.70k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
9.70k
    AddValue(val);
122
9.70k
    return *this;
123
9.70k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <double>(double const&)
Line
Count
Source
119
4.85k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
4.85k
    AddValue(val);
122
4.85k
    return *this;
123
4.85k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long>(long const&)
Line
Count
Source
119
4.85k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
4.85k
    AddValue(val);
122
4.85k
    return *this;
123
4.85k
  }
Unexecuted instantiation: rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <bool>(bool const&)
124
125
 private:
126
  enum JSONWriterState {
127
    kExpectKey,
128
    kExpectValue,
129
    kInArray,
130
    kInArrayedObject,
131
  };
132
  JSONWriterState state_;
133
  bool first_element_;
134
  bool in_array_;
135
  std::ostringstream stream_;
136
};
137
138
class EventLoggerStream {
139
 public:
140
  template <typename T>
141
1.07M
  EventLoggerStream& operator<<(const T& val) {
142
1.07M
    MakeStream();
143
1.07M
    *json_writer_ << val;
144
1.07M
    return *this;
145
1.07M
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [12]>(char const (&) [12])
Line
Count
Source
141
103k
  EventLoggerStream& operator<<(const T& val) {
142
103k
    MakeStream();
143
103k
    *json_writer_ << val;
144
103k
    return *this;
145
103k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long long>(long long const&)
Line
Count
Source
141
101k
  EventLoggerStream& operator<<(const T& val) {
142
101k
    MakeStream();
143
101k
    *json_writer_ << val;
144
101k
    return *this;
145
101k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [4]>(char const (&) [4])
Line
Count
Source
141
101k
  EventLoggerStream& operator<<(const T& val) {
142
101k
    MakeStream();
143
101k
    *json_writer_ << val;
144
101k
    return *this;
145
101k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <int>(int const&)
Line
Count
Source
141
155k
  EventLoggerStream& operator<<(const T& val) {
142
155k
    MakeStream();
143
155k
    *json_writer_ << val;
144
155k
    return *this;
145
155k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [6]>(char const (&) [6])
Line
Count
Source
141
109k
  EventLoggerStream& operator<<(const T& val) {
142
109k
    MakeStream();
143
109k
    *json_writer_ << val;
144
109k
    return *this;
145
109k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [13]>(char const (&) [13])
Line
Count
Source
141
12.5k
  EventLoggerStream& operator<<(const T& val) {
142
12.5k
    MakeStream();
143
12.5k
    *json_writer_ << val;
144
12.5k
    return *this;
145
12.5k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [18]>(char const (&) [18])
Line
Count
Source
141
20.1k
  EventLoggerStream& operator<<(const T& val) {
142
20.1k
    MakeStream();
143
20.1k
    *json_writer_ << val;
144
20.1k
    return *this;
145
20.1k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
141
127k
  EventLoggerStream& operator<<(const T& val) {
142
127k
    MakeStream();
143
127k
    *json_writer_ << val;
144
127k
    return *this;
145
127k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [17]>(char const (&) [17])
Line
Count
Source
141
50.4k
  EventLoggerStream& operator<<(const T& val) {
142
50.4k
    MakeStream();
143
50.4k
    *json_writer_ << val;
144
50.4k
    return *this;
145
50.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
141
9.70k
  EventLoggerStream& operator<<(const T& val) {
142
9.70k
    MakeStream();
143
9.70k
    *json_writer_ << val;
144
9.70k
    return *this;
145
9.70k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [20]>(char const (&) [20])
Line
Count
Source
141
6.94k
  EventLoggerStream& operator<<(const T& val) {
142
6.94k
    MakeStream();
143
6.94k
    *json_writer_ << val;
144
6.94k
    return *this;
145
6.94k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [23]>(char const (&) [23])
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [27]>(char const (&) [27])
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [22]>(char const (&) [22])
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [19]>(char const (&) [19])
Line
Count
Source
141
21.4k
  EventLoggerStream& operator<<(const T& val) {
142
21.4k
    MakeStream();
143
21.4k
    *json_writer_ << val;
144
21.4k
    return *this;
145
21.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
141
61.4k
  EventLoggerStream& operator<<(const T& val) {
142
61.4k
    MakeStream();
143
61.4k
    *json_writer_ << val;
144
61.4k
    return *this;
145
61.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [29]>(char const (&) [29])
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [30]>(char const (&) [30])
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [25]>(char const (&) [25])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [10]>(char const (&) [10])
Line
Count
Source
141
49.0k
  EventLoggerStream& operator<<(const T& val) {
142
49.0k
    MakeStream();
143
49.0k
    *json_writer_ << val;
144
49.0k
    return *this;
145
49.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [15]>(char const (&) [15])
Line
Count
Source
141
2.09k
  EventLoggerStream& operator<<(const T& val) {
142
2.09k
    MakeStream();
143
2.09k
    *json_writer_ << val;
144
2.09k
    return *this;
145
2.09k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [32]>(char const (&) [32])
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [34]>(char const (&) [34])
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [37]>(char const (&) [37])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [8]>(char const (&) [8])
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char const*>(char const* const&)
Line
Count
Source
141
49.0k
  EventLoggerStream& operator<<(const T& val) {
142
49.0k
    MakeStream();
143
49.0k
    *json_writer_ << val;
144
49.0k
    return *this;
145
49.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <double>(double const&)
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [16]>(char const (&) [16])
Line
Count
Source
141
6.94k
  EventLoggerStream& operator<<(const T& val) {
142
6.94k
    MakeStream();
143
6.94k
    *json_writer_ << val;
144
6.94k
    return *this;
145
6.94k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long>(long const&)
Line
Count
Source
141
4.85k
  EventLoggerStream& operator<<(const T& val) {
142
4.85k
    MakeStream();
143
4.85k
    *json_writer_ << val;
144
4.85k
    return *this;
145
4.85k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [21]>(char const (&) [21])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [7]>(char const (&) [7])
Line
Count
Source
141
42.1k
  EventLoggerStream& operator<<(const T& val) {
142
42.1k
    MakeStream();
143
42.1k
    *json_writer_ << val;
144
42.1k
    return *this;
145
42.1k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [5]>(char const (&) [5])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [14]>(char const (&) [14])
Line
Count
Source
141
4.18k
  EventLoggerStream& operator<<(const T& val) {
142
4.18k
    MakeStream();
143
4.18k
    *json_writer_ << val;
144
4.18k
    return *this;
145
4.18k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [24]>(char const (&) [24])
Line
Count
Source
141
2.09k
  EventLoggerStream& operator<<(const T& val) {
142
2.09k
    MakeStream();
143
2.09k
    *json_writer_ << val;
144
2.09k
    return *this;
145
2.09k
  }
146
147
56.6k
  void StartArray() { json_writer_->StartArray(); }
148
56.6k
  void EndArray() { json_writer_->EndArray(); }
149
0
  void StartObject() { json_writer_->StartObject(); }
150
0
  void EndObject() { json_writer_->EndObject(); }
151
152
  ~EventLoggerStream();
153
154
 private:
155
1.07M
  void MakeStream() {
156
1.07M
    if (!json_writer_) {
157
101k
      json_writer_ = new JSONWriter();
158
101k
      *this << "time_micros"
159
101k
            << std::chrono::duration_cast<std::chrono::microseconds>(
160
101k
                   std::chrono::system_clock::now().time_since_epoch())
161
101k
                   .count();
162
101k
    }
163
1.07M
  }
164
  friend class EventLogger;
165
  explicit EventLoggerStream(Logger* logger);
166
  explicit EventLoggerStream(LogBuffer* log_buffer, const size_t max_log_size);
167
  // exactly one is non-nullptr
168
  Logger* const logger_;
169
  LogBuffer* const log_buffer_;
170
  const size_t max_log_size_;  // used only for log_buffer_
171
  // ownership
172
  JSONWriter* json_writer_;
173
};
174
175
// here is an example of the output that will show up in the LOG:
176
// 2015/01/15-14:13:25.788019 1105ef000 EVENT_LOG_v1 {"time_micros":
177
// 1421360005788015, "event": "table_file_creation", "file_number": 12,
178
// "file_size": 1909699}
179
class EventLogger {
180
 public:
181
131k
  static const char* Prefix() { return "EVENT_LOG_v1"; }
182
183
49.7k
  explicit EventLogger(Logger* logger) : logger_(logger) {}
184
91.1k
  EventLoggerStream Log() { return EventLoggerStream(logger_); }
185
3.53k
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer) {
186
3.53k
    return EventLoggerStream(log_buffer, LogBuffer::kDefaultMaxLogSize);
187
3.53k
  }
188
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer,
189
6.94k
                                const size_t max_log_size) {
190
6.94k
    return EventLoggerStream(log_buffer, max_log_size);
191
6.94k
  }
192
  void Log(const JSONWriter& jwriter);
193
  static void Log(Logger* logger, const JSONWriter& jwriter);
194
  static void LogToBuffer(
195
      LogBuffer* log_buffer, const JSONWriter& jwriter,
196
      const size_t max_log_size = LogBuffer::kDefaultMaxLogSize);
197
198
 private:
199
  Logger* logger_;
200
};
201
202
}  // namespace ROCKSDB_NAMESPACE