Files
XCEngine/docs/api/debug/debug-logger.md

3.2 KiB
Raw Blame History

Logger

命名空间: XCEngine::Debug

类型: class (singleton)

头文件: XCEngine/Debug/Logger.h

描述: 全局日志记录器单例,提供多级别、多分类的日志输出。

概述

Logger 是 XCEngine 的核心日志系统单例。它支持多个日志输出目标Sink、日志级别过滤、分类开关并提供宏方便使用。

单例访问

方法 描述
static Logger& Get() 获取单例实例

公共方法

生命周期

方法 描述
void Initialize() 初始化日志系统
void Shutdown() 关闭日志系统

Sink 管理

方法 描述
void AddSink(std::unique_ptr<ILogSink> sink) 添加日志输出目标
void RemoveSink(ILogSink* sink) 移除日志输出目标

日志记录

方法 描述
void Log(LogLevel level, LogCategory category, const Containers::String& message, const char* file = nullptr, int32_t line = 0, const char* function = nullptr) 通用日志记录
void Verbose(LogCategory category, const Containers::String& message) Verbose 级别日志
void Debug(LogCategory category, const Containers::String& message) Debug 级别日志
void Info(LogCategory category, const Containers::String& message) Info 级别日志
void Warning(LogCategory category, const Containers::String& message) Warning 级别日志
void Error(LogCategory category, const Containers::String& message) Error 级别日志
void Fatal(LogCategory category, const Containers::String& message) Fatal 级别日志

配置

方法 描述
void SetMinimumLevel(LogLevel level) 设置最小日志级别
void SetCategoryEnabled(LogCategory category, bool enabled) 开启/关闭指定分类

宏定义

XE_LOG

#define XE_LOG(category, level, message) \
    XCEngine::Debug::Logger::Get().Log(level, category, message, __FILE__, __LINE__, __FUNCTION__)

通用日志宏,自动填充文件名、行号和函数名。

XE_ASSERT

#define XE_ASSERT(condition, message) \
    if (!(condition)) { \
        XCEngine::Debug::Logger::Get().Fatal(XCEngine::Debug::LogCategory::General, message); \
    }

断言宏,条件失败时记录 Fatal 日志。

使用示例

#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");

相关文档