/*
 * (C) 2001 Christian Jacobi
 * (C) 2001 Christoph Berg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <unistd.h>

#include "log.h"

int level_threshold = L_LOG;
char *argv_0;

void writelog(int level, char *format, ...)
{
	FILE *f;
	time_t dt;
	char zeit[80];
	va_list arg;

	if (level > level_threshold) return;
	if (!(f=fopen(TESH_LOGFILENAME, "a"))) {
		fprintf(stderr, "FATAL: can not open log file %s, aborting\n", TESH_LOGFILENAME);
		exit(1);
	}

	dt=time(NULL);
	strftime((char *)&zeit, 79, "%d/%m/%Y %T", localtime(&dt));

	va_start(arg, format);
	fprintf(f, "%s %s[%d]: ", zeit, argv_0, (int)getpid());
	vfprintf(f, format, arg);
	va_end(arg);
	fclose(f);
}
