/src/rocksdb/logging/log_buffer.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 <ctime> |
9 | | |
10 | | #include "memory/arena.h" |
11 | | #include "port/sys_time.h" |
12 | | #include "rocksdb/env.h" |
13 | | #include "util/autovector.h" |
14 | | |
15 | | namespace ROCKSDB_NAMESPACE { |
16 | | |
17 | | class Logger; |
18 | | |
19 | | // A class to buffer info log entries and flush them in the end. |
20 | | class LogBuffer { |
21 | | public: |
22 | | // log_level: the log level for all the logs |
23 | | // info_log: logger to write the logs to |
24 | | LogBuffer(const InfoLogLevel log_level, Logger* info_log); |
25 | | |
26 | | // Add a log entry to the buffer. Use default max_log_size. |
27 | | // max_log_size indicates maximize log size, including some metadata. |
28 | | void AddLogToBuffer(size_t max_log_size, const char* format, va_list ap); |
29 | | |
30 | 1.44k | size_t IsEmpty() const { return logs_.empty(); } |
31 | | |
32 | | // Flush all buffered log to the info log. |
33 | | void FlushBufferToLog(); |
34 | | static const size_t kDefaultMaxLogSize = 512; |
35 | | |
36 | | private: |
37 | | // One log entry with its timestamp |
38 | | struct BufferedLog { |
39 | | port::TimeVal now_tv; // Timestamp of the log |
40 | | char message[1]; // Beginning of log message |
41 | | }; |
42 | | |
43 | | const InfoLogLevel log_level_; |
44 | | Logger* info_log_; |
45 | | Arena arena_; |
46 | | autovector<BufferedLog*> logs_; |
47 | | }; |
48 | | |
49 | | // Add log to the LogBuffer for a delayed info logging. It can be used when |
50 | | // we want to add some logs inside a mutex. |
51 | | // max_log_size indicates maximize log size, including some metadata. |
52 | | void LogToBuffer(LogBuffer* log_buffer, size_t max_log_size, const char* format, |
53 | | ...); |
54 | | // Same as previous function, but with default max log size. |
55 | | void LogToBuffer(LogBuffer* log_buffer, const char* format, ...); |
56 | | |
57 | | } // namespace ROCKSDB_NAMESPACE |