2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-02-05 04:42:40 +01:00
|
|
|
//
|
|
|
|
// Logger implementation that can be shared by all environments
|
|
|
|
// where enough posix functionality is available.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2015-02-02 18:47:24 +01:00
|
|
|
#include <list>
|
2019-05-31 19:45:20 +02:00
|
|
|
#include <queue>
|
2015-10-30 02:07:37 +01:00
|
|
|
#include <string>
|
2015-02-02 18:47:24 +01:00
|
|
|
|
2019-05-30 05:44:08 +02:00
|
|
|
#include "file/filename.h"
|
2013-03-25 18:56:48 +01:00
|
|
|
#include "port/port.h"
|
2015-07-02 01:13:49 +02:00
|
|
|
#include "port/util_logger.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/sync_point.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "util/mutexlock.h"
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2021-01-26 07:07:26 +01:00
|
|
|
class FileSystem;
|
|
|
|
class SystemClock;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2017-02-28 20:05:08 +01:00
|
|
|
#ifndef ROCKSDB_LITE
|
2013-02-05 04:42:40 +01:00
|
|
|
// Rolls the log file by size and/or time
|
|
|
|
class AutoRollLogger : public Logger {
|
|
|
|
public:
|
2021-01-26 07:07:26 +01:00
|
|
|
AutoRollLogger(const std::shared_ptr<FileSystem>& fs,
|
|
|
|
const std::shared_ptr<SystemClock>& clock,
|
|
|
|
const std::string& dbname, const std::string& db_log_dir,
|
|
|
|
size_t log_max_size, size_t log_file_time_to_roll,
|
|
|
|
size_t keep_log_file_num,
|
2019-05-31 19:45:20 +02:00
|
|
|
const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL);
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2015-02-01 20:08:19 +01:00
|
|
|
using Logger::Logv;
|
2015-02-26 20:28:41 +01:00
|
|
|
void Logv(const char* format, va_list ap) override;
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2015-02-02 18:47:24 +01:00
|
|
|
// Write a header entry to the log. All header information will be written
|
|
|
|
// again every time the log rolls over.
|
|
|
|
virtual void LogHeader(const char* format, va_list ap) override;
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
// check if the logger has encountered any problem.
|
|
|
|
Status GetStatus() {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
2016-02-17 21:06:45 +01:00
|
|
|
size_t GetLogFileSize() const override {
|
2019-07-25 00:11:36 +02:00
|
|
|
if (!logger_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-17 21:06:45 +01:00
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
// pin down the current logger_ instance before releasing the mutex.
|
|
|
|
logger = logger_;
|
|
|
|
}
|
|
|
|
return logger->GetLogFileSize();
|
|
|
|
}
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
void Flush() override {
|
2016-02-17 21:06:45 +01:00
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
// pin down the current logger_ instance before releasing the mutex.
|
|
|
|
logger = logger_;
|
|
|
|
}
|
2016-02-23 06:32:19 +01:00
|
|
|
TEST_SYNC_POINT("AutoRollLogger::Flush:PinnedLogger");
|
2016-02-17 21:06:45 +01:00
|
|
|
if (logger) {
|
|
|
|
logger->Flush();
|
2015-02-20 19:58:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
virtual ~AutoRollLogger() {
|
2018-02-23 22:50:02 +01:00
|
|
|
if (logger_ && !closed_) {
|
2020-09-17 00:45:30 +02:00
|
|
|
logger_->Close().PermitUncheckedError();
|
2018-02-23 22:50:02 +01:00
|
|
|
}
|
2020-09-17 00:45:30 +02:00
|
|
|
status_.PermitUncheckedError();
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 07:24:29 +01:00
|
|
|
using Logger::GetInfoLogLevel;
|
|
|
|
InfoLogLevel GetInfoLogLevel() const override {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (!logger_) {
|
|
|
|
return Logger::GetInfoLogLevel();
|
|
|
|
}
|
|
|
|
return logger_->GetInfoLogLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
using Logger::SetInfoLogLevel;
|
|
|
|
void SetInfoLogLevel(const InfoLogLevel log_level) override {
|
|
|
|
MutexLock lock(&mutex_);
|
|
|
|
Logger::SetInfoLogLevel(log_level);
|
|
|
|
if (logger_) {
|
|
|
|
logger_->SetInfoLogLevel(log_level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
void SetCallNowMicrosEveryNRecords(uint64_t call_NowMicros_every_N_records) {
|
|
|
|
call_NowMicros_every_N_records_ = call_NowMicros_every_N_records;
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:59:39 +01:00
|
|
|
// Expose the log file path for testing purpose
|
|
|
|
std::string TEST_log_fname() const {
|
|
|
|
return log_fname_;
|
|
|
|
}
|
|
|
|
|
2017-04-03 20:24:39 +02:00
|
|
|
uint64_t TEST_ctime() const { return ctime_; }
|
|
|
|
|
2020-02-11 07:24:29 +01:00
|
|
|
Logger* TEST_inner_logger() const { return logger_.get(); }
|
|
|
|
|
2018-02-23 22:50:02 +01:00
|
|
|
protected:
|
|
|
|
// Implementation of Close()
|
|
|
|
virtual Status CloseImpl() override {
|
|
|
|
if (logger_) {
|
|
|
|
return logger_->Close();
|
|
|
|
} else {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
private:
|
|
|
|
bool LogExpired();
|
|
|
|
Status ResetLogger();
|
|
|
|
void RollLogFile();
|
2019-05-31 19:45:20 +02:00
|
|
|
// Read all names of old log files into old_log_files_
|
|
|
|
// If there is any error, put the error code in status_
|
|
|
|
void GetExistingFiles();
|
|
|
|
// Delete old log files if it excceeds the limit.
|
|
|
|
Status TrimOldLogFiles();
|
2015-02-02 18:47:24 +01:00
|
|
|
// Log message to logger without rolling
|
|
|
|
void LogInternal(const char* format, ...);
|
|
|
|
// Serialize the va_list to a string
|
|
|
|
std::string ValistToString(const char* format, va_list args) const;
|
|
|
|
// Write the logs marked as headers to the new log file
|
|
|
|
void WriteHeaderInfo();
|
2013-02-05 04:42:40 +01:00
|
|
|
std::string log_fname_; // Current active info log's file name.
|
|
|
|
std::string dbname_;
|
|
|
|
std::string db_log_dir_;
|
|
|
|
std::string db_absolute_path_;
|
2021-01-26 07:07:26 +01:00
|
|
|
std::shared_ptr<FileSystem> fs_;
|
|
|
|
std::shared_ptr<SystemClock> clock_;
|
2013-02-05 04:42:40 +01:00
|
|
|
std::shared_ptr<Logger> logger_;
|
|
|
|
// current status of the logger
|
|
|
|
Status status_;
|
|
|
|
const size_t kMaxLogFileSize;
|
|
|
|
const size_t kLogFileTimeToRoll;
|
2019-05-31 19:45:20 +02:00
|
|
|
const size_t kKeepLogFileNum;
|
2015-02-02 18:47:24 +01:00
|
|
|
// header information
|
|
|
|
std::list<std::string> headers_;
|
2019-05-31 19:45:20 +02:00
|
|
|
// List of all existing info log files. Used for enforcing number of
|
|
|
|
// info log files.
|
|
|
|
// Full path is stored here. It consumes signifianctly more memory
|
|
|
|
// than only storing file name. Can optimize if it causes a problem.
|
|
|
|
std::queue<std::string> old_log_files_;
|
2021-01-26 07:07:26 +01:00
|
|
|
// to avoid frequent clock->NowMicros() calls, we cached the current time
|
2013-02-05 04:42:40 +01:00
|
|
|
uint64_t cached_now;
|
|
|
|
uint64_t ctime_;
|
|
|
|
uint64_t cached_now_access_count;
|
|
|
|
uint64_t call_NowMicros_every_N_records_;
|
2021-01-26 07:07:26 +01:00
|
|
|
IOOptions io_options_;
|
|
|
|
IODebugContext io_context_;
|
2016-02-17 21:06:45 +01:00
|
|
|
mutable port::Mutex mutex_;
|
2013-02-05 04:42:40 +01:00
|
|
|
};
|
2017-02-28 20:05:08 +01:00
|
|
|
#endif // !ROCKSDB_LITE
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Facade to craete logger automatically
|
2015-10-30 02:07:37 +01:00
|
|
|
Status CreateLoggerFromOptions(const std::string& dbname,
|
|
|
|
const DBOptions& options,
|
|
|
|
std::shared_ptr<Logger>* logger);
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|