aboutsummaryrefslogtreecommitdiff
path: root/src/common/Logging
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/common/Logging
parentbe448017289c3c8df2d6dc44fe93b16b91b02daf (diff)
Core/Misc: Remove StringFormat exception handling code from the header
Diffstat (limited to 'src/common/Logging')
-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
4 files changed, 35 insertions, 31 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;