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