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
|
|
|
#include "util/auto_roll_logger.h"
|
2013-03-25 18:56:48 +01:00
|
|
|
#include "util/mutexlock.h"
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2013-02-05 04:42:40 +01:00
|
|
|
|
|
|
|
// -- AutoRollLogger
|
|
|
|
Status AutoRollLogger::ResetLogger() {
|
|
|
|
status_ = env_->NewLogger(log_fname_, &logger_);
|
|
|
|
|
|
|
|
if (!status_.ok()) {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (logger_->GetLogFileSize() ==
|
|
|
|
(size_t)Logger::DO_NOT_SUPPORT_GET_LOG_FILE_SIZE) {
|
|
|
|
status_ = Status::NotSupported(
|
|
|
|
"The underlying logger doesn't support GetLogFileSize()");
|
|
|
|
}
|
|
|
|
if (status_.ok()) {
|
|
|
|
cached_now = static_cast<uint64_t>(env_->NowMicros() * 1e-6);
|
|
|
|
ctime_ = cached_now;
|
|
|
|
cached_now_access_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoRollLogger::RollLogFile() {
|
|
|
|
std::string old_fname = OldInfoLogFileName(
|
|
|
|
dbname_, env_->NowMicros(), db_absolute_path_, db_log_dir_);
|
|
|
|
env_->RenameFile(log_fname_, old_fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoRollLogger::Logv(const char* format, va_list ap) {
|
|
|
|
assert(GetStatus().ok());
|
|
|
|
|
2013-03-25 18:56:48 +01:00
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if ((kLogFileTimeToRoll > 0 && LogExpired()) ||
|
|
|
|
(kMaxLogFileSize > 0 && logger_->GetLogFileSize() >= kMaxLogFileSize)) {
|
|
|
|
RollLogFile();
|
|
|
|
ResetLogger();
|
|
|
|
}
|
2013-02-05 04:42:40 +01:00
|
|
|
|
2013-03-25 18:56:48 +01:00
|
|
|
// pin down the current logger_ instance before releasing the mutex.
|
|
|
|
logger = logger_;
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
2013-03-25 18:56:48 +01:00
|
|
|
|
|
|
|
// Another thread could have put a new Logger instance into logger_ by now.
|
|
|
|
// However, since logger is still hanging on to the previous instance
|
|
|
|
// (reference count is not zero), we don't have to worry about it being
|
|
|
|
// deleted while we are accessing it.
|
|
|
|
// Note that logv itself is not mutex protected to allow maximum concurrency,
|
|
|
|
// as thread safety should have been handled by the underlying logger.
|
|
|
|
logger->Logv(format, ap);
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoRollLogger::LogExpired() {
|
|
|
|
if (cached_now_access_count >= call_NowMicros_every_N_records_) {
|
|
|
|
cached_now = static_cast<uint64_t>(env_->NowMicros() * 1e-6);
|
|
|
|
cached_now_access_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
++cached_now_access_count;
|
|
|
|
return cached_now >= ctime_ + kLogFileTimeToRoll;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CreateLoggerFromOptions(
|
|
|
|
const std::string& dbname,
|
|
|
|
const std::string& db_log_dir,
|
|
|
|
Env* env,
|
|
|
|
const Options& options,
|
|
|
|
std::shared_ptr<Logger>* logger) {
|
|
|
|
std::string db_absolute_path;
|
|
|
|
env->GetAbsolutePath(dbname, &db_absolute_path);
|
|
|
|
std::string fname = InfoLogFileName(dbname, db_absolute_path, db_log_dir);
|
|
|
|
|
|
|
|
// Currently we only support roll by time-to-roll and log size
|
|
|
|
if (options.log_file_time_to_roll > 0 || options.max_log_file_size > 0) {
|
|
|
|
AutoRollLogger* result = new AutoRollLogger(
|
|
|
|
env, dbname, db_log_dir,
|
|
|
|
options.max_log_file_size,
|
2014-03-05 03:10:14 +01:00
|
|
|
options.log_file_time_to_roll, options.info_log_level);
|
2013-02-05 04:42:40 +01:00
|
|
|
Status s = result->GetStatus();
|
|
|
|
if (!s.ok()) {
|
|
|
|
delete result;
|
|
|
|
} else {
|
|
|
|
logger->reset(result);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
} else {
|
|
|
|
// Open a log file in the same directory as the db
|
|
|
|
env->CreateDir(dbname); // In case it does not exist
|
|
|
|
env->RenameFile(fname, OldInfoLogFileName(dbname, env->NowMicros(),
|
|
|
|
db_absolute_path, db_log_dir));
|
2014-03-05 03:10:14 +01:00
|
|
|
auto s = env->NewLogger(fname, logger);
|
|
|
|
if (logger->get() != nullptr) {
|
|
|
|
(*logger)->SetInfoLogLevel(options.info_log_level);
|
|
|
|
}
|
|
|
|
return s;
|
2013-02-05 04:42:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|