From 0890d603e631a619aef04b99e0fea7ce52200975 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 17 Aug 2023 17:45:21 +0800 Subject: [PATCH] fix lock (#414) --- include/ylt/easylog/appender.hpp | 74 +++++++++++++++----------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/include/ylt/easylog/appender.hpp b/include/ylt/easylog/appender.hpp index ec36e8a22..d2c26fbe4 100644 --- a/include/ylt/easylog/appender.hpp +++ b/include/ylt/easylog/appender.hpp @@ -27,6 +27,10 @@ #include "ylt/util/concurrentqueue.h" namespace easylog { +struct empty_mutex { + void lock() {} + void unlock() {} +}; constexpr inline std::string_view BOM_STR = "\xEF\xBB\xBF"; constexpr char digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; @@ -114,7 +118,8 @@ class appender { if (stop_) { if (queue_.size_approx() > 0) { while (queue_.try_dequeue(record)) { - write_record(record); + enable_console_ ? write_record(record) + : write_record(record); } } } @@ -140,14 +145,22 @@ class appender { return {buf, last_len}; } + template + auto &get_mutex() { + if constexpr (sync) { + return mtx_; + } + else { + return empty_mtx_; + } + } + template void write_record(record_t &record) { + std::lock_guard guard(get_mutex()); if constexpr (sync == true) { - std::shared_lock guard(mtx_); if (max_files_ > 0 && file_size_ > max_file_size_ && static_cast(-1) != file_size_) { - guard.unlock(); - std::lock_guard lock{mtx_}; roll_log_files(); } } @@ -159,33 +172,31 @@ class appender { buf[32] = ' '; if constexpr (enable_console) { - if constexpr (sync) { - std::lock_guard guard(console_mtx_); - add_color(record.get_severity()); - } - else { - add_color(record.get_severity()); - } + add_color(record.get_severity()); } - write_str(std::string_view(buf, 33)); + std::string_view str(buf, 33); + + write_file(str); if constexpr (enable_console) { + std::cout << str; clean_color(record.get_severity()); } - write_str(get_tid_buf(record.get_tid())); - write_str(record.get_file_str()); - write_str(record.get_message()); + auto tid_str = get_tid_buf(record.get_tid()); + auto file_str = record.get_file_str(); + auto msg = record.get_message(); + + write_file(tid_str); + write_file(file_str); + write_file(msg); if constexpr (enable_console) { - if constexpr (sync) { - std::lock_guard guard(console_mtx_); - std::cout << std::flush; - } - else { - std::cout << std::flush; - } + std::cout << tid_str; + std::cout << file_str; + std::cout << msg; + std::cout << std::flush; } } @@ -304,7 +315,6 @@ class appender { if (file_size == 0) { if (file_.write(BOM_STR.data(), BOM_STR.size())) { file_size_ += BOM_STR.size(); - is_first_write_ = false; } } } @@ -346,11 +356,9 @@ class appender { } open_log_file(); - is_first_write_ = false; } - template - void write_str(std::string_view str) { + void write_file(std::string_view str) { if (has_init_) { if (file_.write(str.data(), str.size())) { if (flush_every_time_) { @@ -360,16 +368,6 @@ class appender { file_size_ += str.size(); } } - - if constexpr (enable_console) { - if constexpr (sync) { - std::lock_guard guard(console_mtx_); - std::cout << str; - } - else { - std::cout << str; - } - } } bool has_init_ = false; @@ -380,11 +378,9 @@ class appender { size_t file_size_ = 0; size_t max_file_size_ = 0; size_t max_files_ = 0; - bool is_first_write_ = true; - - std::mutex console_mtx_; std::shared_mutex mtx_; + empty_mutex empty_mtx_; std::ofstream file_; std::mutex que_mtx_;