Skip to content

Commit

Permalink
fix async console (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 13, 2023
1 parent 04f39f6 commit cb0298b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions include/ylt/easylog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class logger {
void init(Severity min_severity, bool async, bool enable_console,
const std::string &filename, size_t max_file_size, size_t max_files,
bool flush_every_time) {
static appender appender(filename, async, max_file_size, max_files,
flush_every_time);
static appender appender(filename, async, enable_console, max_file_size,
max_files, flush_every_time);
async_ = async;
appender_ = &appender;
min_severity_ = min_severity;
Expand Down
45 changes: 34 additions & 11 deletions include/ylt/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ inline char *get_time_str(const auto &now) {
class appender {
public:
appender() = default;
appender(const std::string &filename, bool async, size_t max_file_size,
size_t max_files, bool flush_every_time)
appender(const std::string &filename, bool async, bool enable_console,
size_t max_file_size, size_t max_files, bool flush_every_time)
: has_init_(true),
flush_every_time_(flush_every_time),
enable_console_(enable_console),
max_file_size_(max_file_size) {
filename_ = filename;
max_files_ = (std::min)(max_files, static_cast<size_t>(1000));
Expand All @@ -92,7 +93,8 @@ class appender {

record_t record;
if (queue_.try_dequeue(record)) {
write_record(record);
enable_console_ ? write_record<false, true>(record)
: write_record<false, false>(record);
}

if (queue_.size_approx() == 0) {
Expand Down Expand Up @@ -151,21 +153,33 @@ class appender {
buf[32] = ' ';

if constexpr (enable_console) {
add_color(record.get_severity());
if constexpr (sync) {
std::lock_guard guard(console_mtx_);
add_color(record.get_severity());
}
else {
add_color(record.get_severity());
}
}

write_str<enable_console>(std::string_view(buf, 33));
write_str<sync, enable_console>(std::string_view(buf, 33));

if constexpr (enable_console) {
clean_color(record.get_severity());
}

write_str<enable_console>(get_tid_buf(record.get_tid()));
write_str<enable_console>(record.get_file_str());
write_str<enable_console>(record.get_message());
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());

if constexpr (enable_console) {
std::cout << std::flush;
if constexpr (sync) {
std::lock_guard guard(console_mtx_);
std::cout << std::flush;
}
else {
std::cout << std::flush;
}
}
}

Expand Down Expand Up @@ -281,7 +295,7 @@ class appender {
is_first_write_ = false;
}

template <bool enable_console>
template <bool sync, bool enable_console>
void write_str(std::string_view str) {
if (has_init_) {
if (file_.write(str.data(), str.size())) {
Expand All @@ -294,19 +308,28 @@ class appender {
}

if constexpr (enable_console) {
std::cout << str;
if constexpr (sync) {
std::lock_guard guard(console_mtx_);
std::cout << str;
}
else {
std::cout << str;
}
}
}

bool has_init_ = false;
std::string filename_;

bool enable_console_ = false;
bool flush_every_time_;
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_;
std::ofstream file_;

Expand Down

0 comments on commit cb0298b

Please sign in to comment.