diff --git a/src/hictk/main.cpp b/src/hictk/main.cpp index c1b8f8a7..73cfed95 100644 --- a/src/hictk/main.cpp +++ b/src/hictk/main.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -28,8 +29,8 @@ using namespace hictk::tools; template 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 const std::string _msg_pattern{"[%Y-%m-%d %T.%e] %^[%l]%$: %v"}; using HictkLogMsg = std::pair; std::deque _msg_buffer{}; @@ -39,7 +40,7 @@ class GlobalLogger { [[nodiscard]] static std::shared_ptr init_stderr_sink() { auto stderr_sink = std::make_shared(); - stderr_sink->set_pattern(_msg_pattern); + stderr_sink->set_pattern(_msg_pattern.c_str()); stderr_sink->set_level(spdlog::level::debug); return stderr_sink; @@ -49,7 +50,7 @@ class GlobalLogger { if constexpr (CAPACITY != 0 && SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN) { auto callback_sink = std::make_shared( [this](const spdlog::details::log_msg &msg) noexcept { enqueue_msg(msg); }); - callback_sink->set_pattern(_msg_pattern); + callback_sink->set_pattern(_msg_pattern.c_str()); callback_sink->set_level(spdlog::level::warn); return callback_sink; @@ -68,7 +69,7 @@ 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; } @@ -76,7 +77,7 @@ class GlobalLogger { 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()}); @@ -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) { @@ -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>(); static auto acquire_global_logger() noexcept { return std::move(global_logger); }