2019-08-12 13:45:57 +02:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2019-08-12 13:45:57 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/TsFileLog.h"
|
|
|
|
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/FileLog.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/logging.h"
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/port/thread_local.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
|
|
|
|
#include <array>
|
2020-06-26 01:24:13 +02:00
|
|
|
#include <atomic>
|
2019-08-14 02:13:34 +02:00
|
|
|
#include <limits>
|
2020-06-26 01:24:13 +02:00
|
|
|
#include <mutex>
|
2019-08-12 13:45:57 +02:00
|
|
|
|
|
|
|
namespace td {
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
namespace detail {
|
2020-06-24 13:47:36 +02:00
|
|
|
class TsFileLog : public LogInterface {
|
2019-08-12 13:45:57 +02:00
|
|
|
public:
|
2020-06-26 01:24:13 +02:00
|
|
|
Status init(string path, int64 rotate_threshold, bool redirect_stderr) {
|
2019-08-12 13:45:57 +02:00
|
|
|
path_ = std::move(path);
|
2020-06-24 13:47:36 +02:00
|
|
|
rotate_threshold_ = rotate_threshold;
|
|
|
|
redirect_stderr_ = redirect_stderr;
|
2020-06-26 01:24:13 +02:00
|
|
|
for (size_t i = 0; i < logs_.size(); i++) {
|
2019-08-12 13:45:57 +02:00
|
|
|
logs_[i].id = i;
|
|
|
|
}
|
|
|
|
return init_info(&logs_[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> get_file_paths() override {
|
|
|
|
vector<string> res;
|
|
|
|
for (auto &log : logs_) {
|
|
|
|
res.push_back(get_path(&log));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(CSlice cslice) override {
|
|
|
|
return append(cslice, -1);
|
|
|
|
}
|
|
|
|
void append(CSlice cslice, int log_level) override {
|
|
|
|
get_current_logger()->append(cslice, log_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Info {
|
|
|
|
FileLog log;
|
2020-06-24 13:47:36 +02:00
|
|
|
std::atomic<bool> is_inited{false};
|
2020-06-26 01:24:13 +02:00
|
|
|
size_t id;
|
2019-08-12 13:45:57 +02:00
|
|
|
};
|
2020-06-26 01:24:13 +02:00
|
|
|
|
|
|
|
static constexpr size_t MAX_THREAD_ID = 128;
|
|
|
|
int64 rotate_threshold_;
|
2020-06-24 13:47:36 +02:00
|
|
|
bool redirect_stderr_;
|
2019-08-12 13:45:57 +02:00
|
|
|
std::string path_;
|
|
|
|
std::array<Info, MAX_THREAD_ID> logs_;
|
2020-06-26 01:24:13 +02:00
|
|
|
std::mutex init_mutex_;
|
2019-08-12 13:45:57 +02:00
|
|
|
|
|
|
|
LogInterface *get_current_logger() {
|
|
|
|
auto *info = get_current_info();
|
2020-06-24 13:47:36 +02:00
|
|
|
if (!info->is_inited.load(std::memory_order_relaxed)) {
|
2020-06-26 01:24:13 +02:00
|
|
|
std::unique_lock<std::mutex> lock(init_mutex_);
|
|
|
|
if (!info->is_inited.load(std::memory_order_relaxed)) {
|
|
|
|
init_info(info).ensure();
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
|
|
|
return &info->log;
|
|
|
|
}
|
|
|
|
|
|
|
|
Info *get_current_info() {
|
|
|
|
return &logs_[get_thread_id()];
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init_info(Info *info) {
|
2020-06-24 13:47:36 +02:00
|
|
|
TRY_STATUS(info->log.init(get_path(info), std::numeric_limits<int64>::max(), info->id == 0 && redirect_stderr_));
|
2019-08-12 13:45:57 +02:00
|
|
|
info->is_inited = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
string get_path(const Info *info) const {
|
2019-08-12 13:45:57 +02:00
|
|
|
if (info->id == 0) {
|
|
|
|
return path_;
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
return PSTRING() << path_ << ".thread" << info->id << ".log";
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
2019-08-13 22:52:54 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
void rotate() override {
|
|
|
|
for (auto &info : logs_) {
|
2020-06-26 01:24:13 +02:00
|
|
|
if (info.is_inited.load(std::memory_order_acquire)) {
|
2020-06-24 13:47:36 +02:00
|
|
|
info.log.lazy_rotate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-08-12 13:45:57 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
Result<unique_ptr<LogInterface>> TsFileLog::create(string path, int64 rotate_threshold, bool redirect_stderr) {
|
|
|
|
auto res = make_unique<detail::TsFileLog>();
|
2020-06-24 13:47:36 +02:00
|
|
|
TRY_STATUS(res->init(path, rotate_threshold, redirect_stderr));
|
2019-08-12 13:45:57 +02:00
|
|
|
return std::move(res);
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
} // namespace td
|