aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2025-06-23 11:38:34 +0200
committerShauren <shauren.trinity@gmail.com>2025-06-23 11:38:34 +0200
commitd62b412c925ffa348b9586ebbf43adc15cb12462 (patch)
treebb9005ef1135e73821b3f1ad4db493e0646b8b8b /src
parentbe448017289c3c8df2d6dc44fe93b16b91b02daf (diff)
Core/Misc: Remove StringFormat exception handling code from the header
Diffstat (limited to 'src')
-rw-r--r--src/common/Logging/Appender.cpp25
-rw-r--r--src/common/Logging/Appender.h3
-rw-r--r--src/common/Logging/Log.cpp20
-rw-r--r--src/common/Logging/Log.h18
-rw-r--r--src/common/Utilities/StringFormat.cpp24
-rw-r--r--src/common/Utilities/StringFormat.h59
6 files changed, 77 insertions, 72 deletions
diff --git a/src/common/Logging/Appender.cpp b/src/common/Logging/Appender.cpp
index dabd6fcd6f5..6b4c21f81f2 100644
--- a/src/common/Logging/Appender.cpp
+++ b/src/common/Logging/Appender.cpp
@@ -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;
}
}
diff --git a/src/common/Logging/Appender.h b/src/common/Logging/Appender.h
index 8e1563f83bc..95d22fd7949 100644
--- a/src/common/Logging/Appender.h
+++ b/src/common/Logging/Appender.h
@@ -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:
diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp
index 643f0c8483c..4514fbabd1a 100644
--- a/src/common/Logging/Log.cpp
+++ b/src/common/Logging/Log.cpp
@@ -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;
diff --git a/src/common/Logging/Log.h b/src/common/Logging/Log.h
index 53a40d62c61..17e0421d422 100644
--- a/src/common/Logging/Log.h
+++ b/src/common/Logging/Log.h
@@ -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;
diff --git a/src/common/Utilities/StringFormat.cpp b/src/common/Utilities/StringFormat.cpp
index 9550294fd64..acc1b4682e9 100644
--- a/src/common/Utilities/StringFormat.cpp
+++ b/src/common/Utilities/StringFormat.cpp
@@ -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>;
diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h
index f7f2b7c300b..ccad8ef2b34 100644
--- a/src/common/Utilities/StringFormat.h
+++ b/src/common/Utilities/StringFormat.h
@@ -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...); }
- /// Default TC string format function.
- template<typename... Args>
- inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args)
+ namespace Impl
{
- 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());
- }
+ 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;
}
- template<typename OutputIt, typename... Args>
- inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args)
+ using Impl::StringVFormat;
+
+ template<typename OutputIt>
+ inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs 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());
- }
+ auto&& buf = fmt::detail::get_buffer<char>(out);
+ Trinity::Impl::StringVFormatToImpl(buf, fmt, args);
+ return fmt::detail::get_iterator(buf, out);
}
- inline std::string StringVFormat(FormatStringView fmt, FormatArgs args)
+ /// Default TC string format function.
+ template<typename... Args>
+ inline std::string StringFormat(FormatString<Args...> fmt, Args&&... args) noexcept
{
- try
- {
- return fmt::vformat(fmt, args);
- }
- catch (std::exception const& formatError)
- {
- return fmt::format("An error occurred formatting string \"{}\" : {}", fmt, formatError.what());
- }
+ return Trinity::StringVFormat(fmt, Trinity::MakeFormatArgs(std::forward<Args>(args)...));
}
- template<typename OutputIt>
- inline OutputIt StringVFormatTo(OutputIt out, FormatStringView fmt, FormatArgs args)
+ template<typename OutputIt, typename... Args>
+ inline OutputIt StringFormatTo(OutputIt out, FormatString<Args...> fmt, Args&&... args) noexcept
{
- 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.