mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Misc: Remove StringFormat exception handling code from the header
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
*/
|
||||
|
||||
#include "Appender.h"
|
||||
#include "Common.h"
|
||||
#include "LogMessage.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
Appender::Appender(uint8 _id, std::string _name, LogLevel _level /* = LOG_LEVEL_DISABLED */, AppenderFlags _flags /* = APPENDER_FLAGS_NONE */):
|
||||
id(_id), name(std::move(_name)), level(_level), flags(_flags) { }
|
||||
@@ -66,7 +66,12 @@ void Appender::write(LogMessage* message)
|
||||
}
|
||||
|
||||
if (flags & APPENDER_FLAGS_PREFIX_LOGLEVEL)
|
||||
Trinity::StringFormatTo(std::back_inserter(message->prefix), "{:<5} ", getLogLevelString(message->level));
|
||||
{
|
||||
std::string_view levelStr = getLogLevelString(message->level);
|
||||
message->prefix.append(levelStr);
|
||||
if (levelStr.length() < 5)
|
||||
message->prefix.append(5 - levelStr.length(), ' ');
|
||||
}
|
||||
|
||||
if (flags & APPENDER_FLAGS_PREFIX_LOGFILTERTYPE)
|
||||
{
|
||||
@@ -79,23 +84,23 @@ void Appender::write(LogMessage* message)
|
||||
_write(message);
|
||||
}
|
||||
|
||||
char const* Appender::getLogLevelString(LogLevel level)
|
||||
std::string_view Appender::getLogLevelString(LogLevel level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case LOG_LEVEL_FATAL:
|
||||
return "FATAL";
|
||||
return "FATAL"sv;
|
||||
case LOG_LEVEL_ERROR:
|
||||
return "ERROR";
|
||||
return "ERROR"sv;
|
||||
case LOG_LEVEL_WARN:
|
||||
return "WARN";
|
||||
return "WARN"sv;
|
||||
case LOG_LEVEL_INFO:
|
||||
return "INFO";
|
||||
return "INFO"sv;
|
||||
case LOG_LEVEL_DEBUG:
|
||||
return "DEBUG";
|
||||
return "DEBUG"sv;
|
||||
case LOG_LEVEL_TRACE:
|
||||
return "TRACE";
|
||||
return "TRACE"sv;
|
||||
default:
|
||||
return "DISABLED";
|
||||
return "DISABLED"sv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "LogCommon.h"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
struct LogMessage;
|
||||
|
||||
@@ -43,7 +44,7 @@ class TC_COMMON_API Appender
|
||||
|
||||
void setLogLevel(LogLevel);
|
||||
void write(LogMessage* message);
|
||||
static char const* getLogLevelString(LogLevel level);
|
||||
static std::string_view getLogLevelString(LogLevel level);
|
||||
virtual void setRealmId(uint32 /*realmId*/) { }
|
||||
|
||||
private:
|
||||
|
||||
@@ -27,9 +27,8 @@
|
||||
#include "StringConvert.h"
|
||||
#include "Util.h"
|
||||
|
||||
Log::Log() : AppenderId(0), lowestLogLevel(LOG_LEVEL_FATAL), _ioContext(nullptr), _strand(nullptr)
|
||||
Log::Log() : AppenderId(0), lowestLogLevel(LOG_LEVEL_FATAL), m_logsTimestamp('_' + GetTimestampStr()), _ioContext(nullptr), _strand(nullptr)
|
||||
{
|
||||
m_logsTimestamp = "_" + GetTimestampStr();
|
||||
RegisterAppender<AppenderConsole>();
|
||||
RegisterAppender<AppenderFile>();
|
||||
}
|
||||
@@ -214,12 +213,11 @@ void Log::ReadLoggersFromConfig()
|
||||
|
||||
void Log::RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn)
|
||||
{
|
||||
auto itr = appenderFactory.find(index);
|
||||
ASSERT(itr == appenderFactory.end());
|
||||
appenderFactory[index] = appenderCreateFn;
|
||||
[[maybe_unused]] bool isNewAppender = appenderFactory.try_emplace(index, appenderCreateFn).second;
|
||||
ASSERT(isNewAppender);
|
||||
}
|
||||
|
||||
void Log::OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const
|
||||
void Log::OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const noexcept
|
||||
{
|
||||
if (_ioContext)
|
||||
Trinity::Asio::post(*_strand, LogOperation(logger, new LogMessage(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs))));
|
||||
@@ -230,7 +228,7 @@ void Log::OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel
|
||||
}
|
||||
}
|
||||
|
||||
void Log::OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const
|
||||
void Log::OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const noexcept
|
||||
{
|
||||
Logger const* logger = GetLoggerByType("commands.gm");
|
||||
|
||||
@@ -298,7 +296,7 @@ bool Log::SetLogLevel(std::string const& name, int32 newLeveli, bool isLogger /*
|
||||
return true;
|
||||
}
|
||||
|
||||
void Log::OutCharDump(std::string const& str, uint32 accountId, uint64 guid, std::string const& name) const
|
||||
void Log::OutCharDump(std::string const& str, uint32 accountId, uint64 guid, std::string const& name) const noexcept
|
||||
{
|
||||
if (!ShouldLog("entities.player.dump", LOG_LEVEL_INFO))
|
||||
return;
|
||||
@@ -329,7 +327,7 @@ void Log::Close()
|
||||
appenders.clear();
|
||||
}
|
||||
|
||||
bool Log::ShouldLog(std::string_view type, LogLevel level) const
|
||||
bool Log::ShouldLog(std::string_view type, LogLevel level) const noexcept
|
||||
{
|
||||
// TODO: Use cache to store "Type.sub1.sub2": "Type" equivalence, should
|
||||
// Speed up in cases where requesting "Type.sub1.sub2" but only configured
|
||||
@@ -347,7 +345,7 @@ bool Log::ShouldLog(std::string_view type, LogLevel level) const
|
||||
return logLevel != LOG_LEVEL_DISABLED && logLevel <= level;
|
||||
}
|
||||
|
||||
Logger const* Log::GetEnabledLogger(std::string_view type, LogLevel level) const
|
||||
Logger const* Log::GetEnabledLogger(std::string_view type, LogLevel level) const noexcept
|
||||
{
|
||||
// Don't even look for a logger if the LogLevel is lower than lowest log levels across all loggers
|
||||
if (level < lowestLogLevel)
|
||||
@@ -361,7 +359,7 @@ Logger const* Log::GetEnabledLogger(std::string_view type, LogLevel level) const
|
||||
return logLevel != LOG_LEVEL_DISABLED && logLevel <= level ? logger : nullptr;
|
||||
}
|
||||
|
||||
Log* Log::instance()
|
||||
Log* Log::instance() noexcept
|
||||
{
|
||||
static Log instance;
|
||||
return &instance;
|
||||
|
||||
@@ -60,30 +60,30 @@ class TC_COMMON_API Log
|
||||
Log& operator=(Log const&) = delete;
|
||||
Log& operator=(Log&&) = delete;
|
||||
|
||||
static Log* instance();
|
||||
static Log* instance() noexcept;
|
||||
|
||||
void Initialize(Trinity::Asio::IoContext* ioContext);
|
||||
void SetSynchronous(); // Not threadsafe - should only be called from main() after all threads are joined
|
||||
void LoadFromConfig();
|
||||
void Close();
|
||||
bool ShouldLog(std::string_view type, LogLevel level) const;
|
||||
Logger const* GetEnabledLogger(std::string_view type, LogLevel level) const;
|
||||
bool ShouldLog(std::string_view type, LogLevel level) const noexcept;
|
||||
Logger const* GetEnabledLogger(std::string_view type, LogLevel level) const noexcept;
|
||||
bool SetLogLevel(std::string const& name, int32 level, bool isLogger = true);
|
||||
|
||||
template<typename... Args>
|
||||
void OutMessage(std::string_view filter, LogLevel level, Trinity::FormatString<Args...> fmt, Args&&... args)
|
||||
void OutMessage(std::string_view filter, LogLevel level, Trinity::FormatString<Args...> fmt, Args&&... args) noexcept
|
||||
{
|
||||
this->OutMessageImpl(GetLoggerByType(filter), filter, level, fmt, Trinity::MakeFormatArgs(args...));
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void OutMessageTo(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatString<Args...> fmt, Args&&... args)
|
||||
void OutMessageTo(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatString<Args...> fmt, Args&&... args) noexcept
|
||||
{
|
||||
this->OutMessageImpl(logger, filter, level, fmt, Trinity::MakeFormatArgs(args...));
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void OutCommand(uint32 account, Trinity::FormatString<Args...> fmt, Args&&... args)
|
||||
void OutCommand(uint32 account, Trinity::FormatString<Args...> fmt, Args&&... args) noexcept
|
||||
{
|
||||
if (!ShouldLog("commands.gm", LOG_LEVEL_INFO))
|
||||
return;
|
||||
@@ -91,7 +91,7 @@ class TC_COMMON_API Log
|
||||
this->OutCommandImpl(account, fmt, Trinity::MakeFormatArgs(args...));
|
||||
}
|
||||
|
||||
void OutCharDump(std::string const& str, uint32 account_id, uint64 guid, std::string const& name) const;
|
||||
void OutCharDump(std::string const& str, uint32 account_id, uint64 guid, std::string const& name) const noexcept;
|
||||
|
||||
void SetRealmId(uint32 id);
|
||||
|
||||
@@ -136,8 +136,8 @@ class TC_COMMON_API Log
|
||||
void ReadAppendersFromConfig();
|
||||
void ReadLoggersFromConfig();
|
||||
void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn);
|
||||
void OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const;
|
||||
void OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const;
|
||||
void OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const noexcept;
|
||||
void OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const noexcept;
|
||||
|
||||
std::unordered_map<uint8, AppenderCreatorFn> appenderFactory;
|
||||
std::unordered_map<uint8, std::unique_ptr<Appender>> appenders;
|
||||
|
||||
@@ -16,9 +16,31 @@
|
||||
*/
|
||||
|
||||
#include "StringFormat.h"
|
||||
#include "Define.h"
|
||||
#include <fmt/format.h>
|
||||
|
||||
namespace Trinity::Impl
|
||||
{
|
||||
std::string StringVFormat(FormatStringView fmt, FormatArgs args) noexcept
|
||||
try
|
||||
{
|
||||
return fmt::vformat(fmt, args);
|
||||
}
|
||||
catch (std::exception const& formatError)
|
||||
{
|
||||
return fmt::format(R"(An error occurred formatting string "{}" : {})", fmt, formatError.what());
|
||||
}
|
||||
|
||||
void StringVFormatToImpl(fmt::detail::buffer<char>& buffer, FormatStringView fmt, FormatArgs args) noexcept
|
||||
try
|
||||
{
|
||||
fmt::detail::vformat_to(buffer, fmt, args, {});
|
||||
}
|
||||
catch (std::exception const& formatError)
|
||||
{
|
||||
fmt::detail::vformat_to(buffer, FormatStringView(R"(An error occurred formatting string "{}" : {})"), MakeFormatArgs(fmt, formatError.what()), {});
|
||||
}
|
||||
}
|
||||
|
||||
// explicit template instantiations
|
||||
template struct TC_COMMON_API fmt::formatter<int>;
|
||||
template struct TC_COMMON_API fmt::formatter<unsigned>;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#ifndef TRINITYCORE_STRING_FORMAT_H
|
||||
#define TRINITYCORE_STRING_FORMAT_H
|
||||
|
||||
#include "Define.h"
|
||||
#include "Optional.h"
|
||||
#include "StringFormatFwd.h"
|
||||
#include <fmt/core.h>
|
||||
@@ -34,56 +35,34 @@ namespace Trinity
|
||||
template<typename... Args>
|
||||
constexpr auto MakeFormatArgs(Args&&... args) { return fmt::make_format_args(args...); }
|
||||
|
||||
namespace Impl
|
||||
{
|
||||
TC_COMMON_API std::string StringVFormat(FormatStringView fmt, FormatArgs args) noexcept;
|
||||
|
||||
TC_COMMON_API void StringVFormatToImpl(fmt::detail::buffer<char>& buffer, FormatStringView fmt, FormatArgs args) noexcept;
|
||||
}
|
||||
|
||||
using Impl::StringVFormat;
|
||||
|
||||
template<typename OutputIt>
|
||||
inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args) noexcept
|
||||
{
|
||||
auto&& buf = fmt::detail::get_buffer<char>(out);
|
||||
Trinity::Impl::StringVFormatToImpl(buf, fmt, args);
|
||||
return fmt::detail::get_iterator(buf, out);
|
||||
}
|
||||
|
||||
/// Default TC string format function.
|
||||
template<typename... Args>
|
||||
inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args)
|
||||
inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
return fmt::format(fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
catch (std::exception const& formatError)
|
||||
{
|
||||
return fmt::format("An error occurred formatting string \"{}\" : {}", FormatStringView(fmt), formatError.what());
|
||||
}
|
||||
return Trinity::StringVFormat(fmt, Trinity::MakeFormatArgs(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template<typename OutputIt, typename... Args>
|
||||
inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args)
|
||||
inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
return fmt::format_to(out, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
catch (std::exception const& formatError)
|
||||
{
|
||||
return fmt::format_to(out, "An error occurred formatting string \"{}\" : {}", FormatStringView(fmt), formatError.what());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
return Trinity::StringVFormatTo(out, fmt, Trinity::MakeFormatArgs(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
/// Returns true if the given char pointer is null.
|
||||
|
||||
Reference in New Issue
Block a user