Replace string pointer with string_view in log_msg

This commit is contained in:
gabime 2019-06-04 16:10:49 +03:00
parent efc358da9f
commit 33a42202c7
8 changed files with 31 additions and 32 deletions

View File

@ -15,7 +15,7 @@ void bench_formatter(benchmark::State &state, std::string pattern)
std::string logger_name = "logger-name"; std::string logger_name = "logger-name";
const char *text = "Hello. This is some message with length of 80 "; const char *text = "Hello. This is some message with length of 80 ";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, text); spdlog::details::log_msg msg(logger_name, spdlog::level::info, text);
for (auto _ : state) for (auto _ : state)
{ {

View File

@ -12,9 +12,8 @@
namespace spdlog { namespace spdlog {
namespace details { namespace details {
SPDLOG_INLINE log_msg::log_msg( SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
spdlog::source_loc loc, const std::string *loggers_name, spdlog::level::level_enum lvl, spdlog::string_view_t view) : logger_name(logger_name)
: logger_name(loggers_name)
, level(lvl) , level(lvl)
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
, time(os::now()) , time(os::now())
@ -24,11 +23,11 @@ SPDLOG_INLINE log_msg::log_msg(
, thread_id(os::thread_id()) , thread_id(os::thread_id())
#endif #endif
, source(loc) , source(loc)
, payload(view) , payload(msg)
{} {}
SPDLOG_INLINE log_msg::log_msg(const std::string *loggers_name, spdlog::level::level_enum lvl, spdlog::string_view_t view) SPDLOG_INLINE log_msg::log_msg(string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
: log_msg(source_loc{}, loggers_name, lvl, view) : log_msg(source_loc{}, logger_name, lvl, msg)
{} {}
} // namespace details } // namespace details

View File

@ -10,11 +10,11 @@ namespace spdlog {
namespace details { namespace details {
struct log_msg struct log_msg
{ {
log_msg(source_loc loc, const std::string *loggers_name, level::level_enum lvl, string_view_t view); log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg);
log_msg(const std::string *loggers_name, level::level_enum lvl, string_view_t view); log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg);
log_msg(const log_msg &other) = default; log_msg(const log_msg &other) = default;
const std::string *logger_name{nullptr}; const string_view_t logger_name;
level::level_enum level{level::off}; level::level_enum level{level::off};
log_clock::time_point time; log_clock::time_point time;
size_t thread_id{0}; size_t thread_id{0};

View File

@ -60,7 +60,7 @@ public:
} }
} }
scoped_pad(spdlog::string_view_t txt, padding_info &padinfo, fmt::memory_buffer &dest) scoped_pad(const spdlog::string_view_t &txt, padding_info &padinfo, fmt::memory_buffer &dest)
: scoped_pad(txt.size(), padinfo, dest) : scoped_pad(txt.size(), padinfo, dest)
{} {}
@ -99,12 +99,12 @@ public:
{ {
if (padinfo_.enabled()) if (padinfo_.enabled())
{ {
scoped_pad p(*msg.logger_name, padinfo_, dest); scoped_pad p(msg.logger_name, padinfo_, dest);
fmt_helper::append_string_view(*msg.logger_name, dest); fmt_helper::append_string_view(msg.logger_name, dest);
} }
else else
{ {
fmt_helper::append_string_view(*msg.logger_name, dest); fmt_helper::append_string_view(msg.logger_name, dest);
} }
} }
}; };
@ -921,11 +921,11 @@ public:
#endif #endif
#ifndef SPDLOG_NO_NAME #ifndef SPDLOG_NO_NAME
if (!msg.logger_name->empty()) if (msg.logger_name.size() > 0)
{ {
dest.push_back('['); 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); fmt_helper::append_string_view(msg.logger_name, dest);
dest.push_back(']'); dest.push_back(']');
dest.push_back(' '); dest.push_back(' ');
} }

View File

@ -102,7 +102,7 @@ struct async_msg
// copy into log_msg // copy into log_msg
log_msg to_log_msg() log_msg to_log_msg()
{ {
log_msg msg(&worker_ptr->name(), level, string_view_t(raw.data(), raw.size())); log_msg msg(string_view_t(worker_ptr->name()), level, string_view_t(raw.data(), raw.size()));
msg.time = time; msg.time = time;
msg.thread_id = thread_id; msg.thread_id = thread_id;
msg.source = source; msg.source = source;

View File

@ -21,7 +21,7 @@ SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, const char
try try
{ {
details::log_msg log_msg(loc, &name_, lvl, string_view_t(msg)); details::log_msg log_msg(loc, string_view_t(name_), lvl, string_view_t(msg));
sink_it_(log_msg); sink_it_(log_msg);
} }
catch (const std::exception &ex) catch (const std::exception &ex)

View File

@ -71,7 +71,7 @@ public:
{ {
fmt::memory_buffer buf; fmt::memory_buffer buf;
fmt::format_to(buf, fmt, args...); fmt::format_to(buf, fmt, args...);
details::log_msg log_msg(loc, &name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
sink_it_(log_msg); sink_it_(log_msg);
} }
catch (const std::exception &ex) catch (const std::exception &ex)
@ -145,7 +145,7 @@ public:
} }
try try
{ {
details::log_msg log_msg(loc, &name_, lvl, msg); details::log_msg log_msg(loc, name_, lvl, msg);
sink_it_(log_msg); sink_it_(log_msg);
} }
catch (const std::exception &ex) catch (const std::exception &ex)
@ -170,7 +170,7 @@ public:
{ {
fmt::memory_buffer buf; fmt::memory_buffer buf;
fmt::format_to(buf, "{}", msg); fmt::format_to(buf, "{}", msg);
details::log_msg log_msg(loc, &name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
sink_it_(log_msg); sink_it_(log_msg);
} }
catch (const std::exception &ex) catch (const std::exception &ex)
@ -238,7 +238,7 @@ public:
fmt::format_to(wbuf, fmt, args...); fmt::format_to(wbuf, fmt, args...);
fmt::memory_buffer buf; fmt::memory_buffer buf;
details::os::wbuf_to_utf8buf(wbuf, buf); details::os::wbuf_to_utf8buf(wbuf, buf);
details::log_msg log_msg(source, &name_, lvl, string_view_t(buf.data(), buf.size())); details::log_msg log_msg(source, name_, lvl, string_view_t(buf.data(), buf.size()));
sink_it_(log_msg); sink_it_(log_msg);
} }
catch (const std::exception &ex) catch (const std::exception &ex)

View File

@ -65,7 +65,7 @@ TEST_CASE("color range test1", "[pattern_formatter]")
fmt::format_to(buf, "Hello"); fmt::format_to(buf, "Hello");
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size())); spdlog::details::log_msg msg(logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size()));
formatter->format(msg, formatted); formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_start == 0);
REQUIRE(msg.color_range_end == 5); REQUIRE(msg.color_range_end == 5);
@ -76,7 +76,7 @@ TEST_CASE("color range test2", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, ""); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "");
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter->format(msg, formatted); formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_start == 0);
@ -88,7 +88,7 @@ TEST_CASE("color range test3", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$"); auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "ignored"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter->format(msg, formatted); formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_start == 0);
@ -99,7 +99,7 @@ TEST_CASE("color range test4", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "ignored"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter->format(msg, formatted); formatter->format(msg, formatted);
@ -112,7 +112,7 @@ TEST_CASE("color range test5", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^"); auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "ignored"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter->format(msg, formatted); formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 2); REQUIRE(msg.color_range_start == 2);
@ -123,7 +123,7 @@ TEST_CASE("color range test6", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$"); auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "ignored"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
fmt::memory_buffer formatted; fmt::memory_buffer formatted;
formatter->format(msg, formatted); formatter->format(msg, formatted);
REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_start == 0);
@ -200,7 +200,7 @@ TEST_CASE("clone-default-formatter", "[pattern_formatter]")
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>(); auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
auto formatter_2 = formatter_1->clone(); auto formatter_2 = formatter_1->clone();
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "some message"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
fmt::memory_buffer formatted_1; fmt::memory_buffer formatted_1;
fmt::memory_buffer formatted_2; fmt::memory_buffer formatted_2;
@ -215,7 +215,7 @@ TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%+"); auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%+");
auto formatter_2 = formatter_1->clone(); auto formatter_2 = formatter_1->clone();
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "some message"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
fmt::memory_buffer formatted_1; fmt::memory_buffer formatted_1;
fmt::memory_buffer formatted_2; fmt::memory_buffer formatted_2;
@ -230,7 +230,7 @@ TEST_CASE("clone-formatter", "[pattern_formatter]")
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v"); auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v");
auto formatter_2 = formatter_1->clone(); auto formatter_2 = formatter_1->clone();
std::string logger_name = "test"; std::string logger_name = "test";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "some message"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
fmt::memory_buffer formatted_1; fmt::memory_buffer formatted_1;
fmt::memory_buffer formatted_2; fmt::memory_buffer formatted_2;
@ -245,7 +245,7 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n"); auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n");
auto formatter_2 = formatter_1->clone(); auto formatter_2 = formatter_1->clone();
std::string logger_name = "test2"; std::string logger_name = "test2";
spdlog::details::log_msg msg(&logger_name, spdlog::level::info, "some message"); spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
fmt::memory_buffer formatted_1; fmt::memory_buffer formatted_1;
fmt::memory_buffer formatted_2; fmt::memory_buffer formatted_2;