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