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