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