/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/log_buffer.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef YB_ROCKSDB_UTIL_LOG_BUFFER_H |
22 | | #define YB_ROCKSDB_UTIL_LOG_BUFFER_H |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include "yb/rocksdb/env.h" |
27 | | #include "yb/rocksdb/util/arena.h" |
28 | | #include "yb/rocksdb/util/autovector.h" |
29 | | |
30 | 277k | #define LOG_TO_BUFFER(...) LogToBufferWithContext(__FILE__, __LINE__, ##__VA_ARGS__) |
31 | | |
32 | | namespace rocksdb { |
33 | | |
34 | | class Logger; |
35 | | |
36 | | // A class to buffer info log entries and flush them in the end. |
37 | | class LogBuffer { |
38 | | public: |
39 | | // log_level: the log level for all the logs |
40 | | // info_log: logger to write the logs to |
41 | | LogBuffer(const InfoLogLevel log_level, Logger* info_log); |
42 | | |
43 | | ~LogBuffer(); |
44 | | |
45 | | // Add a log entry to the buffer. Use default max_log_size. |
46 | | // max_log_size indicates maximize log size, including some metadata. |
47 | | void AddLogToBuffer( |
48 | | const char* file, |
49 | | const int line, |
50 | | size_t max_log_size, |
51 | | const char* format, |
52 | | va_list ap); |
53 | | |
54 | 153k | size_t IsEmpty() const { return logs_.empty(); } |
55 | | |
56 | | // Flush all buffered log to the info log. |
57 | | void FlushBufferToLog(); |
58 | | |
59 | 2 | static constexpr size_t HeaderSize() { |
60 | 2 | return offsetof(BufferedLog, message); |
61 | 2 | } |
62 | | private: |
63 | | // One log entry with its timestamp |
64 | | struct BufferedLog { |
65 | | const char* file_; |
66 | | int line_; |
67 | | struct timeval now_tv; // Timestamp of the log |
68 | | char message[1]; // Beginning of log message |
69 | | }; |
70 | | |
71 | | const InfoLogLevel log_level_; |
72 | | Logger* info_log_; |
73 | | Arena arena_; |
74 | | autovector<BufferedLog*> logs_; |
75 | | }; |
76 | | |
77 | | // Add log to the LogBuffer for a delayed info logging. It can be used when |
78 | | // we want to add some logs inside a mutex. |
79 | | // max_log_size indicates maximize log size, including some metadata. |
80 | | extern void LogToBufferWithContext( |
81 | | const char* file, |
82 | | const int line, |
83 | | LogBuffer* log_buffer, |
84 | | size_t max_log_size, |
85 | | const char* format, |
86 | | ...); |
87 | | // Same as previous function, but with default max log size. |
88 | | extern void LogToBufferWithContext( |
89 | | const char* file, |
90 | | const int line, |
91 | | LogBuffer* log_buffer, |
92 | | const char* format, |
93 | | ...); |
94 | | |
95 | | } // namespace rocksdb |
96 | | |
97 | | #endif // YB_ROCKSDB_UTIL_LOG_BUFFER_H |