Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/logging/event_logger.h
Line
Count
Source (jump to first uncovered line)
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
41.6k
  JSONWriter() : state_(kExpectKey), first_element_(true), in_array_(false) {
21
41.6k
    stream_ << "{";
22
41.6k
  }
23
24
473k
  void AddKey(const std::string& key) {
25
473k
    assert(state_ == kExpectKey);
26
473k
    if (!first_element_) {
27
425k
      stream_ << ", ";
28
425k
    }
29
473k
    stream_ << "\"" << key << "\": ";
30
473k
    state_ = kExpectValue;
31
473k
    first_element_ = false;
32
473k
  }
33
34
133k
  void AddValue(const char* value) {
35
133k
    assert(state_ == kExpectValue || state_ == kInArray);
36
133k
    if (state_ == kInArray && !first_element_) {
37
0
      stream_ << ", ";
38
0
    }
39
133k
    stream_ << "\"" << value << "\"";
40
133k
    if (state_ != kInArray) {
41
133k
      state_ = kExpectKey;
42
133k
    }
43
133k
    first_element_ = false;
44
133k
  }
45
46
  template <typename T>
47
357k
  void AddValue(const T& value) {
48
357k
    assert(state_ == kExpectValue || state_ == kInArray);
49
357k
    if (state_ == kInArray && !first_element_) {
50
23.2k
      stream_ << ", ";
51
23.2k
    }
52
357k
    stream_ << value;
53
357k
    if (state_ != kInArray) {
54
315k
      state_ = kExpectKey;
55
315k
    }
56
357k
    first_element_ = false;
57
357k
  }
void rocksdb::JSONWriter::AddValue<long long>(long long const&)
Line
Count
Source
47
41.6k
  void AddValue(const T& value) {
48
41.6k
    assert(state_ == kExpectValue || state_ == kInArray);
49
41.6k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
41.6k
    stream_ << value;
53
41.6k
    if (state_ != kInArray) {
54
41.6k
      state_ = kExpectKey;
55
41.6k
    }
56
41.6k
    first_element_ = false;
57
41.6k
  }
void rocksdb::JSONWriter::AddValue<int>(int const&)
Line
Count
Source
47
69.1k
  void AddValue(const T& value) {
48
69.1k
    assert(state_ == kExpectValue || state_ == kInArray);
49
69.1k
    if (state_ == kInArray && !first_element_) {
50
20.4k
      stream_ << ", ";
51
20.4k
    }
52
69.1k
    stream_ << value;
53
69.1k
    if (state_ != kInArray) {
54
45.2k
      state_ = kExpectKey;
55
45.2k
    }
56
69.1k
    first_element_ = false;
57
69.1k
  }
void rocksdb::JSONWriter::AddValue<long>(long const&)
Line
Count
Source
47
3.37k
  void AddValue(const T& value) {
48
3.37k
    assert(state_ == kExpectValue || state_ == kInArray);
49
3.37k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
3.37k
    stream_ << value;
53
3.37k
    if (state_ != kInArray) {
54
3.37k
      state_ = kExpectKey;
55
3.37k
    }
56
3.37k
    first_element_ = false;
57
3.37k
  }
void rocksdb::JSONWriter::AddValue<unsigned long>(unsigned long const&)
Line
Count
Source
47
236k
  void AddValue(const T& value) {
48
236k
    assert(state_ == kExpectValue || state_ == kInArray);
49
236k
    if (state_ == kInArray && !first_element_) {
50
2.74k
      stream_ << ", ";
51
2.74k
    }
52
236k
    stream_ << value;
53
236k
    if (state_ != kInArray) {
54
218k
      state_ = kExpectKey;
55
218k
    }
56
236k
    first_element_ = false;
57
236k
  }
void rocksdb::JSONWriter::AddValue<unsigned int>(unsigned int const&)
Line
Count
Source
47
4.33k
  void AddValue(const T& value) {
48
4.33k
    assert(state_ == kExpectValue || state_ == kInArray);
49
4.33k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
4.33k
    stream_ << value;
53
4.33k
    if (state_ != kInArray) {
54
4.33k
      state_ = kExpectKey;
55
4.33k
    }
56
4.33k
    first_element_ = false;
57
4.33k
  }
