]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/logging/event_logger.h
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).
13 #include "logging/log_buffer.h"
14 #include "rocksdb/env.h"
16 namespace ROCKSDB_NAMESPACE
{
20 JSONWriter() : state_(kExpectKey
), first_element_(true), in_array_(false) {
24 void AddKey(const std::string
& key
) {
25 assert(state_
== kExpectKey
);
26 if (!first_element_
) {
29 stream_
<< "\"" << key
<< "\": ";
30 state_
= kExpectValue
;
31 first_element_
= false;
34 void AddValue(const char* value
) {
35 assert(state_
== kExpectValue
|| state_
== kInArray
);
36 if (state_
== kInArray
&& !first_element_
) {
39 stream_
<< "\"" << value
<< "\"";
40 if (state_
!= kInArray
) {
43 first_element_
= false;
47 void AddValue(const T
& value
) {
48 assert(state_
== kExpectValue
|| state_
== kInArray
);
49 if (state_
== kInArray
&& !first_element_
) {
53 if (state_
!= kInArray
) {
56 first_element_
= false;
60 assert(state_
== kExpectValue
);
64 first_element_
= true;
68 assert(state_
== kInArray
);
72 first_element_
= false;
76 assert(state_
== kExpectValue
);
79 first_element_
= true;
83 assert(state_
== kExpectKey
);
85 first_element_
= false;
88 void StartArrayedObject() {
89 assert(state_
== kInArray
&& in_array_
);
90 state_
= kExpectValue
;
91 if (!first_element_
) {
97 void EndArrayedObject() {
103 std::string
Get() const { return stream_
.str(); }
105 JSONWriter
& operator<<(const char* val
) {
106 if (state_
== kExpectKey
) {
114 JSONWriter
& operator<<(const std::string
& val
) {
115 return *this << val
.c_str();
118 template <typename T
>
119 JSONWriter
& operator<<(const T
& val
) {
120 assert(state_
!= kExpectKey
);
126 enum JSONWriterState
{
132 JSONWriterState state_
;
135 std::ostringstream stream_
;
138 class EventLoggerStream
{
140 template <typename T
>
141 EventLoggerStream
& operator<<(const T
& val
) {
143 *json_writer_
<< val
;
147 void StartArray() { json_writer_
->StartArray(); }
148 void EndArray() { json_writer_
->EndArray(); }
149 void StartObject() { json_writer_
->StartObject(); }
150 void EndObject() { json_writer_
->EndObject(); }
152 ~EventLoggerStream();
157 json_writer_
= new JSONWriter();
158 *this << "time_micros"
159 << std::chrono::duration_cast
<std::chrono::microseconds
>(
160 std::chrono::system_clock::now().time_since_epoch())
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_
172 JSONWriter
* json_writer_
;
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}
181 static const char* Prefix() { return "EVENT_LOG_v1"; }
183 explicit EventLogger(Logger
* logger
) : logger_(logger
) {}
184 EventLoggerStream
Log() { return EventLoggerStream(logger_
); }
185 EventLoggerStream
LogToBuffer(LogBuffer
* log_buffer
) {
186 return EventLoggerStream(log_buffer
, LogBuffer::kDefaultMaxLogSize
);
188 EventLoggerStream
LogToBuffer(LogBuffer
* log_buffer
,
189 const size_t max_log_size
) {
190 return EventLoggerStream(log_buffer
, max_log_size
);
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
);
202 } // namespace ROCKSDB_NAMESPACE