feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
#include "Debug/Logger.h"
|
|
|
|
|
#include "Debug/ConsoleLogSink.h"
|
|
|
|
|
#include <chrono>
|
2026-04-05 03:34:25 +08:00
|
|
|
#include <functional>
|
|
|
|
|
#include <thread>
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Debug {
|
|
|
|
|
|
|
|
|
|
Logger& Logger::Get() {
|
|
|
|
|
static Logger instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::Initialize() {
|
|
|
|
|
if (m_initialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::Shutdown() {
|
|
|
|
|
for (auto& sink : m_sinks) {
|
|
|
|
|
sink->Flush();
|
|
|
|
|
}
|
|
|
|
|
m_sinks.clear();
|
|
|
|
|
m_initialized = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::AddSink(std::unique_ptr<ILogSink> sink) {
|
|
|
|
|
std::lock_guard<Threading::Mutex> lock(m_mutex);
|
|
|
|
|
m_sinks.push_back(std::move(sink));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::RemoveSink(ILogSink* sink) {
|
|
|
|
|
std::lock_guard<Threading::Mutex> lock(m_mutex);
|
|
|
|
|
for (auto it = m_sinks.begin(); it != m_sinks.end(); ++it) {
|
|
|
|
|
if (it->get() == sink) {
|
|
|
|
|
m_sinks.erase(it);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Log(LogLevel level, LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(
|
|
|
|
|
level,
|
|
|
|
|
category,
|
|
|
|
|
message,
|
|
|
|
|
location.file_name(),
|
|
|
|
|
static_cast<int32_t>(location.line()),
|
|
|
|
|
location.function_name());
|
|
|
|
|
}
|
|
|
|
|
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
void Logger::Log(LogLevel level, LogCategory category,
|
|
|
|
|
const Containers::String& message, const char* file,
|
|
|
|
|
int32_t line, const char* function) {
|
|
|
|
|
if (!m_initialized) {
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (level < m_minimumLevel) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int categoryIndex = static_cast<int>(category);
|
|
|
|
|
if (categoryIndex < 0 || categoryIndex > 10 || !m_categoryEnabled[categoryIndex]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LogEntry entry;
|
|
|
|
|
entry.level = level;
|
|
|
|
|
entry.category = category;
|
|
|
|
|
entry.message = message;
|
|
|
|
|
entry.file = file ? file : "";
|
|
|
|
|
entry.line = line;
|
|
|
|
|
entry.function = function ? function : "";
|
|
|
|
|
entry.timestamp = std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
|
entry.threadId = static_cast<uint32_t>(std::hash<std::thread::id>{}(std::this_thread::get_id()));
|
|
|
|
|
|
|
|
|
|
std::lock_guard<Threading::Mutex> lock(m_mutex);
|
|
|
|
|
for (auto& sink : m_sinks) {
|
|
|
|
|
sink->Log(entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Verbose(
|
|
|
|
|
LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(LogLevel::Verbose, category, message, location);
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Debug(
|
|
|
|
|
LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(LogLevel::Debug, category, message, location);
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Info(
|
|
|
|
|
LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(LogLevel::Info, category, message, location);
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Warning(
|
|
|
|
|
LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(LogLevel::Warning, category, message, location);
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Error(
|
|
|
|
|
LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(LogLevel::Error, category, message, location);
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 03:34:25 +08:00
|
|
|
void Logger::Fatal(
|
|
|
|
|
LogCategory category,
|
|
|
|
|
const Containers::String& message,
|
|
|
|
|
const std::source_location& location) {
|
|
|
|
|
Log(LogLevel::Fatal, category, message, location);
|
feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal)
- LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等)
- ILogSink: 日志输出接口
- ConsoleLogSink: 控制台输出, 支持Windows颜色
- FileLogSink: 文件日志输出
- FileWriter: 文件写入器
- Logger: 日志管理器, 支持多sink, 分类控制
- Profiler: 性能分析器
- 单元测试覆盖
2026-03-13 20:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::SetMinimumLevel(LogLevel level) {
|
|
|
|
|
m_minimumLevel = level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::SetCategoryEnabled(LogCategory category, bool enabled) {
|
|
|
|
|
m_categoryEnabled[static_cast<int>(category)] = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Debug
|
|
|
|
|
} // namespace XCEngine
|