110 lines
2.8 KiB
C++
110 lines
2.8 KiB
C++
|
|
#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
|