Clean log_msg_buffer

This commit is contained in:
gabime 2019-08-23 00:16:13 +03:00
parent 0db4978899
commit 2848e51755

View File

@ -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<char, 40> loggername_buf;
fmt::basic_memory_buffer<char, 200> payload_buf;
fmt::basic_memory_buffer<char, 200> 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<log_msg *>(this) = other;
update_buffers();
return *this;
}
log_msg_buffer &operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT
{
*static_cast<log_msg *>(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