-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogSystem.hpp
More file actions
76 lines (58 loc) · 2.23 KB
/
LogSystem.hpp
File metadata and controls
76 lines (58 loc) · 2.23 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
#pragma once
#include "../core/Types.hpp"
#include <cstdarg>
#include <functional>
#include <mutex>
namespace Caffeine::Debug {
enum class LogLevel : u8 {
Trace = 0,
Info,
Warn,
Error,
Fatal
};
class LogSystem {
public:
static LogSystem& instance();
void log(LogLevel level, const char* category, const char* fmt, ...);
void vlog(LogLevel level, const char* category, const char* fmt, va_list args);
void setLevel(LogLevel minLevel);
LogLevel getLevel() const;
void setCategoryEnabled(const char* category, bool enabled);
bool isCategoryEnabled(const char* category) const;
using SinkFn = std::function<void(LogLevel, const char*, const char*)>;
void addSink(SinkFn sink);
void clearSinks();
usize sinkCount() const;
static const char* levelToString(LogLevel level);
private:
LogSystem();
~LogSystem() = default;
LogSystem(const LogSystem&) = delete;
LogSystem& operator=(const LogSystem&) = delete;
struct CategoryEntry {
char name[64];
bool enabled;
};
static constexpr usize MAX_CATEGORIES = 64;
static constexpr usize MAX_SINKS = 16;
static constexpr usize MAX_MESSAGE_LENGTH = 2048;
LogLevel m_minLevel = LogLevel::Trace;
CategoryEntry m_categories[MAX_CATEGORIES]{};
usize m_categoryCount = 0;
SinkFn m_sinks[MAX_SINKS]{};
usize m_sinkCount = 0;
mutable std::mutex m_mutex;
CategoryEntry* findCategory(const char* category);
const CategoryEntry* findCategory(const char* category) const;
};
} // namespace Caffeine::Debug
#ifdef CF_DEBUG
#define CF_TRACE(cat, ...) Caffeine::Debug::LogSystem::instance().log(Caffeine::Debug::LogLevel::Trace, cat, __VA_ARGS__)
#else
#define CF_TRACE(cat, ...) ((void)0)
#endif
#define CF_INFO(cat, ...) Caffeine::Debug::LogSystem::instance().log(Caffeine::Debug::LogLevel::Info, cat, __VA_ARGS__)
#define CF_WARN(cat, ...) Caffeine::Debug::LogSystem::instance().log(Caffeine::Debug::LogLevel::Warn, cat, __VA_ARGS__)
#define CF_ERROR(cat, ...) Caffeine::Debug::LogSystem::instance().log(Caffeine::Debug::LogLevel::Error, cat, __VA_ARGS__)
#define CF_FATAL(cat, ...) Caffeine::Debug::LogSystem::instance().log(Caffeine::Debug::LogLevel::Fatal, cat, __VA_ARGS__)