Coverage Report

Created: 2026-02-14 06:58

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
51.3k
  JSONWriter() : state_(kExpectKey), first_element_(true), in_array_(false) {
21
51.3k
    stream_ << "{";
22
51.3k
  }
23
24
639k
  void AddKey(const std::string& key) {
25
639k
    assert(state_ == kExpectKey);
26
639k
    if (!first_element_) {
27
580k
      stream_ << ", ";
28
580k
    }
29
639k
    stream_ << "\"" << key << "\": ";
30
639k
    state_ = kExpectValue;
31
639k
    first_element_ = false;
32
639k
  }
33
34
198k
  void AddValue(const char* value) {
35
198k
    assert(state_ == kExpectValue || state_ == kInArray);
36
198k
    if (state_ == kInArray && !first_element_) {
37
0
      stream_ << ", ";
38
0
    }
39
198k
    stream_ << "\"" << value << "\"";
40
198k
    if (state_ != kInArray) {
41
198k
      state_ = kExpectKey;
42
198k
    }
43
198k
    first_element_ = false;
44
198k
  }
45
46
  template <typename T>
47
450k
  void AddValue(const T& value) {
48
450k
    assert(state_ == kExpectValue || state_ == kInArray);
49
450k
    if (state_ == kInArray && !first_element_) {
50
17.3k
      stream_ << ", ";
51
17.3k
    }
52
450k
    stream_ << value;
53
450k
    if (state_ != kInArray) {
54
411k
      state_ = kExpectKey;
55
411k
    }
56
450k
    first_element_ = false;
57
450k
  }
void rocksdb::JSONWriter::AddValue<long long>(long long const&)
Line
Count
Source
47
51.3k
  void AddValue(const T& value) {
48
51.3k
    assert(state_ == kExpectValue || state_ == kInArray);
49
51.3k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
51.3k
    stream_ << value;
53
51.3k
    if (state_ != kInArray) {
54
51.3k
      state_ = kExpectKey;
55
51.3k
    }
56
51.3k
    first_element_ = false;
57
51.3k
  }
void rocksdb::JSONWriter::AddValue<int>(int const&)
Line
Count
Source
47
72.4k
  void AddValue(const T& value) {
48
72.4k
    assert(state_ == kExpectValue || state_ == kInArray);
49
72.4k
    if (state_ == kInArray && !first_element_) {
50
15.6k
      stream_ << ", ";
51
15.6k
    }
52
72.4k
    stream_ << value;
53
72.4k
    if (state_ != kInArray) {
54
54.2k
      state_ = kExpectKey;
55
54.2k
    }
56
72.4k
    first_element_ = false;
57
72.4k
  }
void rocksdb::JSONWriter::AddValue<unsigned long>(unsigned long const&)
Line
Count
Source
47
320k
  void AddValue(const T& value) {
48
320k
    assert(state_ == kExpectValue || state_ == kInArray);
49
320k
    if (state_ == kInArray && !first_element_) {
50
1.75k
      stream_ << ", ";
51
1.75k
    }
52
320k
    stream_ << value;
53
320k
    if (state_ != kInArray) {
54
300k
      state_ = kExpectKey;
55
300k
    }
56
320k
    first_element_ = false;
57
320k
  }
void rocksdb::JSONWriter::AddValue<unsigned int>(unsigned int const&)
Line
Count
Source
47
2.92k
  void AddValue(const T& value) {
48
2.92k
    assert(state_ == kExpectValue || state_ == kInArray);
49
2.92k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
2.92k
    stream_ << value;
53
2.92k
    if (state_ != kInArray) {
54
2.92k
      state_ = kExpectKey;
55
2.92k
    }
56
2.92k
    first_element_ = false;
57
2.92k
  }
Unexecuted instantiation: void rocksdb::JSONWriter::AddValue<bool>(bool const&)
void rocksdb::JSONWriter::AddValue<double>(double const&)
Line
Count
Source
47
1.46k
  void AddValue(const T& value) {
48
1.46k
    assert(state_ == kExpectValue || state_ == kInArray);
49
1.46k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
1.46k
    stream_ << value;
53
1.46k
    if (state_ != kInArray) {
54
1.46k
      state_ = kExpectKey;
55
1.46k
    }
56
1.46k
    first_element_ = false;
57
1.46k
  }
void rocksdb::JSONWriter::AddValue<long>(long const&)
Line
Count
Source
47
1.46k
  void AddValue(const T& value) {
48
1.46k
    assert(state_ == kExpectValue || state_ == kInArray);
49
1.46k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
1.46k
    stream_ << value;
53
1.46k
    if (state_ != kInArray) {
54
1.46k
      state_ = kExpectKey;
55
1.46k
    }
56
1.46k
    first_element_ = false;
57
1.46k
  }
