Coverage Report

Created: 2026-09-28 07:52

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
142k
  JSONWriter() : state_(kExpectKey), first_element_(true), in_array_(false) {
21
142k
    stream_ << "{";
22
142k
  }
23
24
1.88M
  void AddKey(const std::string& key) {
25
1.88M
    assert(state_ == kExpectKey);
26
1.88M
    if (!first_element_) {
27
1.71M
      stream_ << ", ";
28
1.71M
    }
29
1.88M
    stream_ << "\"" << key << "\": ";
30
1.88M
    state_ = kExpectValue;
31
1.88M
    first_element_ = false;
32
1.88M
  }
33
34
589k
  void AddValue(const char* value) {
35
589k
    assert(state_ == kExpectValue || state_ == kInArray);
36
589k
    if (state_ == kInArray && !first_element_) {
37
0
      stream_ << ", ";
38
0
    }
39
589k
    stream_ << "\"" << value << "\"";
40
589k
    if (state_ != kInArray) {
41
589k
      state_ = kExpectKey;
42
589k
    }
43
589k
    first_element_ = false;
44
589k
  }
45
46
  template <typename T>
47
1.33M
  void AddValue(const T& value) {
48
1.33M
    assert(state_ == kExpectValue || state_ == kInArray);
49
1.33M
    if (state_ == kInArray && !first_element_) {
50
58.7k
      stream_ << ", ";
51
58.7k
    }
52
1.33M
    stream_ << value;
53
1.33M
    if (state_ != kInArray) {
54
1.21M
      state_ = kExpectKey;
55
1.21M
    }
56
1.33M
    first_element_ = false;
57
1.33M
  }
void rocksdb::JSONWriter::AddValue<long long>(long long const&)
Line
Count
Source
47
142k
  void AddValue(const T& value) {
48
142k
    assert(state_ == kExpectValue || state_ == kInArray);
49
142k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
142k
    stream_ << value;
53
142k
    if (state_ != kInArray) {
54
142k
      state_ = kExpectKey;
55
142k
    }
56
142k
    first_element_ = false;
57
142k
  }
void rocksdb::JSONWriter::AddValue<int>(int const&)
Line
Count
Source
47
201k
  void AddValue(const T& value) {
48
201k
    assert(state_ == kExpectValue || state_ == kInArray);
49
201k
    if (state_ == kInArray && !first_element_) {
50
44.7k
      stream_ << ", ";
51
44.7k
    }
52
201k
    stream_ << value;
53
201k
    if (state_ != kInArray) {
54
148k
      state_ = kExpectKey;
55
148k
    }
56
201k
    first_element_ = false;
57
201k
  }
void rocksdb::JSONWriter::AddValue<unsigned long>(unsigned long const&)
Line
Count
Source
47
966k
  void AddValue(const T& value) {
48
966k
    assert(state_ == kExpectValue || state_ == kInArray);
49
966k
    if (state_ == kInArray && !first_element_) {
50
13.9k
      stream_ << ", ";
51
13.9k
    }
52
966k
    stream_ << value;
53
966k
    if (state_ != kInArray) {
54
899k
      state_ = kExpectKey;
55
899k
    }
56
966k
    first_element_ = false;
57
966k
  }
void rocksdb::JSONWriter::AddValue<unsigned int>(unsigned int const&)
Line
Count
Source
47
10.2k
  void AddValue(const T& value) {
48
10.2k
    assert(state_ == kExpectValue || state_ == kInArray);
49
10.2k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
10.2k
    stream_ << value;
53
10.2k
    if (state_ != kInArray) {
54
10.2k
      state_ = kExpectKey;
55
10.2k
    }
56
10.2k
    first_element_ = false;
57
10.2k
  }
void rocksdb::JSONWriter::AddValue<double>(double const&)
Line
Count
Source
47
5.12k
  void AddValue(const T& value) {
48
5.12k
    assert(state_ == kExpectValue || state_ == kInArray);
49
5.12k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
5.12k
    stream_ << value;
53
5.12k
    if (state_ != kInArray) {
54
5.12k
      state_ = kExpectKey;
55
5.12k
    }
56
5.12k
    first_element_ = false;
57
5.12k
  }
void rocksdb::JSONWriter::AddValue<long>(long const&)
Line
Count
Source
47
5.12k
  void AddValue(const T& value) {
48
5.12k
    assert(state_ == kExpectValue || state_ == kInArray);
49
5.12k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
5.12k
    stream_ << value;
53
5.12k
    if (state_ != kInArray) {
54
5.12k
      state_ = kExpectKey;
55
5.12k
    }
56
5.12k
    first_element_ = false;
57
5.12k
  }
