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