58
59
21.5k
  void StartArray() {
60
21.5k
    assert(state_ == kExpectValue);
61
21.5k
    state_ = kInArray;
62
21.5k
    in_array_ = true;
63
21.5k
    stream_ << "[";
64
21.5k
    first_element_ = true;
65
21.5k
  }
66
67
21.5k
  void EndArray() {
68
21.5k
    assert(state_ == kInArray);
69
21.5k
    state_ = kExpectKey;
70
21.5k
    in_array_ = false;
71
21.5k
    stream_ << "]";
72
21.5k
    first_element_ = false;
73
21.5k
  }
74
75
8.06k
  void StartObject() {
76
8.06k
    assert(state_ == kExpectValue);
77
8.06k
    state_ = kExpectKey;
78
8.06k
    stream_ << "{";
79
8.06k
    first_element_ = true;
80
8.06k
  }
81
82
59.4k
  void EndObject() {
83
59.4k
    assert(state_ == kExpectKey);
84
59.4k
    stream_ << "}";
85
59.4k
    first_element_ = false;
86
59.4k
  }
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
51.3k
  std::string Get() const { return stream_.str(); }
104
105
838k
  JSONWriter& operator<<(const char* val) {
106
838k
    if (state_ == kExpectKey) {
107
639k
      AddKey(val);
108
639k
    } else {
109
198k
      AddValue(val);
110
198k
    }
111
838k
    return *this;
112
838k
  }
113
114
138k
  JSONWriter& operator<<(const std::string& val) {
115
138k
    return *this << val.c_str();
116
138k
  }
117
118
  template <typename T>
119
450k
  JSONWriter& operator<<(const T& val) {
120
450k
    assert(state_ != kExpectKey);
121
450k
    AddValue(val);
122
450k
    return *this;
123
450k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long long>(long long const&)
Line
Count
Source
119
51.3k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
51.3k
    AddValue(val);
122
51.3k
    return *this;
123
51.3k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <int>(int const&)
Line
Count
Source
119
72.4k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
72.4k
    AddValue(val);
122
72.4k
    return *this;
123
72.4k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
119
320k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
320k
    AddValue(val);
122
320k
    return *this;
123
320k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
119
2.92k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
2.92k
    AddValue(val);
122
2.92k
    return *this;
123
2.92k
  }
Unexecuted instantiation: rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <bool>(bool const&)
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <double>(double const&)
Line
Count
Source
119
1.46k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
1.46k
    AddValue(val);
122
1.46k
    return *this;
