aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/StringFormat.h
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2023-07-23 00:27:26 +0200
committerShauren <shauren.trinity@gmail.com>2023-08-15 22:03:47 +0200
commit26836f865511f0f6a48c930264537637eb313cd4 (patch)
tree6a77f352a9f1de9423e46a05721266f9b89cd504 /src/common/Utilities/StringFormat.h
parent4e6118c919bd1f3cd864526345c0b9b7abcbb2e4 (diff)
Core/Logging: Minor compile time reducing refactor of log message formatting
(cherry picked from commit 576ca241ecaea6f357a13f022fb297976aa967ca)
Diffstat (limited to 'src/common/Utilities/StringFormat.h')
-rw-r--r--src/common/Utilities/StringFormat.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h
index 9bef5516139..4aea0a4b230 100644
--- a/src/common/Utilities/StringFormat.h
+++ b/src/common/Utilities/StringFormat.h
@@ -25,6 +25,13 @@ namespace Trinity
template<typename... Args>
using FormatString = fmt::format_string<Args...>;
+ using FormatStringView = fmt::string_view;
+
+ using FormatArgs = fmt::format_args;
+
+ template<typename... Args>
+ constexpr auto MakeFormatArgs(Args&&... args) { return fmt::make_format_args(args...); }
+
/// Default TC string format function.
template<typename... Args>
inline std::string StringFormat(FormatString<Args...> 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<typename OutputIt>
+ 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)
{