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.
141 lines
4.2 KiB
141 lines
4.2 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 years ago
|
// 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).
|
||
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>
|
||
6 years ago
|
#include <queue>
|
||
9 years ago
|
#include <string>
|
||
10 years ago
|
|
||
6 years ago
|
#include "file/filename.h"
|
||
12 years ago
|
#include "port/port.h"
|
||
10 years ago
|
#include "port/util_logger.h"
|
||
6 years ago
|
#include "test_util/sync_point.h"
|
||
6 years ago
|
#include "util/mutexlock.h"
|
||
12 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
12 years ago
|
|
||
8 years ago
|
#ifndef ROCKSDB_LITE
|
||
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,
|
||
6 years ago
|
size_t log_file_time_to_roll, size_t keep_log_file_num,
|
||
|
const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL);
|
||
12 years ago
|
|
||
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_;
|
||
|
}
|
||
|
|
||
9 years ago
|
size_t GetLogFileSize() const override {
|
||
|
std::shared_ptr<Logger> logger;
|
||
|
{
|
||
|
MutexLock l(&mutex_);
|
||
|
// pin down the current logger_ instance before releasing the mutex.
|
||
|
logger = logger_;
|
||
|
}
|
||
|
return logger->GetLogFileSize();
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
void Flush() override {
|
||
9 years ago
|
std::shared_ptr<Logger> logger;
|
||
|
{
|
||
|
MutexLock l(&mutex_);
|
||
|
// pin down the current logger_ instance before releasing the mutex.
|
||
|
logger = logger_;
|
||
|
}
|
||
9 years ago
|
TEST_SYNC_POINT("AutoRollLogger::Flush:PinnedLogger");
|
||
9 years ago
|
if (logger) {
|
||
|
logger->Flush();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
virtual ~AutoRollLogger() {
|
||
7 years ago
|
if (logger_ && !closed_) {
|
||
|
logger_->Close();
|
||
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
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_;
|
||
|
}
|
||
|
|
||
8 years ago
|
uint64_t TEST_ctime() const { return ctime_; }
|
||
|
|
||
7 years ago
|
protected:
|
||
|
// Implementation of Close()
|
||
|
virtual Status CloseImpl() override {
|
||
|
if (logger_) {
|
||
|
return logger_->Close();
|
||
|
} else {
|
||
|
return Status::OK();
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
private:
|
||
|
bool LogExpired();
|
||
|
Status ResetLogger();
|
||
|
void RollLogFile();
|
||
6 years ago
|
// 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();
|
||
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;
|
||
6 years ago
|
const size_t kKeepLogFileNum;
|
||
10 years ago
|
// header information
|
||
|
std::list<std::string> headers_;
|
||
6 years ago
|
// 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_;
|
||
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_;
|
||
9 years ago
|
mutable port::Mutex mutex_;
|
||
12 years ago
|
};
|
||
8 years ago
|
#endif // !ROCKSDB_LITE
|
||
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
|