home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dream 52
/
Amiga_Dream_52.iso
/
Linux
/
Magazine
/
wwwoffle-2.1.tar.gz
/
wwwoffle-2.1
/
errors.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-12-30
|
6KB
|
274 lines
/***************************************
$Header: /home/amb/wwwoffle/RCS/errors.c 2.6 1997/12/29 17:15:47 amb Exp $
WWWOFFLE - World Wide Web Offline Explorer - Version 2.0a.
Generate error messages in a standard format optionally to syslog and stderr.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1996,97 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
/*+ Under Linux, FreeBSD, NetBSD, SGI, Ultrix, AIX and AT&T stdarg is used +*/
#if defined(__linux__) || \
defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__sgi__) || defined(__ultrix__) || \
defined(_AIX) || defined(_ATT4)
#define USE_STD_ARG
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#ifdef USE_STD_ARG
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <syslog.h>
#include <errno.h>
#if defined(__sun__) && !defined(__svr4__)
/* SunOS 4.x does not have strerror(). */
char* strerror(int err);
extern int sys_nerr;
extern char *sys_errlist[];
char* strerror(int err)
{
if(err>0 && err<sys_nerr)
return(sys_errlist[err]);
else
return("Unknown error");
}
#endif /* defined(__sun__) && !defined(__svr4__) */
#include <netdb.h>
/* A function to get an error message for h_errno. */
char* str_h_error(int err);
char* str_h_error(int err)
{
#ifdef NETDB_INTERNAL
if(err==NETDB_INTERNAL)
return("Name Lookup Internal error");
else
#endif
#ifdef NETDB_SUCCESS
if(err==NETDB_SUCCESS)
return("Name Lookup Success");
else
#endif
#ifdef HOST_NOT_FOUND
if(err==HOST_NOT_FOUND)
return("Name Lookup Authoritative Answer Host not found");
else
#endif
#ifdef TRY_AGAIN
if(err==TRY_AGAIN)
return("Name Lookup Non-Authoritive Answer Host not found");
else
#endif
#ifdef NO_RECOVERY
if(err==NO_RECOVERY)
return("Name Lookup Non recoverable error");
else
#endif
#ifdef NO_DATA
if(err==NO_DATA)
return("Name Lookup Valid name, no data record of requested type");
else
#endif
#ifdef NO_ADDRESS
if(err==NO_ADDRESS)
return("Name Lookup Valid name, no data record of requested type");
else
#endif
return("Unknown error");
}
#include "config.h"
#include "errors.h"
/*+ The name of the program. +*/
static char *program=NULL;
/*+ The process id of the program. +*/
static pid_t pid;
/*+ The last time that a message was printed. +*/
static time_t last_time;
/*+ The error messages. +*/
static char *ErrorString[]={"Debug:" ,"Information:","Important:","Warning:" ,"Fatal:"};
/*+ The priority to apply to syslog messages. +*/
static int ErrorPriority[]={LOG_DEBUG,LOG_INFO ,LOG_NOTICE ,LOG_WARNING,LOG_ERR};
/*+ The error facility to use, +*/
static int use_syslog=0, /*+ use syslog. +*/
use_stderr=0; /*+ use stderr. +*/
/*++++++++++++++++++++++++++++++++++++++
Initialise the error handler, get the program name and pid.
char *name The name of the program.
int syslogable Set to true if the errors are allowed to go to syslog (or -1 to remain unchanged).
int stderrable Set to true if the errors are allowed to go to stderr (or -1 to remain unchanged).
++++++++++++++++++++++++++++++++++++++*/
void InitErrorHandler(char *name,int syslogable,int stderrable)
{
if(use_syslog && program)
closelog();
program=name;
if(syslogable!=-1)
use_syslog=syslogable;
if(stderrable!=-1)
use_stderr=stderrable;
if(use_syslog)
{
#if defined(__ultrix__)
openlog(program,LOG_PID);
#else
openlog(program,LOG_CONS|LOG_PID,LOG_DAEMON);
#endif
atexit(closelog);
}
pid=getpid();
last_time=time(NULL)-3300;
}
/*++++++++++++++++++++++++++++++++++++++
Print an error message.
char *PrintMessage Return the error message (except the pid etc.).
ErrorLevel errlev Which error level.
const char* fmt The format of the message.
... The rest of the arguments (printf style).
++++++++++++++++++++++++++++++++++++++*/
char *PrintMessage(ErrorLevel errlev,const char* fmt, ...)
{
int str_len=16+strlen(fmt);
static char* string=NULL;
va_list ap;
int i,j;
time_t this_time=time(NULL);
if((this_time-last_time)>3600)
{
last_time=this_time;
if(use_stderr)
fprintf(stderr,"%s[%d] Timestamp: %s",program,pid,ctime(&this_time));
}
if(string)
free(string);
string=(char*)malloc(str_len);
#ifdef USE_STD_ARG
va_start(ap,fmt);
#else
va_start(ap);
#endif
for(i=0,j=0;fmt[i];i++)
if(fmt[i]!='%')
string[j++]=fmt[i];
else
{
char str[16],*strp=NULL;
switch(fmt[++i])
{
case '!':
if(fmt[++i]=='s')
if(errno!=-1)
strp=strerror(errno);
else
strp=str_h_error(h_errno);
else
if(errno!=-1)
sprintf(strp=str,"%d",errno);
else
sprintf(strp=str,"%d (h_errno)",h_errno);
break;
case 'c':
str[0]=va_arg(ap,char);
str[1]=0;
strp=str;
break;
case 'd':
sprintf(strp=str,"%d",va_arg(ap,int));
break;
case 's':
strp=va_arg(ap,char*);
if(!strp) strp="(null)";
break;
default:
str[0]='%';
str[1]=fmt[i];
str[2]=0;
strp=str;
(void)va_arg(ap,void*);
}
str_len+=strlen(strp);
string=realloc(string,str_len);
strcpy(&string[j],strp);
j+=strlen(strp);
}
if(string[j-1]!='\n')
string[j++]='\n';
string[j]=0;
va_end(ap);
if(UseSyslog && use_syslog && errlev>=LogLevel && ErrorPriority[errlev]!=-1)
syslog(ErrorPriority[errlev],"%s",string);
if(use_stderr && ((DebugLevel==-1 && errlev>=LogLevel) || (DebugLevel!=-1 && errlev>=DebugLevel)))
fprintf(stderr,"%s[%d] %s %s",program,pid,ErrorString[errlev],string);
if(errlev==Fatal)
exit(2);
return(string);
}