-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
395 lines (310 loc) · 10.2 KB
/
log.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <errno.h>
#include <execinfo.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include "macro.h"
#include "color.h"
#define __LOG_BUILD
#include "log.h"
extern struct Logger app_logger;
extern inline int LogLevel_clamp (int level);
extern inline bool Logger_would_log (
const struct Logger * __restrict self, int level);
extern inline int LoggerEvent_log_va_func (
const struct LoggerEvent * __restrict self,
const char * __restrict format, va_list ap);
extern inline int LoggerEvent_write_func (
const struct LoggerEvent * __restrict self,
const char * __restrict str, size_t len);
extern inline int LoggerEvent_puts_func (
const struct LoggerEvent * __restrict self, const char * __restrict str);
extern inline int LoggerEvent_init_func (
struct LoggerEvent * __restrict self,
const struct Logger * __restrict logger, int level,
const char * __restrict file, int line, const char * __restrict func);
extern inline int Logger_log_va_func (
const struct Logger * __restrict self, int level,
const char * __restrict file, int line, const char * __restrict func,
const char * __restrict format, va_list ap);
#define WRITE(fd, str) ((void) (write(fd, str, sizeof(str) - 1) == sizeof(str) - 1))
void print_backtrace (int fd, int skip, bool with_header) {
void *symbols[64];
register const int symbols_size = arraysize(symbols);
// skip itself
skip++;
if unlikely (skip >= symbols_size) {
WRITE(fd, "(too many frames skipped)\n");
return;
}
// get backtrace
int size = backtrace(symbols, symbols_size);
should (size > 0) otherwise {
WRITE(fd, "(no available backtrace)\n");
return;
}
// write header
if (with_header) {
WRITE(fd, "Backtrace:\n");
}
int skipped = size;
// skip libc init
if likely (size < symbols_size) {
skipped -= 2;
}
// skip topmost frame
skipped -= skip;
if likely (skipped > 0) {
backtrace_symbols_fd(symbols + skip, skipped, fd);
}
// detect if all frames are printed
if (size == symbols_size) {
WRITE(fd, "...and possibly more\n");
}
}
static bool wait_debugger (void) {
// attached variable
volatile bool attached = false;
// set non buffered
struct termios stdin_termios;
tcgetattr(STDIN_FILENO, &stdin_termios);
{
struct termios termios = stdin_termios;
termios.c_lflag &= ~(ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &termios);
}
// allow Ctrl-C to work
struct sigaction sigint_sa;
{
static const struct sigaction default_sa = {.sa_handler = SIG_DFL};
sigaction(SIGINT, &default_sa, &sigint_sa);
}
// wait
fprintf(stderr, "(%d) Start debugging? [y/N] ", getpid());
fflush(stderr);
struct pollfd pollfd = {.fd = STDIN_FILENO, .events = POLLIN};
for (int i = 0; i < 5; i++) {
goto_if (poll(&pollfd, 1, 1000) > 0) char_inputed;
fputc('.', stderr);
fflush(stderr);
}
tcsetattr(STDIN_FILENO, TCSANOW, &stdin_termios);
fputc('\n', stderr);
return false;
char_inputed:
tcsetattr(STDIN_FILENO, TCSANOW, &stdin_termios);
char input = getchar();
if (input != '\n') {
fputc('\n', stderr);
}
return_if_not (input == 'Y' || input == 'y') false;
// prepare being attached
if (!attached) {
fprintf(
stderr, "Wait debugger to attach... Enter 'fg' to restore program...\n");
raise(SIGSTOP);
}
// test if attached
return_if_not (attached) false;
fprintf(stderr, "Attached!\n");
// restore
sigaction(SIGINT, &sigint_sa, NULL);
return true;
}
static void exit_debug (int status, bool debug) {
if (debug) {
return_if (wait_debugger());
fprintf(stderr, "terminated\n");
}
_exit(status);
}
static bool sigsegv_debug;
static int sigsegv_exit_status;
static void sigsegv_handler (int sig) {
(void) sig;
LOGEVENT (LOG_LEVEL_CRITICAL) {
LOGEVENT_LOG("segmentation fault");
event.skip = 2;
event.exit_status = sigsegv_exit_status;
event.fatal = true;
event.backtrace = true;
event.debug = sigsegv_debug;
}
}
int diagnose_sigsegv (
bool enable_debug, int exit_status, struct sigaction *oldact) {
sigsegv_debug = enable_debug;
sigsegv_exit_status = exit_status == 0 ? EXIT_FAILURE : exit_status;
static const struct sigaction sigsegv_sa = {.sa_handler = sigsegv_handler};
return sigaction(SIGSEGV, &sigsegv_sa, oldact);
}
/******************************************************************************
* LogLevel
******************************************************************************/
const char * const LogLevel_names[9] = {
"EMERGENCY", "ALERT", "CRITICAL", "ERROR",
"WARNING", "NOTICE", "INFO", "DEBUG", "VERBOSE",
};
/******************************************************************************
* Logger
******************************************************************************/
static int logger_stream_std_begin (
struct LoggerEvent * __restrict self, const char * __restrict domain,
unsigned char level, const char * __restrict file, int line,
const char * __restrict func, const char * __restrict sgr) {
int ret;
int fd = self->stream->fd;
time_t curtime = time(NULL);
struct tm timeinfo;
localtime_r(&curtime, &timeinfo);
char timebuf[64];
strftime(
timebuf, sizeof(timebuf), sgr != NULL ?
COLOR_SEQ(COLOR_FOREGROUND_GREEN) "[%b %e %T]" RESET_SEQ : "[%b %e %T]",
&timeinfo);
if unlikely (file == NULL) {
ret = dprintf(fd, "%s: ", timebuf);
} else if unlikely (func == NULL) {
ret = dprintf(fd, "%s (%s:%d): ", timebuf, file, line);
} else {
ret = dprintf(fd, "%s (%s:%d %s): ", timebuf, file, line, func);
}
if likely (domain != NULL) {
ret += dprintf(fd, "%s-", domain);
}
const char *level_name = LogLevel_names[level];
if likely (sgr != NULL) {
ret += dprintf(fd, SGR_FORMAT_SEQ_START "%s" RESET_SEQ " **: ",
sgr, level_name);
} else {
ret += dprintf(fd, "%s **: ", level_name);
}
return ret;
}
static int logger_stream_std_begin_nocolor (
struct LoggerEvent * __restrict self, const char * __restrict domain,
unsigned char level, const char * __restrict file, int line,
const char * __restrict func, const char * __restrict sgr) {
(void) sgr;
return logger_stream_std_begin(self, domain, level, file, line, func, NULL);
}
const struct LoggerStream logger_stream_stdout = {
.fd = STDOUT_FILENO, .begin = logger_stream_std_begin,
};
const struct LoggerStream logger_stream_stderr = {
.fd = STDERR_FILENO, .begin = logger_stream_std_begin,
};
const struct LoggerStream logger_stream_stdout_nocolor = {
.fd = STDOUT_FILENO, .begin = logger_stream_std_begin_nocolor,
};
const struct LoggerStream logger_stream_stderr_nocolor = {
.fd = STDERR_FILENO, .begin = logger_stream_std_begin_nocolor,
};
struct Logger app_logger = {LOGGER_INIT};
void Logger_set_stream (
struct Logger * __restrict self, int level,
const struct LoggerStream * __restrict stream) {
for (int i = 0; i <= LogLevel_clamp(level); i++) {
self->formatted[i].stream = stream;
}
}
/******************************************************************************
* LoggerEvent
******************************************************************************/
int LoggerEvent_log_func (
const struct LoggerEvent * __restrict self,
const char * __restrict format, ...) {
va_list ap;
va_start(ap, format);
int res = LoggerEvent_log_va_func(self, format, ap);
va_end(ap);
return res;
}
int LoggerEvent_perror_func (
const struct LoggerEvent * __restrict self, int errnum, bool colon) {
if unlikely (errnum == 0) {
errnum = errno;
}
int res = colon ? LoggerEvent_write_func(self, ": ", 2) : 0;
static locale_t locale = 0;
if unlikely (locale == 0) {
locale = newlocale(LC_ALL_MASK, "", 0);
}
if likely (locale != 0) {
res += LoggerEvent_puts_func(self, strerror_l(errnum, locale));
} else {
char buf[1024];
strerror_r(errnum, buf, sizeof(buf));
res += LoggerEvent_puts_func(self, buf);
}
return res;
}
int LoggerEvent_backtrace_func (
const struct LoggerEvent * __restrict self, int skip) {
print_backtrace(self->stream->fd, self->skip + skip + 1, true);
return 0;
}
int LoggerEvent_destroy_func (const struct LoggerEvent * __restrict self) {
int res = write(self->stream->fd, "\n", 1);
if unlikely (self->backtrace) {
print_backtrace(self->stream->fd, self->skip + 1, true);
}
if unlikely (self->stream->end != NULL) {
res += self->stream->end(self);
}
if unlikely (self->fatal) {
exit_debug(
self->exit_status == 0 ? EXIT_FAILURE : self->exit_status, self->debug);
}
return res;
}
/******************************************************************************
* Log
******************************************************************************/
int Logger_log_func (
const struct Logger * __restrict self, int level,
const char * __restrict file, int line, const char * __restrict func,
const char * __restrict format, ...) {
return_if (!Logger_would_log(self, level)) -1;
struct LoggerEvent event;
int res = LoggerEvent_init_func(&event, self, level, file, line, func);
event.skip = 1;
LOGGER_EVENT_GUARD_BEGIN(&event);
va_list ap;
va_start(ap, format);
res += LoggerEvent_log_va_func(&event, format, ap);
va_end(ap);
LOGGER_EVENT_GUARD_END(&event);
res += LoggerEvent_destroy_inline(&event);
LOGGER_EVENT_GUARD_END(&event);
return res;
}
int Logger_log_perror_func (
const struct Logger * __restrict self, int level,
const char * __restrict file, int line, const char * __restrict func,
const char * __restrict format, ...) {
return_if (!Logger_would_log(self, level)) -1;
int errnum = errno;
struct LoggerEvent event;
int res = LoggerEvent_init_func(&event, self, level, file, line, func);
event.skip = 1;
LOGGER_EVENT_GUARD_BEGIN(&event);
va_list ap;
va_start(ap, format);
res += LoggerEvent_log_va_func(&event, format, ap);
va_end(ap);
res += LoggerEvent_perror_func(&event, errnum, true);
LOGGER_EVENT_GUARD_END(&event);
res += LoggerEvent_destroy_inline(&event);
LOGGER_EVENT_GUARD_END(&event);
return res;
}