Skip to content

Commit

Permalink
Async logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkvdb committed Oct 10, 2024
1 parent d31ee35 commit c8b641e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/infra/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Log

static void initialize_console_only(const std::string& name, Colored colored);
static void initialize(const std::string& name);
static void initialize_async(const std::string& name);
static void uninitialize();

static void add_file_sink(const fs::path& filePath);
Expand Down
2 changes: 1 addition & 1 deletion include/infra/progressinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class ProgressTracker
void throw_on_cancel() const
{
if (cancel_requested()) {
throw CancelRequested();
throw CancelRequested("Cancellation requested by user");
}
}

Expand Down
15 changes: 15 additions & 0 deletions log.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "infra/log.h"
#include "infra/string.h"

#include <spdlog/async.h>
#include <spdlog/details/log_msg.h>
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
Expand Down Expand Up @@ -83,6 +84,20 @@ void Log::initialize(const std::string& name)
_log->set_level(spdlog::level::warn);
}

void Log::initialize_async(const std::string& name)
{
if (_sinks.empty()) {
throw std::runtime_error("No sinks added before initializing the logging system");
}

spdlog::init_thread_pool(8192, 1);

_log = std::make_shared<spdlog::async_logger>(name, begin(_sinks), end(_sinks), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
_log->set_pattern("%^[%L] %v%$");
_log->set_level(spdlog::level::warn);

}

void Log::uninitialize()
{
_sinks.clear();
Expand Down

0 comments on commit c8b641e

Please sign in to comment.