2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same 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>
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
#include "db/filename.h"
|
2013-03-25 18:56:48 +01:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/posix_logger.h"
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// Rolls the log file by size and/or time
|
|
|
|
class AutoRollLogger : public Logger {
|
|
|
|
public:
|
|
|
|
AutoRollLogger(Env* env, const std::string& dbname,
|
2014-02-26 23:41:28 +01:00
|
|
|
const std::string& db_log_dir, size_t log_max_size,
|
|
|
|
size_t log_file_time_to_roll,
|
2014-04-11 00:27:42 +02:00
|
|
|
const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL)
|
2014-02-26 23:41:28 +01:00
|
|
|
: Logger(log_level),
|
|
|
|
dbname_(dbname),
|
|
|
|
db_log_dir_(db_log_dir),
|
|
|
|
env_(env),
|
|
|
|
status_(Status::OK()),
|
|
|
|
kMaxLogFileSize(log_max_size),
|
|
|
|
kLogFileTimeToRoll(log_file_time_to_roll),
|
|
|
|
cached_now(static_cast<uint64_t>(env_->NowMicros() * 1e-6)),
|
|
|
|
ctime_(cached_now),
|
|
|
|
cached_now_access_count(0),
|
|
|
|
call_NowMicros_every_N_records_(100),
|
|
|
|
mutex_() {
|
2013-02-05 04:42:40 +01:00
|
|
|
env->GetAbsolutePath(dbname, &db_absolute_path_);
|
|
|
|
log_fname_ = InfoLogFileName(dbname_, db_absolute_path_, db_log_dir_);
|
|
|
|
RollLogFile();
|
|
|
|
ResetLogger();
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
size_t GetLogFileSize() const override { return logger_->GetLogFileSize(); }
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
void Flush() override {
|
2015-02-20 19:58:29 +01:00
|
|
|
if (logger_) {
|
|
|
|
logger_->Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
virtual ~AutoRollLogger() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:42:40 +01:00
|
|
|
private:
|
|
|
|
bool LogExpired();
|
|
|
|
Status ResetLogger();
|
|
|
|
void RollLogFile();
|
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_;
|
|
|
|
Env* env_;
|
|
|
|
std::shared_ptr<Logger> logger_;
|
|
|
|
// current status of the logger
|
|
|
|
Status status_;
|
|
|
|
const size_t kMaxLogFileSize;
|
|
|
|
const size_t kLogFileTimeToRoll;
|
2015-02-02 18:47:24 +01:00
|
|
|
// header information
|
|
|
|
std::list<std::string> headers_;
|
2013-02-05 04:42:40 +01:00
|
|
|
// to avoid frequent env->NowMicros() calls, we cached the current time
|
|
|
|
uint64_t cached_now;
|
|
|
|
uint64_t ctime_;
|
|
|
|
uint64_t cached_now_access_count;
|
|
|
|
uint64_t call_NowMicros_every_N_records_;
|
2013-03-25 18:56:48 +01:00
|
|
|
port::Mutex mutex_;
|
2013-02-05 04:42:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Facade to craete logger automatically
|
|
|
|
Status CreateLoggerFromOptions(
|
|
|
|
const std::string& dbname,
|
|
|
|
const std::string& db_log_dir,
|
|
|
|
Env* env,
|
2014-02-05 01:31:18 +01:00
|
|
|
const DBOptions& options,
|
2013-02-05 04:42:40 +01:00
|
|
|
std::shared_ptr<Logger>* logger);
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|