Home | History | Annotate | Line # | Download | only in common
report.c revision 1.1
      1  1.1  gwr /*
      2  1.1  gwr  * report() - calls syslog
      3  1.1  gwr  */
      4  1.1  gwr 
      5  1.1  gwr #ifdef	__STDC__
      6  1.1  gwr #include <stdarg.h>
      7  1.1  gwr #else
      8  1.1  gwr #include <varargs.h>
      9  1.1  gwr #endif
     10  1.1  gwr 
     11  1.1  gwr #include <stdio.h>
     12  1.1  gwr #include <syslog.h>
     13  1.1  gwr 
     14  1.1  gwr #include "report.h"
     15  1.1  gwr 
     16  1.1  gwr #ifndef LOG_NDELAY
     17  1.1  gwr #define LOG_NDELAY	0
     18  1.1  gwr #endif
     19  1.1  gwr #ifndef LOG_DAEMON
     20  1.1  gwr #define LOG_DAEMON	0
     21  1.1  gwr #endif
     22  1.1  gwr #ifndef	LOG_BOOTP
     23  1.1  gwr #define LOG_BOOTP	LOG_DAEMON
     24  1.1  gwr #endif
     25  1.1  gwr 
     26  1.1  gwr extern int debug;
     27  1.1  gwr extern char *progname;
     28  1.1  gwr 
     29  1.1  gwr /*
     30  1.1  gwr  * This is initialized so you get stderr until you call
     31  1.1  gwr  *	report_init()
     32  1.1  gwr  */
     33  1.1  gwr static int stderr_only = 1;
     34  1.1  gwr 
     35  1.1  gwr void
     36  1.1  gwr report_init(nolog)
     37  1.1  gwr 	int nolog;
     38  1.1  gwr {
     39  1.1  gwr 	stderr_only = nolog;
     40  1.1  gwr #ifdef SYSLOG
     41  1.1  gwr 	if (!stderr_only) {
     42  1.1  gwr 		openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
     43  1.1  gwr 	}
     44  1.1  gwr #endif
     45  1.1  gwr }
     46  1.1  gwr 
     47  1.1  gwr /*
     48  1.1  gwr  * This routine reports errors and such via stderr and syslog() if
     49  1.1  gwr  * appopriate.  It just helps avoid a lot of "#ifdef SYSLOG" constructs
     50  1.1  gwr  * from being scattered throughout the code.
     51  1.1  gwr  *
     52  1.1  gwr  * The syntax is identical to syslog(3), but %m is not considered special
     53  1.1  gwr  * for output to stderr (i.e. you'll see "%m" in the output. . .).  Also,
     54  1.1  gwr  * control strings should normally end with \n since newlines aren't
     55  1.1  gwr  * automatically generated for stderr output (whereas syslog strips out all
     56  1.1  gwr  * newlines and adds its own at the end).
     57  1.1  gwr  */
     58  1.1  gwr 
     59  1.1  gwr static char *levelnames[] = {
     60  1.1  gwr #ifdef LOG_SALERT
     61  1.1  gwr 	"level(0): ",
     62  1.1  gwr 	"alert(1): ",
     63  1.1  gwr 	"alert(2): ",
     64  1.1  gwr 	"emerg(3): ",
     65  1.1  gwr 	"error(4): ",
     66  1.1  gwr 	"crit(5):  ",
     67  1.1  gwr 	"warn(6):  ",
     68  1.1  gwr 	"note(7):  ",
     69  1.1  gwr 	"info(8):  ",
     70  1.1  gwr 	"debug(9): ",
     71  1.1  gwr 	"level(?): "
     72  1.1  gwr #else
     73  1.1  gwr 	"emerg(0): ",
     74  1.1  gwr 	"alert(1): ",
     75  1.1  gwr 	"crit(2):  ",
     76  1.1  gwr 	"error(3): ",
     77  1.1  gwr 	"warn(4):  ",
     78  1.1  gwr 	"note(5):  ",
     79  1.1  gwr 	"info(6):  ",
     80  1.1  gwr 	"debug(7): ",
     81  1.1  gwr 	"level(?): "
     82  1.1  gwr #endif
     83  1.1  gwr };
     84  1.1  gwr static int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
     85  1.1  gwr 
     86  1.1  gwr 
     87  1.1  gwr /*
     88  1.1  gwr  * Print a log message using syslog(3) and/or stderr.
     89  1.1  gwr  * The message passed in should not include a newline.
     90  1.1  gwr  */
     91  1.1  gwr #ifdef	__STDC__
     92  1.1  gwr void
     93  1.1  gwr report(int priority, char *fmt,...)
     94  1.1  gwr #else
     95  1.1  gwr /*VARARGS2*/
     96  1.1  gwr void
     97  1.1  gwr report(priority, fmt, va_alist)
     98  1.1  gwr 	int priority;
     99  1.1  gwr 	char *fmt;
    100  1.1  gwr 	va_dcl
    101  1.1  gwr #endif
    102  1.1  gwr {
    103  1.1  gwr 	va_list ap;
    104  1.1  gwr 	static char buf[128];
    105  1.1  gwr 
    106  1.1  gwr 	if ((priority < 0) || (priority >= numlevels)) {
    107  1.1  gwr 		priority = numlevels - 1;
    108  1.1  gwr 	}
    109  1.1  gwr #ifdef	__STDC__
    110  1.1  gwr 	va_start(ap, fmt);
    111  1.1  gwr #else
    112  1.1  gwr 	va_start(ap);
    113  1.1  gwr #endif
    114  1.1  gwr 	vsprintf(buf, fmt, ap);
    115  1.1  gwr 	va_end(ap);
    116  1.1  gwr 
    117  1.1  gwr 	/*
    118  1.1  gwr 	 * Print the message
    119  1.1  gwr 	 */
    120  1.1  gwr 	if (stderr_only || (debug > 2)) {
    121  1.1  gwr 		fprintf(stderr, "%s: %s %s\n",
    122  1.1  gwr 				progname, levelnames[priority], buf);
    123  1.1  gwr 	}
    124  1.1  gwr #ifdef SYSLOG
    125  1.1  gwr 	if (!stderr_only)
    126  1.1  gwr 		syslog((priority | LOG_BOOTP), "%s", buf);
    127  1.1  gwr #endif
    128  1.1  gwr }
    129  1.1  gwr 
    130  1.1  gwr 
    132  1.1  gwr 
    133  1.1  gwr /*
    134  1.1  gwr  * Return pointer to static string which gives full filesystem error message.
    135  1.1  gwr  */
    136  1.1  gwr char *
    137  1.1  gwr get_errmsg()
    138  1.1  gwr {
    139  1.1  gwr 	extern int errno;
    140  1.1  gwr 	extern char *strerror();
    141  1.1  gwr 
    142  1.1  gwr 	return strerror(errno);
    143  1.1  gwr }
    144  1.1  gwr 
    145  1.1  gwr /*
    146  1.1  gwr  * Local Variables:
    147  1.1  gwr  * tab-width: 4
    148  1.1  gwr  * c-indent-level: 4
    149  1.1  gwr  * c-argdecl-indent: 4
    150  1.1  gwr  * c-continued-statement-offset: 4
    151  1.1  gwr  * c-continued-brace-offset: -4
    152  1.1  gwr  * c-label-offset: -4
    153  1.1  gwr  * c-brace-offset: 0
    154  1.1  gwr  * End:
    155            */
    156