From d3c1ad29a064d001da2435db56f159784ef54964 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 20 Aug 2018 12:43:31 +0300 Subject: [PATCH] Optimize logging for const char* messages --- include/spdlog/details/fmt_helper.h | 8 ++------ include/spdlog/details/logger_impl.h | 7 +++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index a94c7c5e..76a00b4c 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -22,12 +22,8 @@ inline void append_str(const std::string &str, fmt::basic_memory_buffer inline void append_c_str(const char *c_str, fmt::basic_memory_buffer &dest) { - char ch; - while ((ch = *c_str) != '\0') - { - dest.push_back(ch); - ++c_str; - } + auto len = std::char_traits::length(c_str); + dest.append(c_str, c_str + len); } template diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 0a7c5b6b..1451adc6 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -5,9 +5,12 @@ #pragma once +#include "spdlog/details/fmt_helper.h" + #include #include + // create logger with given name, sinks and the default pattern formatter // all other ctors will call this one template @@ -76,8 +79,8 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg) } try { - details::log_msg log_msg(&name_, lvl); - fmt::format_to(log_msg.raw, "{}", msg); + details::log_msg log_msg(&name_, lvl); + details::fmt_helper::append_c_str(msg, log_msg.raw); sink_it_(log_msg); } SPDLOG_CATCH_AND_HANDLE