refactor(logging): 统一日志消息添加类别并修复空格式字符串问题

- 为所有日志消息添加类别标识符
- 修复日志宏处理空格式字符串的问题
- 调整日志消息顺序确保服务初始化优先
- 默认关闭窗口全屏模式
- 处理GL查询字符串可能为空的情况
- 移除mingw平台的-mwindows链接标志
This commit is contained in:
ChestnutYueyue 2026-02-23 04:04:42 +08:00
parent f46978fc31
commit d3973cd820
8 changed files with 31 additions and 22 deletions

View File

@ -166,6 +166,8 @@ std::string format_str(const char *fmt, Args &&...args) {
detail::format_impl(result, fmt, std::forward<Args>(args)...);
return result;
}
inline std::string format_str(const char *fmt) { return fmt; }
} // namespace extra2d
// 日志宏
@ -181,8 +183,10 @@ std::string format_str(const char *fmt, Args &&...args) {
do { \
if (auto log = ::extra2d::ServiceLocator::instance() \
.tryGet<::extra2d::ILogger>()) \
if (log->enabled(lvl)) \
log->log(lvl, ::extra2d::format_str("[{}] {}", cat, __VA_ARGS__)); \
if (log->enabled(lvl)) { \
std::string _e2d_msg = ::extra2d::format_str(__VA_ARGS__); \
log->log(lvl, ::extra2d::format_str("[{}] {}", cat, _e2d_msg)); \
} \
} while (0)
// 带类别的日志宏

View File

@ -20,10 +20,11 @@ bool Application::init() {
if (initialized_)
return true;
E2D_INFO(CAT_APP, "应用程序初始化中:{}", name);
// 先初始化服务定位器(包括日志服务)
ServiceLocator::instance().init();
E2D_INFO(CAT_APP, "应用程序初始化中:{}", name);
window_ = new Window();
if (!window_->create({.title = name, .width = 1280, .height = 720})) {
E2D_ERROR(CAT_APP, "创建窗口失败");

View File

@ -71,7 +71,7 @@ TextureLoader::~TextureLoader() = default;
Ref<TextureAsset> TextureLoader::load(const std::string &path) {
auto data = readFile(path);
if (data.empty()) {
E2D_ERROR("Failed to read texture file: {}", path);
E2D_ERROR(CAT_ASSET, "Failed to read texture file: {}", path);
return nullptr;
}
return loadFromMemory(data.data(), data.size());
@ -87,7 +87,7 @@ Ref<TextureAsset> TextureLoader::loadFromMemory(const u8 *data, size_t size) {
&height, &channels, desiredChannels_);
if (!pixels) {
E2D_ERROR("Failed to load texture from memory: {}", stbi_failure_reason());
E2D_ERROR(CAT_ASSET, "Failed to load texture from memory: {}", stbi_failure_reason());
return nullptr;
}
@ -129,7 +129,7 @@ void TextureLoader::setDesiredChannels(int channels) {
Ref<FontAsset> FontLoader::load(const std::string &path) {
auto data = readFile(path);
if (data.empty()) {
E2D_ERROR("Failed to read font file: {}", path);
E2D_ERROR(CAT_ASSET, "Failed to read font file: {}", path);
return nullptr;
}
return loadFromMemory(data.data(), data.size());
@ -168,7 +168,7 @@ std::vector<std::string> FontLoader::extensions() const {
Ref<ShaderAsset> ShaderLoader::load(const std::string &path) {
auto data = readFile(path);
if (data.empty()) {
E2D_ERROR("Failed to read shader file: {}", path);
E2D_ERROR(CAT_ASSET, "Failed to read shader file: {}", path);
return nullptr;
}
return loadFromMemory(data.data(), data.size());
@ -253,7 +253,7 @@ bool ShaderLoader::parseCombined(const std::string &content,
Ref<AudioAsset> AudioLoader::load(const std::string &path) {
auto data = readFile(path);
if (data.empty()) {
E2D_ERROR("Failed to read audio file: {}", path);
E2D_ERROR(CAT_ASSET, "Failed to read audio file: {}", path);
return nullptr;
}
return loadFromMemory(data.data(), data.size());

View File

@ -73,14 +73,14 @@ std::shared_ptr<Sound> AudioEngine::loadSound(const std::string &name,
Mix_Chunk *chunk = Mix_LoadWAV(filePath.c_str());
if (!chunk) {
E2D_ERROR("Failed to load sound: {} ({})", filePath, Mix_GetError());
E2D_ERROR(CAT_AUDIO, "Failed to load sound: {} ({})", filePath, Mix_GetError());
return nullptr;
}
auto sound = std::shared_ptr<Sound>(new Sound(name, filePath, chunk));
sounds_[name] = sound;
E2D_DEBUG("Loaded sound: {}", filePath);
E2D_DEBUG(CAT_AUDIO, "Loaded sound: {}", filePath);
return sound;
}

View File

@ -131,11 +131,15 @@ void RenderDevice::queryCaps() {
caps_.dsaSupported =
(majorVersion > 4 || (majorVersion == 4 && minorVersion >= 5));
caps_.renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
caps_.vendor = reinterpret_cast<const char *>(glGetString(GL_VENDOR));
caps_.version = reinterpret_cast<const char *>(glGetString(GL_VERSION));
caps_.glslVersion =
reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
const char* renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
const char* vendor = reinterpret_cast<const char *>(glGetString(GL_VENDOR));
const char* version = reinterpret_cast<const char *>(glGetString(GL_VERSION));
const char* glslVersion = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
caps_.renderer = renderer ? renderer : "Unknown";
caps_.vendor = vendor ? vendor : "Unknown";
caps_.version = version ? version : "Unknown";
caps_.glslVersion = glslVersion ? glslVersion : "Unknown";
}
std::unique_ptr<Buffer> RenderDevice::createBuffer(BufferType type,

View File

@ -6,7 +6,7 @@
namespace extra2d {
Window::Window()
: sdlWindow_(nullptr), width_(1280), height_(720), fullscreen_(true),
: sdlWindow_(nullptr), width_(1280), height_(720), fullscreen_(false),
shouldClose_(false) {}
Window::~Window() { destroy(); }
@ -22,7 +22,7 @@ bool Window::create(const WindowConfig &config) {
fullscreen_ = config.fullscreen;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) {
E2D_ERROR("SDL_Init failed: {}", SDL_GetError());
E2D_ERROR(CAT_WINDOWS, "SDL_Init failed: {}", SDL_GetError());
return false;
}
@ -43,12 +43,12 @@ bool Window::create(const WindowConfig &config) {
width_, height_, windowFlags);
if (!sdlWindow_) {
E2D_ERROR("SDL_CreateWindow failed: {}", SDL_GetError());
E2D_ERROR(CAT_WINDOWS, "SDL_CreateWindow failed: {}", SDL_GetError());
SDL_Quit();
return false;
}
E2D_INFO("Window created: {}x{}", width_, height_);
E2D_INFO(CAT_WINDOWS, "Window created: {}x{}", width_, height_);
return true;
}

View File

@ -74,7 +74,7 @@ public:
// 加载字体并创建UI
loadFonts();
E2D_INFO("创建了 {} 个碰撞框", boxes_.size() + 1);
E2D_INFO(CAT_SCENE, "创建了 {} 个碰撞框", boxes_.size() + 1);
}
void onUpdate(float dt) override {

View File

@ -60,7 +60,7 @@ target("hello_world")
elseif is_plat("mingw") then
set_targetdir("../../build/examples/hello_world")
add_ldflags("-mwindows", {force = true})
-- add_ldflags("-mwindows", {force = true})
-- 复制资源到输出目录
after_build(function (target)