Skip to content
Merged
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
77 changes: 58 additions & 19 deletions include/logit_cpp/logit/detail/TaskExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <atomic>
#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
#include <deque>
#include <mutex>
#include <emscripten/emscripten.h>
#else
#include <thread>
Expand Down Expand Up @@ -36,22 +37,40 @@ namespace logit { namespace detail {

void add_task(std::function<void()> task) {
if (!task) return;
if (m_max_queue_size > 0 && m_tasks.size() >= m_max_queue_size) {
switch (m_overflow_policy) {
case QueuePolicy::DropNewest:
++m_dropped_tasks;
return;
case QueuePolicy::DropOldest:
m_tasks.pop_front();
++m_dropped_tasks;
break;
case QueuePolicy::Block:
drain();
bool schedule = false;
for (;;) {
schedule = false;
{
std::lock_guard<std::mutex> lk(m_mutex);
if (m_max_queue_size > 0 && m_tasks.size() >= m_max_queue_size) {
switch (m_overflow_policy) {
case QueuePolicy::DropNewest:
++m_dropped_tasks;
return;
case QueuePolicy::DropOldest:
m_tasks.pop_front();
++m_dropped_tasks;
break;
case QueuePolicy::Block:
break; // handled after unlocking
}
if (m_overflow_policy == QueuePolicy::Block && m_tasks.size() >= m_max_queue_size) {
// fall through to drain outside lock
} else {
m_tasks.emplace_back(std::move(task));
schedule = !m_scheduled;
m_scheduled = m_scheduled || schedule;
break;
}
} else {
m_tasks.emplace_back(std::move(task));
schedule = !m_scheduled;
m_scheduled = m_scheduled || schedule;
break;
}
}
drain();
}
const bool schedule = m_tasks.empty();
m_tasks.push_back(std::move(task));
if (schedule) {
emscripten_async_call(&TaskExecutor::drain_thunk, this, 0);
}
Expand All @@ -60,30 +79,50 @@ namespace logit { namespace detail {
void wait() { drain(); }
void shutdown() { drain(); }

void set_max_queue_size(std::size_t size) { m_max_queue_size = size; }
void set_queue_policy(QueuePolicy policy) { m_overflow_policy = policy; }
void set_max_queue_size(std::size_t size) {
std::lock_guard<std::mutex> lk(m_mutex);
m_max_queue_size = size;
}
void set_queue_policy(QueuePolicy policy) {
std::lock_guard<std::mutex> lk(m_mutex);
m_overflow_policy = policy;
}

private:
TaskExecutor() : m_max_queue_size(0), m_overflow_policy(QueuePolicy::Block), m_dropped_tasks(0) {}
TaskExecutor()
: m_max_queue_size(0),
m_overflow_policy(QueuePolicy::Block),
m_dropped_tasks(0),
m_scheduled(false) {}
~TaskExecutor() = default;
TaskExecutor(const TaskExecutor&) = delete;
TaskExecutor& operator=(const TaskExecutor&) = delete;
TaskExecutor(TaskExecutor&&) = delete;
TaskExecutor& operator=(TaskExecutor&&) = delete;

std::deque<std::function<void()>> m_tasks;
std::mutex m_mutex;
std::size_t m_max_queue_size;
QueuePolicy m_overflow_policy;
std::atomic<std::size_t> m_dropped_tasks;
bool m_scheduled;

static void drain_thunk(void* arg) {
static_cast<TaskExecutor*>(arg)->drain();
}

void drain() {
while (!m_tasks.empty()) {
auto task = std::move(m_tasks.front());
m_tasks.pop_front();
for (;;) {
std::function<void()> task;
{
std::lock_guard<std::mutex> lk(m_mutex);
if (m_tasks.empty()) {
m_scheduled = false;
break;
}
task = std::move(m_tasks.front());
m_tasks.pop_front();
}
task();
}
}
Expand Down
4 changes: 3 additions & 1 deletion include/logit_cpp/logit/loggers/FileLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ namespace logit {
void wait() override {
if (!m_config.async) return;
detail::TaskExecutor::get_instance().wait();
std::lock_guard<std::mutex> lock(m_mutex);
if (m_file.is_open()) m_file.flush();
}

private:
Expand Down Expand Up @@ -333,7 +335,7 @@ namespace logit {
}
}
if (m_file.is_open()) {
m_file << message << std::endl;
m_file << message << '\n';
m_current_file_size += static_cast<uint64_t>(message.size() + 1);
}
remove_old_logs();
Expand Down
20 changes: 10 additions & 10 deletions include/logit_cpp/logit/loggers/UniqueFileLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <fstream>
#include <mutex>
#include <atomic>
#include <regex>
#include <queue>
#include <functional>
#include <sstream>
Expand All @@ -19,6 +18,7 @@
#include <random>
#include <algorithm>
#include <unordered_map>
#include <regex>

namespace logit {

Expand Down Expand Up @@ -396,7 +396,7 @@ namespace logit {
if (!fs::is_regular_file(entry.status())) continue;
std::string filename = entry.path().filename().string();
if (is_valid_log_filename(filename)) {
const int64_t file_ts = get_timestamp_from_filename(filename);
const int64_t file_ts = get_date_ts_from_filename(filename);
if (file_ts < threshold_ts) {
fs::remove(entry.path());
}
Expand All @@ -407,7 +407,7 @@ namespace logit {
for (const auto& file_path : file_list) {
std::string filename = get_file_name(file_path);
if (is_valid_log_filename(filename)) {
const int64_t file_ts = get_timestamp_from_filename(filename);
const int64_t file_ts = get_date_ts_from_filename(filename);
if (file_ts < threshold_ts) {
# if defined(_WIN32)
remove(utf8_to_ansi(file_path).c_str());
Expand All @@ -424,16 +424,16 @@ namespace logit {
/// \param filename The filename to check.
/// \return True if the filename matches the pattern, false otherwise.
bool is_valid_log_filename(const std::string& filename) const {
static const std::regex pattern(R"((\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-\d{3})-[a-zA-Z0-9]{1,}\.log)");
static const std::regex pattern(
R"(^(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-\d{3})-[-_A-Za-z0-9]+\.log(\.gz|\.zst)?$)");
return std::regex_match(filename, pattern);
}

/// \brief Extracts the timestamp from the filename.
/// \param filename The filename to extract the timestamp from.
/// \return The timestamp in milliseconds.
int64_t get_timestamp_from_filename(const std::string& filename) const {
std::string datetime_str = filename.substr(0, 10); // "YYYY-MM-DD"
return time_shield::ts(datetime_str);
/// \brief Extracts the date timestamp from the filename.
/// \param filename The filename to extract the date from.
/// \return The date timestamp.
int64_t get_date_ts_from_filename(const std::string& filename) const {
return time_shield::ts(filename.substr(0, 10));
}

/// \brief Gets the current timestamp in milliseconds.
Expand Down
Loading