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