aboutsummaryrefslogtreecommitdiff
path: root/src/common/Logging/Log.cpp
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2024-07-10 14:51:29 +0200
committerShauren <shauren.trinity@gmail.com>2024-07-10 14:51:29 +0200
commit1cd7898c01b6cfcdcd0314a3d79bf8a86aa1e619 (patch)
treefbd8cb01b0203de70dee9136c45abbcf94360ccd /src/common/Logging/Log.cpp
parente05d194da3d742130cd2f9eb91051610d58f8236 (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)
Diffstat (limited to 'src/common/Logging/Log.cpp')
-rw-r--r--src/common/Logging/Log.cpp46
1 files changed, 25 insertions, 21 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)