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