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