log.c (1468B)
1 #include <errno.h> 2 #include <stdarg.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <syslog.h> 7 8 #include "warp.h" 9 10 int debug; 11 12 static char *progname; 13 14 static void 15 vlog(int priority, char *msg, va_list ap) 16 { 17 if (debug) { 18 fprintf(stderr, "%s: ", progname); 19 vfprintf(stderr, msg, ap); 20 fputc('\n', stderr); 21 } else { 22 vsyslog(priority, msg, ap); 23 } 24 } 25 26 void 27 loginit(char *prog) 28 { 29 progname = prog; 30 if (!debug) 31 openlog(progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 32 } 33 34 void 35 logdbg(char *msg, ...) 36 { 37 char buf[512]; 38 va_list ap; 39 40 va_start(ap, msg); 41 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno)); 42 vlog(LOG_DAEMON | LOG_DEBUG, buf, ap); 43 va_end(ap); 44 } 45 46 void 47 logdbgx(char *msg, ...) 48 { 49 va_list ap; 50 51 va_start(ap, msg); 52 vlog(LOG_DAEMON | LOG_DEBUG, msg, ap); 53 va_end(ap); 54 } 55 56 void 57 logwarn(char *msg, ...) 58 { 59 char buf[512]; 60 va_list ap; 61 62 va_start(ap, msg); 63 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno)); 64 vlog(LOG_DAEMON | LOG_WARNING, buf, ap); 65 va_end(ap); 66 } 67 68 void 69 logwarnx(char *msg, ...) 70 { 71 va_list ap; 72 73 va_start(ap, msg); 74 vlog(LOG_DAEMON | LOG_WARNING, msg, ap); 75 va_end(ap); 76 } 77 78 void 79 fatal(char *msg, ...) 80 { 81 char buf[512]; 82 va_list ap; 83 84 va_start(ap, msg); 85 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno)); 86 vlog(LOG_DAEMON | LOG_ERR, buf, ap); 87 va_end(ap); 88 exit(1); 89 } 90 91 void 92 fatalx(char *msg, ...) 93 { 94 va_list ap; 95 96 va_start(ap, msg); 97 vlog(LOG_DAEMON | LOG_ERR, msg, ap); 98 va_end(ap); 99 exit(1); 100 }