xref: /openbsd-src/usr.sbin/lpd/log.c (revision 3b188dab47094e37e38dcd2dcd35528ab7a95e4d)
1*3b188dabSeric /*	$OpenBSD: log.c,v 1.1.1.1 2018/04/27 16:14:36 eric Exp $	*/
2*3b188dabSeric 
3*3b188dabSeric /*
4*3b188dabSeric  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5*3b188dabSeric  *
6*3b188dabSeric  * Permission to use, copy, modify, and distribute this software for any
7*3b188dabSeric  * purpose with or without fee is hereby granted, provided that the above
8*3b188dabSeric  * copyright notice and this permission notice appear in all copies.
9*3b188dabSeric  *
10*3b188dabSeric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*3b188dabSeric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*3b188dabSeric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*3b188dabSeric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*3b188dabSeric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*3b188dabSeric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*3b188dabSeric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*3b188dabSeric  */
18*3b188dabSeric 
19*3b188dabSeric #include <stdio.h>
20*3b188dabSeric #include <stdlib.h>
21*3b188dabSeric #include <stdarg.h>
22*3b188dabSeric #include <string.h>
23*3b188dabSeric #include <syslog.h>
24*3b188dabSeric #include <errno.h>
25*3b188dabSeric #include <time.h>
26*3b188dabSeric 
27*3b188dabSeric #include "log.h"
28*3b188dabSeric 
29*3b188dabSeric static int		 debug;
30*3b188dabSeric static int		 verbose;
31*3b188dabSeric static const char	*log_procname;
32*3b188dabSeric 
33*3b188dabSeric void
log_init(int n_debug,int facility)34*3b188dabSeric log_init(int n_debug, int facility)
35*3b188dabSeric {
36*3b188dabSeric 	extern char	*__progname;
37*3b188dabSeric 
38*3b188dabSeric 	debug = n_debug;
39*3b188dabSeric 	verbose = n_debug;
40*3b188dabSeric 	log_procinit(__progname);
41*3b188dabSeric 
42*3b188dabSeric 	if (!debug)
43*3b188dabSeric 		openlog(__progname, LOG_PID | LOG_NDELAY, facility);
44*3b188dabSeric 
45*3b188dabSeric 	tzset();
46*3b188dabSeric }
47*3b188dabSeric 
48*3b188dabSeric void
log_procinit(const char * procname)49*3b188dabSeric log_procinit(const char *procname)
50*3b188dabSeric {
51*3b188dabSeric 	if (procname != NULL)
52*3b188dabSeric 		log_procname = procname;
53*3b188dabSeric }
54*3b188dabSeric 
55*3b188dabSeric void
log_setverbose(int v)56*3b188dabSeric log_setverbose(int v)
57*3b188dabSeric {
58*3b188dabSeric 	verbose = v;
59*3b188dabSeric }
60*3b188dabSeric 
61*3b188dabSeric int
log_getverbose(void)62*3b188dabSeric log_getverbose(void)
63*3b188dabSeric {
64*3b188dabSeric 	return (verbose);
65*3b188dabSeric }
66*3b188dabSeric 
67*3b188dabSeric void
logit(int pri,const char * fmt,...)68*3b188dabSeric logit(int pri, const char *fmt, ...)
69*3b188dabSeric {
70*3b188dabSeric 	va_list	ap;
71*3b188dabSeric 
72*3b188dabSeric 	va_start(ap, fmt);
73*3b188dabSeric 	vlog(pri, fmt, ap);
74*3b188dabSeric 	va_end(ap);
75*3b188dabSeric }
76*3b188dabSeric 
77*3b188dabSeric void
vlog(int pri,const char * fmt,va_list ap)78*3b188dabSeric vlog(int pri, const char *fmt, va_list ap)
79*3b188dabSeric {
80*3b188dabSeric 	char	*nfmt;
81*3b188dabSeric 	int	 saved_errno = errno;
82*3b188dabSeric 
83*3b188dabSeric 	if (debug) {
84*3b188dabSeric 		/* best effort in out of mem situations */
85*3b188dabSeric 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
86*3b188dabSeric 			vfprintf(stderr, fmt, ap);
87*3b188dabSeric 			fprintf(stderr, "\n");
88*3b188dabSeric 		} else {
89*3b188dabSeric 			vfprintf(stderr, nfmt, ap);
90*3b188dabSeric 			free(nfmt);
91*3b188dabSeric 		}
92*3b188dabSeric 		fflush(stderr);
93*3b188dabSeric 	} else
94*3b188dabSeric 		vsyslog(pri, fmt, ap);
95*3b188dabSeric 
96*3b188dabSeric 	errno = saved_errno;
97*3b188dabSeric }
98*3b188dabSeric 
99*3b188dabSeric void
log_warn(const char * emsg,...)100*3b188dabSeric log_warn(const char *emsg, ...)
101*3b188dabSeric {
102*3b188dabSeric 	char		*nfmt;
103*3b188dabSeric 	va_list		 ap;
104*3b188dabSeric 	int		 saved_errno = errno;
105*3b188dabSeric 
106*3b188dabSeric 	/* best effort to even work in out of memory situations */
107*3b188dabSeric 	if (emsg == NULL)
108*3b188dabSeric 		logit(LOG_ERR, "%s", strerror(saved_errno));
109*3b188dabSeric 	else {
110*3b188dabSeric 		va_start(ap, emsg);
111*3b188dabSeric 
112*3b188dabSeric 		if (asprintf(&nfmt, "%s: %s", emsg,
113*3b188dabSeric 		    strerror(saved_errno)) == -1) {
114*3b188dabSeric 			/* we tried it... */
115*3b188dabSeric 			vlog(LOG_ERR, emsg, ap);
116*3b188dabSeric 			logit(LOG_ERR, "%s", strerror(saved_errno));
117*3b188dabSeric 		} else {
118*3b188dabSeric 			vlog(LOG_ERR, nfmt, ap);
119*3b188dabSeric 			free(nfmt);
120*3b188dabSeric 		}
121*3b188dabSeric 		va_end(ap);
122*3b188dabSeric 	}
123*3b188dabSeric 
124*3b188dabSeric 	errno = saved_errno;
125*3b188dabSeric }
126*3b188dabSeric 
127*3b188dabSeric void
log_warnx(const char * emsg,...)128*3b188dabSeric log_warnx(const char *emsg, ...)
129*3b188dabSeric {
130*3b188dabSeric 	va_list	 ap;
131*3b188dabSeric 
132*3b188dabSeric 	va_start(ap, emsg);
133*3b188dabSeric 	vlog(LOG_ERR, emsg, ap);
134*3b188dabSeric 	va_end(ap);
135*3b188dabSeric }
136*3b188dabSeric 
137*3b188dabSeric void
log_info(const char * emsg,...)138*3b188dabSeric log_info(const char *emsg, ...)
139*3b188dabSeric {
140*3b188dabSeric 	va_list	 ap;
141*3b188dabSeric 
142*3b188dabSeric 	va_start(ap, emsg);
143*3b188dabSeric 	vlog(LOG_INFO, emsg, ap);
144*3b188dabSeric 	va_end(ap);
145*3b188dabSeric }
146*3b188dabSeric 
147*3b188dabSeric void
log_debug(const char * emsg,...)148*3b188dabSeric log_debug(const char *emsg, ...)
149*3b188dabSeric {
150*3b188dabSeric 	va_list	 ap;
151*3b188dabSeric 
152*3b188dabSeric 	if (verbose) {
153*3b188dabSeric 		va_start(ap, emsg);
154*3b188dabSeric 		vlog(LOG_DEBUG, emsg, ap);
155*3b188dabSeric 		va_end(ap);
156*3b188dabSeric 	}
157*3b188dabSeric }
158*3b188dabSeric 
159*3b188dabSeric static void
vfatalc(int code,const char * emsg,va_list ap)160*3b188dabSeric vfatalc(int code, const char *emsg, va_list ap)
161*3b188dabSeric {
162*3b188dabSeric 	static char	s[BUFSIZ];
163*3b188dabSeric 	const char	*sep;
164*3b188dabSeric 
165*3b188dabSeric 	if (emsg != NULL) {
166*3b188dabSeric 		(void)vsnprintf(s, sizeof(s), emsg, ap);
167*3b188dabSeric 		sep = ": ";
168*3b188dabSeric 	} else {
169*3b188dabSeric 		s[0] = '\0';
170*3b188dabSeric 		sep = "";
171*3b188dabSeric 	}
172*3b188dabSeric 	if (code)
173*3b188dabSeric 		logit(LOG_CRIT, "fatal in %s: %s%s%s",
174*3b188dabSeric 		    log_procname, s, sep, strerror(code));
175*3b188dabSeric 	else
176*3b188dabSeric 		logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
177*3b188dabSeric }
178*3b188dabSeric 
179*3b188dabSeric void
fatal(const char * emsg,...)180*3b188dabSeric fatal(const char *emsg, ...)
181*3b188dabSeric {
182*3b188dabSeric 	va_list	ap;
183*3b188dabSeric 
184*3b188dabSeric 	va_start(ap, emsg);
185*3b188dabSeric 	vfatalc(errno, emsg, ap);
186*3b188dabSeric 	va_end(ap);
187*3b188dabSeric 	exit(1);
188*3b188dabSeric }
189*3b188dabSeric 
190*3b188dabSeric void
fatalx(const char * emsg,...)191*3b188dabSeric fatalx(const char *emsg, ...)
192*3b188dabSeric {
193*3b188dabSeric 	va_list	ap;
194*3b188dabSeric 
195*3b188dabSeric 	va_start(ap, emsg);
196*3b188dabSeric 	vfatalc(0, emsg, ap);
197*3b188dabSeric 	va_end(ap);
198*3b188dabSeric 	exit(1);
199*3b188dabSeric }
200