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