tinywebd

diff src/logger.c @ 7:5ec50ca0d071

separated the server code
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Apr 2015 11:45:08 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/logger.c	Fri Apr 17 11:45:08 2015 +0300
     1.3 @@ -0,0 +1,34 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <stdarg.h>
     1.8 +#include <errno.h>
     1.9 +#include "logger.h"
    1.10 +
    1.11 +static FILE *logfile;
    1.12 +
    1.13 +int set_log_file(const char *fname)
    1.14 +{
    1.15 +	FILE *fp;
    1.16 +
    1.17 +	if(!(fp = fopen(fname, "w"))) {
    1.18 +		fprintf(stderr, "failed to open logfile: %s: %s\n", fname, strerror(errno));
    1.19 +		return -1;
    1.20 +	}
    1.21 +	setvbuf(fp, 0, _IONBF, 0);
    1.22 +	logfile = fp;
    1.23 +	return 0;
    1.24 +}
    1.25 +
    1.26 +void logmsg(const char *fmt, ...)
    1.27 +{
    1.28 +	va_list ap;
    1.29 +
    1.30 +	if(!logfile) {
    1.31 +		logfile = stderr;
    1.32 +	}
    1.33 +
    1.34 +	va_start(ap, fmt);
    1.35 +	vfprintf(logfile, fmt, ap);
    1.36 +	va_end(ap);
    1.37 +}