1 /* $OpenBSD: log.c,v 1.1 2010/06/03 16:41:12 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 if (v > debug) 59 verbose = v; 60 } 61 62 void 63 logit(int pri, const char *fmt, ...) 64 { 65 va_list ap; 66 67 va_start(ap, fmt); 68 vlog(pri, fmt, ap); 69 va_end(ap); 70 } 71 72 void 73 vlog(int pri, const char *fmt, va_list ap) 74 { 75 char *nfmt; 76 77 if (debug) { 78 /* best effort in out of mem situations */ 79 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 80 vfprintf(stderr, fmt, ap); 81 fprintf(stderr, "\n"); 82 } else { 83 vfprintf(stderr, nfmt, ap); 84 free(nfmt); 85 } 86 fflush(stderr); 87 } else 88 vsyslog(pri, fmt, ap); 89 } 90 91 92 void 93 log_warn(const char *emsg, ...) 94 { 95 char *nfmt; 96 va_list ap; 97 98 /* best effort to even work in out of memory situations */ 99 if (emsg == NULL) 100 logit(LOG_CRIT, "%s", strerror(errno)); 101 else { 102 va_start(ap, emsg); 103 104 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 105 /* we tried it... */ 106 vlog(LOG_CRIT, emsg, ap); 107 logit(LOG_CRIT, "%s", strerror(errno)); 108 } else { 109 vlog(LOG_CRIT, nfmt, ap); 110 free(nfmt); 111 } 112 va_end(ap); 113 } 114 } 115 116 void 117 log_warnx(const char *emsg, ...) 118 { 119 va_list ap; 120 121 va_start(ap, emsg); 122 vlog(LOG_CRIT, emsg, ap); 123 va_end(ap); 124 } 125 126 void 127 log_info(const char *emsg, ...) 128 { 129 va_list ap; 130 131 va_start(ap, emsg); 132 vlog(LOG_INFO, emsg, ap); 133 va_end(ap); 134 } 135 136 void 137 log_debug(const char *emsg, ...) 138 { 139 va_list ap; 140 141 if (verbose > 1) { 142 va_start(ap, emsg); 143 vlog(LOG_DEBUG, emsg, ap); 144 va_end(ap); 145 } 146 } 147 148 void 149 print_debug(const char *emsg, ...) 150 { 151 va_list ap; 152 153 if (debug && verbose > 2) { 154 va_start(ap, emsg); 155 vfprintf(stderr, emsg, ap); 156 va_end(ap); 157 } 158 } 159 160 void 161 print_verbose(const char *emsg, ...) 162 { 163 va_list ap; 164 165 if (verbose) { 166 va_start(ap, emsg); 167 vfprintf(stderr, emsg, ap); 168 va_end(ap); 169 } 170 } 171 172 void 173 fatal(const char *emsg) 174 { 175 if (emsg == NULL) 176 logit(LOG_CRIT, "fatal: %s", strerror(errno)); 177 else { 178 if (errno) 179 logit(LOG_CRIT, "fatal: %s: %s", 180 emsg, strerror(errno)); 181 else 182 logit(LOG_CRIT, "fatal: %s", emsg); 183 } 184 185 exit(1); 186 } 187 188 void 189 fatalx(const char *emsg) 190 { 191 errno = 0; 192 fatal(emsg); 193 } 194