owntone-server/src/err.c

253 lines
6.6 KiB
C
Raw Normal View History

2003-10-13 11:03:14 -04:00
/*
* $Id$
* Generic error handling
*
2003-12-29 15:41:08 -05:00
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
2003-10-13 11:03:14 -04:00
*
* 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
*/
/**
* \file err.c
* Error handling, logging, and memory leak checking.
*
* Most of these functions should not be used directly. For the most
2004-11-12 02:27:05 -05:00
* part, they are hidden in macros like #DPRINTF and #MEMNOTIFY. The
* only function here that is really directly useable is log_setdest()
*/
2004-03-13 20:06:15 -05:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-10-13 11:03:14 -04:00
#include <stdio.h>
#include <stdarg.h>
2004-04-13 00:23:36 -04:00
#include <time.h>
2003-10-13 11:03:14 -04:00
#include <errno.h>
2003-11-23 01:10:25 -05:00
#include <pthread.h>
2003-10-13 11:03:14 -04:00
#include <stdlib.h>
2004-02-14 19:51:11 -05:00
#include <string.h>
2003-10-13 11:03:14 -04:00
#include "err.h"
#ifndef ERR_LEAN
# include "os.h"
#endif
2003-10-13 11:03:14 -04:00
2006-02-26 03:46:24 -05:00
static int err_debuglevel=0; /**< current debuglevel, set from command line with -d */
static int err_logdestination=LOGDEST_STDERR; /**< current log destination */
static FILE *err_file=NULL; /**< if logging to file, the handle of that file */
static pthread_mutex_t err_mutex=PTHREAD_MUTEX_INITIALIZER; /**< for serializing log messages */
2004-11-13 03:05:27 -05:00
static unsigned int err_debugmask=0xFFFFFFFF; /**< modules to debug, see \ref log_categories */
/** text list of modules to match for setting debug mask */
static char *err_categorylist[] = {
"config","webserver","database","scan","query","index","browse",
"playlist","art","daap","main","rend","xml","parse",NULL
2004-11-13 03:05:27 -05:00
};
2004-02-14 19:51:11 -05:00
2003-11-23 01:10:25 -05:00
/*
* Forwards
*/
2006-02-26 03:46:24 -05:00
static int _err_lock(void);
static int _err_unlock(void);
2003-11-23 01:10:25 -05:00
/**
* Write a printf-style formatted message to the log destination.
* This can be stderr, syslog/eventviewer, or a logfile, as determined by
2004-11-13 03:05:27 -05:00
* err_setdest(). Note that this function should not be directly
2004-11-12 02:27:05 -05:00
* used, rather it should be used via the #DPRINTF macro.
*
* \param level Level at which to log \ref log_levels
2004-11-13 03:06:56 -05:00
* \param cat the category to log \ref log_categories
* \param fmt printf-style
*/
2004-11-13 03:05:27 -05:00
void err_log(int level, unsigned int cat, char *fmt, ...)
2003-10-13 11:03:14 -04:00
{
va_list ap;
2004-04-13 00:23:36 -04:00
char timebuf[256];
char errbuf[4096];
2004-04-13 00:23:36 -04:00
struct tm tm_now;
time_t tt_now;
2004-11-13 02:14:26 -05:00
if(level) {
2006-02-26 03:46:24 -05:00
if(level > err_debuglevel)
return;
2004-11-13 02:14:26 -05:00
2006-02-26 03:46:24 -05:00
if(!(cat & err_debugmask))
return;
2004-11-13 02:14:26 -05:00
} /* we'll *always* process a log level 0 */
2003-10-13 11:03:14 -04:00
va_start(ap, fmt);
vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
va_end(ap);
2006-02-26 03:46:24 -05:00
_err_lock(); /* atomic file writes */
2003-10-13 11:03:14 -04:00
if((!level) && (err_logdestination != LOGDEST_STDERR)) {
2006-02-26 03:46:24 -05:00
fprintf(stderr,"%s",errbuf);
fprintf(stderr,"Aborting\n");
fflush(stderr); /* shouldn't have to do this? */
2004-12-06 19:24:08 -05:00
}
2003-10-13 11:03:14 -04:00
switch(err_logdestination) {
2004-04-13 00:23:36 -04:00
case LOGDEST_LOGFILE:
2006-02-26 03:46:24 -05:00
tt_now=time(NULL);
localtime_r(&tt_now,&tm_now);
strftime(timebuf,sizeof(timebuf),"%Y-%m-%d %T",&tm_now);
fprintf(err_file,"%s: %s",timebuf,errbuf);
if(!level) fprintf(err_file,"%s: Aborting\n",timebuf);
fflush(err_file);
break;
2003-10-13 11:03:14 -04:00
case LOGDEST_STDERR:
2004-11-13 03:05:27 -05:00
fprintf(stderr, "%s",errbuf);
2006-02-26 03:46:24 -05:00
if(!level) fprintf(stderr,"Aborting\n");
2003-10-13 11:03:14 -04:00
break;
case LOGDEST_SYSLOG:
2006-02-26 03:46:24 -05:00
os_syslog(level,errbuf);
if(!level) os_syslog(0, "Fatal error... Aborting\n");
2003-10-13 11:03:14 -04:00
break;
}
2006-02-26 03:46:24 -05:00
_err_unlock();
2004-04-13 00:23:36 -04:00
if(!level) {
2006-02-26 03:46:24 -05:00
exit(EXIT_FAILURE); /* this should go to an OS-specific exit routine */
2003-10-13 11:03:14 -04:00
}
}
2006-02-26 03:46:24 -05:00
/*
* simple get/set interface to debuglevel to avoid global
2006-02-26 03:46:24 -05:00
*/
void err_setlevel(int level) {
err_debuglevel = level;
}
int err_getlevel(void) {
return err_debuglevel;
}
/**
* Sets the log destination. (stderr, syslog, or logfile)
*
* \param app appname (used only for syslog destination)
* \param destination where to log to \ref log_dests "as defined in err.h"
2004-11-12 02:27:05 -05:00
*/
2006-02-26 03:46:24 -05:00
void err_setdest(char *cvalue, int destination) {
2004-04-13 00:23:36 -04:00
if(err_logdestination == destination)
2006-02-26 03:46:24 -05:00
return;
2004-04-13 00:23:36 -04:00
switch(err_logdestination) {
2003-10-13 11:03:14 -04:00
case LOGDEST_SYSLOG:
2006-02-26 03:46:24 -05:00
os_closesyslog();
break;
2004-04-13 00:23:36 -04:00
case LOGDEST_LOGFILE:
2006-02-26 03:46:24 -05:00
fclose(err_file);
break;
2004-04-13 00:23:36 -04:00
}
switch(destination) {
case LOGDEST_LOGFILE:
2006-02-26 03:46:24 -05:00
err_file=fopen(cvalue,"a");
if(err_file==NULL) {
fprintf(stderr,"Error opening %s: %s\n",cvalue,strerror(errno));
exit(EXIT_FAILURE);
}
break;
2004-04-13 00:23:36 -04:00
case LOGDEST_SYSLOG:
2006-02-26 03:46:24 -05:00
os_opensyslog(); // (app,LOG_PID,LOG_DAEMON);
break;
2003-10-13 11:03:14 -04:00
}
2004-04-06 23:51:01 -04:00
err_logdestination=destination;
2003-10-13 11:03:14 -04:00
}
2004-11-13 03:05:27 -05:00
/**
* Set the debug mask. Given a comma separated list, this walks
* through the err_categorylist and sets the bitfields for the
* requested log modules.
*
* \param list comma separated list of modules to debug.
2004-11-13 03:05:27 -05:00
*/
extern int err_setdebugmask(char *list) {
unsigned int rack;
char *token, *str, *last;
2004-11-13 15:59:10 -05:00
int index;
2004-11-13 03:05:27 -05:00
err_debugmask=0x80000000; /* always log L_MISC! */
str=list;
2003-11-23 01:10:25 -05:00
2004-11-13 03:05:27 -05:00
while(1) {
2006-02-26 03:46:24 -05:00
token=strtok_r(str,",",&last);
str=NULL;
if(token) {
rack=1;
index=0;
while((err_categorylist[index]) &&
2006-02-26 03:46:24 -05:00
(strcasecmp(err_categorylist[index],token))) {
rack <<= 1;
index++;
}
if(!err_categorylist[index]) {
DPRINTF(E_LOG,L_MISC,"Unknown module: %s\n",token);
return 1;
} else {
DPRINTF(E_DBG,L_MISC,"Adding module %s to debug list (0x%08x)\n",token,rack);
err_debugmask |= rack;
}
} else break; /* !token */
2004-11-13 03:05:27 -05:00
}
2004-11-13 15:59:10 -05:00
DPRINTF(E_INF,L_MISC,"Debug mask is 0x%08x\n",err_debugmask);
2004-11-13 03:05:27 -05:00
return 0;
}
2003-11-23 01:10:25 -05:00
/**
* Lock the error mutex. This is used to serialize
* log messages, as well as protect access to the memory
* list, when memory debugging is enabled.
2003-11-23 01:10:25 -05:00
*
* \returns 0 on success, otherwise -1 with errno set
2003-11-23 01:10:25 -05:00
*/
2006-02-26 03:46:24 -05:00
int _err_lock(void) {
2003-11-23 01:10:25 -05:00
int err;
2004-02-14 19:51:11 -05:00
if((err=pthread_mutex_lock(&err_mutex))) {
2006-02-26 03:46:24 -05:00
errno=err;
return -1;
2003-11-23 01:10:25 -05:00
}
return 0;
}
/**
2003-11-23 01:10:25 -05:00
* Unlock the error mutex
*
* \returns 0 on success, otherwise -1 with errno set
2003-11-23 01:10:25 -05:00
*/
2006-02-26 03:46:24 -05:00
int _err_unlock(void) {
2003-11-23 01:10:25 -05:00
int err;
2004-02-14 19:51:11 -05:00
if((err=pthread_mutex_unlock(&err_mutex))) {
2006-02-26 03:46:24 -05:00
errno=err;
return -1;
2003-11-23 01:10:25 -05:00
}
return 0;
}