]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | // |
6 | // Logger implementation that can be shared by all environments | |
7 | // where enough posix functionality is available. | |
8 | ||
9 | #pragma once | |
10 | #include <list> | |
f67539c2 | 11 | #include <queue> |
7c673cae FG |
12 | #include <string> |
13 | ||
f67539c2 | 14 | #include "file/filename.h" |
7c673cae FG |
15 | #include "port/port.h" |
16 | #include "port/util_logger.h" | |
f67539c2 | 17 | #include "test_util/sync_point.h" |
7c673cae | 18 | #include "util/mutexlock.h" |
7c673cae | 19 | |
f67539c2 | 20 | namespace ROCKSDB_NAMESPACE { |
1e59de90 TL |
21 | class FileSystem; |
22 | class SystemClock; | |
7c673cae FG |
23 | |
24 | #ifndef ROCKSDB_LITE | |
25 | // Rolls the log file by size and/or time | |
26 | class AutoRollLogger : public Logger { | |
27 | public: | |
1e59de90 TL |
28 | AutoRollLogger(const std::shared_ptr<FileSystem>& fs, |
29 | const std::shared_ptr<SystemClock>& clock, | |
30 | const std::string& dbname, const std::string& db_log_dir, | |
31 | size_t log_max_size, size_t log_file_time_to_roll, | |
32 | size_t keep_log_file_num, | |
f67539c2 | 33 | const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL); |
7c673cae FG |
34 | |
35 | using Logger::Logv; | |
36 | void Logv(const char* format, va_list ap) override; | |
37 | ||
38 | // Write a header entry to the log. All header information will be written | |
39 | // again every time the log rolls over. | |
40 | virtual void LogHeader(const char* format, va_list ap) override; | |
41 | ||
42 | // check if the logger has encountered any problem. | |
1e59de90 | 43 | Status GetStatus() { return status_; } |
7c673cae FG |
44 | |
45 | size_t GetLogFileSize() const override { | |
f67539c2 TL |
46 | if (!logger_) { |
47 | return 0; | |
48 | } | |
49 | ||
7c673cae FG |
50 | std::shared_ptr<Logger> logger; |
51 | { | |
52 | MutexLock l(&mutex_); | |
53 | // pin down the current logger_ instance before releasing the mutex. | |
54 | logger = logger_; | |
55 | } | |
56 | return logger->GetLogFileSize(); | |
57 | } | |
58 | ||
59 | void Flush() override { | |
60 | std::shared_ptr<Logger> logger; | |
61 | { | |
62 | MutexLock l(&mutex_); | |
63 | // pin down the current logger_ instance before releasing the mutex. | |
64 | logger = logger_; | |
65 | } | |
66 | TEST_SYNC_POINT("AutoRollLogger::Flush:PinnedLogger"); | |
67 | if (logger) { | |
68 | logger->Flush(); | |
69 | } | |
70 | } | |
71 | ||
72 | virtual ~AutoRollLogger() { | |
11fdf7f2 | 73 | if (logger_ && !closed_) { |
20effc67 | 74 | logger_->Close().PermitUncheckedError(); |
11fdf7f2 | 75 | } |
20effc67 | 76 | status_.PermitUncheckedError(); |
7c673cae FG |
77 | } |
78 | ||
f67539c2 TL |
79 | using Logger::GetInfoLogLevel; |
80 | InfoLogLevel GetInfoLogLevel() const override { | |
81 | MutexLock l(&mutex_); | |
82 | if (!logger_) { | |
83 | return Logger::GetInfoLogLevel(); | |
84 | } | |
85 | return logger_->GetInfoLogLevel(); | |
86 | } | |
87 | ||
88 | using Logger::SetInfoLogLevel; | |
89 | void SetInfoLogLevel(const InfoLogLevel log_level) override { | |
90 | MutexLock lock(&mutex_); | |
91 | Logger::SetInfoLogLevel(log_level); | |
92 | if (logger_) { | |
93 | logger_->SetInfoLogLevel(log_level); | |
94 | } | |
95 | } | |
96 | ||
7c673cae FG |
97 | void SetCallNowMicrosEveryNRecords(uint64_t call_NowMicros_every_N_records) { |
98 | call_NowMicros_every_N_records_ = call_NowMicros_every_N_records; | |
99 | } | |
100 | ||
101 | // Expose the log file path for testing purpose | |
1e59de90 | 102 | std::string TEST_log_fname() const { return log_fname_; } |
7c673cae FG |
103 | |
104 | uint64_t TEST_ctime() const { return ctime_; } | |
105 | ||
f67539c2 TL |
106 | Logger* TEST_inner_logger() const { return logger_.get(); } |
107 | ||
11fdf7f2 TL |
108 | protected: |
109 | // Implementation of Close() | |
110 | virtual Status CloseImpl() override { | |
111 | if (logger_) { | |
112 | return logger_->Close(); | |
113 | } else { | |
114 | return Status::OK(); | |
115 | } | |
116 | } | |
117 | ||
7c673cae FG |
118 | private: |
119 | bool LogExpired(); | |
120 | Status ResetLogger(); | |
121 | void RollLogFile(); | |
f67539c2 TL |
122 | // Read all names of old log files into old_log_files_ |
123 | // If there is any error, put the error code in status_ | |
124 | void GetExistingFiles(); | |
125 | // Delete old log files if it excceeds the limit. | |
126 | Status TrimOldLogFiles(); | |
7c673cae FG |
127 | // Log message to logger without rolling |
128 | void LogInternal(const char* format, ...); | |
129 | // Serialize the va_list to a string | |
130 | std::string ValistToString(const char* format, va_list args) const; | |
131 | // Write the logs marked as headers to the new log file | |
132 | void WriteHeaderInfo(); | |
1e59de90 | 133 | std::string log_fname_; // Current active info log's file name. |
7c673cae FG |
134 | std::string dbname_; |
135 | std::string db_log_dir_; | |
136 | std::string db_absolute_path_; | |
1e59de90 TL |
137 | std::shared_ptr<FileSystem> fs_; |
138 | std::shared_ptr<SystemClock> clock_; | |
7c673cae FG |
139 | std::shared_ptr<Logger> logger_; |
140 | // current status of the logger | |
141 | Status status_; | |
142 | const size_t kMaxLogFileSize; | |
143 | const size_t kLogFileTimeToRoll; | |
f67539c2 | 144 | const size_t kKeepLogFileNum; |
7c673cae FG |
145 | // header information |
146 | std::list<std::string> headers_; | |
f67539c2 TL |
147 | // List of all existing info log files. Used for enforcing number of |
148 | // info log files. | |
149 | // Full path is stored here. It consumes signifianctly more memory | |
150 | // than only storing file name. Can optimize if it causes a problem. | |
151 | std::queue<std::string> old_log_files_; | |
1e59de90 | 152 | // to avoid frequent clock->NowMicros() calls, we cached the current time |
7c673cae FG |
153 | uint64_t cached_now; |
154 | uint64_t ctime_; | |
155 | uint64_t cached_now_access_count; | |
156 | uint64_t call_NowMicros_every_N_records_; | |
1e59de90 TL |
157 | IOOptions io_options_; |
158 | IODebugContext io_context_; | |
7c673cae FG |
159 | mutable port::Mutex mutex_; |
160 | }; | |
161 | #endif // !ROCKSDB_LITE | |
162 | ||
163 | // Facade to craete logger automatically | |
164 | Status CreateLoggerFromOptions(const std::string& dbname, | |
165 | const DBOptions& options, | |
166 | std::shared_ptr<Logger>* logger); | |
167 | ||
f67539c2 | 168 | } // namespace ROCKSDB_NAMESPACE |