Skip to content

Commit

Permalink
Core/Logging: Fix intellisense errors - people don't like it when cod…
Browse files Browse the repository at this point in the history
…e glows red without a reason
  • Loading branch information
Shauren committed Mar 19, 2024
1 parent c0b4f93 commit e99482c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
28 changes: 20 additions & 8 deletions src/common/Logging/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogMessage>(level, filter, Trinity::StringVFormat(messageFormat, messageFormatArgs)));
write(logger, std::make_unique<LogMessage>(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<LogMessage>(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account)));
write(GetLoggerByType("commands.gm"), std::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", Trinity::StringVFormat(messageFormat, messageFormatArgs), Trinity::ToString(account)));
}

void Log::write(std::unique_ptr<LogMessage> msg) const
void Log::write(Logger const* logger, std::unique_ptr<LogMessage> msg) const
{
Logger const* logger = GetLoggerByType(msg->type);

if (_ioContext)
{
std::shared_ptr<LogOperation> logOperation = std::make_shared<LogOperation>(logger, std::move(msg));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 26 additions & 11 deletions src/common/Logging/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename... Args>
void OutMessage(std::string_view filter, LogLevel const level, Trinity::FormatString<Args...> fmt, Args&&... args)
void OutMessage(std::string_view filter, LogLevel level, Trinity::FormatString<Args...> fmt, Args&&... args)
{
OutMessageImpl(filter, level, fmt, Trinity::MakeFormatArgs(args...));
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)
{
this->OutMessageImpl(logger, filter, level, fmt, Trinity::MakeFormatArgs(args...));
}

template<typename... Args>
Expand All @@ -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);
Expand All @@ -90,7 +97,7 @@ class TC_COMMON_API Log
template<class AppenderImpl>
void RegisterAppender()
{
RegisterAppender(AppenderImpl::type, &CreateAppender<AppenderImpl>);
this->RegisterAppender(AppenderImpl::type, &CreateAppender<AppenderImpl>);
}

std::string const& GetLogsDir() const { return m_logsDir; }
Expand All @@ -111,9 +118,15 @@ class TC_COMMON_API Log
return { std::begin(chars), (chars[CharArraySize - 1] == '\0' ? CharArraySize - 1 : CharArraySize) };
}

template <size_t CharArraySize>
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<LogMessage> msg) const;
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);
Expand All @@ -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<uint8, AppenderCreatorFn> appenderFactory;
std::unordered_map<uint8, std::unique_ptr<Appender>> appenders;
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit e99482c

Please sign in to comment.