diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h index 019433aa..2dbe5aa4 100644 --- a/include/spdlog/details/log_msg_buffer.h +++ b/include/spdlog/details/log_msg_buffer.h @@ -9,56 +9,26 @@ namespace spdlog { namespace details { -// regular log_msgs hold string_views to stack data - so they cannot be stored for later use. -// this one can, since it contains and owns the payload buffer. +// extend log_msg with internal buffer to store its payload. +// this is needed since log_msg holds string_views that points to stack data. + struct log_msg_buffer : log_msg { - fmt::basic_memory_buffer loggername_buf; - fmt::basic_memory_buffer payload_buf; + fmt::basic_memory_buffer buffer; log_msg_buffer() = default; explicit log_msg_buffer(const log_msg &orig_msg) : log_msg(orig_msg) { - update_buffers(); + buffer.append(logger_name.begin(), logger_name.end()); + logger_name = string_view_t{buffer.data(), buffer.size()}; + + buffer.append(payload.begin(), payload.end()); + payload = string_view_t{logger_name.end(), payload.size()}; } - log_msg_buffer(const log_msg_buffer &other) - : log_msg(other) - { - update_buffers(); - } - - log_msg_buffer(const log_msg_buffer &&other) SPDLOG_NOEXCEPT - : log_msg(std::move(other)) - { - update_buffers(); - } - - log_msg_buffer &operator=(const log_msg_buffer &other) SPDLOG_NOEXCEPT - { - *static_cast(this) = other; - update_buffers(); - return *this; - } - - log_msg_buffer &operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT - { - *static_cast(this) = std::move(other); - update_buffers(); - return *this; - } - - void update_buffers() SPDLOG_NOEXCEPT - { - loggername_buf.clear(); - loggername_buf.append(logger_name.data(), logger_name.data() + logger_name.size()); - logger_name = string_view_t{loggername_buf.data(), loggername_buf.size()}; - - payload_buf.clear(); - payload_buf.append(payload.data(), payload.data() + payload.size()); - payload = string_view_t{payload_buf.data(), payload_buf.size()}; - } + log_msg_buffer(log_msg_buffer &&other) = default; + log_msg_buffer &operator=(log_msg_buffer &&other) = default; }; } // namespace details