123
1.46k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long>(long const&)
Line
Count
Source
119
1.46k
  JSONWriter& operator<<(const T& val) {
120
    assert(state_ != kExpectKey);
121
1.46k
    AddValue(val);
122
1.46k
    return *this;
123
1.46k
  }
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
415k
  EventLoggerStream& operator<<(const T& val) {
142
415k
    MakeStream();
143
415k
    *json_writer_ << val;
144
415k
    return *this;
145
415k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [12]>(char const (&) [12])
Line
Count
Source
141
41.8k
  EventLoggerStream& operator<<(const T& val) {
142
41.8k
    MakeStream();
143
41.8k
    *json_writer_ << val;
144
41.8k
    return *this;
145
41.8k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long long>(long long const&)
Line
Count
Source
141
40.6k
  EventLoggerStream& operator<<(const T& val) {
142
40.6k
    MakeStream();
143
40.6k
    *json_writer_ << val;
144
40.6k
    return *this;
145
40.6k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [4]>(char const (&) [4])
Line
Count
Source
141
40.6k
  EventLoggerStream& operator<<(const T& val) {
142
40.6k
    MakeStream();
143
40.6k
    *json_writer_ << val;
144
40.6k
    return *this;
145
40.6k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <int>(int const&)
Line
Count
Source
141
61.7k
  EventLoggerStream& operator<<(const T& val) {
142
61.7k
    MakeStream();
143
61.7k
    *json_writer_ << val;
144
61.7k
    return *this;
145
61.7k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [6]>(char const (&) [6])
Line
Count
Source
141
43.8k
  EventLoggerStream& operator<<(const T& val) {
142
43.8k
    MakeStream();
143
43.8k
    *json_writer_ << val;
144
43.8k
    return *this;
145
43.8k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [13]>(char const (&) [13])
Line
Count
Source
141
5.47k
  EventLoggerStream& operator<<(const T& val) {
142
5.47k
    MakeStream();
143
5.47k
    *json_writer_ << val;
144
5.47k
    return *this;
145
5.47k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [18]>(char const (&) [18])
Line
Count
Source
141
7.25k
  EventLoggerStream& operator<<(const T& val) {
142
7.25k
    MakeStream();
143
7.25k
    *json_writer_ << val;
144
7.25k
    return *this;
145
7.25k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
141
44.2k
  EventLoggerStream& operator<<(const T& val) {
142
44.2k
    MakeStream();
143
44.2k
    *json_writer_ << val;
144
44.2k
    return *this;
145
44.2k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [17]>(char const (&) [17])
Line
Count
Source
141
20.0k
  EventLoggerStream& operator<<(const T& val) {
142
20.0k
    MakeStream();
143
20.0k
    *json_writer_ << val;
144
20.0k
    return *this;
145
20.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [10]>(char const (&) [10])
Line
Count
Source
141
19.4k
  EventLoggerStream& operator<<(const T& val) {
142
19.4k
    MakeStream();
143
19.4k
    *json_writer_ << val;
144
19.4k
    return *this;
145
19.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char const*>(char const* const&)
Line
Count
Source
141
19.4k
  EventLoggerStream& operator<<(const T& val) {
142
19.4k
    MakeStream();
143
19.4k
    *json_writer_ << val;
144
19.4k
    return *this;
145
19.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [7]>(char const (&) [7])
Line
Count
Source
141
16.8k
  EventLoggerStream& operator<<(const T& val) {
142
16.8k
    MakeStream();
143
16.8k
    *json_writer_ << val;
144
16.8k
    return *this;
145
16.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
23.0k
  EventLoggerStream& operator<<(const T& val) {
142
23.0k
    MakeStream();
143
23.0k
    *json_writer_ << val;
144
23.0k
    return *this;
145
23.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [16]>(char const (&) [16])
Line
Count
Source
141
2.60k
  EventLoggerStream& operator<<(const T& val) {
142
2.60k
    MakeStream();
143
2.60k
    *json_writer_ << val;
144
2.60k
    return *this;
145
2.60k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [15]>(char const (&) [15])
Line
Count
Source
141
1.14k
  EventLoggerStream& operator<<(const T& val) {
142
1.14k
    MakeStream();
143
1.14k
    *json_writer_ << val;
144
1.14k
    return *this;
145
1.14k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [5]>(char const (&) [5])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [19]>(char const (&) [19])
Line
Count
Source
141
6.98k
  EventLoggerStream& operator<<(const T& val) {
142
6.98k
    MakeStream();
143
6.98k
    *json_writer_ << val;
144
6.98k
    return *this;
145
6.98k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [20]>(char const (&) [20])
Line
Count
Source
141
2.60k
  EventLoggerStream& operator<<(const T& val) {
142
2.60k
    MakeStream();
143
2.60k
    *json_writer_ << val;
144
2.60k
    return *this;
145
2.60k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [22]>(char const (&) [22])
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [25]>(char const (&) [25])
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [21]>(char const (&) [21])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [14]>(char const (&) [14])
Line
Count
Source
141
2.29k
  EventLoggerStream& operator<<(const T& val) {
142
2.29k
    MakeStream();
143
2.29k
    *json_writer_ << val;
144
2.29k
    return *this;
145
2.29k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [24]>(char const (&) [24])
Line
Count
Source
141
1.14k
  EventLoggerStream& operator<<(const T& val) {
142
1.14k
    MakeStream();
143
1.14k
    *json_writer_ << val;
144
1.14k
    return *this;
145
1.14k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
141
2.92k
  EventLoggerStream& operator<<(const T& val) {
142
2.92k
    MakeStream();
143
2.92k
    *json_writer_ << val;
144
2.92k
    return *this;
145
2.92k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [23]>(char const (&) [23])
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [27]>(char const (&) [27])
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [29]>(char const (&) [29])
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [30]>(char const (&) [30])
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
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
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <double>(double const&)
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long>(long const&)
Line
Count
Source
141
1.46k
  EventLoggerStream& operator<<(const T& val) {
142
1.46k
    MakeStream();
143
1.46k
    *json_writer_ << val;
144
1.46k
    return *this;
145
1.46k
  }
146
147
21.5k
  void StartArray() { json_writer_->StartArray(); }
148
21.5k
  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
415k
  void MakeStream() {
156
415k
    if (!json_writer_) {
157
40.6k
      json_writer_ = new JSONWriter();
158
40.6k
      *this << "time_micros"
159
40.6k
            << std::chrono::duration_cast<std::chrono::microseconds>(
160
40.6k
                   std::chrono::system_clock::now().time_since_epoch())
161
40.6k
                   .count();
162
40.6k
    }
163
415k
  }
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
51.3k
  static const char* Prefix() { return "EVENT_LOG_v1"; }
182
183
19.7k
  explicit EventLogger(Logger* logger) : logger_(logger) {}
184
36.3k
  EventLoggerStream Log() { return EventLoggerStream(logger_); }
185
1.72k
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer) {
186
1.72k
    return EventLoggerStream(log_buffer, LogBuffer::kDefaultMaxLogSize);
187
1.72k
  }
188
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer,
189
2.60k
                                const size_t max_log_size) {
190
2.60k
    return EventLoggerStream(log_buffer, max_log_size);
191
2.60k
  }
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