Major change to log_msg - contain only string_view instead of buffer

This commit is contained in:
gabime 2018-10-19 02:15:50 +03:00
parent abf4af2645
commit 6355e9895d
9 changed files with 71 additions and 100 deletions

View File

@ -13,20 +13,6 @@ namespace spdlog {
namespace details {
namespace fmt_helper {
template<size_t Buffer_Size>
inline void append_str(const std::string &str, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
auto *str_ptr = str.data();
dest.append(str_ptr, str_ptr + str.size());
}
template<size_t Buffer_Size>
inline void append_c_str(const char *c_str, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
auto len = std::char_traits<char>::length(c_str);
dest.append(c_str, c_str + len);
}
template<size_t Buffer_Size1, size_t Buffer_Size2>
inline void append_buf(const fmt::basic_memory_buffer<char, Buffer_Size1> &buf, fmt::basic_memory_buffer<char, Buffer_Size2> &dest)
{
@ -34,6 +20,13 @@ inline void append_buf(const fmt::basic_memory_buffer<char, Buffer_Size1> &buf,
dest.append(buf_ptr, buf_ptr + buf.size());
}
template<size_t Buffer_Size>
inline void append_string_view(fmt::string_view view, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
auto *buf_ptr = view.data();
dest.append(buf_ptr, buf_ptr + view.size());
}
template<typename T, size_t Buffer_Size>
inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
@ -41,17 +34,6 @@ inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
dest.append(i.data(), i.data() + i.size());
}
template<size_t Buffer_Size>
inline void append_msg(const details::log_msg &msg, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
auto *c_buf = msg.c_string.data();
if (c_buf != nullptr) {
dest.append(c_buf, c_buf + msg.c_string.size());
} else {
append_buf(msg.raw, dest);
}
}
template<size_t Buffer_Size>
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{

View File

@ -17,7 +17,7 @@ struct log_msg
{
log_msg() = default;
log_msg(const std::string *loggers_name, level::level_enum lvl)
log_msg(const std::string *loggers_name, level::level_enum lvl, fmt::string_view view)
: logger_name(loggers_name)
, level(lvl)
#ifndef SPDLOG_NO_DATETIME
@ -26,27 +26,31 @@ struct log_msg
#ifndef SPDLOG_NO_THREAD_ID
, thread_id(os::thread_id())
, payload(view)
#endif
{
}
log_msg(const log_msg &other) = delete;
log_msg(log_msg &&other) = delete;
log_msg &operator=(log_msg &&other) = delete;
log_msg(const log_msg &other) = default;
log_msg(log_msg &&other) = default;
log_msg &operator=(log_msg &&other) = default;
const std::string *logger_name{nullptr};
level::level_enum level {level::off};
level::level_enum level{level::off};
log_clock::time_point time;
size_t thread_id {0};
fmt::memory_buffer raw;
// if c_string.data() is not nullptr, c_string should be used as the message
// instead of raw above
fmt::string_view c_string;
size_t thread_id{0};
size_t msg_id;
// info about wrapping the formatted text with color (updated by pattern_formatter).
mutable size_t color_range_start{0};
mutable size_t color_range_end{0};
operator fmt::string_view() const SPDLOG_NOEXCEPT
{
return payload;
}
const fmt::string_view payload;
};
} // namespace details
} // namespace spdlog

View File

@ -63,28 +63,24 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
try
{
details::log_msg log_msg(&name_, lvl);
fmt::format_to(log_msg.raw, fmt, args...);
fmt::memory_buffer buf;
fmt::format_to(buf, fmt, args...);
details::log_msg log_msg(&name_, lvl, fmt::string_view(buf.data(), buf.size()));
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
{
logn(lvl, msg, std::char_traits<char>::length(msg));
}
inline void spdlog::logger::logn(level::level_enum lvl, const char *msg, size_t msg_len)
{
if (!should_log(lvl))
{
return;
}
try
{
details::log_msg log_msg(&name_, lvl);
log_msg.c_string = fmt::string_view(msg, msg_len);
details::log_msg log_msg(&name_, lvl, msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
@ -99,8 +95,9 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
}
try
{
details::log_msg log_msg(&name_, lvl);
fmt::format_to(log_msg.raw, "{}", msg);
fmt::memory_buffer buf;
fmt::format_to(buf, "{}", msg);
details::log_msg log_msg(&name_, lvl, fmt::string_view(buf.data(), buf.size()));
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE

View File

@ -38,7 +38,8 @@ class name_formatter : public flag_formatter
{
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
fmt_helper::append_str(*msg.logger_name, dest);
// fmt_helper::append_str(*msg.logger_name, dest);
fmt_helper::append_string_view(*msg.logger_name, dest);
}
};
@ -47,7 +48,8 @@ class level_formatter : public flag_formatter
{
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(level::to_c_str(msg.level), dest);
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
}
};
@ -56,7 +58,8 @@ class short_level_formatter : public flag_formatter
{
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(level::to_short_c_str(msg.level), dest);
// fmt_helper::append_string_view(level::to_short_c_str(msg.level), dest);
fmt_helper::append_string_view(level::to_short_c_str(msg.level), dest);
}
};
@ -80,7 +83,8 @@ class a_formatter : public flag_formatter
{
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(days[tm_time.tm_wday], dest);
// fmt_helper::append_string_view(days[tm_time.tm_wday], dest);
fmt_helper::append_string_view(days[tm_time.tm_wday], dest);
}
};
@ -90,7 +94,8 @@ class A_formatter : public flag_formatter
{
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(full_days[tm_time.tm_wday], dest);
// fmt_helper::append_string_view(full_days[tm_time.tm_wday], dest);
fmt_helper::append_string_view(full_days[tm_time.tm_wday], dest);
}
};
@ -100,7 +105,8 @@ class b_formatter : public flag_formatter
{
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(months[tm_time.tm_mon], dest);
// fmt_helper::append_string_view(months[tm_time.tm_mon], dest);
fmt_helper::append_string_view(months[tm_time.tm_mon], dest);
}
};
@ -111,7 +117,7 @@ class B_formatter : public flag_formatter
{
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(full_months[tm_time.tm_mon], dest);
fmt_helper::append_string_view(full_months[tm_time.tm_mon], dest);
}
};
@ -123,9 +129,9 @@ class c_formatter final : public flag_formatter
// fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday],
// months[tm_time.tm_mon], tm_time.tm_mday);
// date
fmt_helper::append_c_str(days[tm_time.tm_wday], dest);
fmt_helper::append_string_view(days[tm_time.tm_wday], dest);
dest.push_back(' ');
fmt_helper::append_c_str(months[tm_time.tm_mon], dest);
fmt_helper::append_string_view(months[tm_time.tm_mon], dest);
dest.push_back(' ');
fmt_helper::append_int(tm_time.tm_mday, dest);
dest.push_back(' ');
@ -272,7 +278,7 @@ class p_formatter final : public flag_formatter
{
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
{
fmt_helper::append_c_str(ampm(tm_time), dest);
fmt_helper::append_string_view(ampm(tm_time), dest);
}
};
@ -287,7 +293,7 @@ class r_formatter final : public flag_formatter
dest.push_back(':');
fmt_helper::pad2(tm_time.tm_sec, dest);
dest.push_back(' ');
fmt_helper::append_c_str(ampm(tm_time), dest);
fmt_helper::append_string_view(ampm(tm_time), dest);
}
};
@ -401,7 +407,7 @@ class v_formatter final : public flag_formatter
{
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
fmt_helper::append_msg(msg, dest);
fmt_helper::append_string_view(msg, dest);
}
};
@ -433,7 +439,7 @@ public:
}
void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override
{
fmt_helper::append_str(str_, dest);
fmt_helper::append_string_view(str_, dest);
}
private:
@ -511,7 +517,8 @@ class full_formatter final : public flag_formatter
if (!msg.logger_name->empty())
{
dest.push_back('[');
fmt_helper::append_str(*msg.logger_name, dest);
// fmt_helper::append_str(*msg.logger_name, dest);
fmt_helper::append_string_view(*msg.logger_name, dest);
dest.push_back(']');
dest.push_back(' ');
}
@ -520,11 +527,13 @@ class full_formatter final : public flag_formatter
dest.push_back('[');
// wrap the level name with color
msg.color_range_start = dest.size();
fmt_helper::append_c_str(level::to_c_str(msg.level), dest);
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
msg.color_range_end = dest.size();
dest.push_back(']');
dest.push_back(' ');
fmt_helper::append_msg(msg, dest);
// fmt_helper::append_string_view(msg.msg(), dest);
fmt_helper::append_string_view(msg, dest);
}
private:
@ -571,7 +580,8 @@ public:
f->format(msg, cached_tm_, dest);
}
// write eol
details::fmt_helper::append_c_str(eol_.c_str(), dest);
// details::fmt_helper::append_string_view(eol_.c_str(), dest);
details::fmt_helper::append_string_view(eol_.c_str(), dest);
}
private:

