Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
robomics committed Nov 7, 2024
1 parent 66efea2 commit 6536566
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/hictk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <deque>
#include <memory>
#include <mutex>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
Expand All @@ -28,8 +29,8 @@ using namespace hictk::tools;

template <std::size_t CAPACITY>
class GlobalLogger {
// [2021-08-12 17:49:34.581] [info]: my log msg
static constexpr auto *_msg_pattern{"[%Y-%m-%d %T.%e] %^[%l]%$: %v"};
// [2021-08-12 17:49:34.581] [info]: my log msg
static constexpr std::string_view _msg_pattern{"[%Y-%m-%d %T.%e] %^[%l]%$: %v\0"};
using HictkLogMsg = std::pair<spdlog::level::level_enum, std::string>;
std::deque<HictkLogMsg> _msg_buffer{};

Expand All @@ -39,7 +40,7 @@ class GlobalLogger {

[[nodiscard]] static std::shared_ptr<spdlog::sinks::stderr_color_sink_mt> init_stderr_sink() {
auto stderr_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
stderr_sink->set_pattern(_msg_pattern);
stderr_sink->set_pattern(_msg_pattern.data());
stderr_sink->set_level(spdlog::level::debug);

return stderr_sink;
Expand All @@ -49,7 +50,7 @@ class GlobalLogger {
if constexpr (CAPACITY != 0 && SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN) {
auto callback_sink = std::make_shared<spdlog::sinks::callback_sink_mt>(
[this](const spdlog::details::log_msg &msg) noexcept { enqueue_msg(msg); });
callback_sink->set_pattern(_msg_pattern);
callback_sink->set_pattern(_msg_pattern.data());
callback_sink->set_level(spdlog::level::warn);

return callback_sink;
Expand All @@ -68,15 +69,15 @@ class GlobalLogger {
}

void enqueue_msg(const spdlog::details::log_msg &msg) noexcept {
if (msg.level < spdlog::level::warn) [[likely]] {
if (msg.level < spdlog::level::warn) {
return;
}

++_num_msg_enqueued;

try {
[[maybe_unused]] const std::scoped_lock lck(_mtx);
if (_msg_buffer.size() == CAPACITY) [[unlikely]] {
if (_msg_buffer.size() == CAPACITY) {
_msg_buffer.pop_front();
}
_msg_buffer.emplace_back(msg.level, std::string{msg.payload.begin(), msg.payload.end()});
Expand Down Expand Up @@ -122,10 +123,30 @@ class GlobalLogger {
}

GlobalLogger(const GlobalLogger &other) = delete;
GlobalLogger(GlobalLogger &&other) noexcept = default;
GlobalLogger(GlobalLogger &&other) noexcept
: _msg_buffer(std::move(other._msg_buffer)),
_num_msg_enqueued(other._num_msg_enqueued.load()),
_ok(other._ok.load()) {
other._num_msg_enqueued = 0;
other._ok = false;
}

GlobalLogger &operator=(const GlobalLogger &other) = delete;
GlobalLogger &operator=(GlobalLogger &&other) noexcept = default;
GlobalLogger &operator=(GlobalLogger &&other) noexcept {
if (this == &other) {
return *this;
}

[[maybe_unused]] const auto lck = std::scoped_lock(other._mtx);
_msg_buffer = std::move(other._msg_buffer);
_num_msg_enqueued = other._num_msg_enqueued.load();
_ok = other._ok.load();

other._num_msg_enqueued = 0;
other._ok = false;

return *this;
}

~GlobalLogger() noexcept {
if (!_ok) {
Expand Down Expand Up @@ -166,7 +187,7 @@ class GlobalLogger {
}
};

// NOLINTNEXTLINE(*-err58-cpp, *-avoid-non-const-global-variables)
// NOLINTNEXTLINE(*-err58-cpp, *-avoid-non-const-global-variables, *-avoid-magic-numbers)
static auto global_logger = std::make_unique<GlobalLogger<256>>();

static auto acquire_global_logger() noexcept { return std::move(global_logger); }
Expand Down

0 comments on commit 6536566

Please sign in to comment.