diff options
| author | Shauren <shauren.trinity@gmail.com> | 2023-08-15 20:10:04 +0200 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2023-08-15 20:10:04 +0200 |
| commit | aaa6e73c8ca6d60e943cb964605536eb78219db2 (patch) | |
| tree | f5a0187925e646ef071d647efa7a5dac20501813 /src/common/Utilities | |
| parent | 825c697a764017349ca94ecfca8f30a8365666c0 (diff) | |
Core/Logging: Switch from fmt::sprintf to fmt::format (c++20 standard compatible api)
(cherry picked from commit d791afae1dfcfaf592326f787755ca32d629e4d3)
Diffstat (limited to 'src/common/Utilities')
| -rw-r--r-- | src/common/Utilities/StartProcess.cpp | 12 | ||||
| -rw-r--r-- | src/common/Utilities/StringFormat.h | 22 | ||||
| -rw-r--r-- | src/common/Utilities/Util.cpp | 10 |
3 files changed, 26 insertions, 18 deletions
diff --git a/src/common/Utilities/StartProcess.cpp b/src/common/Utilities/StartProcess.cpp index 00e5bc0bfe7..dc4fc4e7646 100644 --- a/src/common/Utilities/StartProcess.cpp +++ b/src/common/Utilities/StartProcess.cpp @@ -87,8 +87,8 @@ static int CreateChildProcess(T waiter, std::string const& executable, if (!secure) { - TC_LOG_TRACE(logger, "Starting process \"%s\" with arguments: \"%s\".", - executable.c_str(), boost::algorithm::join(argsVector, " ").c_str()); + TC_LOG_TRACE(logger, "Starting process \"{}\" with arguments: \"{}\".", + executable, boost::algorithm::join(argsVector, " ")); } // prepare file with only read permission (boost process opens with read_write) @@ -129,12 +129,12 @@ static int CreateChildProcess(T waiter, std::string const& executable, auto outInfo = MakeTCLogSink([&](std::string_view msg) { - TC_LOG_INFO(logger, STRING_VIEW_FMT, STRING_VIEW_FMT_ARG(msg)); + TC_LOG_INFO(logger, "{}", msg); }); auto outError = MakeTCLogSink([&](std::string_view msg) { - TC_LOG_ERROR(logger, STRING_VIEW_FMT, STRING_VIEW_FMT_ARG(msg)); + TC_LOG_ERROR(logger, "{}", msg); }); copy(outStream, outInfo); @@ -146,8 +146,8 @@ static int CreateChildProcess(T waiter, std::string const& executable, if (!secure) { - TC_LOG_TRACE(logger, ">> Process \"%s\" finished with return value %i.", - executable.c_str(), result); + TC_LOG_TRACE(logger, ">> Process \"{}\" finished with return value {}.", + executable, result); } return result; diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h index da1615575a4..b4d4d9e5743 100644 --- a/src/common/Utilities/StringFormat.h +++ b/src/common/Utilities/StringFormat.h @@ -18,22 +18,24 @@ #ifndef TRINITYCORE_STRING_FORMAT_H #define TRINITYCORE_STRING_FORMAT_H -#include "fmt/printf.h" +#include "fmt/core.h" namespace Trinity { + template<typename... Args> + using FormatString = std::string_view; + /// Default TC string format function. - template<typename Format, typename... Args> - inline std::string StringFormat(Format&& fmt, Args&&... args) + template<typename... Args> + inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args) { try { - return fmt::sprintf(std::forward<Format>(fmt), std::forward<Args>(args)...); + return fmt::format(fmt, std::forward<Args>(args)...); } - catch (const fmt::format_error& formatError) + catch (std::exception const& formatError) { - std::string error = "An error occurred formatting string \"" + std::string(fmt) + "\" : " + std::string(formatError.what()); - return error; + return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what()); } } @@ -48,6 +50,12 @@ namespace Trinity { return fmt.empty(); } + + /// Returns true if the given std::string is empty. + inline constexpr bool IsFormatEmptyOrNull(std::string_view fmt) + { + return fmt.empty(); + } } #endif diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 4693bd9eb08..6763685ba70 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -106,13 +106,13 @@ std::string secsToTimeString(uint64 timeInSecs, TimeFormat timeFormat, bool hour if (timeFormat == TimeFormat::Numeric) { if (days) - return Trinity::StringFormat("%u:%02u:%02u:%02u", days, hours, minutes, secs); + return Trinity::StringFormat("{}:{:02}:{:02}:{:02}", days, hours, minutes, secs); else if (hours) - return Trinity::StringFormat("%u:%02u:%02u", hours, minutes, secs); + return Trinity::StringFormat("{}:{:02}:{:02}", hours, minutes, secs); else if (minutes) - return Trinity::StringFormat("%u:%02u", minutes, secs); + return Trinity::StringFormat("{}:{:02}", minutes, secs); else - return Trinity::StringFormat("0:%02u", secs); + return Trinity::StringFormat("0:{:02}", secs); } std::ostringstream ss; @@ -281,7 +281,7 @@ std::string TimeToTimestampStr(time_t t) // HH hour (2 digits 00-23) // MM minutes (2 digits 00-59) // SS seconds (2 digits 00-59) - return Trinity::StringFormat("%04d-%02d-%02d_%02d-%02d-%02d", aTm.tm_year + 1900, aTm.tm_mon + 1, aTm.tm_mday, aTm.tm_hour, aTm.tm_min, aTm.tm_sec); + return Trinity::StringFormat("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", aTm.tm_year + 1900, aTm.tm_mon + 1, aTm.tm_mday, aTm.tm_hour, aTm.tm_min, aTm.tm_sec); } std::string TimeToHumanReadable(time_t t) |
