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 >
C/C++ Source or Header  |  1997-12-30  |  6KB  |  274 lines

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/errors.c 2.6 1997/12/29 17:15:47 amb Exp $
  3.  
  4.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.0a.
  5.   Generate error messages in a standard format optionally to syslog and stderr.
  6.   ******************/ /******************
  7.   Written by Andrew M. Bishop
  8.  
  9.   This file Copyright 1996,97 Andrew M. Bishop
  10.   It may be distributed under the GNU Public License, version 2, or
  11.   any higher version.  See section COPYING of the GNU Public license
  12.   for conditions under which this file may be redistributed.
  13.   ***************************************/
  14.  
  15.  
  16. /*+ Under Linux, FreeBSD, NetBSD, SGI, Ultrix, AIX and AT&T stdarg is used +*/
  17. #if defined(__linux__) || \
  18.     defined(__FreeBSD__) || defined(__NetBSD__) || \
  19.     defined(__sgi__) || defined(__ultrix__) || \
  20.     defined(_AIX) || defined(_ATT4)
  21. #define USE_STD_ARG
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include <unistd.h>
  29. #include <time.h>
  30. #include <sys/types.h>
  31.  
  32. #ifdef USE_STD_ARG
  33. #include <stdarg.h>
  34. #else
  35. #include <varargs.h>
  36. #endif
  37.  
  38. #include <syslog.h>
  39.  
  40. #include <errno.h>
  41.  
  42. #if defined(__sun__) && !defined(__svr4__)
  43.  
  44. /* SunOS 4.x does not have strerror(). */
  45.  
  46. char* strerror(int err);
  47.  
  48. extern int sys_nerr;
  49. extern char *sys_errlist[];
  50.  
  51. char* strerror(int err)
  52. {
  53.  if(err>0 && err<sys_nerr)
  54.     return(sys_errlist[err]);
  55.  else
  56.     return("Unknown error");
  57. }
  58.  
  59. #endif /* defined(__sun__) && !defined(__svr4__) */
  60.  
  61. #include <netdb.h>
  62.  
  63. /* A function to get an error message for h_errno. */
  64.  
  65. char* str_h_error(int err);
  66.  
  67. char* str_h_error(int err)
  68. {
  69. #ifdef NETDB_INTERNAL
  70.  if(err==NETDB_INTERNAL)
  71.     return("Name Lookup Internal error");
  72.  else
  73. #endif
  74. #ifdef NETDB_SUCCESS
  75.  if(err==NETDB_SUCCESS)
  76.     return("Name Lookup Success");
  77.  else
  78. #endif
  79. #ifdef HOST_NOT_FOUND
  80.  if(err==HOST_NOT_FOUND)
  81.     return("Name Lookup Authoritative Answer Host not found");
  82.  else
  83. #endif
  84. #ifdef TRY_AGAIN
  85.  if(err==TRY_AGAIN)
  86.     return("Name Lookup Non-Authoritive Answer Host not found");
  87.  else
  88. #endif
  89. #ifdef NO_RECOVERY
  90.  if(err==NO_RECOVERY)
  91.     return("Name Lookup Non recoverable error");
  92.  else
  93. #endif
  94. #ifdef NO_DATA
  95.  if(err==NO_DATA)
  96.     return("Name Lookup Valid name, no data record of requested type");
  97.  else
  98. #endif
  99. #ifdef NO_ADDRESS
  100.  if(err==NO_ADDRESS)
  101.     return("Name Lookup Valid name, no data record of requested type");
  102.  else
  103. #endif
  104.  return("Unknown error");
  105. }
  106.  
  107. #include "config.h"
  108. #include "errors.h"
  109.  
  110.  
  111. /*+ The name of the program. +*/
  112. static char *program=NULL;
  113.  
  114. /*+ The process id of the program. +*/
  115. static pid_t pid;
  116.  
  117. /*+ The last time that a message was printed. +*/
  118. static time_t last_time;
  119.  
  120. /*+ The error messages. +*/
  121. static char *ErrorString[]={"Debug:" ,"Information:","Important:","Warning:" ,"Fatal:"};
  122.  
  123. /*+ The priority to apply to syslog messages. +*/
  124. static int ErrorPriority[]={LOG_DEBUG,LOG_INFO      ,LOG_NOTICE  ,LOG_WARNING,LOG_ERR};
  125.  
  126. /*+ The error facility to use, +*/
  127. static int use_syslog=0,        /*+ use syslog. +*/
  128.            use_stderr=0;        /*+ use stderr. +*/
  129.  
  130.  
  131. /*++++++++++++++++++++++++++++++++++++++
  132.   Initialise the error handler, get the program name and pid.
  133.  
  134.   char *name The name of the program.
  135.  
  136.   int syslogable Set to true if the errors are allowed to go to syslog (or -1 to remain unchanged).
  137.  
  138.   int stderrable Set to true if the errors are allowed to go to stderr (or -1 to remain unchanged).
  139.   ++++++++++++++++++++++++++++++++++++++*/
  140.  
  141. void InitErrorHandler(char *name,int syslogable,int stderrable)
  142. {
  143.  if(use_syslog && program)
  144.     closelog();
  145.  
  146.  program=name;
  147.  if(syslogable!=-1)
  148.     use_syslog=syslogable;
  149.  if(stderrable!=-1)
  150.     use_stderr=stderrable;
  151.  
  152.  if(use_syslog)
  153.    {
  154. #if defined(__ultrix__)
  155.     openlog(program,LOG_PID);
  156. #else
  157.     openlog(program,LOG_CONS|LOG_PID,LOG_DAEMON);
  158. #endif
  159.     atexit(closelog);
  160.    }
  161.  
  162.  pid=getpid();
  163.  
  164.  last_time=time(NULL)-3300;
  165. }
  166.  
  167.  
  168. /*++++++++++++++++++++++++++++++++++++++
  169.   Print an error message.
  170.  
  171.   char *PrintMessage Return the error message (except the pid etc.).
  172.  
  173.   ErrorLevel errlev Which error level.
  174.  
  175.   const char* fmt The format of the message.
  176.  
  177.   ... The rest of the arguments (printf style).
  178.   ++++++++++++++++++++++++++++++++++++++*/
  179.  
  180. char *PrintMessage(ErrorLevel errlev,const char* fmt, ...)
  181. {
  182.  int str_len=16+strlen(fmt);
  183.  static char* string=NULL;
  184.  va_list ap;
  185.  int i,j;
  186.  time_t this_time=time(NULL);
  187.  
  188.  if((this_time-last_time)>3600)
  189.    {
  190.     last_time=this_time;
  191.     if(use_stderr)
  192.        fprintf(stderr,"%s[%d] Timestamp: %s",program,pid,ctime(&this_time));
  193.    }
  194.  
  195.  if(string)
  196.     free(string);
  197.  
  198.  string=(char*)malloc(str_len);
  199.  
  200. #ifdef USE_STD_ARG
  201.  va_start(ap,fmt);
  202. #else
  203.  va_start(ap);
  204. #endif
  205.  
  206.  for(i=0,j=0;fmt[i];i++)
  207.     if(fmt[i]!='%')
  208.        string[j++]=fmt[i];
  209.     else
  210.       {
  211.        char str[16],*strp=NULL;
  212.  
  213.        switch(fmt[++i])
  214.          {
  215.          case '!':
  216.           if(fmt[++i]=='s')
  217.              if(errno!=-1)
  218.                 strp=strerror(errno);
  219.              else
  220.                 strp=str_h_error(h_errno);
  221.           else
  222.              if(errno!=-1)
  223.                 sprintf(strp=str,"%d",errno);
  224.              else
  225.                 sprintf(strp=str,"%d (h_errno)",h_errno);
  226.           break;
  227.  
  228.          case 'c':
  229.           str[0]=va_arg(ap,char);
  230.           str[1]=0;
  231.           strp=str;
  232.           break;
  233.  
  234.          case 'd':
  235.           sprintf(strp=str,"%d",va_arg(ap,int));
  236.           break;
  237.  
  238.          case 's':
  239.           strp=va_arg(ap,char*);
  240.           if(!strp) strp="(null)";
  241.           break;
  242.  
  243.          default:
  244.           str[0]='%';
  245.           str[1]=fmt[i];
  246.           str[2]=0;
  247.           strp=str;
  248.           (void)va_arg(ap,void*);
  249.          }
  250.  
  251.        str_len+=strlen(strp);
  252.        string=realloc(string,str_len);
  253.        strcpy(&string[j],strp);
  254.        j+=strlen(strp);
  255.       }
  256.  
  257.  if(string[j-1]!='\n')
  258.     string[j++]='\n';
  259.  string[j]=0;
  260.  
  261.  va_end(ap);
  262.  
  263.  if(UseSyslog && use_syslog && errlev>=LogLevel && ErrorPriority[errlev]!=-1)
  264.     syslog(ErrorPriority[errlev],"%s",string);
  265.  
  266.  if(use_stderr && ((DebugLevel==-1 && errlev>=LogLevel) || (DebugLevel!=-1 && errlev>=DebugLevel)))
  267.     fprintf(stderr,"%s[%d] %s %s",program,pid,ErrorString[errlev],string);
  268.  
  269.  if(errlev==Fatal)
  270.     exit(2);
  271.  
  272.  return(string);
  273. }
  274.