1/* $OpenBSD: log.c,v 1.12 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 <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 27static int debug; 28static int verbose; 29const char *log_procname; 30 31void log_init(int, int); 32void log_procinit(const char *); 33void log_setverbose(int); 34int log_getverbose(void); 35void log_warn(const char *, ...) 36 __attribute__((__format__ (printf, 1, 2))); 37void log_warnx(const char *, ...) 38 __attribute__((__format__ (printf, 1, 2))); 39void log_info(const char *, ...) 40 __attribute__((__format__ (printf, 1, 2))); 41void log_debug(const char *, ...) 42 __attribute__((__format__ (printf, 1, 2))); 43void logit(int, const char *, ...) 44 __attribute__((__format__ (printf, 2, 3))); 45void vlog(int, const char *, va_list) 46 __attribute__((__format__ (printf, 2, 0))); 47__dead void fatal(const char *, ...) 48 __attribute__((__format__ (printf, 1, 2))); 49__dead void fatalx(const char *, ...) 50 __attribute__((__format__ (printf, 1, 2))); 51 52void 53log_init(int n_debug, int facility) 54{ 55 extern char *__progname; 56 57 debug = n_debug; 58 verbose = n_debug; 59 log_procinit(__progname); 60 61 if (!debug) 62 openlog(__progname, LOG_PID | LOG_NDELAY, facility); 63 64 tzset(); 65} 66 67void 68log_procinit(const char *procname) 69{ 70 if (procname != NULL) 71 log_procname = procname; 72} 73 74void 75log_setverbose(int v) 76{ 77 verbose = v; 78} 79 80int 81log_getverbose(void) 82{ 83 return (verbose); 84} 85 86void 87logit(int pri, const char *fmt, ...) 88{ 89 va_list ap; 90 91 va_start(ap, fmt); 92 vlog(pri, fmt, ap); 93 va_end(ap); 94} 95 96void 97vlog(int pri, const char *fmt, va_list ap) 98{ 99 char *nfmt; 100 int saved_errno = errno; 101 102 if (debug) { 103 /* best effort in out of mem situations */ 104 if (asprintf(&nfmt, "%s\n", fmt) == -1) { 105 vfprintf(stderr, fmt, ap); 106 fprintf(stderr, "\n"); 107 } else { 108 vfprintf(stderr, nfmt, ap); 109 free(nfmt); 110 } 111 fflush(stderr); 112 } else 113 vsyslog(pri, fmt, ap); 114 115 errno = saved_errno; 116} 117 118void 119log_warn(const char *emsg, ...) 120{ 121 char *nfmt; 122 va_list ap; 123 int saved_errno = errno; 124 125 /* best effort to even work in out of memory situations */ 126 if (emsg == NULL) 127 logit(LOG_ERR, "%s", strerror(saved_errno)); 128 else { 129 va_start(ap, emsg); 130 131 if (asprintf(&nfmt, "%s: %s", emsg, 132 strerror(saved_errno)) == -1) { 133 /* we tried it... */ 134 vlog(LOG_ERR, emsg, ap); 135 logit(LOG_ERR, "%s", strerror(saved_errno)); 136 } else { 137 vlog(LOG_ERR, nfmt, ap); 138 free(nfmt); 139 } 140 va_end(ap); 141 } 142 143 errno = saved_errno; 144} 145 146void 147log_warnx(const char *emsg, ...) 148{ 149 va_list ap; 150 151 va_start(ap, emsg); 152 vlog(LOG_ERR, emsg, ap); 153 va_end(ap); 154} 155 156void 157log_info(const char *emsg, ...) 158{ 159 va_list ap; 160 161 va_start(ap, emsg); 162 vlog(LOG_INFO, emsg, ap); 163 va_end(ap); 164} 165 166void 167log_debug(const char *emsg, ...) 168{ 169 va_list ap; 170 171 if (verbose > 1) { 172 va_start(ap, emsg); 173 vlog(LOG_DEBUG, emsg, ap); 174 va_end(ap); 175 } 176} 177 178static void 179vfatalc(int code, const char *emsg, va_list ap) 180{ 181 static char s[BUFSIZ]; 182 const char *sep; 183 184 if (emsg != NULL) { 185 (void)vsnprintf(s, sizeof(s), emsg, ap); 186 sep = ": "; 187 } else { 188 s[0] = '\0'; 189 sep = ""; 190 } 191 if (code) 192 logit(LOG_CRIT, "%s: %s%s%s", 193 log_procname, s, sep, strerror(code)); 194 else 195 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s); 196} 197 198void 199fatal(const char *emsg, ...) 200{ 201 va_list ap; 202 203 va_start(ap, emsg); 204 vfatalc(errno, emsg, ap); 205 va_end(ap); 206 exit(1); 207} 208 209void 210fatalx(const char *emsg, ...) 211{ 212 va_list ap; 213 214 va_start(ap, emsg); 215 vfatalc(0, emsg, ap); 216 va_end(ap); 217 exit(1); 218} 219