View File

@ -78,7 +78,7 @@ struct async_msg
, msg_id(m.msg_id)
, worker_ptr(std::move(worker))
{
fmt_helper::append_msg(m, raw);
fmt_helper::append_string_view(m, raw);
}
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
@ -92,16 +92,15 @@ struct async_msg
}
// copy into log_msg
void to_log_msg(log_msg &msg)
log_msg to_log_msg()
{
msg.logger_name = &worker_ptr->name();
msg.level = level;
log_msg msg(&worker_ptr->name(), level, fmt::string_view(raw.data(), raw.size()));
msg.time = time;
msg.thread_id = thread_id;
fmt_helper::append_buf(raw, msg.raw);
msg.msg_id = msg_id;
msg.color_range_start = 0;
msg.color_range_end = 0;
return msg;
}
};
@ -204,8 +203,7 @@ private:
{
case async_msg_type::log:
{
log_msg msg;
incoming_async_msg.to_log_msg(msg);
auto msg = incoming_async_msg.to_log_msg();
incoming_async_msg.worker_ptr->backend_log_(msg);
return true;
}

View File

@ -47,7 +47,7 @@ protected:
fmt::memory_buffer formatted;
if (use_raw_msg_)
{
details::fmt_helper::append_msg(msg, formatted);
details::fmt_helper::append_string_view(msg, formatted);
}
else
{

View File

@ -53,12 +53,7 @@ public:
protected:
void sink_it_(const details::log_msg &msg) override
{
if (msg.c_string.data() != nullptr)
{
::syslog(syslog_prio_from_level(msg), "%.*s", static_cast<int>(msg.c_string.size()), msg.c_string.data());
} else {
::syslog(syslog_prio_from_level(msg), "%s", fmt::to_string(msg.raw).c_str());
}
::syslog(syslog_prio_from_level(msg), "%s", fmt::to_string(msg).c_str());
}
void flush_() override {}

View File

@ -231,22 +231,3 @@ TEST_CASE("default logger API", "[default logger]")
spdlog::drop_all();
spdlog::set_pattern("%v");
}
TEST_CASE("C string", "[c_string]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
oss_logger.set_level(spdlog::level::debug);
oss_logger.set_pattern("*** %v");
const char *test_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit massa nunc";
oss_logger.log(spdlog::level::debug, test_string);
REQUIRE(oss.str() == "*** " + std::string(test_string) + std::string(spdlog::details::os::default_eol));
oss.str("");
oss_logger.logn(spdlog::level::debug, test_string, 11);
REQUIRE(oss.str() == "*** " + std::string(test_string, 11) + std::string(spdlog::details::os::default_eol));
}

View File

@ -60,9 +60,12 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
TEST_CASE("color range test1", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
fmt::format_to(msg.raw, "Hello");
fmt::memory_buffer buf;
fmt::format_to(buf, "Hello");
fmt::memory_buffer formatted;
std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, fmt::string_view(buf.data(), buf.size()));
formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 5);
@ -93,8 +96,9 @@ TEST_CASE("color range test3", "[pattern_formatter]")
TEST_CASE("color range test4", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
spdlog::details::log_msg msg;
fmt::format_to(msg.raw, "ignored");
std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "ignored");
fmt::memory_buffer formatted;
formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 2);