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