-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.h
72 lines (56 loc) · 2.08 KB
/
log.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
#ifndef LOGGING
#define LOGGING
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <time.h>
#define LOG_LEVEL_ERROR 4
#define LOG_LEVEL_WARN 3
#define LOG_LEVEL_INFO 2
#define LOG_LEVEL_DEBUG 1
// logger struct describes logger configuration params.
typedef struct LOGGER {
FILE* file;
bool log_file;
bool log_func;
bool log_level;
bool log_color;
} logger;
// lg stores logger configuration.
logger lg;
// init_logger initialises logger with output stream and configurations settings
// like
// log_file - whether to log the file from what the log call was made.
// log_func - whether to log the function that the log was called from.
// log_level - whether to log the logs level, like ERROR, WARN, INFO, DEBUG.
// log_color - whether to use ASCII escape codes to color the log.
void init_logger(
FILE* file,
bool log_file,
bool log_func,
bool log_level,
bool log_color
);
// log_all is not intended for external use. generates a single log line with
// the settings specified and writes it to file specified in logger settings.
int log_all(
const char* file,
int line,
const char* func,
int level,
const char* fmt,
...
);
// log_error logs the specified arguments with log level - ERROR. log_error
// expects arguments in format log_error(char* fmt, ...)
#define log_error(...) log_all(__FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_ERROR, __VA_ARGS__)
// log_warn logs the specified arguments with log level - WARN. log_error
// expects arguments in format log_warn(char* fmt, ...)
#define log_warn(...) log_all(__FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_WARN, __VA_ARGS__)
// log_info logs the specified arguments with log level - WARN. log_error
// expects arguments in format log_info(char* fmt, ...)
#define log_info(...) log_all(__FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_INFO, __VA_ARGS__)
// log_debug logs the specified arguments with log level - DEBUG. log_error
// expects arguments in format log_debug(char* fmt, ...)
#define log_debug(...) log_all(__FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_DEBUG, __VA_ARGS__)
#endif