Unexecuted instantiation: void rocksdb::JSONWriter::AddValue<bool>(bool const&)
58
59
60.6k
  void StartArray() {
60
60.6k
    assert(state_ == kExpectValue);
61
60.6k
    state_ = kInArray;
62
60.6k
    in_array_ = true;
63
60.6k
    stream_ << "[";
64
60.6k
    first_element_ = true;
65
60.6k
  }
66
67
60.6k
  void EndArray() {
68
60.6k
    assert(state_ == kInArray);
69
60.6k
    state_ = kExpectKey;
70
60.6k
    in_array_ = false;
71
60.6k
    stream_ << "]";
72
60.6k
    first_element_ = false;
73
60.6k
  }
74
75
24.1k
  void StartObject() {
76
24.1k
    assert(state_ == kExpectValue);
77
24.1k
    state_ = kExpectKey;
78
24.1k
    stream_ << "{";
79
24.1k
    first_element_ = true;
80
24.1k
  }
81
82
166k
  void EndObject() {
83
166k
    assert(state_ == kExpectKey);
84
166k
    stream_ << "}";
85
166k
    first_element_ = false;
86
166k
  }
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
142k
  std::string Get() const { return stream_.str(); }
104
105
2.47M
  JSONWriter& operator<<(const char* val) {
106
2.47M
    if (state_ == kExpectKey) {
107
1.88M
      AddKey(val);
108
1.88M
    } else {
109
589k
      AddValue(val);
110
589k
    }
111
2.47M
    return *this;
112
2.47M
  }
113
114
423k
  JSONWriter& operator<<(const std::string& val) {
115
423k
    return *this << val.c_str();
116
423k
  }
117
118
  template <typename T>
