aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2023-07-23 00:27:26 +0200
committerShauren <shauren.trinity@gmail.com>2023-07-23 00:27:26 +0200
commit576ca241ecaea6f357a13f022fb297976aa967ca (patch)
treeca7080859e2755f9103d0dea538d7fbf2348b333
parent3be66cee2642f67c4fbe200cb3b056b8d68735dd (diff)
Core/Logging: Minor compile time reducing refactor of log message formatting
-rw-r--r--src/common/Logging/Log.cpp10
-rw-r--r--src/common/Logging/Log.h10
-rw-r--r--src/common/Logging/LogMessage.cpp8
-rw-r--r--src/common/Logging/LogMessage.h4
-rw-r--r--src/common/Utilities/StringConvert.h4
-rw-r--r--src/common/Utilities/StringFormat.h32
6 files changed, 51 insertions, 17 deletions
diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp
index 19523e60cc7..c61ebb2bf85 100644
--- a/src/common/Logging/Log.cpp
+++ b/src/common/Logging/Log.cpp
@@ -221,17 +221,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 f835031aa2f..1e413ece0bb 100644
--- a/src/common/Logging/Log.h
+++ b/src/common/Logging/Log.h
@@ -71,7 +71,7 @@ class TC_COMMON_API Log
template<typename... Args>
void OutMessage(std::string_view filter, LogLevel const level, Trinity::FormatString<Args...> fmt, Args&&... args)
{
- OutMessageImpl(filter, level, Trinity::StringFormat(fmt, std::forward<Args>(args)...));
+ OutMessageImpl(filter, level, fmt, Trinity::MakeFormatArgs(args...));
}
template<typename... Args>
@@ -80,7 +80,7 @@ class TC_COMMON_API Log
if (!ShouldLog("commands.gm", LOG_LEVEL_INFO))
return;
- OutCommandImpl(Trinity::StringFormat(fmt, std::forward<Args>(args)...), std::to_string(account));
+ 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_view 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 b2248aff6f1..d34528a04df 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)
{