From 13a21b4fe2597a48ba6fd593b9080e4d93579ca1 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 17 May 2021 16:18:19 +0300 Subject: [PATCH] Make LogInterface::append non-virtual. --- td/telegram/cli.cpp | 5 ++--- tdutils/td/utils/FileLog.cpp | 6 +----- tdutils/td/utils/FileLog.h | 8 ++++---- tdutils/td/utils/MemoryLog.h | 24 ++++++++++-------------- tdutils/td/utils/TsFileLog.cpp | 26 +++++++++++++------------- tdutils/td/utils/logging.cpp | 17 ++++++++++------- tdutils/td/utils/logging.h | 25 ++++++++++++++----------- tdutils/test/log.cpp | 16 ++++++---------- 8 files changed, 60 insertions(+), 67 deletions(-) diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index b6dfed246..519c60c89 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -189,8 +189,7 @@ static char **tg_cli_completion(const char *text, int start, int end) { #endif class CliLog : public LogInterface { - public: - void append(CSlice slice, int log_level) override { + void do_append(int log_level, CSlice slice) final { #ifdef USE_READLINE deactivate_readline(); SCOPE_EXIT { @@ -204,7 +203,7 @@ class CliLog : public LogInterface { TsCerr() << TC_GREEN << slice << TC_EMPTY; #endif } else { - default_log_interface->append(slice, log_level); + default_log_interface->do_append(log_level, slice); } } }; diff --git a/tdutils/td/utils/FileLog.cpp b/tdutils/td/utils/FileLog.cpp index 14f8211db..54e3fe8d4 100644 --- a/tdutils/td/utils/FileLog.cpp +++ b/tdutils/td/utils/FileLog.cpp @@ -70,8 +70,7 @@ bool FileLog::get_redirect_stderr() const { return redirect_stderr_; } -void FileLog::append(CSlice cslice, int log_level) { - Slice slice = cslice; +void FileLog::do_append(int log_level, CSlice slice) { while (!slice.empty()) { auto r_size = fd_.write(slice); if (r_size.is_error()) { @@ -81,9 +80,6 @@ void FileLog::append(CSlice cslice, int log_level) { size_ += static_cast(written); slice.remove_prefix(written); } - if (log_level == VERBOSITY_NAME(FATAL)) { - process_fatal_error(cslice); - } if (size_ > rotate_threshold_ || want_rotate_.load(std::memory_order_relaxed)) { auto status = rename(path_, PSLICE() << path_ << ".old"); diff --git a/tdutils/td/utils/FileLog.h b/tdutils/td/utils/FileLog.h index 728c873c7..a057fc7b3 100644 --- a/tdutils/td/utils/FileLog.h +++ b/tdutils/td/utils/FileLog.h @@ -26,7 +26,7 @@ class FileLog : public LogInterface { Slice get_path() const; - vector get_file_paths() override; + vector get_file_paths() final; void set_rotate_threshold(int64 rotate_threshold); @@ -34,9 +34,7 @@ class FileLog : public LogInterface { bool get_redirect_stderr() const; - void append(CSlice cslice, int log_level) override; - - void rotate() override; + void rotate() final; void lazy_rotate(); @@ -48,6 +46,8 @@ class FileLog : public LogInterface { bool redirect_stderr_ = false; std::atomic want_rotate_{false}; + void do_append(int log_level, CSlice slice) final; + void do_rotate(); }; diff --git a/tdutils/td/utils/MemoryLog.h b/tdutils/td/utils/MemoryLog.h index 0e1e34b19..06bc2508c 100644 --- a/tdutils/td/utils/MemoryLog.h +++ b/tdutils/td/utils/MemoryLog.h @@ -28,7 +28,16 @@ class MemoryLog : public LogInterface { std::memset(buffer_, ' ', sizeof(buffer_)); } - void append(CSlice new_slice, int log_level) override { + Slice get_buffer() const { + return Slice(buffer_, sizeof(buffer_)); + } + + size_t get_pos() const { + return pos_ & (buffer_size - 1); + } + + private: + void do_append(int log_level, CSlice new_slice) final { Slice slice = new_slice; slice.truncate(MAX_OUTPUT_SIZE); while (!slice.empty() && slice.back() == '\n') { @@ -61,21 +70,8 @@ class MemoryLog : public LogInterface { size_t printed = std::snprintf(&buffer_[start_pos + 1], MAGIC_SIZE - 1, "LOG:%08x: ", real_pos); CHECK(printed == MAGIC_SIZE - 2); buffer_[start_pos + MAGIC_SIZE - 1] = ' '; - - if (log_level == VERBOSITY_NAME(FATAL)) { - process_fatal_error(new_slice); - } } - Slice get_buffer() const { - return Slice(buffer_, sizeof(buffer_)); - } - - size_t get_pos() const { - return pos_ & (buffer_size - 1); - } - - private: char buffer_[buffer_size]; std::atomic pos_{0}; }; diff --git a/tdutils/td/utils/TsFileLog.cpp b/tdutils/td/utils/TsFileLog.cpp index ecd72df75..f7be661b1 100644 --- a/tdutils/td/utils/TsFileLog.cpp +++ b/tdutils/td/utils/TsFileLog.cpp @@ -33,18 +33,6 @@ class TsFileLog : public LogInterface { return init_info(&logs_[0]); } - vector get_file_paths() override { - vector res; - for (auto &log : logs_) { - res.push_back(get_path(&log)); - } - return res; - } - - void append(CSlice cslice, int log_level) override { - get_current_logger()->append(cslice, log_level); - } - private: struct Info { FileLog log; @@ -87,13 +75,25 @@ class TsFileLog : public LogInterface { return PSTRING() << path_ << ".thread" << info->id << ".log"; } - void rotate() override { + void do_append(int log_level, CSlice cslice) final { + get_current_logger()->do_append(log_level, cslice); + } + + void rotate() final { for (auto &info : logs_) { if (info.is_inited.load(std::memory_order_acquire)) { info.log.lazy_rotate(); } } } + + vector get_file_paths() final { + vector res; + for (auto &log : logs_) { + res.push_back(get_path(&log)); + } + return res; + } }; } // namespace detail diff --git a/tdutils/td/utils/logging.cpp b/tdutils/td/utils/logging.cpp index 64bbda507..6910ba5e5 100644 --- a/tdutils/td/utils/logging.cpp +++ b/tdutils/td/utils/logging.cpp @@ -32,6 +32,13 @@ namespace td { LogOptions log_options; +void LogInterface::append(int log_level, CSlice slice) { + do_append(log_level, slice); + if (log_level == VERBOSITY_NAME(FATAL)) { + process_fatal_error(slice); + } +} + TD_THREAD_LOCAL const char *Logger::tag_ = nullptr; TD_THREAD_LOCAL const char *Logger::tag2_ = nullptr; @@ -121,9 +128,9 @@ Logger::~Logger() { slice.back() = '\0'; slice = MutableCSlice(slice.begin(), slice.begin() + slice.size() - 1); } - log_.append(slice, log_level_); + log_.append(log_level_, slice); } else { - log_.append(as_cslice(), log_level_); + log_.append(log_level_, as_cslice()); } } @@ -180,8 +187,7 @@ void TsLog::exit_critical() { } class DefaultLog : public LogInterface { - public: - void append(CSlice slice, int log_level) override { + void do_append(int log_level, CSlice slice) final { #if TD_ANDROID switch (log_level) { case VERBOSITY_NAME(FATAL): @@ -259,9 +265,6 @@ class DefaultLog : public LogInterface { // TODO: color TsCerr() << slice; #endif - if (log_level == VERBOSITY_NAME(FATAL)) { - process_fatal_error(slice); - } } }; static DefaultLog default_log; diff --git a/tdutils/td/utils/logging.h b/tdutils/td/utils/logging.h index 28c970b2b..528186aa8 100644 --- a/tdutils/td/utils/logging.h +++ b/tdutils/td/utils/logging.h @@ -167,7 +167,7 @@ class LogInterface { LogInterface &operator=(LogInterface &&) = delete; virtual ~LogInterface() = default; - virtual void append(CSlice slice, int log_level) = 0; + void append(int log_level, CSlice slice); virtual void rotate() { } @@ -175,11 +175,12 @@ class LogInterface { virtual vector get_file_paths() { return {}; } + + virtual void do_append(int log_level, CSlice slice) = 0; }; class NullLog : public LogInterface { - public: - void append(CSlice /*slice*/, int /*log_level*/) override { + void do_append(int /*log_level*/, CSlice /*slice*/) final { } }; @@ -217,8 +218,9 @@ class TsCerr { }; class Logger { - public: static const size_t BUFFER_SIZE = 128 * 1024; + + public: Logger(LogInterface &log, const LogOptions &options, int log_level) : buffer_(StackAllocator::alloc(BUFFER_SIZE)) , log_(log) @@ -276,17 +278,12 @@ class TsLog : public LogInterface { log_ = log; exit_critical(); } - void append(CSlice slice, int level) override { - enter_critical(); - log_->append(slice, level); - exit_critical(); - } - void rotate() override { + void rotate() final { enter_critical(); log_->rotate(); exit_critical(); } - vector get_file_paths() override { + vector get_file_paths() final { enter_critical(); auto result = log_->get_file_paths(); exit_critical(); @@ -294,6 +291,12 @@ class TsLog : public LogInterface { } private: + void do_append(int log_level, CSlice slice) final { + enter_critical(); + log_->do_append(log_level, slice); + exit_critical(); + } + LogInterface *log_ = nullptr; std::atomic_flag lock_ = ATOMIC_FLAG_INIT; void enter_critical(); diff --git a/tdutils/test/log.cpp b/tdutils/test/log.cpp index 9444b6e8b..0d3b7d6d6 100644 --- a/tdutils/test/log.cpp +++ b/tdutils/test/log.cpp @@ -106,12 +106,10 @@ TEST(Log, Bench) { file_log_.init("tmplog", std::numeric_limits::max(), false).ensure(); ts_log_.init(&file_log_); } - ~FileLog() { + void do_append(int log_level, td::CSlice slice) final { + static_cast(ts_log_).do_append(log_level, slice); } - void append(td::CSlice slice, int log_level) override { - ts_log_.append(slice, log_level); - } - std::vector get_file_paths() override { + std::vector get_file_paths() final { return file_log_.get_file_paths(); } @@ -128,12 +126,10 @@ TEST(Log, Bench) { FileLog() { file_log_.init("tmplog", std::numeric_limits::max(), false).ensure(); } - ~FileLog() { + void do_append(int log_level, td::CSlice slice) final { + static_cast(file_log_).do_append(log_level, slice); } - void append(td::CSlice slice, int log_level) override { - file_log_.append(slice, log_level); - } - std::vector get_file_paths() override { + std::vector get_file_paths() final { return file_log_.get_file_paths(); }