Make LogInterface::append non-virtual.

This commit is contained in:
levlam 2021-05-17 16:18:19 +03:00
parent a6a4eb6616
commit 13a21b4fe2
8 changed files with 60 additions and 67 deletions

View File

@ -189,8 +189,7 @@ static char **tg_cli_completion(const char *text, int start, int end) {
#endif #endif
class CliLog : public LogInterface { class CliLog : public LogInterface {
public: void do_append(int log_level, CSlice slice) final {
void append(CSlice slice, int log_level) override {
#ifdef USE_READLINE #ifdef USE_READLINE
deactivate_readline(); deactivate_readline();
SCOPE_EXIT { SCOPE_EXIT {
@ -204,7 +203,7 @@ class CliLog : public LogInterface {
TsCerr() << TC_GREEN << slice << TC_EMPTY; TsCerr() << TC_GREEN << slice << TC_EMPTY;
#endif #endif
} else { } else {
default_log_interface->append(slice, log_level); default_log_interface->do_append(log_level, slice);
} }
} }
}; };

View File

@ -70,8 +70,7 @@ bool FileLog::get_redirect_stderr() const {
return redirect_stderr_; return redirect_stderr_;
} }
void FileLog::append(CSlice cslice, int log_level) { void FileLog::do_append(int log_level, CSlice slice) {
Slice slice = cslice;
while (!slice.empty()) { while (!slice.empty()) {
auto r_size = fd_.write(slice); auto r_size = fd_.write(slice);
if (r_size.is_error()) { if (r_size.is_error()) {
@ -81,9 +80,6 @@ void FileLog::append(CSlice cslice, int log_level) {
size_ += static_cast<int64>(written); size_ += static_cast<int64>(written);
slice.remove_prefix(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)) { if (size_ > rotate_threshold_ || want_rotate_.load(std::memory_order_relaxed)) {
auto status = rename(path_, PSLICE() << path_ << ".old"); auto status = rename(path_, PSLICE() << path_ << ".old");

View File

@ -26,7 +26,7 @@ class FileLog : public LogInterface {
Slice get_path() const; Slice get_path() const;
vector<string> get_file_paths() override; vector<string> get_file_paths() final;
void set_rotate_threshold(int64 rotate_threshold); void set_rotate_threshold(int64 rotate_threshold);
@ -34,9 +34,7 @@ class FileLog : public LogInterface {
bool get_redirect_stderr() const; bool get_redirect_stderr() const;
void append(CSlice cslice, int log_level) override; void rotate() final;
void rotate() override;
void lazy_rotate(); void lazy_rotate();
@ -48,6 +46,8 @@ class FileLog : public LogInterface {
bool redirect_stderr_ = false; bool redirect_stderr_ = false;
std::atomic<bool> want_rotate_{false}; std::atomic<bool> want_rotate_{false};
void do_append(int log_level, CSlice slice) final;
void do_rotate(); void do_rotate();
}; };

View File

@ -28,7 +28,16 @@ class MemoryLog : public LogInterface {
std::memset(buffer_, ' ', sizeof(buffer_)); 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 slice = new_slice;
slice.truncate(MAX_OUTPUT_SIZE); slice.truncate(MAX_OUTPUT_SIZE);
while (!slice.empty() && slice.back() == '\n') { 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); size_t printed = std::snprintf(&buffer_[start_pos + 1], MAGIC_SIZE - 1, "LOG:%08x: ", real_pos);
CHECK(printed == MAGIC_SIZE - 2); CHECK(printed == MAGIC_SIZE - 2);
buffer_[start_pos + MAGIC_SIZE - 1] = ' '; 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]; char buffer_[buffer_size];
std::atomic<uint32> pos_{0}; std::atomic<uint32> pos_{0};
}; };

View File

@ -33,18 +33,6 @@ class TsFileLog : public LogInterface {
return init_info(&logs_[0]); 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, int log_level) override {
get_current_logger()->append(cslice, log_level);
}
private: private:
struct Info { struct Info {
FileLog log; FileLog log;
@ -87,13 +75,25 @@ class TsFileLog : public LogInterface {
return PSTRING() << path_ << ".thread" << info->id << ".log"; 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_) { for (auto &info : logs_) {
if (info.is_inited.load(std::memory_order_acquire)) { if (info.is_inited.load(std::memory_order_acquire)) {
info.log.lazy_rotate(); info.log.lazy_rotate();
} }
} }
} }
vector<string> get_file_paths() final {
vector<string> res;
for (auto &log : logs_) {
res.push_back(get_path(&log));
}
return res;
}
}; };
} // namespace detail } // namespace detail

View File

@ -32,6 +32,13 @@ namespace td {
LogOptions log_options; 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::tag_ = nullptr;
TD_THREAD_LOCAL const char *Logger::tag2_ = nullptr; TD_THREAD_LOCAL const char *Logger::tag2_ = nullptr;
@ -121,9 +128,9 @@ Logger::~Logger() {
slice.back() = '\0'; slice.back() = '\0';
slice = MutableCSlice(slice.begin(), slice.begin() + slice.size() - 1); slice = MutableCSlice(slice.begin(), slice.begin() + slice.size() - 1);
} }
log_.append(slice, log_level_); log_.append(log_level_, slice);
} else { } 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 { class DefaultLog : public LogInterface {
public: void do_append(int log_level, CSlice slice) final {
void append(CSlice slice, int log_level) override {
#if TD_ANDROID #if TD_ANDROID
switch (log_level) { switch (log_level) {
case VERBOSITY_NAME(FATAL): case VERBOSITY_NAME(FATAL):
@ -259,9 +265,6 @@ class DefaultLog : public LogInterface {
// TODO: color // TODO: color
TsCerr() << slice; TsCerr() << slice;
#endif #endif
if (log_level == VERBOSITY_NAME(FATAL)) {
process_fatal_error(slice);
}
} }
}; };
static DefaultLog default_log; static DefaultLog default_log;

View File

@ -167,7 +167,7 @@ class LogInterface {
LogInterface &operator=(LogInterface &&) = delete; LogInterface &operator=(LogInterface &&) = delete;
virtual ~LogInterface() = default; virtual ~LogInterface() = default;
virtual void append(CSlice slice, int log_level) = 0; void append(int log_level, CSlice slice);
virtual void rotate() { virtual void rotate() {
} }
@ -175,11 +175,12 @@ class LogInterface {
virtual vector<string> get_file_paths() { virtual vector<string> get_file_paths() {
return {}; return {};
} }
virtual void do_append(int log_level, CSlice slice) = 0;
}; };
class NullLog : public LogInterface { class NullLog : public LogInterface {
public: void do_append(int /*log_level*/, CSlice /*slice*/) final {
void append(CSlice /*slice*/, int /*log_level*/) override {
} }
}; };
@ -217,8 +218,9 @@ class TsCerr {
}; };
class Logger { class Logger {
public:
static const size_t BUFFER_SIZE = 128 * 1024; static const size_t BUFFER_SIZE = 128 * 1024;
public:
Logger(LogInterface &log, const LogOptions &options, int log_level) Logger(LogInterface &log, const LogOptions &options, int log_level)
: buffer_(StackAllocator::alloc(BUFFER_SIZE)) : buffer_(StackAllocator::alloc(BUFFER_SIZE))
, log_(log) , log_(log)
@ -276,17 +278,12 @@ class TsLog : public LogInterface {
log_ = log; log_ = log;
exit_critical(); exit_critical();
} }
void append(CSlice slice, int level) override { void rotate() final {
enter_critical();
log_->append(slice, level);
exit_critical();
}
void rotate() override {
enter_critical(); enter_critical();
log_->rotate(); log_->rotate();
exit_critical(); exit_critical();
} }
vector<string> get_file_paths() override { vector<string> get_file_paths() final {
enter_critical(); enter_critical();
auto result = log_->get_file_paths(); auto result = log_->get_file_paths();
exit_critical(); exit_critical();
@ -294,6 +291,12 @@ class TsLog : public LogInterface {
} }
private: private:
void do_append(int log_level, CSlice slice) final {
enter_critical();
log_->do_append(log_level, slice);
exit_critical();
}
LogInterface *log_ = nullptr; LogInterface *log_ = nullptr;
std::atomic_flag lock_ = ATOMIC_FLAG_INIT; std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
void enter_critical(); void enter_critical();

View File

@ -106,12 +106,10 @@ TEST(Log, Bench) {
file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure(); file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure();
ts_log_.init(&file_log_); ts_log_.init(&file_log_);
} }
~FileLog() { void do_append(int log_level, td::CSlice slice) final {
static_cast<td::LogInterface &>(ts_log_).do_append(log_level, slice);
} }
void append(td::CSlice slice, int log_level) override { std::vector<std::string> get_file_paths() final {
ts_log_.append(slice, log_level);
}
std::vector<std::string> get_file_paths() override {
return file_log_.get_file_paths(); return file_log_.get_file_paths();
} }
@ -128,12 +126,10 @@ TEST(Log, Bench) {
FileLog() { FileLog() {
file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure(); file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure();
} }
~FileLog() { void do_append(int log_level, td::CSlice slice) final {
static_cast<td::LogInterface &>(file_log_).do_append(log_level, slice);
} }
void append(td::CSlice slice, int log_level) override { std::vector<std::string> get_file_paths() final {
file_log_.append(slice, log_level);
}
std::vector<std::string> get_file_paths() override {
return file_log_.get_file_paths(); return file_log_.get_file_paths();
} }