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