Skip to content

Commit

Permalink
Core/Logging: Improved generated code for log statements (length of t…
Browse files Browse the repository at this point in the history
…ext is computed at compile time)
  • Loading branch information
Shauren committed Mar 15, 2024
1 parent 783f9c0 commit e121ed8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
63 changes: 36 additions & 27 deletions src/common/Logging/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ class TC_COMMON_API Log
void CreateAppenderFromConfigLine(std::string const& name, std::string const& options);
void CreateLoggerFromConfigLine(std::string const& name, std::string const& options);

template <typename StringOrStringView>
static constexpr std::string_view make_string_view(StringOrStringView const& stringOrStringView)
{
return stringOrStringView;
}

template <size_t CharArraySize>
static consteval std::string_view make_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;
Expand Down Expand Up @@ -129,43 +141,40 @@ 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__); \
} while (0)

#ifdef PERFORMANCE_PROFILING
#define TC_LOG_MESSAGE_BODY(filterType__, level__, ...) ((void)0)
#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) ((void)0)
#elif TRINITY_PLATFORM != TRINITY_PLATFORM_WINDOWS

// This will catch format errors on build time
#define TC_LOG_MESSAGE_BODY(filterType__, level__, ...) \
do { \
if (Log* logInstance = sLog; logInstance->ShouldLog(filterType__, level__)) \
logInstance->OutMessage(filterType__, level__, __VA_ARGS__); \
} while (0)
#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ## __VA_ARGS__)
#else
#define TC_LOG_MESSAGE_BODY(filterType__, level__, ...) \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
do { \
if (Log* logInstance = sLog; logInstance->ShouldLog(filterType__, level__)) \
logInstance->OutMessage(filterType__, level__, __VA_ARGS__); \
} while (0) \
#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ## __VA_ARGS__) \
__pragma(warning(pop))
#endif

#define TC_LOG_TRACE(filterType__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_TRACE, __VA_ARGS__)
#define TC_LOG_TRACE(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_TRACE, message__, ## __VA_ARGS__)

#define TC_LOG_DEBUG(filterType__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_DEBUG, __VA_ARGS__)
#define TC_LOG_DEBUG(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_DEBUG, message__, ## __VA_ARGS__)

#define TC_LOG_INFO(filterType__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_INFO, __VA_ARGS__)
#define TC_LOG_INFO(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_INFO, message__, ## __VA_ARGS__)

#define TC_LOG_WARN(filterType__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_WARN, __VA_ARGS__)
#define TC_LOG_WARN(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_WARN, message__, ## __VA_ARGS__)

#define TC_LOG_ERROR(filterType__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_ERROR, __VA_ARGS__)
#define TC_LOG_ERROR(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_ERROR, message__, ## __VA_ARGS__)

#define TC_LOG_FATAL(filterType__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_FATAL, __VA_ARGS__)
#define TC_LOG_FATAL(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_FATAL, message__, ## __VA_ARGS__)

#endif
4 changes: 2 additions & 2 deletions src/server/game/Maps/GridMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ GridMap::LoadResult GridMap::loadData(char const* filename)
return LoadResult::Ok;
}

TC_LOG_ERROR("maps", "Map file '{}' is from an incompatible map version (%.*s v{}), %.*s v{} is expected. Please pull your source, recompile tools and recreate maps using the updated mapextractor, then replace your old map files with new files. If you still have problems search on forum for error TCE00018.",
filename, 4, header.mapMagic.data(), header.versionMagic, 4, MapMagic.data(), MapVersionMagic);
TC_LOG_ERROR("maps", "Map file '{}' is from an incompatible map version ({} v{}), {} v{} is expected. Please pull your source, recompile tools and recreate maps using the updated mapextractor, then replace your old map files with new files. If you still have problems search on forum for error TCE00018.",
filename, std::string_view(header.mapMagic.data(), 4), header.versionMagic, std::string_view(MapMagic.data(), 4), MapVersionMagic);
fclose(in);
return LoadResult::InvalidFile;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/game/Maps/TerrainMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ bool TerrainInfo::ExistMap(uint32 mapid, int32 gx, int32 gy, bool log /*= true*/
if (header.mapMagic != MapMagic || header.versionMagic != MapVersionMagic)
{
if (log)
TC_LOG_ERROR("maps", "Map file '{}' is from an incompatible map version (%.*s v{}), %.*s v{} is expected. Please pull your source, recompile tools and recreate maps using the updated mapextractor, then replace your old map files with new files. If you still have problems search on forum for error TCE00018.",
fileName, 4, header.mapMagic.data(), header.versionMagic, 4, MapMagic.data(), MapVersionMagic);
TC_LOG_ERROR("maps", "Map file '{}' is from an incompatible map version ({} v{}), {} v{} is expected. Please pull your source, recompile tools and recreate maps using the updated mapextractor, then replace your old map files with new files. If you still have problems search on forum for error TCE00018.",
fileName, std::string_view(header.mapMagic.data(), 4), header.versionMagic, std::string_view(MapMagic.data(), 4), MapVersionMagic);
}
else
ret = true;
Expand Down
10 changes: 5 additions & 5 deletions src/server/scripts/World/chat_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#include "Log.h"
#include "Player.h"

#define TC_LOG_CHAT(TYPE, ...) \
if (lang != LANG_ADDON && lang != LANG_ADDON_LOGGED) \
TC_LOG_DEBUG("chat.log." TYPE, __VA_ARGS__); \
else \
TC_LOG_DEBUG("chat.log.addon." TYPE, __VA_ARGS__);
#define TC_LOG_CHAT(TYPE, MESSAGE, ...) \
if (lang != LANG_ADDON && lang != LANG_ADDON_LOGGED) \
TC_LOG_DEBUG("chat.log." TYPE, MESSAGE, __VA_ARGS__); \
else \
TC_LOG_DEBUG("chat.log.addon." TYPE, MESSAGE, __VA_ARGS__);

class ChatLogScript : public PlayerScript
{
Expand Down

0 comments on commit e121ed8

Please sign in to comment.