Core/Logging: Improved generated code for log statements (length of text is computed at compile time)

This commit is contained in:
Shauren
2024-03-15 20:46:10 +01:00
parent 783f9c0ea3
commit e121ed81d1
4 changed files with 46 additions and 37 deletions

View File

@@ -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;
@@ -129,43 +141,40 @@ class TC_COMMON_API Log
#define sLog Log::instance()
#ifdef PERFORMANCE_PROFILING
#define TC_LOG_MESSAGE_BODY(filterType__, level__, ...) ((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__); \
#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__, message__, ...) ((void)0)
#elif TRINITY_PLATFORM != TRINITY_PLATFORM_WINDOWS
#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

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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
{