fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.4 KiB
113 lines
3.4 KiB
11 years ago
|
// 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.
|
||
12 years ago
|
//
|
||
|
// Logger implementation that can be shared by all environments
|
||
|
// where enough posix functionality is available.
|
||
|
|
||
11 years ago
|
#pragma once
|
||
10 years ago
|
#include <list>
|
||
9 years ago
|
#include <string>
|
||
10 years ago
|
|
||
12 years ago
|
#include "db/filename.h"
|
||
12 years ago
|
#include "port/port.h"
|
||
10 years ago
|
#include "port/util_logger.h"
|
||
12 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
12 years ago
|
|
||
|
// Rolls the log file by size and/or time
|
||
|
class AutoRollLogger : public Logger {
|
||
|
public:
|
||
|
AutoRollLogger(Env* env, const std::string& dbname,
|
||
11 years ago
|
const std::string& db_log_dir, size_t log_max_size,
|
||
|
size_t log_file_time_to_roll,
|
||
11 years ago
|
const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL)
|
||
11 years ago
|
: 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_() {
|
||
12 years ago
|
env->GetAbsolutePath(dbname, &db_absolute_path_);
|
||
|
log_fname_ = InfoLogFileName(dbname_, db_absolute_path_, db_log_dir_);
|
||
|
RollLogFile();
|
||
|
ResetLogger();
|
||
|
}
|
||
|
|
||
10 years ago
|
using Logger::Logv;
|
||
10 years ago
|
void Logv(const char* format, va_list ap) override;
|
||
12 years ago
|
|
||
10 years ago
|
// 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;
|
||
|
|
||
12 years ago
|
// check if the logger has encountered any problem.
|
||
|
Status GetStatus() {
|
||
|
return status_;
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t GetLogFileSize() const override { return logger_->GetLogFileSize(); }
|
||
12 years ago
|
|
||
10 years ago
|
void Flush() override {
|
||
10 years ago
|
if (logger_) {
|
||
|
logger_->Flush();
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
virtual ~AutoRollLogger() {
|
||
|
}
|
||
|
|
||
|
void SetCallNowMicrosEveryNRecords(uint64_t call_NowMicros_every_N_records) {
|
||
|
call_NowMicros_every_N_records_ = call_NowMicros_every_N_records;
|
||
|
}
|
||
|
|
||
10 years ago
|
// Expose the log file path for testing purpose
|
||
|
std::string TEST_log_fname() const {
|
||
|
return log_fname_;
|
||
|
}
|
||
|
|
||
12 years ago
|
private:
|
||
|
bool LogExpired();
|
||
|
Status ResetLogger();
|
||
|
void RollLogFile();
|
||
10 years ago
|
// 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();
|
||
12 years ago
|
|
||
|
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;
|
||
10 years ago
|
// header information
|
||
|
std::list<std::string> headers_;
|
||
12 years ago
|
// 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_;
|
||
12 years ago
|
port::Mutex mutex_;
|
||
12 years ago
|
};
|
||
|
|
||
|
// Facade to craete logger automatically
|
||
9 years ago
|
Status CreateLoggerFromOptions(const std::string& dbname,
|
||
|
const DBOptions& options,
|
||
|
std::shared_ptr<Logger>* logger);
|
||
12 years ago
|
|
||
11 years ago
|
} // namespace rocksdb
|