Skip to content

Commit

Permalink
fix lock (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Aug 17, 2023
1 parent a8ead76 commit 0890d60
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions include/ylt/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand Down Expand Up @@ -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<false, true>(record)
: write_record<false, false>(record);
}
}
}
Expand All @@ -140,14 +145,22 @@ class appender {
return {buf, last_len};
}

template <bool sync>
auto &get_mutex() {
if constexpr (sync) {
return mtx_;
}
else {
return empty_mtx_;
}
}

template <bool sync = false, bool enable_console = false>
void write_record(record_t &record) {
std::lock_guard guard(get_mutex<sync>());
if constexpr (sync == true) {
std::shared_lock guard(mtx_);
if (max_files_ > 0 && file_size_ > max_file_size_ &&
static_cast<size_t>(-1) != file_size_) {
guard.unlock();
std::lock_guard lock{mtx_};
roll_log_files();
}
}
Expand All @@ -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<sync, enable_console>(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<sync, enable_console>(get_tid_buf(record.get_tid()));
write_str<sync, enable_console>(record.get_file_str());
write_str<sync, enable_console>(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;
}
}

Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -346,11 +356,9 @@ class appender {
}

open_log_file();
is_first_write_ = false;
}

template <bool sync, bool enable_console>
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_) {
Expand All @@ -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;
Expand All @@ -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_;
Expand Down

0 comments on commit 0890d60

Please sign in to comment.