diff --git a/Engine/include/Engine/Core/Log.h b/Engine/include/Engine/Core/Log.h index 4b7f2b4..f8b08b9 100644 --- a/Engine/include/Engine/Core/Log.h +++ b/Engine/include/Engine/Core/Log.h @@ -11,6 +11,12 @@ namespace Engine { + namespace Logger + { + void Initialize(); + void Shutdown(); + } + #define ENGINE_TRACE(...) ::spdlog::trace(__VA_ARGS__) #define ENGINE_INFO(...) ::spdlog::info(__VA_ARGS__) #define ENGINE_DEBUG(...) ::spdlog::debug(__VA_ARGS__) diff --git a/Engine/src/Core/Application.cpp b/Engine/src/Core/Application.cpp index 8fbe455..fbbc7c5 100644 --- a/Engine/src/Core/Application.cpp +++ b/Engine/src/Core/Application.cpp @@ -22,6 +22,8 @@ namespace Engine { Application::Application(const std::string& title, u32 width, u32 height) { + Logger::Initialize(); + m_Window = CreateScope(title, width, height); // 事件通道:GLFW 回调 → 本类 DispatchEvent → 派生类钩子 @@ -51,6 +53,8 @@ namespace Engine { if (m_Renderer) { m_Renderer->GetDevice().GetDevice().waitIdle(); } + + Logger::Shutdown(); } void Application::EnableImGui() diff --git a/Engine/src/Core/Log.cpp b/Engine/src/Core/Log.cpp index bb80704..d2a3f05 100644 --- a/Engine/src/Core/Log.cpp +++ b/Engine/src/Core/Log.cpp @@ -1,3 +1,51 @@ // // Created by atdunbg on 2026/9/20. // +#include + +#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(); + console->set_pattern("%^[%T.%e] [%l] [%n] %v%$"); + + // 文件 + auto file = std::make_shared("HelloVulkan.log", true); + file->set_pattern("[%Y-%m-%d %T.%e] [%l] [%t] %v"); + + // 多sink 组合默认 logger + + // auto logger = std::make_shared("hello", spdlog::sinks_init_list{console, file}); + auto logger = std::make_shared("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(); + } +} +