Files
XCSDD/docs/api/debug/logger/logger.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +08:00

93 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Logger
**命名空间**: `XCEngine::Debug`
**类型**: `class` (singleton)
**头文件**: `XCEngine/Debug/Logger.h`
**描述**: 全局日志记录器单例,提供多级别、多分类的日志输出。
## 概述
`Logger` 是 XCEngine 的核心日志系统单例。它支持多个日志输出目标Sink、日志级别过滤、分类开关并提供宏方便使用。
## 单例访问
| 方法 | 描述 |
|------|------|
| `static Logger& Get()` | 获取单例实例 |
## 公共方法
| 方法 | 描述 |
|------|------|
| `void Initialize()` | [初始化日志系统](initialize.md) |
| `void Shutdown()` | [关闭日志系统](shutdown.md) |
| `void AddSink(std::unique_ptr<ILogSink> sink)` | [添加日志输出目标](addsink.md) |
| `void RemoveSink(ILogSink* sink)` | [移除日志输出目标](removesink.md) |
| `void Log(...)` | [通用日志记录](log.md) |
| `void Verbose(...)` | [Verbose 级别日志](verbose.md) |
| `void Debug(...)` | [Debug 级别日志](debug.md) |
| `void Info(...)` | [Info 级别日志](info.md) |
| `void Warning(...)` | [Warning 级别日志](warning.md) |
| `void Error(...)` | [Error 级别日志](error.md) |
| `void Fatal(...)` | [Fatal 级别日志](fatal.md) |
| `void SetMinimumLevel(...)` | [设置最小日志级别](setminimumlevel.md) |
| `void SetCategoryEnabled(...)` | [开启/关闭指定分类](setcategoryenabled.md) |
## 宏定义
### XE_LOG
```cpp
#define XE_LOG(category, level, message) \
XCEngine::Debug::Logger::Get().Log(level, category, message, __FILE__, __LINE__, __FUNCTION__)
```
通用日志宏,自动填充文件名、行号和函数名。
### XE_ASSERT
```cpp
#define XE_ASSERT(condition, message) \
if (!(condition)) { \
XCEngine::Debug::Logger::Get().Fatal(XCEngine::Debug::LogCategory::General, message); \
}
```
断言宏,条件失败时记录 Fatal 日志。
## 使用示例
```cpp
#include <XCEngine/Debug/Logger.h>
// 初始化
Logger::Get().Initialize();
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
Logger::Get().AddSink(std::make_unique<FileLogSink>("app.log"));
// 设置日志级别
Logger::Get().SetMinimumLevel(LogLevel::Debug);
// 使用便捷方法
Logger::Get().Info(LogCategory::Rendering, "Render started");
Logger::Get().Warning(LogCategory::Memory, "High memory usage");
Logger::Get().Error(LogCategory::FileSystem, "Failed to load file");
// 使用宏(自动带位置信息)
XE_LOG(LogCategory::General, LogLevel::Info, "Application initialized");
// 使用断言
XE_ASSERT(ptr != nullptr, "Pointer must not be null");
```
## 相关文档
- [Debug 模块总览](../debug.md) - 返回模块总览
- [ILogSink](../ilogsink/ilogsink.md) - 日志输出接口
- [ConsoleLogSink](../consolelogsink/consolelogsink.md) - 控制台输出
- [FileLogSink](../filelogsink/filelogsink.md) - 文件输出
- [LogLevel](../loglevel/loglevel.md) - 日志级别枚举