-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.h
More file actions
31 lines (24 loc) · 877 Bytes
/
Copy pathlog.h
File metadata and controls
31 lines (24 loc) · 877 Bytes
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
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#include <time.h>
#define LOG_DEBUG 0
#define LOG_INFO 1
#define LOG_WARN 2
#define LOG_ERROR 3
extern int LOG_LEVEL;
static const char* level_str[] = {"DEBUG", "INFO ", "WARN ", "ERROR"};
#define log_msg(level, fmt, ...) do { \
if (level >= LOG_LEVEL) { \
time_t t = time(NULL); \
struct tm *tm_info = localtime(&t); \
char timebuf[20]; \
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_info); \
printf("[%s] [%s] " fmt "\n", timebuf, level_str[level], ##__VA_ARGS__); \
} \
} while(0)
#define log_debug(fmt, ...) log_msg(LOG_DEBUG, fmt, ##__VA_ARGS__)
#define log_info(fmt, ...) log_msg(LOG_INFO, fmt, ##__VA_ARGS__)
#define log_warn(fmt, ...) log_msg(LOG_WARN, fmt, ##__VA_ARGS__)
#define log_error(fmt, ...) log_msg(LOG_ERROR, fmt, ##__VA_ARGS__)
#endif