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