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