File: logit.c

package info (click to toggle)
levee 4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: ansic: 7,619; pascal: 994; sh: 185; makefile: 74; asm: 29
file content (51 lines) | stat: -rw-r--r-- 839 bytes parent folder | download | duplicates (2)
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
#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
    }
}