Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions include/ylt/easylog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

#include <ylt/util/b_stacktrace.h>

#include <memory>

#include "easylog/appender.hpp"

namespace easylog {
Expand Down Expand Up @@ -100,10 +102,10 @@ class logger {
bool flush_every_time,
std::chrono::milliseconds log_sample_interval = {},
std::chrono::milliseconds log_sample_duartion = {}) {
static appender appender(filename, async, enable_console, max_file_size,
max_files, flush_every_time);
appender_ =
std::make_unique<appender>(filename, async, enable_console,
max_file_size, max_files, flush_every_time);
async_ = async;
appender_ = &appender;
min_severity_ = min_severity;
enable_console_ = enable_console;
log_sample_interval_ = log_sample_interval;
Expand All @@ -128,7 +130,11 @@ class logger {
appenders_.emplace_back(std::move(fn));
}

void stop_async_log() { appender_->stop(); }
void stop_async_log() {
if (appender_) {
appender_->stop();
}
}

// set and get
void set_min_severity(Severity severity) { min_severity_ = severity; }
Expand Down Expand Up @@ -157,14 +163,13 @@ class logger {

private:
logger() {
static appender appender{};
appender.start_thread();
appender.enable_console(true);
appender_ = std::make_unique<appender>();
appender_->enable_console(true);
async_ = true;
appender_ = &appender;
}

logger(const logger &) = default;
logger(const logger &) = delete;
logger& operator=(const logger &) = delete;

void append_record(record_t record) { appender_->write(std::move(record)); }

Expand All @@ -190,7 +195,7 @@ class logger {
std::atomic<std::chrono::milliseconds> log_sample_interval_;
std::atomic<std::chrono::milliseconds> log_sample_duration_;
std::chrono::system_clock::time_point init_time_{};
appender *appender_ = nullptr;
std::unique_ptr<appender> appender_ = nullptr;
std::vector<std::function<void(record_t &record)>> appenders_;
inline static std::atomic<bool> has_destruct_ = false;
};
Expand Down
21 changes: 13 additions & 8 deletions include/ylt/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ class appender {

buf[0] = '[';
auto [ptr, ec] = std::to_chars(buf + 1, buf + 21, tid);
buf[22] = ']';
buf[23] = ' ';
last_tid = tid;
last_len = ptr - buf;
buf[last_len++] = ']';
Expand Down Expand Up @@ -288,27 +286,31 @@ class appender {

void write(record_t &&r) {
queue_.enqueue(std::move(r));
cnd_.notify_one();
if(queue_.size_approx() <= 1) {
cnd_.notify_one();
}
}

void flush() {
std::lock_guard guard(mtx_);
if (file_.is_open()) {
file_.flush();
file_.sync_with_stdio();
}
}

void stop() {
std::lock_guard guard(mtx_);
if (!write_thd_.joinable()) {
return;
}

if (stop_) {
return;
{
std::lock_guard guard(que_mtx_);
if (stop_) {
return;
}
stop_ = true;
}
stop_ = true;

cnd_.notify_one();
}

Expand Down Expand Up @@ -347,6 +349,9 @@ class appender {
file_size_ += BOM_STR.size();
}
}
else {
file_size_ = file_size;
}
}
}

Expand Down
Loading