1 /* $OpenBSD: log.c,v 1.8 2017/03/21 12:06:55 bluhm 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 <sys/time.h> 20 #include <errno.h> 21 #include <stdarg.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <syslog.h> 26 #include <time.h> 27 #include <unistd.h> 28 29 #include "log.h" 30 31 int debug; 32 int verbose; 33 34 void 35 log_init(int n_debug) 36 { 37 extern char *__progname; 38 39 debug = n_debug; 40 41 if (!debug) 42 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); 43 44 tzset(); 45 } 46 47 void 48 log_verbose(int v) 49 { 50 verbose = v; 51 } 52 53 void 54 logit(int pri, const char *fmt, ...) 55 { 56 va_list ap; 57 58 va_start(ap, fmt); 59 vlog(pri, fmt, ap); 60 va_end(ap); 61 } 62 63 void 64 vlog(int pri, const char *fmt, va_list ap) 65 { 66 char datebuf[24]; 67 struct timeval tv; 68 struct tm *tm; 69 char *nfmt; 70 size_t rc; 71 time_t now; 72 73 if (debug) { 74 gettimeofday(&tv, NULL); 75 now = tv.tv_sec; 76 tm = localtime(&now); 77 rc = strftime(datebuf, sizeof(datebuf), "%b %e %H:%M:%S", tm); 78 if (rc == 0) 79 datebuf[0] = 0; 80 fprintf(stderr, "%s.%03ld [%d] ", datebuf, 81 tv.tv_usec / 1000, getpid()); 82 83 /* best effort in out of mem situations */ 84 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 85 vfprintf(stderr, fmt, ap); 86 fprintf(stderr, "\n"); 87 } else { 88 vfprintf(stderr, nfmt, ap); 89 free(nfmt); 90 } 91 fflush(stderr); 92 } else 93 vsyslog(pri, fmt, ap); 94 } 95 96 void 97 log_warn(const char *emsg, ...) 98 { 99 char *nfmt; 100 va_list ap; 101 102 /* best effort to even work in out of memory situations */ 103 if (emsg == NULL) 104 logit(LOG_ERR, "%s", strerror(errno)); 105 else { 106 va_start(ap, emsg); 107 108 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { 109 /* we tried it... */ 110 vlog(LOG_ERR, emsg, ap); 111 logit(LOG_ERR, "%s", strerror(errno)); 112 } else { 113 vlog(LOG_ERR, nfmt, ap); 114 free(nfmt); 115 } 116 va_end(ap); 117 } 118 } 119 120 void 121 log_warnx(const char *emsg, ...) 122 { 123 va_list ap; 124 125 va_start(ap, emsg); 126 vlog(LOG_ERR, emsg, ap); 127 va_end(ap); 128 } 129 130 void 131 log_info(const char *emsg, ...) 132 { 133 va_list ap; 134 135 va_start(ap, emsg); 136 vlog(LOG_INFO, emsg, ap); 137 va_end(ap); 138 } 139 140 void 141 log_debug(const char *emsg, ...) 142 { 143 va_list ap; 144 145 if (verbose) { 146 va_start(ap, emsg); 147 vlog(LOG_DEBUG, emsg, ap); 148 va_end(ap); 149 } 150 } 151 152 void 153 fatal(const char *emsg) 154 { 155 if (emsg == NULL) 156 logit(LOG_CRIT, "fatal: %s", strerror(errno)); 157 else 158 if (errno) 159 logit(LOG_CRIT, "fatal: %s: %s", 160 emsg, strerror(errno)); 161 else 162 logit(LOG_CRIT, "fatal: %s", emsg); 163 164 exit(1); 165 } 166 167 void 168 fatalx(const char *emsg) 169 { 170 errno = 0; 171 fatal(emsg); 172 } 173