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
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);
}
}
};

View File

@ -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<int64>(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");

View File

@ -26,7 +26,7 @@ class FileLog : public LogInterface {
Slice get_path() const;
vector<string> get_file_paths() override;
vector<string> 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<bool> want_rotate_{false};
void do_append(int log_level, CSlice slice) final;
void do_rotate();
};

View File

@ -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<uint32> pos_{0};
};

View File

@ -33,18 +33,6 @@ class TsFileLog : public LogInterface {
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:
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<string> get_file_paths() final {
vector<string> res;
for (auto &log : logs_) {
res.push_back(get_path(&log));
}
return res;
}
};
} // namespace detail

View File

@ -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;

View File

@ -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<string> 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<string> get_file_paths() override {
vector<string> 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();

View File

@ -106,12 +106,10 @@ TEST(Log, Bench) {
file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure();
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 {
ts_log_.append(slice, log_level);
}
std::vector<std::string> get_file_paths() override {
std::vector<std::string> 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<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 {
file_log_.append(slice, log_level);
}
std::vector<std::string> get_file_paths() override {
std::vector<std::string> get_file_paths() final {
return file_log_.get_file_paths();
}