From e99482ce9bb7918e23a104fe28305c60d28ed598 Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 19 Mar 2024 13:24:23 +0100 Subject: Core/Logging: Fix intellisense errors - people don't like it when code glows red without a reason --- src/common/Logging/Log.cpp | 28 +++++++++++----- src/common/Logging/Log.h | 37 +++++++++++++++------- .../game/AuctionHouseBot/AuctionHouseBotSeller.cpp | 8 ++--- 3 files changed, 50 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp index 75ea4fae380..dc8f1005d93 100644 --- a/src/common/Logging/Log.cpp +++ b/src/common/Logging/Log.cpp @@ -221,20 +221,18 @@ void Log::RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn) appenderFactory[index] = appenderCreateFn; } -void Log::OutMessageImpl(std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) +void Log::OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const { - write(std::make_unique(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs))); + write(logger, std::make_unique(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs))); } -void Log::OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) +void Log::OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const { - write(std::make_unique(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account))); + write(GetLoggerByType("commands.gm"), std::make_unique(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account))); } -void Log::write(std::unique_ptr msg) const +void Log::write(Logger const* logger, std::unique_ptr msg) const { - Logger const* logger = GetLoggerByType(msg->type); - if (_ioContext) { std::shared_ptr logOperation = std::make_shared(logger, std::move(msg)); @@ -335,7 +333,7 @@ void Log::OutCharDump(char const* str, uint32 accountId, uint64 guid, char const msg->param1 = param.str(); - write(std::move(msg)); + write(GetLoggerByType("entities.player.dump"), std::move(msg)); } void Log::SetRealmId(uint32 id) @@ -368,6 +366,20 @@ 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 +{ + // Don't even look for a logger if the LogLevel is lower than lowest log levels across all loggers + if (level < lowestLogLevel) + return nullptr; + + Logger const* logger = GetLoggerByType(type); + if (!logger) + return nullptr; + + LogLevel logLevel = logger->getLogLevel(); + return logLevel != LOG_LEVEL_DISABLED && logLevel <= level ? logger : nullptr; +} + Log* Log::instance() { static Log instance; diff --git a/src/common/Logging/Log.h b/src/common/Logging/Log.h index 74a115eac4b..dd6149f42b0 100644 --- a/src/common/Logging/Log.h +++ b/src/common/Logging/Log.h @@ -66,12 +66,19 @@ class TC_COMMON_API Log void LoadFromConfig(); void Close(); bool ShouldLog(std::string_view type, LogLevel level) const; + Logger const* GetEnabledLogger(std::string_view type, LogLevel level) const; bool SetLogLevel(std::string const& name, int32 level, bool isLogger = true); template - void OutMessage(std::string_view filter, LogLevel const level, Trinity::FormatString fmt, Args&&... args) + void OutMessage(std::string_view filter, LogLevel level, Trinity::FormatString fmt, Args&&... args) { - OutMessageImpl(filter, level, fmt, Trinity::MakeFormatArgs(args...)); + this->OutMessageImpl(GetLoggerByType(filter), filter, level, fmt, Trinity::MakeFormatArgs(args...)); + } + + template + void OutMessageTo(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatString fmt, Args&&... args) + { + this->OutMessageImpl(logger, filter, level, fmt, Trinity::MakeFormatArgs(args...)); } template @@ -80,7 +87,7 @@ class TC_COMMON_API Log if (!ShouldLog("commands.gm", LOG_LEVEL_INFO)) return; - OutCommandImpl(account, fmt, Trinity::MakeFormatArgs(args...)); + this->OutCommandImpl(account, fmt, Trinity::MakeFormatArgs(args...)); } void OutCharDump(char const* str, uint32 account_id, uint64 guid, char const* name); @@ -90,7 +97,7 @@ class TC_COMMON_API Log template void RegisterAppender() { - RegisterAppender(AppenderImpl::type, &CreateAppender); + this->RegisterAppender(AppenderImpl::type, &CreateAppender); } std::string const& GetLogsDir() const { return m_logsDir; } @@ -111,9 +118,15 @@ class TC_COMMON_API Log return { std::begin(chars), (chars[CharArraySize - 1] == '\0' ? CharArraySize - 1 : CharArraySize) }; } + template + static consteval Trinity::FormatStringView make_format_string_view(char const(&chars)[CharArraySize]) + { + return { std::begin(chars), (chars[CharArraySize - 1] == '\0' ? CharArraySize - 1 : CharArraySize) }; + } + private: static std::string GetTimestampStr(); - void write(std::unique_ptr msg) const; + void write(Logger const* logger, std::unique_ptr msg) const; Logger const* GetLoggerByType(std::string_view type) const; Appender* GetAppenderByName(std::string_view name); @@ -123,8 +136,8 @@ class TC_COMMON_API Log void ReadAppendersFromConfig(); void ReadLoggersFromConfig(); void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn); - void OutMessageImpl(std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs); - void OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs); + 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; std::unordered_map appenderFactory; std::unordered_map> appenders; @@ -141,10 +154,12 @@ class TC_COMMON_API Log #define sLog Log::instance() -#define TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ...) \ - do { \ - if (Log* logInstance = sLog; logInstance->ShouldLog(Log::make_string_view(filterType__), level__)) \ - logInstance->OutMessage(Log::make_string_view(filterType__), level__, Log::make_string_view(message__), ## __VA_ARGS__); \ +#define TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ...) \ + do { \ + Log* logInstance = sLog; \ + if (Logger const* loggerInstance = logInstance->GetEnabledLogger(Log::make_string_view((filterType__)), (level__))) \ + logInstance->OutMessageTo(loggerInstance, Log::make_string_view((filterType__)), (level__), \ + Log::make_format_string_view((message__)), ## __VA_ARGS__); \ } while (0) #ifdef PERFORMANCE_PROFILING diff --git a/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp b/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp index 36b11cfb685..2d7023cdac9 100644 --- a/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp +++ b/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp @@ -357,10 +357,10 @@ bool AuctionBotSeller::Initialize() { sLog->OutMessage("ahbot", LOG_LEVEL_DEBUG, "Items loaded \tGray\tWhite\tGreen\tBlue\tPurple\tOrange\tYellow"); for (uint32 i = 0; i < MAX_ITEM_CLASS; ++i) - sLog->OutMessage("ahbot", LOG_LEVEL_DEBUG, "\t\t%u\t%u\t%u\t%u\t%u\t%u\t%u", - (uint32)_itemPool[0][i].size(), (uint32)_itemPool[1][i].size(), (uint32)_itemPool[2][i].size(), - (uint32)_itemPool[3][i].size(), (uint32)_itemPool[4][i].size(), (uint32)_itemPool[5][i].size(), - (uint32)_itemPool[6][i].size()); + sLog->OutMessage("ahbot", LOG_LEVEL_DEBUG, "\t\t{}\t{}\t{}\t{}\t{}\t{}\t{}", + _itemPool[0][i].size(), _itemPool[1][i].size(), _itemPool[2][i].size(), + _itemPool[3][i].size(), _itemPool[4][i].size(), _itemPool[5][i].size(), + _itemPool[6][i].size()); } TC_LOG_DEBUG("ahbot", "AHBot seller configuration data loaded and initialized"); -- cgit v1.2.3