feat(Core/Logging): add support fmt::format logging (#6893)

This commit is contained in:
Kargatum
2021-08-01 04:54:55 +07:00
committed by GitHub
parent 87b2cd1299
commit dc1945196a
7 changed files with 90 additions and 23 deletions

View File

@@ -222,6 +222,11 @@ void Log::outMessage(std::string const& filter, LogLevel level, std::string&& me
write(std::make_unique<LogMessage>(level, filter, std::move(message)));
}
void Log::_outMessageFmt(std::string const& filter, LogLevel level, std::string&& message)
{
write(std::make_unique<LogMessage>(level, filter, std::move(message)));
}
void Log::outCommand(std::string&& message, std::string&& param1)
{
write(std::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", std::move(message), std::move(param1)));

View File

@@ -9,7 +9,6 @@
#include "Define.h"
#include "LogCommon.h"
#include "StringFormat.h"
#include <memory>
#include <unordered_map>
#include <vector>
@@ -55,6 +54,12 @@ public:
outMessage(filter, level, Acore::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...));
}
template<typename... Args>
inline void outMessageFmt(std::string const& filter, LogLevel const level, std::string_view fmt, Args&&... args)
{
_outMessageFmt(filter, level, fmt::format(fmt, std::forward<Args>(args)...));
}
template<typename Format, typename... Args>
void outCommand(uint32 account, Format&& fmt, Args&&... args)
{
@@ -177,6 +182,7 @@ private:
void ReadLoggersFromConfig();
void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn);
void outMessage(std::string const& filter, LogLevel level, std::string&& message);
void _outMessageFmt(std::string const& filter, LogLevel level, std::string&& message);
void outCommand(std::string&& message, std::string&& param1);
std::unordered_map<uint8, AppenderCreatorFn> appenderFactory;
@@ -265,4 +271,49 @@ void check_args(std::string const&, ...);
#define LOG_GM(accountId__, ...) \
sLog->outCommand(accountId__, __VA_ARGS__)
// New format logging
#define FMT_LOG_EXCEPTION_FREE(filterType__, level__, ...) \
{ \
try \
{ \
sLog->outMessageFmt(filterType__, level__, fmt::format(__VA_ARGS__)); \
} \
catch (const std::exception& e) \
{ \
sLog->outMessageFmt("server", LogLevel::LOG_LEVEL_ERROR, "Wrong format occurred ({}) at '{}:{}'", \
e.what(), __FILE__, __LINE__); \
} \
}
#define FMT_LOG_MESSAGE_BODY(filterType__, level__, ...) \
do \
{ \
if (sLog->ShouldLog(filterType__, level__)) \
FMT_LOG_EXCEPTION_FREE(filterType__, level__, __VA_ARGS__); \
} while (0)
// Fatal - 1
#define FMT_LOG_FATAL(filterType__, ...) \
FMT_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_FATAL, __VA_ARGS__)
// Error - 2
#define FMT_LOG_ERROR(filterType__, ...) \
FMT_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_ERROR, __VA_ARGS__)
// Warning - 3
#define FMT_LOG_WARN(filterType__, ...) \
FMT_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_WARN, __VA_ARGS__)
// Info - 4
#define FMT_LOG_INFO(filterType__, ...) \
FMT_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_INFO, __VA_ARGS__)
// Debug - 5
#define FMT_LOG_DEBUG(filterType__, ...) \
FMT_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_DEBUG, __VA_ARGS__)
// Trace - 6
#define FMT_LOG_TRACE(filterType__, ...) \
FMT_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_TRACE, __VA_ARGS__)
#endif // _LOG_H__