Add CombinedLog.h.

This commit is contained in:
levlam 2021-05-17 16:53:35 +03:00
parent 13a21b4fe2
commit 135f9de4a9
4 changed files with 95 additions and 2 deletions

View File

@ -182,6 +182,7 @@ set(TDUTILS_SOURCE
td/utils/ChangesProcessor.h
td/utils/check.h
td/utils/Closure.h
td/utils/CombinedLog.h
td/utils/common.h
td/utils/ConcurrentHashTable.h
td/utils/Container.h

View File

@ -0,0 +1,78 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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)
//
#pragma once
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/Slice.h"
namespace td {
class CombinedLog : public LogInterface {
public:
void set_first(LogInterface *first) {
first_ = first;
}
void set_second(LogInterface *second) {
second_ = second;
}
void set_first_verbosity_level(int verbosity_level) {
first_verbosity_level_ = verbosity_level;
}
void set_second_verbosity_level(int verbosity_level) {
second_verbosity_level_ = verbosity_level;
}
int get_first_verbosity_level() const {
return first_verbosity_level_;
}
int get_second_verbosity_level() const {
return second_verbosity_level_;
}
private:
LogInterface *first_ = nullptr;
int first_verbosity_level_ = VERBOSITY_NAME(FATAL);
LogInterface *second_ = nullptr;
int second_verbosity_level_ = VERBOSITY_NAME(FATAL);
void do_append(int log_level, CSlice slice) final {
if (first_ && log_level <= first_verbosity_level_) {
first_->do_append(log_level, slice);
}
if (second_ && log_level <= second_verbosity_level_) {
second_->do_append(log_level, slice);
}
}
void rotate() final {
if (first_) {
first_->rotate();
}
if (second_) {
second_->rotate();
}
}
vector<string> get_file_paths() final {
vector<string> result;
if (first_) {
td::append(result, first_->get_file_paths());
}
if (second_) {
td::append(result, second_->get_file_paths());
}
return result;
}
};
} // namespace td

View File

@ -75,8 +75,8 @@ class TsFileLog : public LogInterface {
return PSTRING() << path_ << ".thread" << info->id << ".log";
}
void do_append(int log_level, CSlice cslice) final {
get_current_logger()->do_append(log_level, cslice);
void do_append(int log_level, CSlice slice) final {
get_current_logger()->do_append(log_level, slice);
}
void rotate() final {

View File

@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/benchmark.h"
#include "td/utils/CombinedLog.h"
#include "td/utils/FileLog.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
@ -96,6 +97,19 @@ TEST(Log, Bench) {
bench_log("MemoryLog", [] { return td::make_unique<td::MemoryLog<1 << 20>>(); });
bench_log("CombinedLogEmpty", [] { return td::make_unique<td::CombinedLog>(); });
bench_log("CombinedLogMemory", [] {
auto result = td::make_unique<td::CombinedLog>();
static td::NullLog null_log;
static td::MemoryLog<1 << 20> memory_log;
result->set_first(&null_log);
result->set_second(&memory_log);
result->set_first_verbosity_level(VERBOSITY_NAME(DEBUG));
result->set_second_verbosity_level(VERBOSITY_NAME(DEBUG));
return result;
});
bench_log("TsFileLog",
[] { return td::TsFileLog::create("tmplog", std::numeric_limits<td::int64>::max(), false).move_as_ok(); });