feat: spdlog 显式初始化——控制台彩色 + 文件双 sink
Application 构造时 Logger::Initialize(APP logger:stderr 彩色 + HelloVulkan.log 双写,warn 以上立即 flush),析构 Shutdown。
This commit is contained in:
@@ -1,3 +1,51 @@
|
||||
//
|
||||
// Created by atdunbg on 2026/9/20.
|
||||
//
|
||||
#include <Engine/Core/Log.h>
|
||||
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
|
||||
namespace Engine::Logger
|
||||
{
|
||||
static bool IsInited = false;
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
if (IsInited)
|
||||
{
|
||||
throw std::runtime_error("spdlog already inited");
|
||||
}
|
||||
IsInited = true;
|
||||
|
||||
// 控制台彩色 sink (多线程安全)
|
||||
auto console = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
|
||||
console->set_pattern("%^[%T.%e] [%l] [%n] %v%$");
|
||||
|
||||
// 文件
|
||||
auto file = std::make_shared<spdlog::sinks::basic_file_sink_mt>("HelloVulkan.log", true);
|
||||
file->set_pattern("[%Y-%m-%d %T.%e] [%l] [%t] %v");
|
||||
|
||||
// 多sink 组合默认 logger
|
||||
|
||||
// auto logger = std::make_shared<spdlog::logger>("hello", spdlog::sinks_init_list{console, file});
|
||||
auto logger = std::make_shared<spdlog::logger>("APP", spdlog::sinks_init_list{console, file});
|
||||
logger->set_level(spdlog::level::trace);
|
||||
logger->flush_on(spdlog::level::warn); // warn 及以上立即 flush,避免崩溃丢日志
|
||||
|
||||
spdlog::set_default_logger(logger);
|
||||
}
|
||||
|
||||
|
||||
void Shutdown()
|
||||
{
|
||||
if (!IsInited)
|
||||
{
|
||||
throw std::runtime_error("spdlog already shutdown or not init");
|
||||
}
|
||||
IsInited = false;
|
||||
|
||||
spdlog::shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user