2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2013-10-16 21:59:46 +00:00
|
|
|
// 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.
|
2017-04-28 00:50:56 +00:00
|
|
|
// This source code is also licensed under the GPLv2 license found in the
|
|
|
|
// COPYING file in the root directory of this source tree.
|
2013-02-05 03:42:40 +00:00
|
|
|
//
|
|
|
|
// Logger implementation that can be shared by all environments
|
|
|
|
// where enough posix functionality is available.
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2015-02-02 17:47:24 +00:00
|
|
|
#include <list>
|
2015-10-30 01:07:37 +00:00
|
|
|
#include <string>
|
2015-02-02 17:47:24 +00:00
|
|
|
|
2013-03-25 17:56:48 +00:00
|
|
|
#include "port/port.h"
|
2015-07-01 23:13:49 +00:00
|
|
|
#include "port/util_logger.h"
|
2017-04-04 01:27:24 +00:00
|
|
|
#include "util/filename.h"
|
2016-02-19 20:29:54 +00:00
|
|
|
#include "util/mutexlock.h"
|
2017-04-04 01:27:24 +00:00
|
|
|
#include "util/sync_point.h"
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2017-02-28 19:05:08 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
2013-02-05 03:42:40 +00: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 22:41:28 +00:00
|
|
|
const std::string& db_log_dir, size_t log_max_size,
|
|
|
|
size_t log_file_time_to_roll,
|
2014-04-10 22:27:42 +00:00
|
|
|
const InfoLogLevel log_level = InfoLogLevel::INFO_LEVEL)
|
2014-02-26 22:41:28 +00: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 03:42:40 +00:00
|
|
|
env->GetAbsolutePath(dbname, &db_absolute_path_);
|
|
|
|
log_fname_ = InfoLogFileName(dbname_, db_absolute_path_, db_log_dir_);
|
|
|
|
RollLogFile();
|
|
|
|
ResetLogger();
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:08:19 +00:00
|
|
|
using Logger::Logv;
|
2015-02-26 19:28:41 +00:00
|
|
|
void Logv(const char* format, va_list ap) override;
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2015-02-02 17:47:24 +00: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 03:42:40 +00:00
|
|
|
// check if the logger has encountered any problem.
|
|
|
|
Status GetStatus() {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
2016-02-17 20:06:45 +00:00
|
|
|
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();
|
|
|
|
}
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Flush() override {
|
2016-02-17 20:06:45 +00:00
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
// pin down the current logger_ instance before releasing the mutex.
|
|
|
|
logger = logger_;
|
|
|
|
}
|
2016-02-23 05:32:19 +00:00
|
|
|
TEST_SYNC_POINT("AutoRollLogger::Flush:PinnedLogger");
|
2016-02-17 20:06:45 +00:00
|
|
|
if (logger) {
|
|
|
|
logger->Flush();
|
2015-02-20 18:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 03:42:40 +00: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 19:59:39 +00:00
|
|
|
// Expose the log file path for testing purpose
|
|
|
|
std::string TEST_log_fname() const {
|
|
|
|
return log_fname_;
|
|
|
|
}
|
|
|
|
|
2017-04-03 18:24:39 +00:00
|
|
|
uint64_t TEST_ctime() const { return ctime_; }
|
|
|
|
|
2013-02-05 03:42:40 +00:00
|
|
|
private:
|
|
|
|
bool LogExpired();
|
|
|
|
Status ResetLogger();
|
|
|
|
void RollLogFile();
|
2015-02-02 17:47:24 +00: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 03:42:40 +00: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 17:47:24 +00:00
|
|
|
// header information
|
|
|
|
std::list<std::string> headers_;
|
2013-02-05 03:42:40 +00: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_;
|
2016-02-17 20:06:45 +00:00
|
|
|
mutable port::Mutex mutex_;
|
2013-02-05 03:42:40 +00:00
|
|
|
};
|
2017-02-28 19:05:08 +00:00
|
|
|
#endif // !ROCKSDB_LITE
|
2013-02-05 03:42:40 +00:00
|
|
|
|
|
|
|
// Facade to craete logger automatically
|
2015-10-30 01:07:37 +00:00
|
|
|
Status CreateLoggerFromOptions(const std::string& dbname,
|
|
|
|
const DBOptions& options,
|
|
|
|
std::shared_ptr<Logger>* logger);
|
2013-02-05 03:42:40 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|