2006-02-02 16:25:55 +01:00
|
|
|
/***************************************************************************
|
2006-11-24 21:53:25 +01:00
|
|
|
* Copyright (C) 06/2006 by Olaf Rempel *
|
2006-02-02 16:25:55 +01:00
|
|
|
* 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>
|
|
|
|
|
2006-11-24 21:53:25 +01:00
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
#define BUFSIZE 8192
|
2006-02-02 16:24:06 +01:00
|
|
|
|
2006-02-02 16:41:56 +01:00
|
|
|
static FILE *log_fd = NULL;
|
2006-11-24 21:53:25 +01:00
|
|
|
static char *buffer = NULL;
|
2006-02-02 16:24:06 +01:00
|
|
|
|
2006-11-24 21:53:25 +01:00
|
|
|
void log_print(int prio, const char *fmt, ...)
|
2006-02-02 16:47:20 +01:00
|
|
|
{
|
2006-02-02 16:24:06 +01:00
|
|
|
va_list az;
|
2006-11-24 21:53:25 +01:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (buffer == NULL) {
|
|
|
|
buffer = malloc(BUFSIZE);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
fprintf(stderr, "log_print: out of memory\nBailing out!\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2006-02-02 16:24:06 +01:00
|
|
|
|
|
|
|
va_start(az, fmt);
|
2006-11-24 21:53:25 +01:00
|
|
|
len = vsnprintf(buffer, BUFSIZE, fmt, az);
|
2006-02-02 16:24:06 +01:00
|
|
|
va_end(az);
|
|
|
|
|
2006-11-24 21:53:25 +01:00
|
|
|
if (len < 0 || len >= BUFSIZE) {
|
|
|
|
log_print(LOG_ERROR, "log_print: arguments too long");
|
|
|
|
errno = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno) {
|
|
|
|
strncpy(buffer + len, ": ", BUFSIZE - len);
|
|
|
|
len += 2;
|
|
|
|
strncpy(buffer + len, strerror(errno), BUFSIZE - len);
|
|
|
|
}
|
|
|
|
|
2006-02-02 16:24:06 +01:00
|
|
|
if (log_fd) {
|
2006-02-02 16:31:52 +01:00
|
|
|
char tbuf[64];
|
2006-11-24 21:53:25 +01:00
|
|
|
time_t tzgr;
|
|
|
|
|
2006-02-02 16:31:52 +01:00
|
|
|
time(&tzgr);
|
2006-11-24 21:53:25 +01:00
|
|
|
strftime(tbuf, sizeof(tbuf), "%b %d %H:%M:%S :", localtime(&tzgr));
|
2006-02-02 16:31:52 +01:00
|
|
|
|
2006-11-24 21:53:25 +01:00
|
|
|
fprintf(log_fd, "%s %s\n", tbuf, buffer);
|
2006-02-02 16:44:46 +01:00
|
|
|
fflush(log_fd);
|
2006-11-24 21:53:25 +01:00
|
|
|
|
2006-02-02 16:24:06 +01:00
|
|
|
} else {
|
2006-11-24 21:53:25 +01:00
|
|
|
fprintf(stderr, "%s\n", buffer);
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
2006-11-24 21:53:25 +01:00
|
|
|
|
2006-02-02 16:24:06 +01:00
|
|
|
errno = 0;
|
|
|
|
}
|
2006-02-02 16:41:56 +01:00
|
|
|
|
2006-11-24 21:53:25 +01:00
|
|
|
static void log_close(void)
|
2006-02-02 16:47:20 +01:00
|
|
|
{
|
2006-11-24 21:53:25 +01:00
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
|
|
|
|
2006-02-02 16:41:56 +01:00
|
|
|
fclose(log_fd);
|
|
|
|
}
|
|
|
|
|
2006-02-02 16:47:20 +01:00
|
|
|
int log_init(char *logfile)
|
|
|
|
{
|
2006-11-24 21:53:25 +01:00
|
|
|
log_fd = fopen(logfile, "a");
|
|
|
|
if (log_fd == NULL) {
|
|
|
|
log_print(LOG_ERROR, "log_open('%s'): %s", logfile);
|
2006-02-02 16:43:41 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2006-02-02 16:44:46 +01:00
|
|
|
|
2006-02-02 16:43:41 +01:00
|
|
|
if (atexit(log_close) != 0) {
|
2006-11-24 21:53:25 +01:00
|
|
|
log_print(LOG_ERROR, "log_open(): atexit()");
|
2006-02-02 16:43:41 +01:00
|
|
|
return 0;
|
2006-02-02 16:41:56 +01:00
|
|
|
}
|
2006-11-24 21:53:25 +01:00
|
|
|
|
|
|
|
log_print(LOG_EVERYTIME, "==========================");
|
2006-02-02 16:41:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|