sammler/logging.c

115 lines
2.9 KiB
C
Raw Normal View History

2006-06-13 21:34:36 +02:00
/***************************************************************************
2009-05-01 21:29:17 +02:00
* Copyright (C) 07/2007 by Olaf Rempel *
2006-06-13 21:34:36 +02: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 *
2009-05-01 21:29:17 +02:00
* the Free Software Foundation; version 2 of the License *
2006-06-13 21:34:36 +02:00
* *
* 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. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
2009-05-01 21:29:17 +02:00
#include <string.h>
2006-06-13 21:34:36 +02:00
#include <time.h>
#include <stdarg.h>
#include <errno.h>
2009-05-01 21:29:17 +02:00
#include <fcntl.h>
2006-06-13 21:34:36 +02:00
#include "logging.h"
#define BUFSIZE 8192
static FILE *log_fd = NULL;
2007-03-31 22:15:00 +02:00
static int log_prio = LOG_EVERYTIME;
2006-06-13 21:34:36 +02:00
static char *buffer = NULL;
2007-03-31 22:15:00 +02:00
int log_print(int prio, const char *fmt, ...)
2006-06-13 21:34:36 +02:00
{
va_list az;
2007-03-31 22:15:00 +02:00
int len = 0, retval;
if (prio < log_prio)
return 0;
2006-06-13 21:34:36 +02:00
if (buffer == NULL) {
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
2007-03-31 22:15:00 +02:00
fprintf(stderr, "log_print(): out of memory\n");
return -1;
2006-06-13 21:34:36 +02:00
}
}
2007-03-31 22:15:00 +02:00
if (log_fd != NULL) {
time_t tzgr;
time(&tzgr);
len += strftime(buffer, BUFSIZE, "%b %d %H:%M:%S :", localtime(&tzgr));
}
2006-06-13 21:34:36 +02:00
va_start(az, fmt);
2007-03-31 22:15:00 +02:00
len += vsnprintf(buffer + len, BUFSIZE - len, fmt, az);
2006-06-13 21:34:36 +02:00
va_end(az);
2006-06-22 20:33:30 +02:00
if (len < 0 || len >= BUFSIZE) {
2006-06-13 21:34:36 +02:00
errno = 0;
2007-03-31 22:15:00 +02:00
return log_print(LOG_ERROR, "log_print: arguments too long");
2006-06-13 21:34:36 +02:00
}
if (errno) {
2007-03-31 22:15:00 +02:00
len += snprintf(buffer + len, BUFSIZE - len, ": %s", strerror(errno));
errno = 0;
2006-06-13 21:34:36 +02:00
}
2007-03-31 22:15:00 +02:00
retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer);
fflush(log_fd);
return retval;
2006-06-13 21:34:36 +02:00
}
2007-03-31 22:15:00 +02:00
void log_close(void)
2006-06-13 21:34:36 +02:00
{
2009-05-02 15:32:33 +02:00
if (buffer) {
2006-06-13 21:34:36 +02:00
free(buffer);
2009-05-02 15:32:33 +02:00
buffer = NULL;
}
2006-06-13 21:34:36 +02:00
2009-05-02 15:32:33 +02:00
if (log_fd) {
2007-03-31 22:15:00 +02:00
fclose(log_fd);
2009-05-02 15:32:33 +02:00
log_fd = NULL;
}
2006-06-13 21:34:36 +02:00
}
2007-03-31 22:15:00 +02:00
int log_init(const char *logfile)
2006-06-13 21:34:36 +02:00
{
2007-03-31 22:15:00 +02:00
if (log_fd != NULL)
log_close();
2006-06-13 21:34:36 +02:00
log_fd = fopen(logfile, "a");
if (log_fd == NULL) {
2007-03-31 22:15:00 +02:00
fprintf(stderr, "log_init(): can not open logfile");
return -1;
2006-06-13 21:34:36 +02:00
}
2009-05-01 21:29:17 +02:00
if (fcntl(fileno(log_fd), F_SETFD, FD_CLOEXEC) < 0) {
fprintf(stderr, "log_init(): fcntl(FD_CLOEXEC)");
return -1;
}
2009-05-02 15:32:33 +02:00
log_prio = LOG_EVERYTIME;
2007-03-31 22:15:00 +02:00
return 0;
}
2006-06-13 21:34:36 +02:00
2007-03-31 22:15:00 +02:00
void log_setprio(int prio)
{
log_prio = prio;
2006-06-13 21:34:36 +02:00
}