From 135f9de4a951d0dd8237793783ff60165339db36 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 17 May 2021 16:53:35 +0300 Subject: [PATCH] Add CombinedLog.h. --- tdutils/CMakeLists.txt | 1 + tdutils/td/utils/CombinedLog.h | 78 ++++++++++++++++++++++++++++++++++ tdutils/td/utils/TsFileLog.cpp | 4 +- tdutils/test/log.cpp | 14 ++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tdutils/td/utils/CombinedLog.h diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index f288f5d3c..4db85ab35 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -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 diff --git a/tdutils/td/utils/CombinedLog.h b/tdutils/td/utils/CombinedLog.h new file mode 100644 index 000000000..76619cf9d --- /dev/null +++ b/tdutils/td/utils/CombinedLog.h @@ -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 get_file_paths() final { + vector result; + if (first_) { + td::append(result, first_->get_file_paths()); + } + if (second_) { + td::append(result, second_->get_file_paths()); + } + return result; + } +}; + +} // namespace td diff --git a/tdutils/td/utils/TsFileLog.cpp b/tdutils/td/utils/TsFileLog.cpp index f7be661b1..c4095af1d 100644 --- a/tdutils/td/utils/TsFileLog.cpp +++ b/tdutils/td/utils/TsFileLog.cpp @@ -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 { diff --git a/tdutils/test/log.cpp b/tdutils/test/log.cpp index 0d3b7d6d6..b76f6c2bc 100644 --- a/tdutils/test/log.cpp +++ b/tdutils/test/log.cpp @@ -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>(); }); + bench_log("CombinedLogEmpty", [] { return td::make_unique(); }); + + bench_log("CombinedLogMemory", [] { + auto result = td::make_unique(); + 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::max(), false).move_as_ok(); });