-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_boii.c
246 lines (234 loc) · 5.95 KB
/
log_boii.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "log_boii.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef LOG_BOII__COLORED_LOGS
#define COLOR_TRACE "\x1b[37m"
#define COLOR_DEBUG "\x1b[36m"
#define COLOR_INFO "\x1b[32m"
#define COLOR_WARN "\x1b[35m"
#define COLOR_ERROR "\x1b[33m"
#define COLOR_FATAL "\x1b[31m"
#define COLOR_RESET "\x1b[0m"
#endif
/*
* Returns string format of `log_level`.
*
*/
const char*
log_level_string(log_level level)
{
switch (level) {
case LOG_TRACE:
return "TRACE";
case LOG_DEBUG:
return "DEBUG";
case LOG_INFO:
return "INFO ";
case LOG_WARN:
return "WARN ";
case LOG_ERROR:
return "ERROR";
default:
break;
}
return "FATAL";
}
char*
format_to_2_digits(int n)
{
char* result = (char*)malloc(sizeof(char)*3);
result[0] = '0';
result[1] = '0';
result[2] = '\0';
result[1] += n%10;
if (n < 10) {
return result;
}
result[0] += n/10;
return result;
}
/*
* Returns time struct for current time.
*
*/
struct tm*
time_info()
{
time_t rawtime;
struct tm* time_info;
time(&rawtime);
time_info = localtime(&rawtime);
return time_info;
}
/*
* Base function for all logging functions.
*
* Use `log_level` properly :)
*
* References how to use log levels properly:
* https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
* https://betterstack.com/community/guides/logging/log-levels-explained/
*
*/
void
log_boii(log_level level,
const char* function,
const char* file,
int line,
const char* format_string,
...)
{
struct tm* timeinfo = time_info();
char* formatted_hour_str = format_to_2_digits(timeinfo->tm_hour);
char* formatted_minute_str = format_to_2_digits(timeinfo->tm_min);
char* formatted_second_str = format_to_2_digits(timeinfo->tm_sec);
#ifdef LOG_BOII__COLORED_LOGS
switch (level) {
case LOG_TRACE: {
fprintf(stderr,
"[%s:%s:%s] " COLOR_TRACE "%s" COLOR_RESET " %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
break;
}
case LOG_DEBUG: {
fprintf(stderr,
"[%s:%s:%s] " COLOR_DEBUG "%s" COLOR_RESET " %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
break;
}
case LOG_INFO: {
fprintf(stderr,
"[%s:%s:%s] " COLOR_INFO "%s" COLOR_RESET " %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
break;
}
case LOG_WARN: {
#ifdef LOG_BOII__HIGHLIGHT_WARN_ERROR_FATAL_STRINGS
fprintf(stderr,
"[%s:%s:%s] " COLOR_WARN "%s %s:%s:%d: " COLOR_RESET,
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#else
fprintf(stderr,
"[%s:%s:%s] " COLOR_WARN "%s" COLOR_RESET " %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#endif
break;
}
case LOG_ERROR: {
#ifdef LOG_BOII__HIGHLIGHT_WARN_ERROR_FATAL_STRINGS
fprintf(stderr,
"[%s:%s:%s] " COLOR_ERROR "%s %s:%s:%d: " COLOR_RESET,
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#else
fprintf(stderr,
"[%s:%s:%s] " COLOR_ERROR "%s" COLOR_RESET " %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#endif
break;
}
case LOG_FATAL: {
#ifdef LOG_BOII__HIGHLIGHT_WARN_ERROR_FATAL_STRINGS
fprintf(stderr,
"[%s:%s:%s] " COLOR_FATAL "%s %s:%s:%d: " COLOR_RESET,
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#else
fprintf(stderr,
"[%s:%s:%s] " COLOR_FATAL "%s" COLOR_RESET " %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#endif
break;
}
default:
break;
}
#else
// printing usual stuff: time, log level, function, file, line
fprintf(stderr,
"[%s:%s:%s] %s %s:%s:%d: ",
formatted_hour_str,
formatted_minute_str,
formatted_second_str,
log_level_string(level),
function,
file,
line);
#endif
// handling variable arguments
va_list format_args;
va_start(format_args, format_string);
#ifdef LOG_BOII__HIGHLIGHT_WARN_ERROR_FATAL_STRINGS
if (level == LOG_WARN) {
fprintf(stderr, COLOR_WARN);
} else if (level == LOG_ERROR) {
fprintf(stderr, COLOR_ERROR);
} else if (level == LOG_FATAL) {
fprintf(stderr, COLOR_FATAL);
}
#endif
vfprintf(stderr, format_string, format_args);
#ifdef LOG_BOII__HIGHLIGHT_WARN_ERROR_FATAL_STRINGS
fprintf(stderr, COLOR_RESET);
#endif
va_end(format_args);
fprintf(stderr, "\n");
// Freeing formatted strings
free(formatted_hour_str);
free(formatted_minute_str);
free(formatted_second_str);
}