ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Log.h
浏览该文件的文档.
1#pragma once
2
3#include <memory>
4#include <utility>
5#include <cstdlib>
6#include <functional>
7#include <string>
8
9#include <spdlog/spdlog.h>
10#include <spdlog/sinks/stdout_color_sinks.h>
11#include <spdlog/sinks/basic_file_sink.h>
12#include <spdlog/fmt/ostr.h>
13#include "Core.h"
14
15namespace Shit {
19 class SHIT_API Log {
20 public:
21 static bool Init(); //初始化日志
22
23 //静态调用API
24 template<typename... Args>
25 inline static void Trace(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->trace(fmt, std::forward<Args>(args)...); }
26
27 template<typename... Args>
28 inline static void Debug(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->debug(fmt, std::forward<Args>(args)...); }
29
30 template<typename... Args>
31 inline static void Info(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->info(fmt, std::forward<Args>(args)...); }
32
33 template<typename... Args>
34 inline static void Warn(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->warn(fmt, std::forward<Args>(args)...); }
35
36 template<typename... Args>
37 inline static void Error(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->error(fmt, std::forward<Args>(args)...); }
38
39 template<typename... Args>
40 inline static void Critical(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->critical(fmt, std::forward<Args>(args)...); }
41
42 inline static std::shared_ptr<spdlog::logger>& GetCoreLogger() { return s_CoreLogger; }
43 inline static std::shared_ptr<spdlog::logger>& GetClientLogger() { return s_ClientLogger; }
44
45 // --- 日志转发(编辑器日志面板接入) ---
47 using MessageCallback = std::function<void(bool isCore, int level, const std::string& message)>;
51 static const MessageCallback& GetMessageCallback() { return s_messageCallback; }
52
57 static void SetLogDirectory(const std::string& dir);
58
59 private:
60 static std::shared_ptr<spdlog::logger> s_CoreLogger;
61 static std::shared_ptr<spdlog::logger> s_ClientLogger;
62 static MessageCallback s_messageCallback;
63 };
64}
65
66// Core 日志宏(空指针安全:Log::Init 之前调用时静默跳过,避免引擎上下文构造期间崩溃)
67#define ST_CORE_TRACE(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->trace(__VA_ARGS__); } while (0)
68#define ST_CORE_DEBUG(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->debug(__VA_ARGS__); } while (0)
69#define ST_CORE_INFO(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->info(__VA_ARGS__); } while (0)
70#define ST_CORE_WARN(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->warn(__VA_ARGS__); } while (0)
71#define ST_CORE_ERROR(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->error(__VA_ARGS__); } while (0)
72#define ST_CORE_CRITICAL(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->critical(__VA_ARGS__); } while (0)
73
74//Client 日志宏(空指针安全)
75#define ST_TRACE(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->trace(__VA_ARGS__); } while (0)
76#define ST_DEBUG(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->debug(__VA_ARGS__); } while (0)
77#define ST_INFO(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->info(__VA_ARGS__); } while (0)
78#define ST_WARN(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->warn(__VA_ARGS__); } while (0)
79#define ST_ERROR(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->error(__VA_ARGS__); } while (0)
80#define ST_CRITICAL(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->critical(__VA_ARGS__); } while (0)
81
82// ════════════════════════════════════════════════════════════
83// 断言(错误处理契约的"逻辑不变量"层)
84//
85// 契约约定:
86// - 致命初始化失败 → return false + ST_CORE_ERROR
87// - 非致命降级 → return nullptr + ST_CORE_WARN
88// - 逻辑不变量(不应发生)→ ST_CORE_ASSERT(Debug 拦截,Release 编译为 no-op)
89//
90// 用于"若此处为假则代码逻辑一定有 bug"的检查,如"System 必须有 scene"。
91// 不要用于用户输入/可恢复错误(那应该走 WARN + fallback)。
92// ════════════════════════════════════════════════════════════
93#ifdef NDEBUG
94 // Release:断言编译为 no-op
95 #define ST_CORE_ASSERT(cond, msg) ((void)0)
96 #define ST_ASSERT(cond, msg) ((void)0)
97#else
98 // Debug:cond 为假 → 记录 CRITICAL 并 abort
99 #define ST_CORE_ASSERT(cond, msg) \
100 do { if (!(cond)) { \
101 if (auto lg = ::Shit::Log::GetCoreLogger()) \
102 lg->critical("ASSERT FAILED: {} -- {}", std::string(#cond), std::string(msg)); \
103 std::abort(); \
104 } } while (0)
105 #define ST_ASSERT(cond, msg) \
106 do { if (!(cond)) { \
107 if (auto lg = ::Shit::Log::GetClientLogger()) \
108 lg->critical("ASSERT FAILED: {} -- {}", std::string(#cond), std::string(msg)); \
109 std::abort(); \
110 } } while (0)
111#endif
#define SHIT_API
定义 Core.h:14
日志封装类
定义 Log.h:19
static void SetLogDirectory(const std::string &dir)
设置文件日志目录(P32:编辑器打开项目后把日志写入项目 .shitengine/log) dir 为最终日志目录(引擎内部不再拼接子目录);空 = 恢复默认(进程工作目录/....
static std::shared_ptr< spdlog::logger > & GetCoreLogger()
定义 Log.h:42
static void Trace(fmt::format_string< Args... > fmt, Args &&... args)
定义 Log.h:25
static void Debug(fmt::format_string< Args... > fmt, Args &&... args)
定义 Log.h:28
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
定义 Log.h:37
static void Warn(fmt::format_string< Args... > fmt, Args &&... args)
定义 Log.h:34
static std::shared_ptr< spdlog::logger > & GetClientLogger()
定义 Log.h:43
static void SetMessageCallback(MessageCallback cb)
注册全局日志转发回调(Init 前/后均可;重新注册替换旧回调;传 nullptr 解除转发)
std::function< void(bool isCore, int level, const std::string &message)> MessageCallback
日志消息回调:isCore = 引擎日志(Shit)/ 用户日志(App);level = spdlog 等级;message = 文本
定义 Log.h:47
static void Info(fmt::format_string< Args... > fmt, Args &&... args)
定义 Log.h:31
static const MessageCallback & GetMessageCallback()
当前回调(供引擎内部 sink 读取)
定义 Log.h:51
static bool Init()
static void Critical(fmt::format_string< Args... > fmt, Args &&... args)
定义 Log.h:40
定义 AnimationClip.h:12