ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
Log.h
浏览该文件的文档.
1
#pragma once
2
3
#include <memory>
4
#include <utility>
5
#include <cstdlib>
6
7
#include <spdlog/spdlog.h>
8
#include <spdlog/sinks/stdout_color_sinks.h>
9
#include <spdlog/fmt/ostr.h>
10
#include "
Core.h
"
11
12
namespace
Shit
{
16
class
SHIT_API
Log
{
17
public
:
18
static
bool
Init
();
//初始化日志
19
20
//静态调用API
21
template
<
typename
... Args>
22
inline
static
void
Trace
(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->trace(fmt, std::forward<Args>(args)...); }
23
24
template
<
typename
... Args>
25
inline
static
void
Debug
(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->debug(fmt, std::forward<Args>(args)...); }
26
27
template
<
typename
... Args>
28
inline
static
void
Info
(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->info(fmt, std::forward<Args>(args)...); }
29
30
template
<
typename
... Args>
31
inline
static
void
Warn
(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->warn(fmt, std::forward<Args>(args)...); }
32
33
template
<
typename
... Args>
34
inline
static
void
Error
(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->error(fmt, std::forward<Args>(args)...); }
35
36
template
<
typename
... Args>
37
inline
static
void
Critical
(fmt::format_string<Args...> fmt, Args&&... args) { s_ClientLogger->critical(fmt, std::forward<Args>(args)...); }
38
39
inline
static
std::shared_ptr<spdlog::logger>&
GetCoreLogger
() {
return
s_CoreLogger; }
40
inline
static
std::shared_ptr<spdlog::logger>&
GetClientLogger
() {
return
s_ClientLogger; }
41
42
private
:
43
static
std::shared_ptr<spdlog::logger> s_CoreLogger;
44
static
std::shared_ptr<spdlog::logger> s_ClientLogger;
45
};
46
}
47
48
// Core 日志宏(空指针安全:Log::Init 之前调用时静默跳过,避免引擎上下文构造期间崩溃)
49
#define ST_CORE_TRACE(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->trace(__VA_ARGS__); } while (0)
50
#define ST_CORE_DEBUG(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->debug(__VA_ARGS__); } while (0)
51
#define ST_CORE_INFO(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->info(__VA_ARGS__); } while (0)
52
#define ST_CORE_WARN(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->warn(__VA_ARGS__); } while (0)
53
#define ST_CORE_ERROR(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->error(__VA_ARGS__); } while (0)
54
#define ST_CORE_CRITICAL(...) do { if (auto lg = ::Shit::Log::GetCoreLogger()) lg->critical(__VA_ARGS__); } while (0)
55
56
//Client 日志宏(空指针安全)
57
#define ST_TRACE(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->trace(__VA_ARGS__); } while (0)
58
#define ST_DEBUG(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->debug(__VA_ARGS__); } while (0)
59
#define ST_INFO(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->info(__VA_ARGS__); } while (0)
60
#define ST_WARN(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->warn(__VA_ARGS__); } while (0)
61
#define ST_ERROR(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->error(__VA_ARGS__); } while (0)
62
#define ST_CRITICAL(...) do { if (auto lg = ::Shit::Log::GetClientLogger()) lg->critical(__VA_ARGS__); } while (0)
63
64
// ════════════════════════════════════════════════════════════
65
// 断言(错误处理契约的"逻辑不变量"层)
66
//
67
// 契约约定:
68
// - 致命初始化失败 → return false + ST_CORE_ERROR
69
// - 非致命降级 → return nullptr + ST_CORE_WARN
70
// - 逻辑不变量(不应发生)→ ST_CORE_ASSERT(Debug 拦截,Release 编译为 no-op)
71
//
72
// 用于"若此处为假则代码逻辑一定有 bug"的检查,如"System 必须有 scene"。
73
// 不要用于用户输入/可恢复错误(那应该走 WARN + fallback)。
74
// ════════════════════════════════════════════════════════════
75
#ifdef NDEBUG
76
// Release:断言编译为 no-op
77
#define ST_CORE_ASSERT(cond, msg) ((void)0)
78
#define ST_ASSERT(cond, msg) ((void)0)
79
#else
80
// Debug:cond 为假 → 记录 CRITICAL 并 abort
81
#define ST_CORE_ASSERT(cond, msg) \
82
do { if (!(cond)) { \
83
if (auto lg = ::Shit::Log::GetCoreLogger()) \
84
lg->critical("ASSERT FAILED: {} -- {}", std::string(#cond), std::string(msg)); \
85
std::abort(); \
86
} } while (0)
87
#define ST_ASSERT(cond, msg) \
88
do { if (!(cond)) { \
89
if (auto lg = ::Shit::Log::GetClientLogger()) \
90
lg->critical("ASSERT FAILED: {} -- {}", std::string(#cond), std::string(msg)); \
91
std::abort(); \
92
} } while (0)
93
#endif
Core.h
SHIT_API
#define SHIT_API
定义
Core.h:14
Shit::Log
日志封装类
定义
Log.h:16
Shit::Log::GetCoreLogger
static std::shared_ptr< spdlog::logger > & GetCoreLogger()
定义
Log.h:39
Shit::Log::Trace
static void Trace(fmt::format_string< Args... > fmt, Args &&... args)
定义
Log.h:22
Shit::Log::Debug
static void Debug(fmt::format_string< Args... > fmt, Args &&... args)
定义
Log.h:25
Shit::Log::Error
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
定义
Log.h:34
Shit::Log::Warn
static void Warn(fmt::format_string< Args... > fmt, Args &&... args)
定义
Log.h:31
Shit::Log::GetClientLogger
static std::shared_ptr< spdlog::logger > & GetClientLogger()
定义
Log.h:40
Shit::Log::Info
static void Info(fmt::format_string< Args... > fmt, Args &&... args)
定义
Log.h:28
Shit::Log::Init
static bool Init()
Shit::Log::Critical
static void Critical(fmt::format_string< Args... > fmt, Args &&... args)
定义
Log.h:37
Shit
定义
AudioPlayer.h:10
include
ShitEngine
Core
Log.h
制作者
1.17.0