feat(Core/Logging): add support fmt::format logging (#6893)
This commit is contained in:
@@ -6,9 +6,9 @@
|
|||||||
#include "GitRevision.h"
|
#include "GitRevision.h"
|
||||||
#include "StringFormat.h"
|
#include "StringFormat.h"
|
||||||
|
|
||||||
void Acore::Banner::Show(char const* applicationName, void(*log)(char const* text), void(*logExtraInfo)())
|
void Acore::Banner::Show(std::string_view applicationName, void(*log)(std::string_view text), void(*logExtraInfo)())
|
||||||
{
|
{
|
||||||
log(Acore::StringFormat("%s (%s)", GitRevision::GetFullVersion(), applicationName).c_str());
|
log(Acore::StringFormatFmt("{} ({})", GitRevision::GetFullVersion(), applicationName));
|
||||||
log("<Ctrl-C> to stop.\n");
|
log("<Ctrl-C> to stop.\n");
|
||||||
log(" █████╗ ███████╗███████╗██████╗ ██████╗ ████████╗██╗ ██╗");
|
log(" █████╗ ███████╗███████╗██████╗ ██████╗ ████████╗██╗ ██╗");
|
||||||
log(" ██╔══██╗╚══███╔╝██╔════╝██╔══██╗██╔═══██╗╚══██╔══╝██║ ██║");
|
log(" ██╔══██╗╚══███╔╝██╔════╝██╔══██╗██╔═══██╗╚══██╔══╝██║ ██║");
|
||||||
|
|||||||
+3
-5
@@ -6,13 +6,11 @@
|
|||||||
#define AZEROTHCORE_BANNER_H
|
#define AZEROTHCORE_BANNER_H
|
||||||
|
|
||||||
#include "Define.h"
|
#include "Define.h"
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace Acore
|
namespace Acore::Banner
|
||||||
{
|
{
|
||||||
namespace Banner
|
AC_COMMON_API void Show(std::string_view applicationName, void(*log)(std::string_view text), void(*logExtraInfo)());
|
||||||
{
|
|
||||||
void Show(char const* applicationName, void(*log)(char const* text), void(*logExtraInfo)());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // AZEROTHCORE_BANNER_H
|
#endif // AZEROTHCORE_BANNER_H
|
||||||
|
|||||||
@@ -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)));
|
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)
|
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)));
|
write(std::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", std::move(message), std::move(param1)));
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
#include "Define.h"
|
#include "Define.h"
|
||||||
#include "LogCommon.h"
|
#include "LogCommon.h"
|
||||||
#include "StringFormat.h"
|
#include "StringFormat.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -55,6 +54,12 @@ public:
|
|||||||
outMessage(filter, level, Acore::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...));
|
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>
|
template<typename Format, typename... Args>
|
||||||
void outCommand(uint32 account, Format&& fmt, Args&&... args)
|
void outCommand(uint32 account, Format&& fmt, Args&&... args)
|
||||||
{
|
{
|
||||||
@@ -177,6 +182,7 @@ private:
|
|||||||
void ReadLoggersFromConfig();
|
void ReadLoggersFromConfig();
|
||||||
void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn);
|
void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn);
|
||||||
void outMessage(std::string const& filter, LogLevel level, std::string&& message);
|
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);
|
void outCommand(std::string&& message, std::string&& param1);
|
||||||
|
|
||||||
std::unordered_map<uint8, AppenderCreatorFn> appenderFactory;
|
std::unordered_map<uint8, AppenderCreatorFn> appenderFactory;
|
||||||
@@ -265,4 +271,49 @@ void check_args(std::string const&, ...);
|
|||||||
#define LOG_GM(accountId__, ...) \
|
#define LOG_GM(accountId__, ...) \
|
||||||
sLog->outCommand(accountId__, __VA_ARGS__)
|
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__
|
#endif // _LOG_H__
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#ifndef _STRING_FORMAT_H_
|
#ifndef _STRING_FORMAT_H_
|
||||||
#define _STRING_FORMAT_H_
|
#define _STRING_FORMAT_H_
|
||||||
|
|
||||||
|
#include <fmt/core.h>
|
||||||
#include <fmt/printf.h>
|
#include <fmt/printf.h>
|
||||||
|
|
||||||
namespace Acore
|
namespace Acore
|
||||||
@@ -27,6 +28,20 @@ namespace Acore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default string format function.
|
||||||
|
template<typename... Args>
|
||||||
|
inline std::string StringFormatFmt(std::string_view fmt, Args&&... args)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return fmt::format(fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
catch (const fmt::format_error& formatError)
|
||||||
|
{
|
||||||
|
return fmt::format("An error occurred formatting string \"{}\": {}", fmt, formatError.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true if the given char pointer is null.
|
/// Returns true if the given char pointer is null.
|
||||||
inline bool IsFormatEmptyOrNull(char const* fmt)
|
inline bool IsFormatEmptyOrNull(char const* fmt)
|
||||||
{
|
{
|
||||||
@@ -34,7 +49,7 @@ namespace Acore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the given std::string is empty.
|
/// Returns true if the given std::string is empty.
|
||||||
inline bool IsFormatEmptyOrNull(std::string const& fmt)
|
inline bool IsFormatEmptyOrNull(std::string_view fmt)
|
||||||
{
|
{
|
||||||
return fmt.empty();
|
return fmt.empty();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,17 +91,16 @@ int main(int argc, char** argv)
|
|||||||
sLog->Initialize();
|
sLog->Initialize();
|
||||||
|
|
||||||
Acore::Banner::Show("authserver",
|
Acore::Banner::Show("authserver",
|
||||||
[](char const* text)
|
[](std::string_view text)
|
||||||
{
|
{
|
||||||
LOG_INFO("server.authserver", "%s", text);
|
FMT_LOG_INFO("server.authserver", text);
|
||||||
},
|
},
|
||||||
[]()
|
[]()
|
||||||
{
|
{
|
||||||
LOG_INFO("server.authserver", "> Using configuration file %s.", sConfigMgr->GetFilename().c_str());
|
FMT_LOG_INFO("server.authserver", "> Using configuration file {}", sConfigMgr->GetFilename());
|
||||||
LOG_INFO("server.authserver", "> Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
|
FMT_LOG_INFO("server.authserver", "> Using SSL version: {} (library: {})", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
|
||||||
LOG_INFO("server.authserver", "> Using Boost version: %i.%i.%i", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
|
FMT_LOG_INFO("server.authserver", "> Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// authserver PID file creation
|
// authserver PID file creation
|
||||||
std::string pidFile = sConfigMgr->GetOption<std::string>("PidFile", "");
|
std::string pidFile = sConfigMgr->GetOption<std::string>("PidFile", "");
|
||||||
|
|||||||
@@ -199,17 +199,16 @@ int main(int argc, char** argv)
|
|||||||
sLog->Initialize();
|
sLog->Initialize();
|
||||||
|
|
||||||
Acore::Banner::Show("worldserver-daemon",
|
Acore::Banner::Show("worldserver-daemon",
|
||||||
[](char const* text)
|
[](std::string_view text)
|
||||||
{
|
{
|
||||||
LOG_INFO("server.worldserver", "%s", text);
|
FMT_LOG_INFO("server.worldserver", text);
|
||||||
},
|
},
|
||||||
[]()
|
[]()
|
||||||
{
|
{
|
||||||
LOG_INFO("server.worldserver", "> Using configuration file: %s", sConfigMgr->GetFilename().c_str());
|
FMT_LOG_INFO("server.worldserver", "> Using configuration file {}", sConfigMgr->GetFilename());
|
||||||
LOG_INFO("server.worldserver", "> Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
|
FMT_LOG_INFO("server.worldserver", "> Using SSL version: {} (library: {})", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
|
||||||
LOG_INFO("server.worldserver", "> Using Boost version: %i.%i.%i", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
|
FMT_LOG_INFO("server.worldserver", "> Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
OpenSSLCrypto::threadsSetup();
|
OpenSSLCrypto::threadsSetup();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user