-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.hpp
79 lines (62 loc) · 3.59 KB
/
log.hpp
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
#pragma once
#include "json.hpp"
using json = nlohmann::ordered_json;
StringWrapper* logStringWrapper;
void server_log_callback(const char * level, const char * function, int line, const char * message, const json & extra) {
json log = json{
{"timestamp", time(nullptr)},
};
log.merge_patch({
{"level", level},
{"function", function},
{"line", line},
{"msg", message},
});
if (!extra.empty()) {
log.merge_patch(extra);
}
std::string str = log.dump(-1, ' ', false, json::error_handler_t::replace);
printf("%s\n", str.c_str());
if(level != "DEBUG" && logStringWrapper != nullptr) logStringWrapper->AddContent(str+"\n");
fflush(stdout);
}
#define LOG_INFO( MSG, ...) server_log_callback("INFO", __func__, __LINE__, MSG, __VA_ARGS__)
#define LOG_WARNING(MSG, ...) server_log_callback("WARN", __func__, __LINE__, MSG, __VA_ARGS__)
#define LOG_ERROR( MSG, ...) server_log_callback("ERR", __func__, __LINE__, MSG, __VA_ARGS__)
#define LOG_DEBUG( MSG, ...) server_log_callback("DEBUG", __func__, __LINE__, MSG, __VA_ARGS__)
#define SLT_LOG(level, slot, fmt, ...) \
do { \
char formatted_msg[512]; \
snprintf(formatted_msg, sizeof(formatted_msg), \
"slot %12.*s: id %2d | task %d | " fmt, \
12, __func__, (slot).id, (slot).id_task, __VA_ARGS__); \
server_log_callback(level, __func__, __LINE__, formatted_msg, json{}); \
} while (0)
#define SLT_INF(slot, fmt, ...) SLT_LOG("INFO", slot, fmt, __VA_ARGS__)
#define SLT_WRN(slot, fmt, ...) SLT_LOG("WARN", slot, fmt, __VA_ARGS__)
#define SLT_ERR(slot, fmt, ...) SLT_LOG("ERR", slot, fmt, __VA_ARGS__)
#define SLT_DBG(slot, fmt, ...) SLT_LOG("DEBUG", slot, fmt, __VA_ARGS__)
#define SRV_LOG(level, fmt, ...) \
do { \
char formatted_msg[512]; \
snprintf(formatted_msg, sizeof(formatted_msg), \
"srv %12.*s: " fmt, \
12, __func__, __VA_ARGS__); \
server_log_callback(level, __func__, __LINE__, formatted_msg, json{}); \
} while (0)
#define SRV_INF(fmt, ...) SRV_LOG("INFO", fmt, __VA_ARGS__)
#define SRV_WRN(fmt, ...) SRV_LOG("WARN", fmt, __VA_ARGS__)
#define SRV_ERR(fmt, ...) SRV_LOG("ERR", fmt, __VA_ARGS__)
#define SRV_DBG(fmt, ...) SRV_LOG("DEBUG", fmt, __VA_ARGS__)
#define QUE_LOG(level, fmt, ...) \
do { \
char formatted_msg[512]; \
snprintf(formatted_msg, sizeof(formatted_msg), \
"que %12.*s: " fmt, \
12, __func__, __VA_ARGS__); \
server_log_callback(level, __func__, __LINE__, formatted_msg, json{}); \
} while (0)
#define QUE_INF(fmt, ...) QUE_LOG("INFO", fmt, __VA_ARGS__)
#define QUE_WRN(fmt, ...) QUE_LOG("WARN", fmt, __VA_ARGS__)
#define QUE_ERR(fmt, ...) QUE_LOG("ERR", fmt, __VA_ARGS__)
#define QUE_DBG(fmt, ...) QUE_LOG("DEBUG", fmt, __VA_ARGS__)