Unexecuted instantiation: void rocksdb::JSONWriter::AddValue<bool>(bool const&)
void rocksdb::JSONWriter::AddValue<double>(double const&)
Line
Count
Source
47
2.16k
  void AddValue(const T& value) {
48
2.16k
    assert(state_ == kExpectValue || state_ == kInArray);
49
2.16k
    if (state_ == kInArray && !first_element_) {
50
0
      stream_ << ", ";
51
0
    }
52
2.16k
    stream_ << value;
53
2.16k
    if (state_ != kInArray) {
54
2.16k
      state_ = kExpectKey;
55
2.16k
    }
56
2.16k
    first_element_ = false;
57
2.16k
  }
58
59
18.6k
  void StartArray() {
60
18.6k
    assert(state_ == kExpectValue);
61
18.6k
    state_ = kInArray;
62
18.6k
    in_array_ = true;
63
18.6k
    stream_ << "[";
64
18.6k
    first_element_ = true;
65
18.6k
  }
66
67
18.6k
  void EndArray() {
68
18.6k
    assert(state_ == kInArray);
69
18.6k
    state_ = kExpectKey;
70
18.6k
    in_array_ = false;
71
18.6k
    stream_ << "]";
72
18.6k
    first_element_ = false;
73
18.6k
  }
74
75
6.06k
  void StartObject() {
76
6.06k
    assert(state_ == kExpectValue);
77
6.06k
    state_ = kExpectKey;
78
6.06k
    stream_ << "{";
79
6.06k
    first_element_ = true;
80
6.06k
  }
81
82
47.6k
  void EndObject() {
83
47.6k
    assert(state_ == kExpectKey);
84
47.6k
    stream_ << "}";
85
47.6k
    first_element_ = false;
86
47.6k
  }
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
41.6k
  std::string Get() const { return stream_.str(); }
104
105
606k
  JSONWriter& operator<<(const char* val) {
106
606k
    if (state_ == kExpectKey) {
107
473k
      AddKey(val);
108
473k
    } else {
109
133k
      AddValue(val);
110
133k
    }
111
606k
    return *this;
112
606k
  }
113
114
85.4k
  JSONWriter& operator<<(const std::string& val) {
115
85.4k
    return *this << val.c_str();
116
85.4k
  }
117
118
  template <typename T>
