1 /* $OpenBSD: log.c,v 1.16 2014/07/08 10:30:52 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/socket.h> 23 24 #include <errno.h> 25 #include <pwd.h> 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <syslog.h> 31 #include <time.h> 32 33 #include "log.h" 34 35 #define TRACE_DEBUG 0x1 36 37 static int foreground; 38 static int verbose; 39 40 void vlog(int, const char *, va_list); 41 void logit(int, const char *, ...) 42 __attribute__((format (printf, 2, 3))); 43 44 45 void 46 log_init(int n_foreground) 47 { 48 extern char *__progname; 49 50 foreground = n_foreground; 51 if (! foreground) 52 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_MAIL); 53 54 tzset(); 55 } 56 57 void 58 log_verbose(int v) 59 { 60 verbose = v; 61 } 62 63 void 64 logit(int pri, const char *fmt, ...) 65 { 66 va_list ap; 67 68 va_start(ap, fmt); 69 vlog(pri, fmt, ap); 70 va_end(ap); 71 } 72 73 void 74 vlog(int pri, const char *fmt, va_list ap) 75 { 76 char *nfmt; 77 78 if (foreground) { 79 /* best effort in out of mem situations */ 80 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 81 vfprintf(stderr, fmt, ap); 82 fprintf(stderr, "\n"); 83 } else { 84 vfprintf(stderr, nfmt, ap); 85 free(nfmt); 86 } 87 fflush(stderr); 88 } else 89 vsyslog(pri, fmt, ap); 90 } 91 92 93 void 94 log_warn(const char *emsg, ...) 95 { 96 char *nfmt; 97 va_list ap; 98 99 /* best effort to even work in out of memory situations */ 100 if (emsg == NULL) 101 logit(LOG_CRIT, "%s", strerror(errno)); 102 else { 103 va_start(ap, emsg); 104 105 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 106 /* we tried it... */ 107 vlog(LOG_CRIT, emsg, ap); 108 logit(LOG_CRIT, "%s", strerror(errno)); 109 } else { 110 vlog(LOG_CRIT, nfmt, ap); 111 free(nfmt); 112 } 113 va_end(ap); 114 } 115 } 116 117 void 118 log_warnx(const char *emsg, ...) 119 { 120 va_list ap; 121 122 va_start(ap, emsg); 123 vlog(LOG_CRIT, emsg, ap); 124 va_end(ap); 125 } 126 127 void 128 log_info(const char *emsg, ...) 129 { 130 va_list ap; 131 132 va_start(ap, emsg); 133 vlog(LOG_INFO, emsg, ap); 134 va_end(ap); 135 } 136 137 void 138 log_debug(const char *emsg, ...) 139 { 140 va_list ap; 141 142 if (verbose & TRACE_DEBUG) { 143 va_start(ap, emsg); 144 vlog(LOG_DEBUG, emsg, ap); 145 va_end(ap); 146 } 147 } 148 149 void 150 log_trace(int mask, const char *emsg, ...) 151 { 152 va_list ap; 153 154 if (verbose & mask) { 155 va_start(ap, emsg); 156 vlog(LOG_DEBUG, emsg, ap); 157 va_end(ap); 158 } 159 } 160 161 static void 162 fatal_arg(const char *emsg, va_list ap) 163 { 164 #define FATALBUFSIZE 1024 165 static char ebuffer[FATALBUFSIZE]; 166 167 if (emsg == NULL) 168 (void)strlcpy(ebuffer, strerror(errno), sizeof ebuffer); 169 else { 170 if (errno) { 171 (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); 172 (void)strlcat(ebuffer, ": ", sizeof ebuffer); 173 (void)strlcat(ebuffer, strerror(errno), sizeof ebuffer); 174 } 175 else 176 (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap); 177 } 178 logit(LOG_CRIT, "fatal: %s", ebuffer); 179 } 180 181 void 182 fatal(const char *emsg, ...) 183 { 184 va_list ap; 185 186 va_start(ap, emsg); 187 fatal_arg(emsg, ap); 188 va_end(ap); 189 exit(1); 190 } 191 192 void 193 fatalx(const char *emsg, ...) 194 { 195 va_list ap; 196 197 errno = 0; 198 va_start(ap, emsg); 199 fatal_arg(emsg, ap); 200 va_end(ap); 201 exit(1); 202 } 203