aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2024-07-10 14:51:29 +0200
committerOvahlord <dreadkiller@gmx.de>2024-07-11 19:06:51 +0200
commit4c4f5f0ae4c7ad01d846b62051959b2cab16f306 (patch)
tree6d08f61c95fe3d480dcb785e8bbd6495ffb9fc51 /src
parenta638c3690547e17c129abf2737c8ed6b0732a55f (diff)
Core/Logging: Removed one layer of allocations from log message writes (async doesn't wrap in shared_ptr anymore and sync creates LogMessage on the stack)
(cherry picked from commit 1cd7898c01b6cfcdcd0314a3d79bf8a86aa1e619)
Diffstat (limited to 'src')
-rw-r--r--src/common/Logging/Log.cpp46
-rw-r--r--src/common/Logging/Log.h3
-rw-r--r--src/common/Logging/LogOperation.cpp11
-rw-r--r--src/common/Logging/LogOperation.h11
-rw-r--r--src/server/game/Handlers/CharacterHandler.cpp2
5 files changed, 38 insertions, 35 deletions
diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp
index dc8f1005d93..99ff568ecd4 100644
--- a/src/common/Logging/Log.cpp
+++ b/src/common/Logging/Log.cpp
@@ -21,9 +21,9 @@
#include "Config.h"
#include "Duration.h"
#include "Errors.h"
-#include "Logger.h"
#include "LogMessage.h"
#include "LogOperation.h"
+#include "Logger.h"
#include "Strand.h"
#include "StringConvert.h"
#include "Util.h"
@@ -223,23 +223,26 @@ void Log::RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn)
void Log::OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const
{
- write(logger, std::make_unique<LogMessage>(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs)));
+ if (_ioContext)
+ Trinity::Asio::post(*_strand, LogOperation(logger, new LogMessage(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs))));
+ else
+ {
+ LogMessage msg(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs));
+ logger->write(&msg);
+ }
}
void Log::OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const
{
- write(GetLoggerByType("commands.gm"), std::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account)));
-}
+ Logger const* logger = GetLoggerByType("commands.gm");
-void Log::write(Logger const* logger, std::unique_ptr<LogMessage> msg) const
-{
if (_ioContext)
+ Trinity::Asio::post(*_strand, LogOperation(logger, new LogMessage(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account))));
+ else
{
- std::shared_ptr<LogOperation> logOperation = std::make_shared<LogOperation>(logger, std::move(msg));
- Trinity::Asio::post(*_strand, [logOperation]() { logOperation->call(); });
+ LogMessage msg(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account));
+ logger->write(&msg);
}
- else
- logger->write(msg.get());
}
Logger const* Log::GetLoggerByType(std::string_view type) const
@@ -318,22 +321,23 @@ bool Log::SetLogLevel(std::string const& name, int32 newLeveli, bool isLogger /*
return true;
}
-void Log::OutCharDump(char const* str, uint32 accountId, uint64 guid, char const* name)
+void Log::OutCharDump(std::string const& str, uint32 accountId, uint64 guid, std::string const& name) const
{
- if (!str || !ShouldLog("entities.player.dump", LOG_LEVEL_INFO))
+ if (!ShouldLog("entities.player.dump", LOG_LEVEL_INFO))
return;
- std::ostringstream ss;
- ss << "== START DUMP == (account: " << accountId << " guid: " << guid << " name: " << name
- << ")\n" << str << "\n== END DUMP ==\n";
+ std::string ss = Trinity::StringFormat("== START DUMP == (account: {} guid: {} name: {})\n{}\n== END DUMP ==\n", accountId, guid, name, str);
+ std::string param = Trinity::StringFormat("{}_{}", guid, name);
- std::unique_ptr<LogMessage> msg(new LogMessage(LOG_LEVEL_INFO, "entities.player.dump", ss.str()));
- std::ostringstream param;
- param << guid << '_' << name;
+ Logger const* logger = GetLoggerByType("entities.player.dump");
- msg->param1 = param.str();
-
- write(GetLoggerByType("entities.player.dump"), std::move(msg));
+ if (_ioContext)
+ Trinity::Asio::post(*_strand, LogOperation(logger, new LogMessage(LOG_LEVEL_INFO, "entities.player.dump", std::move(ss), std::move(param))));
+ else
+ {
+ LogMessage msg(LOG_LEVEL_INFO, "entities.player.dump", std::move(ss), std::move(param));
+ logger->write(&msg);
+ }
}
void Log::SetRealmId(uint32 id)
diff --git a/src/common/Logging/Log.h b/src/common/Logging/Log.h
index dd6149f42b0..9f9634a40ae 100644
--- a/src/common/Logging/Log.h
+++ b/src/common/Logging/Log.h
@@ -90,7 +90,7 @@ class TC_COMMON_API Log
this->OutCommandImpl(account, fmt, Trinity::MakeFormatArgs(args...));
}
- void OutCharDump(char const* str, uint32 account_id, uint64 guid, char const* name);
+ void OutCharDump(std::string const& str, uint32 account_id, uint64 guid, std::string const& name) const;
void SetRealmId(uint32 id);
@@ -126,7 +126,6 @@ class TC_COMMON_API Log
private:
static std::string GetTimestampStr();
- void write(Logger const* logger, std::unique_ptr<LogMessage> msg) const;
Logger const* GetLoggerByType(std::string_view type) const;
Appender* GetAppenderByName(std::string_view name);
diff --git a/src/common/Logging/LogOperation.cpp b/src/common/Logging/LogOperation.cpp
index 8370c075ed7..4a1bad6ff41 100644
--- a/src/common/Logging/LogOperation.cpp
+++ b/src/common/Logging/LogOperation.cpp
@@ -16,19 +16,16 @@
*/
#include "LogOperation.h"
-#include "Logger.h"
#include "LogMessage.h"
+#include "Logger.h"
-LogOperation::LogOperation(Logger const* _logger, std::unique_ptr<LogMessage>&& _msg) : logger(_logger), msg(std::forward<std::unique_ptr<LogMessage>>(_msg))
+LogOperation::LogOperation(Logger const* _logger, LogMessage* _msg) : logger(_logger), msg(_msg)
{
}
-LogOperation::~LogOperation()
-{
-}
+LogOperation::~LogOperation() = default;
-int LogOperation::call()
+void LogOperation::operator()() const
{
logger->write(msg.get());
- return 0;
}
diff --git a/src/common/Logging/LogOperation.h b/src/common/Logging/LogOperation.h
index 25dc075bd2f..0a642d56e12 100644
--- a/src/common/Logging/LogOperation.h
+++ b/src/common/Logging/LogOperation.h
@@ -24,14 +24,17 @@
class Logger;
struct LogMessage;
-class TC_COMMON_API LogOperation
+class LogOperation
{
public:
- LogOperation(Logger const* _logger, std::unique_ptr<LogMessage>&& _msg);
-
+ LogOperation(Logger const* _logger, LogMessage* _msg);
+ LogOperation(LogOperation const&) = delete;
+ LogOperation(LogOperation&&) noexcept = default;
+ LogOperation& operator=(LogOperation const&) = delete;
+ LogOperation& operator=(LogOperation&&) noexcept = default;
~LogOperation();
- int call();
+ void operator()() const;
protected:
Logger const* logger;
diff --git a/src/server/game/Handlers/CharacterHandler.cpp b/src/server/game/Handlers/CharacterHandler.cpp
index d8f09e84deb..eb3da5399a8 100644
--- a/src/server/game/Handlers/CharacterHandler.cpp
+++ b/src/server/game/Handlers/CharacterHandler.cpp
@@ -986,7 +986,7 @@ void WorldSession::HandleCharDeleteOpcode(WorldPackets::Character::CharDelete& c
{
std::string dump;
if (PlayerDumpWriter().GetDump(charDelete.Guid.GetCounter(), dump))
- sLog->OutCharDump(dump.c_str(), accountId, charDelete.Guid.GetCounter(), name.c_str());
+ sLog->OutCharDump(dump, accountId, charDelete.Guid.GetCounter(), name);
}
sCalendarMgr->RemoveAllPlayerEventsAndInvites(charDelete.Guid);