2006-02-02 16:25:55 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 03/2005 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
2006-02-02 16:24:06 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static FILE *log_fd = NULL;
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* log_close()
|
|
|
|
* schliesst das logfile
|
|
|
|
*/
|
2006-02-02 16:24:06 +01:00
|
|
|
void log_close() {
|
|
|
|
fclose(log_fd);
|
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* log_open()
|
|
|
|
* oeffnet ein logfile
|
|
|
|
* wenn der prozess sich beendet, wird das logfile auch wieder geschlossen
|
|
|
|
*
|
|
|
|
* @param char *logfile
|
|
|
|
*/
|
2006-02-02 16:24:06 +01:00
|
|
|
void log_open(char *logfile) {
|
|
|
|
if ((log_fd = fopen(logfile, "a" )) == NULL) {
|
|
|
|
fprintf(stderr, "log_open(\"%s\"): %s\n", logfile, strerror(errno));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (atexit(log_close) != 0) {
|
|
|
|
fprintf(stderr, "log_open(): atexit(): %s\n", strerror(errno));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* log_print()
|
|
|
|
* schreibt eine Zeile ins Logfile oder auf stderr
|
|
|
|
* wenn errno gesetzt ist, wird der error-string mit ausgegeben
|
|
|
|
*
|
|
|
|
* @param variable parameterlist
|
|
|
|
*/
|
2006-02-02 16:24:06 +01:00
|
|
|
void log_print(const char *fmt, ...) {
|
|
|
|
va_list az;
|
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
va_start(az, fmt);
|
|
|
|
vsprintf(buffer, fmt, az);
|
|
|
|
va_end(az);
|
|
|
|
|
|
|
|
if (log_fd) {
|
2006-02-02 16:31:52 +01:00
|
|
|
time_t tzgr;
|
|
|
|
char tbuf[64];
|
|
|
|
time(&tzgr);
|
|
|
|
strftime(tbuf, 64, "%b %d %H:%M:%S :", localtime(&tzgr));
|
|
|
|
|
2006-02-02 16:24:06 +01:00
|
|
|
if (errno) {
|
|
|
|
fprintf(log_fd, "%s %s: %s\n", tbuf, buffer, strerror(errno));
|
|
|
|
} else {
|
|
|
|
fprintf(log_fd, "%s %s\n", tbuf, buffer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (errno) {
|
2006-02-02 16:31:52 +01:00
|
|
|
fprintf(stderr, "%s: %s\n", buffer, strerror(errno));
|
2006-02-02 16:24:06 +01:00
|
|
|
} else {
|
2006-02-02 16:31:52 +01:00
|
|
|
fprintf(stderr, "%s\n", buffer);
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
fflush(log_fd);
|
|
|
|
}
|