qnapd/logging.c

116 lines
3.0 KiB
C
Raw Normal View History

2011-04-17 16:59:51 +02:00
/***************************************************************************
* Copyright (C) 07/2007 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; version 2 of the License *
* *
* 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>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include "logging.h"
#define BUFSIZE 8192
2011-05-22 16:06:50 +02:00
static FILE *log_file = NULL;
2011-04-17 16:59:51 +02:00
static int log_prio = LOG_EVERYTIME;
static char *buffer = NULL;
int log_print(int prio, const char *fmt, ...)
{
va_list az;
int len = 0, retval;
if (prio < log_prio)
return 0;
if (buffer == NULL) {
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
2011-05-22 16:06:50 +02:00
fprintf(stderr, "%s(): out of memory\n", __FUNCTION__);
2011-04-17 16:59:51 +02:00
return -1;
}
}
2011-05-22 16:06:50 +02:00
if (log_file != NULL) {
2011-04-17 16:59:51 +02:00
time_t tzgr;
time(&tzgr);
len += strftime(buffer, BUFSIZE, "%b %d %H:%M:%S :", localtime(&tzgr));
}
va_start(az, fmt);
len += vsnprintf(buffer + len, BUFSIZE - len, fmt, az);
va_end(az);
if (len < 0 || len >= BUFSIZE) {
errno = 0;
2011-05-22 16:06:50 +02:00
return log_print(LOG_ERROR, "%s: arguments too long", __FUNCTION__);
2011-04-17 16:59:51 +02:00
}
if (errno) {
len += snprintf(buffer + len, BUFSIZE - len, ": %s", strerror(errno));
errno = 0;
}
2011-05-22 16:06:50 +02:00
retval = fprintf((log_file ? log_file : stderr), "%s\n", buffer);
fflush(log_file);
2011-04-17 16:59:51 +02:00
return retval;
}
void log_close(void)
{
if (buffer) {
free(buffer);
buffer = NULL;
}
2011-05-22 16:06:50 +02:00
if (log_file) {
fclose(log_file);
log_file = NULL;
2011-04-17 16:59:51 +02:00
}
}
int log_init(const char *logfile)
{
2011-05-22 16:06:50 +02:00
if (log_file != NULL)
2011-04-17 16:59:51 +02:00
log_close();
2011-05-22 16:06:50 +02:00
log_file = fopen(logfile, "a");
if (log_file == NULL) {
fprintf(stderr, "%s(): can not open logfile", __FUNCTION__);
2011-04-17 16:59:51 +02:00
return -1;
}
2011-05-22 16:06:50 +02:00
if (fcntl(fileno(log_file), F_SETFD, FD_CLOEXEC) < 0) {
fprintf(stderr, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
fclose(log_file);
2011-04-17 16:59:51 +02:00
return -1;
}
log_prio = LOG_EVERYTIME;
return 0;
}
void log_setprio(int prio)
{
log_prio = prio;
}