119
1.33M
  JSONWriter& operator<<(const T& val) {
120
1.33M
    assert(state_ != kExpectKey);
121
1.33M
    AddValue(val);
122
1.33M
    return *this;
123
1.33M
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long long>(long long const&)
Line
Count
Source
119
142k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
142k
    AddValue(val);
122
142k
    return *this;
123
142k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <int>(int const&)
Line
Count
Source
119
201k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
201k
    AddValue(val);
122
201k
    return *this;
123
201k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
119
966k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
966k
    AddValue(val);
122
966k
    return *this;
123
966k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
119
10.2k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
10.2k
    AddValue(val);
122
10.2k
    return *this;
123
10.2k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <double>(double const&)
Line
Count
Source
119
5.12k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
5.12k
    AddValue(val);
122
5.12k
    return *this;
123
5.12k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long>(long const&)
Line
Count
Source
119
5.12k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
5.12k
    AddValue(val);
122
5.12k
    return *this;
123
5.12k
  }
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.15M
  EventLoggerStream& operator<<(const T& val) {
142
1.15M
    MakeStream();
143
1.15M
    *json_writer_ << val;
144
1.15M
    return *this;
145
1.15M
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [12]>(char const (&) [12])
Line
Count
Source
141
111k
  EventLoggerStream& operator<<(const T& val) {
142
111k
    MakeStream();
143
111k
    *json_writer_ << val;
144
111k
    return *this;
145
111k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long long>(long long const&)
Line
Count
Source
141
108k
  EventLoggerStream& operator<<(const T& val) {
142
108k
    MakeStream();
143
108k
    *json_writer_ << val;
144
108k
    return *this;
145
108k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [4]>(char const (&) [4])
Line
Count
Source
141
108k
  EventLoggerStream& operator<<(const T& val) {
142
108k
    MakeStream();
143
108k
    *json_writer_ << val;
144
108k
    return *this;
145
108k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <int>(int const&)
Line
Count
Source
141
167k
  EventLoggerStream& operator<<(const T& val) {
142
167k
    MakeStream();
143
167k
    *json_writer_ << val;
144
167k
    return *this;
145
167k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [6]>(char const (&) [6])
Line
Count
Source
141
117k
  EventLoggerStream& operator<<(const T& val) {
142
117k
    MakeStream();
143
117k
    *json_writer_ << val;
144
117k
    return *this;
145
117k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [13]>(char const (&) [13])
Line
Count
Source
141
13.7k
  EventLoggerStream& operator<<(const T& val) {
142
13.7k
    MakeStream();
143
13.7k
    *json_writer_ << val;
144
13.7k
    return *this;
145
13.7k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [18]>(char const (&) [18])
Line
Count
Source
141
21.6k
  EventLoggerStream& operator<<(const T& val) {
142
21.6k
    MakeStream();
143
21.6k
    *json_writer_ << val;
144
21.6k
    return *this;
145
21.6k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
141
135k
  EventLoggerStream& operator<<(const T& val) {
142
135k
    MakeStream();
143
135k
    *json_writer_ << val;
144
135k
    return *this;
145
135k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [17]>(char const (&) [17])
Line
Count
Source
141
54.0k
  EventLoggerStream& operator<<(const T& val) {
142
54.0k
    MakeStream();
143
54.0k
    *json_writer_ << val;
144
54.0k
    return *this;
145
54.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
141
10.2k
  EventLoggerStream& operator<<(const T& val) {
142
10.2k
    MakeStream();
143
10.2k
    *json_writer_ << val;
144
10.2k
    return *this;
145
10.2k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [20]>(char const (&) [20])
Line
Count
Source
141
7.46k
  EventLoggerStream& operator<<(const T& val) {
142
7.46k
    MakeStream();
143
7.46k
    *json_writer_ << val;
144
7.46k
    return *this;
145
7.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [23]>(char const (&) [23])
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [27]>(char const (&) [27])
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [22]>(char const (&) [22])
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [19]>(char const (&) [19])
Line
Count
Source
141
22.8k
  EventLoggerStream& operator<<(const T& val) {
142
22.8k
    MakeStream();
143
22.8k
    *json_writer_ << val;
144
22.8k
    return *this;
145
22.8k
  }
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
65.7k
  EventLoggerStream& operator<<(const T& val) {
142
65.7k
    MakeStream();
143
65.7k
    *json_writer_ << val;
144
65.7k
    return *this;
145
65.7k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [29]>(char const (&) [29])
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [30]>(char const (&) [30])
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
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
52.3k
  EventLoggerStream& operator<<(const T& val) {
142
52.3k
    MakeStream();
143
52.3k
    *json_writer_ << val;
144
52.3k
    return *this;
145
52.3k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [15]>(char const (&) [15])
Line
Count
Source
141
2.33k
  EventLoggerStream& operator<<(const T& val) {
142
2.33k
    MakeStream();
143
2.33k
    *json_writer_ << val;
144
2.33k
    return *this;
145
2.33k
  }
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
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char const*>(char const* const&)
Line
Count
Source
141
52.3k
  EventLoggerStream& operator<<(const T& val) {
142
52.3k
    MakeStream();
143
52.3k
    *json_writer_ << val;
144
52.3k
    return *this;
145
52.3k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <double>(double const&)
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [16]>(char const (&) [16])
Line
Count
Source
141
7.46k
  EventLoggerStream& operator<<(const T& val) {
142
7.46k
    MakeStream();
143
7.46k
    *json_writer_ << val;
144
7.46k
    return *this;
145
7.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long>(long const&)
Line
Count
Source
141
5.12k
  EventLoggerStream& operator<<(const T& val) {
142
5.12k
    MakeStream();
143
5.12k
    *json_writer_ << val;
144
5.12k
    return *this;
145
5.12k
  }
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
44.9k
  EventLoggerStream& operator<<(const T& val) {
142
44.9k
    MakeStream();
143
44.9k
    *json_writer_ << val;
144
44.9k
    return *this;
145
44.9k
  }
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.67k
  EventLoggerStream& operator<<(const T& val) {
142
4.67k
    MakeStream();
143
4.67k
    *json_writer_ << val;
144
4.67k
    return *this;
145
4.67k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [24]>(char const (&) [24])
Line
Count
Source
141
2.33k
  EventLoggerStream& operator<<(const T& val) {
142
2.33k
    MakeStream();
143
2.33k
    *json_writer_ << val;
144
2.33k
    return *this;
145
2.33k
  }
146
147
60.6k
  void StartArray() { json_writer_->StartArray(); }
148
60.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.15M
  void MakeStream() {
156
1.15M
    if (!json_writer_) {
157
108k
      json_writer_ = new JSONWriter();
158
108k
      *this << "time_micros"
159
108k
            << std::chrono::duration_cast<std::chrono::microseconds>(
160
108k
                   std::chrono::system_clock::now().time_since_epoch())
161
108k
                   .count();
162
108k
    }
163
1.15M
  }
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
142k
  static const char* Prefix() { return "EVENT_LOG_v1"; }
182
183
53.3k
  explicit EventLogger(Logger* logger) : logger_(logger) {}
184
97.3k
  EventLoggerStream Log() { return EventLoggerStream(logger_); }
185
3.95k
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer) {
186
3.95k
    return EventLoggerStream(log_buffer, LogBuffer::kDefaultMaxLogSize);
187
3.95k
  }
188
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer,
189
7.46k
                                const size_t max_log_size) {
190
7.46k
    return EventLoggerStream(log_buffer, max_log_size);
191
7.46k
  }
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