This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLogger.h
98 lines (86 loc) · 2.96 KB
/
Logger.h
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
#pragma once
#define LOG_DEBUG(str, ...) Logger::Log(Logger::LogLevel::DEBUG, LOG_SECTION, str, __VA_ARGS__)
#define LOG_INFO(str, ...) Logger::Log(Logger::LogLevel::INFO, LOG_SECTION, str, __VA_ARGS__)
#define LOG_WARN(str, ...) Logger::Log(Logger::LogLevel::WARN, LOG_SECTION, str, __VA_ARGS__)
#define LOG_ERROR(str, ...) Logger::Log(Logger::LogLevel::ERROR_, LOG_SECTION, str, __VA_ARGS__)
#define LOG_FATAL(str, ...) Logger::Log(Logger::LogLevel::FATAL, LOG_SECTION, str, __VA_ARGS__)
#define LOG_VA_DEBUG(str, va) Logger::Log(Logger::LogLevel::DEBUG, LOG_SECTION, str, va)
#define LOG_VA_INFO(str, va) Logger::Log(Logger::LogLevel::INFO, LOG_SECTION, str, va)
#define LOG_VA_WARN(str, va) Logger::Log(Logger::LogLevel::WARN, LOG_SECTION, str, va)
#define LOG_VA_ERROR(str, va) Logger::Log(Logger::LogLevel::ERROR_, LOG_SECTION, str, va)
#define LOG_VA_FATAL(str, va) Logger::Log(Logger::LogLevel::FATAL, LOG_SECTION, str, va)
#include <functional>
#include <memory>
#include <thread>
class Logger {
public:
enum class LogLevel : uint8_t {
UNKNOWN,
DEBUG,
INFO,
WARN,
ERROR_,
FATAL
};
static bool Setup();
inline static uint32_t GetThreadId() {
auto id = std::this_thread::get_id();
return *(uint32_t*)&id;
}
template<typename... Args>
static void Log(LogLevel level, const char* section, const char* str, Args... args) {
auto size = snprintf(nullptr, 0, str, args...) + 1;
auto buf = std::make_unique<char[]>(size);
snprintf(buf.get(), size, str, args...);
printf("%s%s - %d %s: %s\n%s", Logger::LevelAsColor(level), Logger::LevelAsString(level), GetThreadId(), section, buf.get(), Logger::ResetColor);
if (Callback) {
Callback(level, section, buf.get());
}
}
static void Log(LogLevel level, const char* section, const char* str, va_list args) {
auto size = vsnprintf(nullptr, 0, str, args) + 1;
auto buf = std::make_unique<char[]>(size);
vsnprintf(buf.get(), size, str, args);
printf("%s%s - %d %s: %s\n%s", Logger::LevelAsColor(level), Logger::LevelAsString(level), GetThreadId(), section, buf.get(), Logger::ResetColor);
if (Callback) {
Callback(level, section, buf.get());
}
}
static constexpr const char* LevelAsString(LogLevel level) {
switch (level)
{
case LogLevel::DEBUG:
return "Debug";
case LogLevel::INFO:
return "Info ";
case LogLevel::WARN:
return "Warn ";
case LogLevel::ERROR_:
return "Error";
case LogLevel::FATAL:
return "Fatal";
default:
return "Unkwn";
}
}
static constexpr const char* LevelAsColor(LogLevel level) {
switch (level)
{
case LogLevel::DEBUG:
return "\33[0;37m";
case LogLevel::INFO:
return "\33[0;92m";
case LogLevel::WARN:
return "\33[0;93m";
case LogLevel::ERROR_:
return "\33[0;91m";
case LogLevel::FATAL:
return "\33[0;31m";
default:
return "\33[0;95m";
}
}
static constexpr const char* ResetColor = "\33[0m";
using callback = std::function<void(LogLevel, const char*, const char*)>;
static inline callback Callback = nullptr;
};