From 33a42202c7b79065fb3346161bd3394e8407d36f Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 4 Jun 2019 16:10:49 +0300 Subject: [PATCH] Replace string pointer with string_view in log_msg --- bench/formatter-bench.cpp | 2 +- include/spdlog/details/log_msg-inl.h | 11 +++++----- include/spdlog/details/log_msg.h | 6 +++--- .../spdlog/details/pattern_formatter-inl.h | 12 +++++------ include/spdlog/details/thread_pool.h | 2 +- include/spdlog/logger-inl.h | 2 +- include/spdlog/logger.h | 8 ++++---- tests/test_pattern_formatter.cpp | 20 +++++++++---------- 8 files changed, 31 insertions(+), 32 deletions(-) diff --git a/bench/formatter-bench.cpp b/bench/formatter-bench.cpp index a4ddaa7b..b2cc0dcf 100644 --- a/bench/formatter-bench.cpp +++ b/bench/formatter-bench.cpp @@ -15,7 +15,7 @@ void bench_formatter(benchmark::State &state, std::string pattern) std::string logger_name = "logger-name"; 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) { diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h index 60ab3324..cb130532 100644 --- a/include/spdlog/details/log_msg-inl.h +++ b/include/spdlog/details/log_msg-inl.h @@ -12,9 +12,8 @@ namespace spdlog { namespace details { -SPDLOG_INLINE log_msg::log_msg( - spdlog::source_loc loc, const std::string *loggers_name, spdlog::level::level_enum lvl, spdlog::string_view_t view) - : logger_name(loggers_name) +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) + : logger_name(logger_name) , level(lvl) #ifndef SPDLOG_NO_DATETIME , time(os::now()) @@ -24,11 +23,11 @@ SPDLOG_INLINE log_msg::log_msg( , thread_id(os::thread_id()) #endif , 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) - : log_msg(source_loc{}, loggers_name, lvl, 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{}, logger_name, lvl, msg) {} } // namespace details diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index d4183c1e..f2976df8 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -10,11 +10,11 @@ namespace spdlog { namespace details { struct log_msg { - log_msg(source_loc loc, const std::string *loggers_name, level::level_enum lvl, string_view_t view); - log_msg(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(string_view_t logger_name, level::level_enum lvl, string_view_t msg); 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}; log_clock::time_point time; size_t thread_id{0}; diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 0a040115..99964030 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -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) {} @@ -99,12 +99,12 @@ public: { if (padinfo_.enabled()) { - scoped_pad p(*msg.logger_name, padinfo_, dest); - fmt_helper::append_string_view(*msg.logger_name, dest); + scoped_pad p(msg.logger_name, padinfo_, dest); + fmt_helper::append_string_view(msg.logger_name, dest); } 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 #ifndef SPDLOG_NO_NAME - if (!msg.logger_name->empty()) + if (msg.logger_name.size() > 0) { dest.push_back('['); // 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(' '); } diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 3923ffbe..d8e821aa 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -102,7 +102,7 @@ struct async_msg // copy into 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.thread_id = thread_id; msg.source = source; diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index d669c632..5a7143ce 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -21,7 +21,7 @@ SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, const char 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); } catch (const std::exception &ex) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index c309b976..f2732d28 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -71,7 +71,7 @@ public: { fmt::memory_buffer buf; 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); } catch (const std::exception &ex) @@ -145,7 +145,7 @@ public: } try { - details::log_msg log_msg(loc, &name_, lvl, msg); + details::log_msg log_msg(loc, name_, lvl, msg); sink_it_(log_msg); } catch (const std::exception &ex) @@ -170,7 +170,7 @@ public: { fmt::memory_buffer buf; 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); } catch (const std::exception &ex) @@ -238,7 +238,7 @@ public: fmt::format_to(wbuf, fmt, args...); fmt::memory_buffer 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); } catch (const std::exception &ex) diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index fb6f75da..43d2e547 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -65,7 +65,7 @@ TEST_CASE("color range test1", "[pattern_formatter]") fmt::format_to(buf, "Hello"); fmt::memory_buffer formatted; 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); REQUIRE(msg.color_range_start == 0); REQUIRE(msg.color_range_end == 5); @@ -76,7 +76,7 @@ TEST_CASE("color range test2", "[pattern_formatter]") { auto formatter = std::make_shared("%^%$", spdlog::pattern_time_type::local, "\n"); 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; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 0); @@ -88,7 +88,7 @@ TEST_CASE("color range test3", "[pattern_formatter]") { auto formatter = std::make_shared("%^***%$"); 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; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 0); @@ -99,7 +99,7 @@ TEST_CASE("color range test4", "[pattern_formatter]") { auto formatter = std::make_shared("XX%^YYY%$", spdlog::pattern_time_type::local, "\n"); 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; formatter->format(msg, formatted); @@ -112,7 +112,7 @@ TEST_CASE("color range test5", "[pattern_formatter]") { auto formatter = std::make_shared("**%^"); 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; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 2); @@ -123,7 +123,7 @@ TEST_CASE("color range test6", "[pattern_formatter]") { auto formatter = std::make_shared("**%$"); 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; formatter->format(msg, formatted); REQUIRE(msg.color_range_start == 0); @@ -200,7 +200,7 @@ TEST_CASE("clone-default-formatter", "[pattern_formatter]") auto formatter_1 = std::make_shared(); auto formatter_2 = formatter_1->clone(); 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_2; @@ -215,7 +215,7 @@ TEST_CASE("clone-default-formatter2", "[pattern_formatter]") auto formatter_1 = std::make_shared("%+"); auto formatter_2 = formatter_1->clone(); 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_2; @@ -230,7 +230,7 @@ TEST_CASE("clone-formatter", "[pattern_formatter]") auto formatter_1 = std::make_shared("%D %X [%] [%n] %v"); auto formatter_2 = formatter_1->clone(); 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_2; @@ -245,7 +245,7 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]") auto formatter_1 = std::make_shared("%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n"); auto formatter_2 = formatter_1->clone(); 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_2;