Files
XCEngine/engine/src/Debug/Logger.cpp

142 lines
3.6 KiB
C++

#include "Debug/Logger.h"
#include "Debug/ConsoleLogSink.h"
#include <chrono>
#include <functional>
#include <thread>
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 std::source_location& location) {
Log(
level,
category,
message,
location.file_name(),
static_cast<int32_t>(location.line()),
location.function_name());
}
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,
const std::source_location& location) {
Log(LogLevel::Verbose, category, message, location);
}
void Logger::Debug(
LogCategory category,
const Containers::String& message,
const std::source_location& location) {
Log(LogLevel::Debug, category, message, location);
}
void Logger::Info(
LogCategory category,
const Containers::String& message,
const std::source_location& location) {
Log(LogLevel::Info, category, message, location);
}
void Logger::Warning(
LogCategory category,
const Containers::String& message,
const std::source_location& location) {
Log(LogLevel::Warning, category, message, location);
}
void Logger::Error(
LogCategory category,
const Containers::String& message,
const std::source_location& location) {
Log(LogLevel::Error, category, message, location);
}
void Logger::Fatal(
LogCategory category,
const Containers::String& message,
const std::source_location& location) {
Log(LogLevel::Fatal, category, message, location);
}
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