119
357k
  JSONWriter& operator<<(const T& val) {
120
357k
    assert(state_ != kExpectKey);
121
357k
    AddValue(val);
122
357k
    return *this;
123
357k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long long>(long long const&)
Line
Count
Source
119
41.6k
  JSONWriter& operator<<(const T& val) {
120
41.6k
    assert(state_ != kExpectKey);
121
41.6k
    AddValue(val);
122
41.6k
    return *this;
123
41.6k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <int>(int const&)
Line
Count
Source
119
69.1k
  JSONWriter& operator<<(const T& val) {
120
69.1k
    assert(state_ != kExpectKey);
121
69.1k
    AddValue(val);
122
69.1k
    return *this;
123
69.1k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <long>(long const&)
Line
Count
Source
119
3.37k
  JSONWriter& operator<<(const T& val) {
120
3.37k
    assert(state_ != kExpectKey);
121
3.37k
    AddValue(val);
122
3.37k
    return *this;
123
3.37k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned long>(unsigned long const&)
Line
Count
Source
119
236k
  JSONWriter& operator<<(const T& val) {
120
236k
    assert(state_ != kExpectKey);
121
236k
    AddValue(val);
122
236k
    return *this;
123
236k
  }
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
119
4.33k
  JSONWriter& operator<<(const T& val) {
120
4.33k
    assert(state_ != kExpectKey);
121
4.33k
    AddValue(val);
122
4.33k
    return *this;
123
4.33k
  }
Unexecuted instantiation: rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <bool>(bool const&)
rocksdb::JSONWriter& rocksdb::JSONWriter::operator<< <double>(double const&)
Line
Count
Source
119
2.16k
  JSONWriter& operator<<(const T& val) {
120
2.16k
    assert(state_ != kExpectKey);
121
2.16k
    AddValue(val);
122
2.16k
    return *this;
123
2.16k
  }
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
347k
  EventLoggerStream& operator<<(const T& val) {
142
347k
    MakeStream();
143
347k
    *json_writer_ << val;
144
347k
    return *this;
145
347k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [12]>(char const (&) [12])
Line
Count
Source
141
34.5k
  EventLoggerStream& operator<<(const T& val) {
142
34.5k
    MakeStream();
143
34.5k
    *json_writer_ << val;
144
34.5k
    return *this;
145
34.5k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long long>(long long const&)
Line
Count
Source
141
32.0k
  EventLoggerStream& operator<<(const T& val) {
142
32.0k
    MakeStream();
143
32.0k
    *json_writer_ << val;
144
32.0k
    return *this;
145
32.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [4]>(char const (&) [4])
Line
Count
Source
141
32.0k
  EventLoggerStream& operator<<(const T& val) {
142
32.0k
    MakeStream();
143
32.0k
    *json_writer_ << val;
144
32.0k
    return *this;
145
32.0k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <int>(int const&)
Line
Count
Source
141
59.6k
  EventLoggerStream& operator<<(const T& val) {
142
59.6k
    MakeStream();
143
59.6k
    *json_writer_ << val;
144
59.6k
    return *this;
145
59.6k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [6]>(char const (&) [6])
Line
Count
Source
141
35.4k
  EventLoggerStream& operator<<(const T& val) {
142
35.4k
    MakeStream();
143
35.4k
    *json_writer_ << val;
144
35.4k
    return *this;
145
35.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [13]>(char const (&) [13])
Line
Count
Source
141
5.87k
  EventLoggerStream& operator<<(const T& val) {
142
5.87k
    MakeStream();
143
5.87k
    *json_writer_ << val;
144
5.87k
    return *this;
145
5.87k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [18]>(char const (&) [18])
Line
Count
Source
141
20.9k
  EventLoggerStream& operator<<(const T& val) {
142
20.9k
    MakeStream();
143
20.9k
    *json_writer_ << val;
144
20.9k
    return *this;
145
20.9k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [17]>(char const (&) [17])
Line
Count
Source
141
15.4k
  EventLoggerStream& operator<<(const T& val) {
142
15.4k
    MakeStream();
143
15.4k
    *json_writer_ << val;
144
15.4k
    return *this;
145
15.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <long>(long const&)
Line
Count
Source
141
3.37k
  EventLoggerStream& operator<<(const T& val) {
142
3.37k
    MakeStream();
143
3.37k
    *json_writer_ << val;
144
3.37k
    return *this;
145
3.37k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [10]>(char const (&) [10])
Line
Count
Source
141
15.4k
  EventLoggerStream& operator<<(const T& val) {
142
15.4k
    MakeStream();
143
15.4k
    *json_writer_ << val;
144
15.4k
    return *this;
145
15.4k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned long>(unsigned long const&)
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
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [16]>(char const (&) [16])
Line
Count
Source
141
3.41k
  EventLoggerStream& operator<<(const T& val) {
142
3.41k
    MakeStream();
143
3.41k
    *json_writer_ << val;
144
3.41k
    return *this;
145
3.41k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [15]>(char const (&) [15])
Line
Count
Source
141
1.24k
  EventLoggerStream& operator<<(const T& val) {
142
1.24k
    MakeStream();
143
1.24k
    *json_writer_ << val;
144
1.24k
    return *this;
145
1.24k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [5]>(char const (&) [5])
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
6.58k
  EventLoggerStream& operator<<(const T& val) {
142
6.58k
    MakeStream();
143
6.58k
    *json_writer_ << val;
144
6.58k
    return *this;
145
6.58k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [19]>(char const (&) [19])
Line
Count
Source
141
9.90k
  EventLoggerStream& operator<<(const T& val) {
142
9.90k
    MakeStream();
143
9.90k
    *json_writer_ << val;
144
9.90k
    return *this;
145
9.90k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [20]>(char const (&) [20])
Line
Count
Source
141
3.41k
  EventLoggerStream& operator<<(const T& val) {
142
3.41k
    MakeStream();
143
3.41k
    *json_writer_ << val;
144
3.41k
    return *this;
145
3.41k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [22]>(char const (&) [22])
Line
Count
Source
141
2.16k
  EventLoggerStream& operator<<(const T& val) {
142
2.16k
    MakeStream();
143
2.16k
    *json_writer_ << val;
144
2.16k
    return *this;
145
2.16k
  }
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.49k
  EventLoggerStream& operator<<(const T& val) {
142
2.49k
    MakeStream();
143
2.49k
    *json_writer_ << val;
144
2.49k
    return *this;
145
2.49k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char const*>(char const* const&)
Line
Count
Source
141
3.41k
  EventLoggerStream& operator<<(const T& val) {
142
3.41k
    MakeStream();
143
3.41k
    *json_writer_ << val;
144
3.41k
    return *this;
145
3.41k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <unsigned int>(unsigned int const&)
Line
Count
Source
141
4.33k
  EventLoggerStream& operator<<(const T& val) {
142
4.33k
    MakeStream();
143
4.33k
    *json_writer_ << val;
144
4.33k
    return *this;
145
4.33k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [23]>(char const (&) [23])
Line
Count
Source
141
2.16k
  EventLoggerStream& operator<<(const T& val) {
142
2.16k
    MakeStream();
143
2.16k
    *json_writer_ << val;
144
2.16k
    return *this;
145
2.16k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [27]>(char const (&) [27])
Line
Count
Source
141
2.16k
  EventLoggerStream& operator<<(const T& val) {
142
2.16k
    MakeStream();
143
2.16k
    *json_writer_ << val;
144
2.16k
    return *this;
145
2.16k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [29]>(char const (&) [29])
Line
Count
Source
141
2.16k
  EventLoggerStream& operator<<(const T& val) {
142
2.16k
    MakeStream();
143
2.16k
    *json_writer_ << val;
144
2.16k
    return *this;
145
2.16k
  }
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [30]>(char const (&) [30])
Line
Count
Source
141
2.16k
  EventLoggerStream& operator<<(const T& val) {
142
2.16k
    MakeStream();
143
2.16k
    *json_writer_ << val;
144
2.16k
    return *this;
145
2.16k
  }
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [35]>(char const (&) [35])
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [32]>(char const (&) [32])
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [37]>(char const (&) [37])
Unexecuted instantiation: rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <char [40]>(char const (&) [40])
rocksdb::EventLoggerStream& rocksdb::EventLoggerStream::operator<< <double>(double const&)
Line
Count
Source
141
2.16k
  EventLoggerStream& operator<<(const T& val) {
142
2.16k
    MakeStream();
143
2.16k
    *json_writer_ << val;
144
2.16k
    return *this;
145
2.16k
  }
146
147
18.6k
  void StartArray() { json_writer_->StartArray(); }
148
18.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
347k
  void MakeStream() {
156
347k
    if (!json_writer_) {
157
32.0k
      json_writer_ = new JSONWriter();
158
32.0k
      *this << "time_micros"
159
32.0k
            << std::chrono::duration_cast<std::chrono::microseconds>(
160
32.0k
                   std::chrono::system_clock::now().time_since_epoch())
161
32.0k
                   .count();
162
32.0k
    }
163
347k
  }
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
41.6k
  static const char* Prefix() { return "EVENT_LOG_v1"; }
182
183
13.9k
  explicit EventLogger(Logger* logger) : logger_(logger) {}
184
27.4k
  EventLoggerStream Log() { return EventLoggerStream(logger_); }
185
1.21k
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer) {
186
1.21k
    return EventLoggerStream(log_buffer, LogBuffer::kDefaultMaxLogSize);
187
1.21k
  }
188
  EventLoggerStream LogToBuffer(LogBuffer* log_buffer,
189
3.41k
                                const size_t max_log_size) {
190
3.41k
    return EventLoggerStream(log_buffer, max_log_size);
191
3.41k
  }
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