-
Notifications
You must be signed in to change notification settings - Fork 5
/
logit.c
52 lines (45 loc) · 857 Bytes
/
logit.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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
static FILE *logfile = 0;
void
logger(char *fmt, ...)
{
va_list args;
char *p, *endp;
#if HAVE_VASPRINTF
char *line;
#else
char biglogbuf[1024];
#endif
int size;
if ( logfile == 0 ) {
logfile = fopen("levee.log", "w");
setvbuf(logfile, 0, _IOLBF, 0);
}
va_start(args, fmt);
#if HAVE_VASPRINTF
if ( (size = vasprintf(&line, fmt, args)) > 0 ) {
p = line;
endp = &line[size];
}
#else
if ( (size = vsprintf(biglogbuf, fmt, args)) > 0 ) {
p = biglogbuf;
endp = &biglogbuf[size];
}
#endif
if (size > 0) {
for ( ; p < endp; p++ ) {
if ( *p >= ' ' && *p < 127 )
fputc(*p, logfile);
else
fprintf(logfile, "%%%02x", (unsigned int)(*p));
}
fputc('\n', logfile);
#if HAVE_VASPRINTF
free(line);
#endif
}
va_end(args);
}