-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.hpp
More file actions
46 lines (39 loc) · 1.3 KB
/
logging.hpp
File metadata and controls
46 lines (39 loc) · 1.3 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
#pragma once
#include <ctime>
#include <cstdarg>
#include <cstdio>
#define INFO "INF"
#define ERROR "ERR"
inline void log_message_va(const char* module, const char* level, const char* fmt, va_list args) {
FILE* out = (level[0] == 'E') ? stderr : stdout;
char ts[32];
std::time_t t = std::time(nullptr);
std::strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
std::fprintf(out, "[%s] [%s] [%s] ", ts, level, module);
std::vfprintf(out, fmt, args);
std::fprintf(out, "\n");
}
inline void log_message(const char* module, const char* level, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
log_message_va(module, level, fmt, args);
va_end(args);
}
#define LOG_INFO(module, fmt, ...) log_message(module, INFO, fmt, ##__VA_ARGS__)
#define LOG_ERROR(module, fmt, ...) log_message(module, ERROR, fmt, ##__VA_ARGS__)
class Loggable {
public:
virtual const char* module_name() const = 0;
void log_info(const char* fmt, ...) const {
va_list args;
va_start(args, fmt);
log_message_va(module_name(), INFO, fmt, args);
va_end(args);
}
void log_error(const char* fmt, ...) const {
va_list args;
va_start(args, fmt);
log_message_va(module_name(), ERROR, fmt, args);
va_end(args);
}
};