feat: 实现日志与调试系统(Debug模块)
- LogLevel: 日志级别枚举 (Verbose, Debug, Info, Warning, Error, Fatal) - LogCategory: 日志分类 (General, Rendering, Physics, Memory, Threading等) - ILogSink: 日志输出接口 - ConsoleLogSink: 控制台输出, 支持Windows颜色 - FileLogSink: 文件日志输出 - FileWriter: 文件写入器 - Logger: 日志管理器, 支持多sink, 分类控制 - Profiler: 性能分析器 - 单元测试覆盖
This commit is contained in:
109
engine/src/Debug/Logger.cpp
Normal file
109
engine/src/Debug/Logger.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "Debug/Logger.h"
|
||||
#include "Debug/ConsoleLogSink.h"
|
||||
#include <chrono>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::Verbose(LogCategory category, const Containers::String& message) {
|
||||
Log(LogLevel::Verbose, category, message);
|
||||
}
|
||||
|
||||
void Logger::Debug(LogCategory category, const Containers::String& message) {
|
||||
Log(LogLevel::Debug, category, message);
|
||||
}
|
||||
|
||||
void Logger::Info(LogCategory category, const Containers::String& message) {
|
||||
Log(LogLevel::Info, category, message);
|
||||
}
|
||||
|
||||
void Logger::Warning(LogCategory category, const Containers::String& message) {
|
||||
Log(LogLevel::Warning, category, message);
|
||||
}
|
||||
|
||||
void Logger::Error(LogCategory category, const Containers::String& message) {
|
||||
Log(LogLevel::Error, category, message);
|
||||
}
|
||||
|
||||
void Logger::Fatal(LogCategory category, const Containers::String& message) {
|
||||
Log(LogLevel::Fatal, category, message);
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user