mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Logging: Minor compile time reducing refactor of log message formatting
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user