-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclog.h
More file actions
250 lines (208 loc) · 6.21 KB
/
Copy pathclog.h
File metadata and controls
250 lines (208 loc) · 6.21 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
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
247
248
249
#ifndef CLOG_H
#define CLOG_H
// Alias for Error
#ifdef LOG_LEVEL_ERROR
#define LOG_LEVEL_ERR
#endif
#ifdef LOG_LEVEL_ALL
#define LOG_LEVEL_INFO
#endif
// Sets LOG_LEVEL_INFO as default if nothing other is provided
#if !defined(LOG_LEVEL_ERR) && !defined(LOG_LEVEL_WARN) && !defined(LOG_LEVEL_INFO) && !defined(LOG_LEVEL_NONE)
#define LOG_LEVEL_INFO
#endif
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#ifndef LOG_NO_TIMESTAMPS
#include <time.h>
#endif
/* LOG LEVEL
* LOG_LEVEL_NONE -> No Logging
* LOG_LEVEL_ERR/ERROR -> only errors
* LOG_LEVEL_WARN -> only warnings, errors
* LOG_LEVEL_INFO -> warnings, errors, info
*/
#define STDOUT 0
#define STDERR 1
#define _CLOG_NEWLINE &_new_line
#define _START_BYTE 27
#define _FINAL_BYTE 109
#define _COMMAND_COLOR 91
// colors
#define CLOG_COLOR_ERR _color_err
#define CLOG_COLOR_INFO _color_info
#define CLOG_COLOR_WARN _color_warn
#define CLOG_COLOR_EXTRA _color_extra
#define CLOG_RESET_COLOR _color_reset
void clog_info(const char* message);
void clog_error(const char* message);
void clog_warn(const char* message);
void clog_extra(const char* message);
void clogf_info(const char* format, ...);
void clogf_error(const char* format, ...);
void clogf_warn(const char* format, ...);
void clogf_extra(const char* format, ...);
uint32_t clog_err_count();
uint32_t clog_warn_count();
uint32_t clog_info_count();
uint32_t clog_extra_count();
#endif
#ifdef CLOG_IMPLEMENTATION
#ifndef LOG_NO_TIMESTAMPS
static char time_f[100];
static void update_time() {
time_t now = time(0);
char tmp[sizeof(time_f)];
strftime(tmp, sizeof(tmp), "[%Y-%m-%d %H:%M:%S]", localtime (&now));
snprintf(time_f, sizeof(time_f), "%-24s", tmp);
}
#endif
static char _new_line = '\n';
static char _color_err[] = {_START_BYTE, _COMMAND_COLOR, '3', '1', _FINAL_BYTE};
static char _color_info[] = {_START_BYTE, _COMMAND_COLOR, '3', '7', _FINAL_BYTE};
static char _color_warn[] = {_START_BYTE, _COMMAND_COLOR, '3', '3', _FINAL_BYTE};
static char _color_extra[] = {_START_BYTE, _COMMAND_COLOR, '3', '6', _FINAL_BYTE};
static char _color_reset[] = {_START_BYTE, _COMMAND_COLOR, '0', _FINAL_BYTE};
static uint32_t errors;
static uint32_t infos;
static uint32_t warnings;
static uint32_t extras;
static size_t len(const char* message) {
char* p = (char*) message;
while (*p++ != '\0');
return p - message;
}
#ifndef LOG_COLORS_ONLY
#define SHOW_LOG_LEVEL(level) do { \
write(STDOUT, level, len(level)); \
} while (0)
#else
#define SHOW_LOG_LEVEL(level)
#endif
#ifndef LOG_NO_TIMESTAMPS
#define LOG_TIME() do { \
update_time(); \
write(STDOUT, time_f, len(time_f)); \
} while (0)
#else
#define LOG_TIME()
#endif
void clog_info(const char* message) {
#ifdef LOG_LEVEL_INFO
++infos;
write(STDOUT, CLOG_COLOR_INFO, sizeof(CLOG_COLOR_INFO));
LOG_TIME();
SHOW_LOG_LEVEL("INFO ");
write(STDOUT, message, len(message));
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
write(STDOUT, _CLOG_NEWLINE, 1);
fflush(stdout);
#endif
}
void clog_error(const char* message) {
#if defined(LOG_LEVEL_ERR) || defined(LOG_LEVEL_WARN) || defined(LOG_LEVEL_INFO)
++errors;
write(STDOUT, CLOG_COLOR_ERR, sizeof(CLOG_COLOR_ERR));
LOG_TIME();
SHOW_LOG_LEVEL("ERROR ");
write(STDOUT, message, len(message));
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
write(STDOUT, _CLOG_NEWLINE, 1);
fflush(stdout);
#endif
}
void clog_warn(const char* message) {
#if defined(LOG_LEVEL_WARN) || defined(LOG_LEVEL_INFO)
++warnings;
write(STDOUT, CLOG_COLOR_WARN, sizeof(CLOG_COLOR_WARN));
LOG_TIME();
SHOW_LOG_LEVEL("WARN ");
write(STDOUT, message, len(message));
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
write(STDOUT, _CLOG_NEWLINE, 1);
fflush(stdout);
#endif
}
void clog_extra(const char* message) {
#ifdef LOG_LEVEL_INFO
++extras;
write(STDOUT, CLOG_COLOR_EXTRA, sizeof(CLOG_COLOR_EXTRA));
LOG_TIME();
SHOW_LOG_LEVEL("EXTRA ");
write(STDOUT, message, len(message));
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
write(STDOUT, _CLOG_NEWLINE, 1);
fflush(stdout);
#endif
}
void clogf_info(const char* format, ...) {
#ifdef LOG_LEVEL_INFO
va_list arg;
++infos;
write(STDOUT, CLOG_COLOR_INFO, sizeof(CLOG_COLOR_INFO));
LOG_TIME();
SHOW_LOG_LEVEL("INFO ");
va_start(arg, format);
vfprintf(stdout, format, arg);
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
fflush(stdout);
va_end(arg);
#endif
}
void clogf_error(const char* format, ...) {
#if defined(LOG_LEVEL_ERR) || defined(LOG_LEVEL_WARN) || defined(LOG_LEVEL_INFO)
va_list arg;
++errors;
write(STDOUT, CLOG_COLOR_ERR, sizeof(CLOG_COLOR_ERR));
LOG_TIME();
SHOW_LOG_LEVEL("ERROR ");
va_start(arg, format);
vfprintf(stdout, format, arg);
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
fflush(stdout);
va_end(arg);
#endif
}
void clogf_warn(const char* format, ...) {
#if defined(LOG_LEVEL_WARN) || defined(LOG_LEVEL_INFO)
va_list arg;
++warnings;
write(STDOUT, CLOG_COLOR_WARN, sizeof(CLOG_COLOR_WARN));
LOG_TIME();
SHOW_LOG_LEVEL("WARN ");
va_start(arg, format);
vfprintf(stdout, format, arg);
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
fflush(stdout);
va_end(arg);
#endif
}
void clogf_extra(const char* format, ...) {
#ifdef LOG_LEVEL_INFO
va_list arg;
++extras;
write(STDOUT, CLOG_COLOR_EXTRA, sizeof(CLOG_COLOR_EXTRA));
LOG_TIME();
SHOW_LOG_LEVEL("EXTRA ");
va_start(arg, format);
vfprintf(stdout, format, arg);
write(STDOUT, CLOG_RESET_COLOR, sizeof(CLOG_RESET_COLOR));
fflush(stdout);
va_end(arg);
#endif
}
uint32_t clog_err_count() {
return errors;
}
uint32_t clog_warn_count() {
return warnings;
}
uint32_t clog_info_count() {
return infos;
}
uint32_t clog_extra_count() {
return extras;
}
#endif