diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common/Logging/Log.cpp | 10 | ||||
-rw-r--r-- | src/common/Logging/Log.h | 10 | ||||
-rw-r--r-- | src/common/Logging/LogMessage.cpp | 8 | ||||
-rw-r--r-- | src/common/Logging/LogMessage.h | 4 | ||||
-rw-r--r-- | src/common/Utilities/StringConvert.h | 4 | ||||
-rw-r--r-- | src/common/Utilities/StringFormat.h | 32 |
6 files changed, 51 insertions, 17 deletions
diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp index 2e7e39e62ec..304cee0fbfa 100644 --- a/src/common/Logging/Log.cpp +++ b/src/common/Logging/Log.cpp @@ -214,17 +214,17 @@ void Log::RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn) appenderFactory[index] = appenderCreateFn; } -void Log::OutMessageImpl(std::string_view filter, LogLevel level, std::string&& message) +void Log::OutMessageImpl(std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) { - write(std::make_unique<LogMessage>(level, std::string(filter), std::move(message))); + write(std::make_unique<LogMessage>(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs))); } -void Log::OutCommandImpl(std::string&& message, std::string&& param1) +void Log::OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) { - write(std::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", std::move(message), std::move(param1))); + write(std::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account))); } -void Log::write(std::unique_ptr<LogMessage>&& msg) const +void Log::write(std::unique_ptr<LogMessage> msg) const { Logger const* logger = GetLoggerByType(msg->type); diff --git a/src/common/Logging/Log.h b/src/common/Logging/Log.h index 17ba5682ceb..33a4d6c5731 100644 --- a/src/common/Logging/Log.h +++ b/src/common/Logging/Log.h @@ -74,7 +74,7 @@ class TC_COMMON_API Log template<typename... Args> void OutMessage(std::string_view filter, LogLevel const level, Trinity::FormatString<Args...> fmt, Args&&... args) { - this->OutMessageImpl(filter, level, Trinity::StringFormat(fmt, std::forward<Args>(args)...)); + this->OutMessageImpl(filter, level, fmt, Trinity::MakeFormatArgs(args...)); } template<typename... Args> @@ -83,7 +83,7 @@ class TC_COMMON_API Log if (!ShouldLog("commands.gm", LOG_LEVEL_INFO)) return; - this->OutCommandImpl(Trinity::StringFormat(fmt, std::forward<Args>(args)...), std::to_string(account)); + this->OutCommandImpl(account, fmt, Trinity::MakeFormatArgs(args...)); } void OutCharDump(char const* str, uint32 account_id, uint64 guid, char const* name); @@ -101,7 +101,7 @@ class TC_COMMON_API Log private: static std::string GetTimestampStr(); - void write(std::unique_ptr<LogMessage>&& msg) const; + void write(std::unique_ptr<LogMessage> msg) const; Logger const* GetLoggerByType(std::string const& type) const; Appender* GetAppenderByName(std::string_view name); @@ -111,8 +111,8 @@ class TC_COMMON_API Log void ReadAppendersFromConfig(); void ReadLoggersFromConfig(); void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn); - void OutMessageImpl(std::string_view filter, LogLevel level, std::string&& message); - void OutCommandImpl(std::string&& message, std::string&& param1); + void OutMessageImpl(std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs); + void OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs); std::unordered_map<uint8, AppenderCreatorFn> appenderFactory; std::unordered_map<uint8, std::unique_ptr<Appender>> appenders; diff --git a/src/common/Logging/LogMessage.cpp b/src/common/Logging/LogMessage.cpp index a9a99312db1..9d66e0a2d3b 100644 --- a/src/common/Logging/LogMessage.cpp +++ b/src/common/Logging/LogMessage.cpp @@ -19,13 +19,13 @@ #include "StringFormat.h" #include "Util.h" -LogMessage::LogMessage(LogLevel _level, std::string const& _type, std::string&& _text) - : level(_level), type(_type), text(std::forward<std::string>(_text)), mtime(time(nullptr)) +LogMessage::LogMessage(LogLevel _level, std::string_view _type, std::string _text) + : level(_level), type(_type), text(std::move(_text)), mtime(time(nullptr)) { } -LogMessage::LogMessage(LogLevel _level, std::string const& _type, std::string&& _text, std::string&& _param1) - : level(_level), type(_type), text(std::forward<std::string>(_text)), param1(std::forward<std::string>(_param1)), mtime(time(nullptr)) +LogMessage::LogMessage(LogLevel _level, std::string_view _type, std::string _text, std::string _param1) + : level(_level), type(_type), text(std::move(_text)), param1(std::move(_param1)), mtime(time(nullptr)) { } diff --git a/src/common/Logging/LogMessage.h b/src/common/Logging/LogMessage.h index d9709b43c4c..90aad60346c 100644 --- a/src/common/Logging/LogMessage.h +++ b/src/common/Logging/LogMessage.h @@ -25,8 +25,8 @@ struct TC_COMMON_API LogMessage { - LogMessage(LogLevel _level, std::string const& _type, std::string&& _text); - LogMessage(LogLevel _level, std::string const& _type, std::string&& _text, std::string&& _param1); + LogMessage(LogLevel _level, std::string_view _type, std::string _text); + LogMessage(LogLevel _level, std::string_view _type, std::string _text, std::string _param1); LogMessage(LogMessage const& /*other*/) = delete; LogMessage& operator=(LogMessage const& /*other*/) = delete; 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<size_t, sizeof(T) < 8 ? 11 : 20>; + + 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 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) { |