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