1 /* $OpenBSD: log.c,v 1.19 2019/07/03 05:04:19 otto 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 USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <stdarg.h> 22 #include <string.h> 23 #include <syslog.h> 24 #include <errno.h> 25 #include <time.h> 26 #include "log.h" 27 28 static int dest; 29 static int verbose; 30 const char *log_procname; 31 32 void 33 log_init(int n_dest, int n_verbose, int facility) 34 { 35 extern char *__progname; 36 37 dest = n_dest; 38 verbose = n_verbose; 39 log_procinit(__progname); 40 41 if (dest & LOG_TO_SYSLOG) 42 openlog(__progname, LOG_PID | LOG_NDELAY, facility); 43 44 tzset(); 45 } 46 47 void 48 log_procinit(const char *procname) 49 { 50 if (procname != NULL) 51 log_procname = procname; 52 } 53 54 void 55 log_setverbose(int v) 56 { 57 verbose = v; 58 } 59 60 int 61 log_getverbose(void) 62 { 63 return (verbose); 64 } 65 66 void 67 logit(int pri, const char *fmt, ...) 68 { 69 va_list ap; 70 71 va_start(ap, fmt); 72 vlog(pri, fmt, ap); 73 va_end(ap); 74 } 75 76 void 77 vlog(int pri, const char *fmt, va_list ap) 78 { 79 char *nfmt; 80 int saved_errno = errno; 81 va_list ap2; 82 83 va_copy(ap2, ap); 84 if (dest & LOG_TO_STDERR) { 85 /* best effort in out of mem situations */ 86 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 87 vfprintf(stderr, fmt, ap); 88 fprintf(stderr, "\n"); 89 } else { 90 vfprintf(stderr, nfmt, ap); 91 free(nfmt); 92 } 93 fflush(stderr); 94 } 95 if (dest & LOG_TO_SYSLOG) 96 vsyslog(pri, fmt, ap2); 97 va_end(ap2); 98 99 errno = saved_errno; 100 } 101 102 void 103 log_warn(const char *emsg, ...) 104 { 105 char *nfmt; 106 va_list ap; 107 int saved_errno = errno; 108 109 /* best effort to even work in out of memory situations */ 110 if (emsg == NULL) 111 logit(LOG_ERR, "%s", strerror(saved_errno)); 112 else { 113 va_start(ap, emsg); 114 115 if (asprintf(&nfmt, "%s: %s", emsg, 116 strerror(saved_errno)) == -1) { 117 /* we tried it... */ 118 vlog(LOG_ERR, emsg, ap); 119 logit(LOG_ERR, "%s", strerror(saved_errno)); 120 } else { 121 vlog(LOG_ERR, nfmt, ap); 122 free(nfmt); 123 } 124 va_end(ap); 125 } 126 127 errno = saved_errno; 128 } 129 130 void 131 log_warnx(const char *emsg, ...) 132 { 133 va_list ap; 134 135 va_start(ap, emsg); 136 vlog(LOG_ERR, emsg, ap); 137 va_end(ap); 138 } 139 140 void 141 log_info(const char *emsg, ...) 142 { 143 va_list ap; 144 145 va_start(ap, emsg); 146 vlog(LOG_INFO, emsg, ap); 147 va_end(ap); 148 } 149 150 void 151 log_debug(const char *emsg, ...) 152 { 153 va_list ap; 154 155 if (verbose > 1) { 156 va_start(ap, emsg); 157 vlog(LOG_DEBUG, emsg, ap); 158 va_end(ap); 159 } 160 } 161 162 static void 163 vfatalc(int code, const char *emsg, va_list ap) 164 { 165 static char s[BUFSIZ]; 166 const char *sep; 167 168 if (emsg != NULL) { 169 (void)vsnprintf(s, sizeof(s), emsg, ap); 170 sep = ": "; 171 } else { 172 s[0] = '\0'; 173 sep = ""; 174 } 175 if (code) 176 logit(LOG_CRIT, "%s: %s%s%s", 177 log_procname, s, sep, strerror(code)); 178 else 179 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s); 180 } 181 182 void 183 fatal(const char *emsg, ...) 184 { 185 va_list ap; 186 187 va_start(ap, emsg); 188 vfatalc(errno, emsg, ap); 189 va_end(ap); 190 exit(1); 191 } 192 193 void 194 fatalx(const char *emsg, ...) 195 { 196 va_list ap; 197 198 va_start(ap, emsg); 199 vfatalc(0, emsg, ap); 200 va_end(ap); 201 exit(1); 202 } 203