From 576ca241ecaea6f357a13f022fb297976aa967ca Mon Sep 17 00:00:00 2001 From: Shauren Date: Sun, 23 Jul 2023 00:27:26 +0200 Subject: Core/Logging: Minor compile time reducing refactor of log message formatting --- src/common/Utilities/StringConvert.h | 4 +++- src/common/Utilities/StringFormat.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'src/common/Utilities') diff --git a/src/common/Utilities/StringConvert.h b/src/common/Utilities/StringConvert.h index 4ec3324fb33..0d11dbb6667 100644 --- a/src/common/Utilities/StringConvert.h +++ b/src/common/Utilities/StringConvert.h @@ -76,7 +76,9 @@ namespace Trinity::Impl::StringConvertImpl static std::string ToString(T val) { - std::string buf(20,'\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */ + using buffer_size = std::integral_constant; + + std::string buf(buffer_size::value,'\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */ char* const start = buf.data(); char* const end = (start + buf.length()); std::to_chars_result const res = std::to_chars(start, end, val); diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index b2248aff6f1..d34528a04df 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -25,6 +25,13 @@ namespace Trinity template using FormatString = fmt::format_string; + using FormatStringView = fmt::string_view; + + using FormatArgs = fmt::format_args; + + template + constexpr auto MakeFormatArgs(Args&&... args) { return fmt::make_format_args(args...); } + /// Default TC string format function. template inline std::string StringFormat(FormatString fmt, Args&&... args) @@ -52,6 +59,31 @@ namespace Trinity } } + inline std::string StringVFormat(FormatStringView fmt, FormatArgs args) + { + try + { + return fmt::vformat(fmt, args); + } + catch (std::exception const& formatError) + { + return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what()); + } + } + + template + inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args) + { + try + { + return fmt::vformat_to(out, fmt, args); + } + catch (std::exception const& formatError) + { + return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", fmt, formatError.what()); + } + } + /// Returns true if the given char pointer is null. inline bool IsFormatEmptyOrNull(char const* fmt) { -- cgit v1.2.3