1 /* $OpenBSD: log.c,v 1.2 2010/06/24 20:15:30 reyk Exp $ */ 2 /* $vantronix: log.c,v 1.4 2010/04/21 19:29:09 reyk Exp $ */ 3 4 /* 5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/param.h> 21 #include <sys/queue.h> 22 #include <sys/socket.h> 23 #include <sys/tree.h> 24 25 #include <errno.h> 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <syslog.h> 31 #include <event.h> 32 33 #include "iked.h" 34 35 int debug; 36 int verbose; 37 38 void vlog(int, const char *, va_list); 39 void logit(int, const char *, ...); 40 41 void 42 log_init(int n_debug) 43 { 44 extern char *__progname; 45 46 debug = n_debug; 47 verbose = n_debug; 48 49 if (!debug) 50 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 51 52 tzset(); 53 } 54 55 void 56 log_verbose(int v) 57 { 58 verbose = v; 59 } 60 61 void 62 logit(int pri, const char *fmt, ...) 63 { 64 va_list ap; 65 66 va_start(ap, fmt); 67 vlog(pri, fmt, ap); 68 va_end(ap); 69 } 70 71 void 72 vlog(int pri, const char *fmt, va_list ap) 73 { 74 char *nfmt; 75 76 if (debug) { 77 /* best effort in out of mem situations */ 78 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 79 vfprintf(stderr, fmt, ap); 80 fprintf(stderr, "\n"); 81 } else { 82 vfprintf(stderr, nfmt, ap); 83 free(nfmt); 84 } 85 fflush(stderr); 86 } else 87 vsyslog(pri, fmt, ap); 88 } 89 90 91 void 92 log_warn(const char *emsg, ...) 93 { 94 char *nfmt; 95 va_list ap; 96 97 /* best effort to even work in out of memory situations */ 98 if (emsg == NULL) 99 logit(LOG_CRIT, "%s", strerror(errno)); 100 else { 101 va_start(ap, emsg); 102 103 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 104 /* we tried it... */ 105 vlog(LOG_CRIT, emsg, ap); 106 logit(LOG_CRIT, "%s", strerror(errno)); 107 } else { 108 vlog(LOG_CRIT, nfmt, ap); 109 free(nfmt); 110 } 111 va_end(ap); 112 } 113 } 114 115 void 116 log_warnx(const char *emsg, ...) 117 { 118 va_list ap; 119 120 va_start(ap, emsg); 121 vlog(LOG_CRIT, emsg, ap); 122 va_end(ap); 123 } 124 125 void 126 log_info(const char *emsg, ...) 127 { 128 va_list ap; 129 130 va_start(ap, emsg); 131 vlog(LOG_INFO, emsg, ap); 132 va_end(ap); 133 } 134 135 void 136 log_debug(const char *emsg, ...) 137 { 138 va_list ap; 139 140 if (verbose > 1) { 141 va_start(ap, emsg); 142 vlog(LOG_DEBUG, emsg, ap); 143 va_end(ap); 144 } 145 } 146 147 void 148 print_debug(const char *emsg, ...) 149 { 150 va_list ap; 151 152 if (debug && verbose > 2) { 153 va_start(ap, emsg); 154 vfprintf(stderr, emsg, ap); 155 va_end(ap); 156 } 157 } 158 159 void 160 print_verbose(const char *emsg, ...) 161 { 162 va_list ap; 163 164 if (verbose) { 165 va_start(ap, emsg); 166 vfprintf(stderr, emsg, ap); 167 va_end(ap); 168 } 169 } 170 171 void 172 fatal(const char *emsg) 173 { 174 if (emsg == NULL) 175 logit(LOG_CRIT, "fatal: %s", strerror(errno)); 176 else { 177 if (errno) 178 logit(LOG_CRIT, "fatal: %s: %s", 179 emsg, strerror(errno)); 180 else 181 logit(LOG_CRIT, "fatal: %s", emsg); 182 } 183 184 exit(1); 185 } 186 187 void 188 fatalx(const char *emsg) 189 { 190 errno = 0; 191 fatal(emsg); 192 } 193