1 /* $FreeBSD: src/libexec/bootpd/report.c,v 1.2.6.2 2001/03/05 10:58:59 kris Exp $ */ 2 3 /* 4 * report() - calls syslog 5 */ 6 7 #ifdef __STDC__ 8 #include <stdarg.h> 9 #else 10 #include <varargs.h> 11 #endif 12 13 #include <stdio.h> 14 #include <syslog.h> 15 #include <string.h> 16 #include <errno.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 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 #ifdef __STDC__ 95 void 96 report(int priority, const char *fmt,...) 97 #else 98 /*VARARGS2*/ 99 void 100 report(priority, fmt, va_alist) 101 int priority; 102 const char *fmt; 103 va_dcl 104 #endif 105 { 106 va_list ap; 107 static char buf[128]; 108 109 if ((priority < 0) || (priority >= numlevels)) { 110 priority = numlevels - 1; 111 } 112 #ifdef __STDC__ 113 va_start(ap, fmt); 114 #else 115 va_start(ap); 116 #endif 117 vsnprintf(buf, sizeof(buf), fmt, ap); 118 va_end(ap); 119 120 /* 121 * Print the message 122 */ 123 if (stderr_only || (debug > 2)) { 124 fprintf(stderr, "%s: %s %s\n", 125 progname, levelnames[priority], buf); 126 } 127 #ifdef SYSLOG 128 if (!stderr_only) 129 syslog((priority | LOG_BOOTP), "%s", buf); 130 #endif 131 } 132 133 134 135 /* 136 * Return pointer to static string which gives full filesystem error message. 137 */ 138 const char * 139 get_errmsg(void) 140 { 141 return strerror(errno); 142 } 143