1/* $NetBSD: report.c,v 1.6 2002/07/14 00:26:18 wiz Exp $ */ 2 3#include <sys/cdefs.h> 4#ifndef lint 5__RCSID("$NetBSD: report.c,v 1.6 2002/07/14 00:26:18 wiz 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 30extern int debug; 31extern char *progname; 32 33/* 34 * This is initialized so you get stderr until you call 35 * report_init() 36 */ 37static int stderr_only = 1; 38 39void 40report_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 62static 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}; 87static 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 */ 94void 95report(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 121 122/* 123 * Return pointer to static string which gives full filesystem error message. 124 */ 125const char * 126get_errmsg(void) 127{ 128 extern int errno; 129 130 return strerror(errno); 131} 132 133/* 134 * Local Variables: 135 * tab-width: 4 136 * c-indent-level: 4 137 * c-argdecl-indent: 4 138 * c-continued-statement-offset: 4 139 * c-continued-brace-offset: -4 140 * c-label-offset: -4 141 * c-brace-offset: 0 142 * End: 143 */ 144