|
| 1 | +#include "LogSystem.hpp" |
| 2 | +#include <cstdio> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +namespace Caffeine::Debug { |
| 6 | + |
| 7 | +LogSystem& LogSystem::instance() { |
| 8 | + static LogSystem s; |
| 9 | + return s; |
| 10 | +} |
| 11 | + |
| 12 | +LogSystem::LogSystem() {} |
| 13 | + |
| 14 | +void LogSystem::log(LogLevel level, const char* category, const char* fmt, ...) { |
| 15 | + va_list args; |
| 16 | + va_start(args, fmt); |
| 17 | + vlog(level, category, fmt, args); |
| 18 | + va_end(args); |
| 19 | +} |
| 20 | + |
| 21 | +void LogSystem::vlog(LogLevel level, const char* category, const char* fmt, va_list args) { |
| 22 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 23 | + |
| 24 | + if (level < m_minLevel) return; |
| 25 | + |
| 26 | + if (category) { |
| 27 | + const CategoryEntry* entry = findCategory(category); |
| 28 | + if (entry && !entry->enabled) return; |
| 29 | + } |
| 30 | + |
| 31 | + char buffer[MAX_MESSAGE_LENGTH]; |
| 32 | + vsnprintf(buffer, MAX_MESSAGE_LENGTH, fmt, args); |
| 33 | + |
| 34 | + for (usize i = 0; i < m_sinkCount; ++i) { |
| 35 | + if (m_sinks[i]) { |
| 36 | + m_sinks[i](level, category ? category : "", buffer); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void LogSystem::setLevel(LogLevel minLevel) { |
| 42 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 43 | + m_minLevel = minLevel; |
| 44 | +} |
| 45 | + |
| 46 | +LogLevel LogSystem::getLevel() const { |
| 47 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 48 | + return m_minLevel; |
| 49 | +} |
| 50 | + |
| 51 | +void LogSystem::setCategoryEnabled(const char* category, bool enabled) { |
| 52 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 53 | + |
| 54 | + CategoryEntry* entry = findCategory(category); |
| 55 | + if (entry) { |
| 56 | + entry->enabled = enabled; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (m_categoryCount < MAX_CATEGORIES) { |
| 61 | + auto& e = m_categories[m_categoryCount]; |
| 62 | + strncpy(e.name, category, sizeof(e.name) - 1); |
| 63 | + e.name[sizeof(e.name) - 1] = '\0'; |
| 64 | + e.enabled = enabled; |
| 65 | + ++m_categoryCount; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +bool LogSystem::isCategoryEnabled(const char* category) const { |
| 70 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 71 | + const CategoryEntry* entry = findCategory(category); |
| 72 | + if (entry) return entry->enabled; |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +void LogSystem::addSink(SinkFn sink) { |
| 77 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 78 | + if (m_sinkCount < MAX_SINKS) { |
| 79 | + m_sinks[m_sinkCount] = std::move(sink); |
| 80 | + ++m_sinkCount; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void LogSystem::clearSinks() { |
| 85 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 86 | + for (usize i = 0; i < m_sinkCount; ++i) { |
| 87 | + m_sinks[i] = nullptr; |
| 88 | + } |
| 89 | + m_sinkCount = 0; |
| 90 | +} |
| 91 | + |
| 92 | +usize LogSystem::sinkCount() const { |
| 93 | + std::lock_guard<std::mutex> lock(m_mutex); |
| 94 | + return m_sinkCount; |
| 95 | +} |
| 96 | + |
| 97 | +const char* LogSystem::levelToString(LogLevel level) { |
| 98 | + switch (level) { |
| 99 | + case LogLevel::Trace: return "TRACE"; |
| 100 | + case LogLevel::Info: return "INFO"; |
| 101 | + case LogLevel::Warn: return "WARN"; |
| 102 | + case LogLevel::Error: return "ERROR"; |
| 103 | + case LogLevel::Fatal: return "FATAL"; |
| 104 | + } |
| 105 | + return "UNKNOWN"; |
| 106 | +} |
| 107 | + |
| 108 | +LogSystem::CategoryEntry* LogSystem::findCategory(const char* category) { |
| 109 | + for (usize i = 0; i < m_categoryCount; ++i) { |
| 110 | + if (strcmp(m_categories[i].name, category) == 0) { |
| 111 | + return &m_categories[i]; |
| 112 | + } |
| 113 | + } |
| 114 | + return nullptr; |
| 115 | +} |
| 116 | + |
| 117 | +const LogSystem::CategoryEntry* LogSystem::findCategory(const char* category) const { |
| 118 | + for (usize i = 0; i < m_categoryCount; ++i) { |
| 119 | + if (strcmp(m_categories[i].name, category) == 0) { |
| 120 | + return &m_categories[i]; |
| 121 | + } |
| 122 | + } |
| 123 | + return nullptr; |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace Caffeine::Debug |
0 commit comments