|
@@ -20,76 +20,53 @@
|
|
|
#include <boost/beast/core.hpp> // 包含 boost::beast::error_code
|
|
|
|
|
|
namespace fmt {
|
|
|
-
|
|
|
- // 为 boost::asio::ip::basic_endpoint 提供格式化支持
|
|
|
template<typename Protocol>
|
|
|
struct formatter<boost::asio::ip::basic_endpoint<Protocol>> {
|
|
|
- // 不使用额外的格式说明符
|
|
|
+ // 无需额外格式说明符
|
|
|
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
|
|
|
template<typename FormatContext>
|
|
|
auto format(const boost::asio::ip::basic_endpoint<Protocol>& ep, FormatContext& ctx) {
|
|
|
return format_to(ctx.out(), "{}:{}", ep.address().to_string(), ep.port());
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
- // 为 boost::system::error_code 提供格式化支持
|
|
|
- template <>
|
|
|
- struct formatter<boost::system::error_code> {
|
|
|
- constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
|
|
|
- template<typename FormatContext>
|
|
|
- auto format(const boost::system::error_code& ec, FormatContext& ctx) {
|
|
|
- return format_to(ctx.out(), "{}", ec.message());
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
-} // namespace fmt
|
|
|
+}
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
namespace logger {
|
|
|
|
|
|
- // 使用静态变量持久化线程池,避免过早销毁
|
|
|
- inline std::shared_ptr<spdlog::details::thread_pool> thread_pool;
|
|
|
-
|
|
|
inline void init_async_logger() {
|
|
|
- // 设置线程池:队列大小 8192,线程数 1(一般足够日志写入)
|
|
|
- constexpr size_t queue_size = 8192;
|
|
|
- constexpr size_t thread_count = 1;
|
|
|
- thread_pool = std::make_shared<spdlog::details::thread_pool>(queue_size, thread_count);
|
|
|
+ // 使用 spdlog 官方推荐方法初始化全局线程池(队列大小 8192,线程数 1)
|
|
|
+ spdlog::init_thread_pool(8192, 1);
|
|
|
|
|
|
// 创建终端输出的 sink,并设置日志格式
|
|
|
// 格式说明:
|
|
|
- // %Y-%m-%d %H:%M:%S.%e => 时间戳,毫秒级精度
|
|
|
- // [%5l] => 日志级别,固定宽度 5(如 [INFO ])
|
|
|
+ // %Y-%m-%d %H:%M:%S.%e => 时间戳(毫秒级精度)
|
|
|
+ // [%5l] => 日志级别(固定宽度 5,如 [INFO ])
|
|
|
// [%n] => logger 名称(本例设为 "main")
|
|
|
// %v => 日志内容
|
|
|
// 输出示例:
|
|
|
// 2025-03-11 16:00:20.043 [INFO ] [main] com.incubator.game.GameServerStart - 正在启动游戏服务器...
|
|
|
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
|
|
- console_sink->set_pattern("%Y-%m-%d %H:%M:%S.%e [%5l] [%n] %v");
|
|
|
+ console_sink->set_pattern("%Y-%m-%d %H:%M:%S.%e [%5l] [%t] [%s %!:%#] %v");
|
|
|
|
|
|
- // 创建异步 logger,使用线程池以及当队列满时阻塞的策略,保证日志不丢失
|
|
|
+ // 使用 spdlog::thread_pool() 获取全局线程池
|
|
|
auto async_logger = std::make_shared<spdlog::async_logger>(
|
|
|
- "main", // logger 名称
|
|
|
- console_sink, // 输出 sink
|
|
|
- thread_pool, // 使用的线程池
|
|
|
- spdlog::async_overflow_policy::block // 队列满时阻塞
|
|
|
+ "main", // logger 名称
|
|
|
+ console_sink, // 输出 sink
|
|
|
+ spdlog::thread_pool(), // 使用全局线程池
|
|
|
+ spdlog::async_overflow_policy::block // 队列满时阻塞,保证日志不丢失
|
|
|
);
|
|
|
|
|
|
- // 注册 logger 并设为默认 logger
|
|
|
- spdlog::register_logger(async_logger);
|
|
|
+ // 将异步 logger 设为默认 logger,并设置日志等级及自动刷新策略
|
|
|
spdlog::set_default_logger(async_logger);
|
|
|
-
|
|
|
- // 设置全局日志等级为 INFO
|
|
|
spdlog::set_level(spdlog::level::info);
|
|
|
-
|
|
|
- // 定时刷新日志(例如每 3 秒刷新一次)
|
|
|
spdlog::flush_every(std::chrono::seconds(3));
|
|
|
}
|
|
|
|
|
|
// 在 main() 退出前手动调用,确保日志系统干净关闭
|
|
|
inline void shutdown_logger() {
|
|
|
spdlog::shutdown();
|
|
|
- thread_pool.reset(); // 显式释放线程池
|
|
|
+ spdlog::thread_pool().reset(); // 显式释放线程池
|
|
|
}
|
|
|
|
|
|
} // namespace logger
|