From acb06ea9770f0f6e7667af3bd182e413a4400292 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 7 Apr 2015 19:57:27 +0300 Subject: [PATCH] Fixed thread id bug in async mode by passing thread id in log_msg struct --- include/spdlog/details/async_log_helper.h | 2 ++ include/spdlog/details/line_logger.h | 3 ++- include/spdlog/details/log_msg.h | 12 +++++++++--- include/spdlog/details/pattern_formatter_impl.h | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/async_log_helper.h b/include/spdlog/details/async_log_helper.h index 86acd362..4508297c 100644 --- a/include/spdlog/details/async_log_helper.h +++ b/include/spdlog/details/async_log_helper.h @@ -60,6 +60,7 @@ class async_log_helper level::level_enum level; log_clock::time_point time; std::string txt; + std::thread::id thread_id; async_msg() = default; ~async_msg() = default; @@ -99,6 +100,7 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT: msg.logger_name = logger_name; msg.level = level; msg.time = time; + msg.thread_id = thread_id; msg.raw << txt; } }; diff --git a/include/spdlog/details/line_logger.h b/include/spdlog/details/line_logger.h index fa616fcf..9afe5f11 100644 --- a/include/spdlog/details/line_logger.h +++ b/include/spdlog/details/line_logger.h @@ -26,7 +26,7 @@ #include #include "../common.h" #include "../logger.h" - +#include // Line logger class - aggregates operator<< calls to fast ostream // and logs upon destruction @@ -65,6 +65,7 @@ public: { _log_msg.logger_name = _callback_logger->name(); _log_msg.time = os::now(); + _log_msg.thread_id = std::this_thread::get_id(); _callback_logger->_log_msg(_log_msg); } } diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index d9fb39b0..5c4bda30 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -24,6 +24,7 @@ #pragma once +#include #include "../common.h" #include "./format.h" @@ -38,14 +39,16 @@ struct log_msg logger_name(), level(l), time(), + thread_id(), raw(), formatted() {} log_msg(const log_msg& other) : logger_name(other.logger_name), - level(other.level), - time(other.time) + level(other.level), + time(other.time), + thread_id(other.thread_id) { if (other.raw.size()) raw << fmt::BasicStringRef(other.raw.data(), other.raw.size()); @@ -57,6 +60,7 @@ struct log_msg logger_name(std::move(other.logger_name)), level(other.level), time(std::move(other.time)), + thread_id(other.thread_id), raw(std::move(other.raw)), formatted(std::move(other.formatted)) { @@ -71,6 +75,7 @@ struct log_msg logger_name = std::move(other.logger_name); level = other.level; time = std::move(other.time); + thread_id = other.thread_id; raw = std::move(other.raw); formatted = std::move(other.formatted); other.clear(); @@ -79,7 +84,7 @@ struct log_msg void clear() { - level = level::off; + level = level::off; raw.clear(); formatted.clear(); } @@ -87,6 +92,7 @@ struct log_msg std::string logger_name; level::level_enum level; log_clock::time_point time; + std::thread::id thread_id; fmt::MemoryWriter raw; fmt::MemoryWriter formatted; }; diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index cc9311c5..0bf2ec3a 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -354,7 +354,7 @@ class t_formatter :public flag_formatter { void format(details::log_msg& msg, const std::tm&) override { - msg.formatted << std::hash()(std::this_thread::get_id()); + msg.formatted << std::hash()(msg.thread_id); } };