xref: /openbsd-src/sbin/iked/log.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: log.c,v 1.3 2013/01/08 10:38:19 reyk 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 <sys/param.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/tree.h>
23 
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <event.h>
31 
32 #include "iked.h"
33 
34 int	 debug;
35 int	 verbose;
36 
37 void	 vlog(int, const char *, va_list);
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 	verbose = n_debug;
47 
48 	if (!debug)
49 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
50 
51 	tzset();
52 }
53 
54 void
55 log_verbose(int v)
56 {
57 	verbose = v;
58 }
59 
60 void
61 logit(int pri, const char *fmt, ...)
62 {
63 	va_list	ap;
64 
65 	va_start(ap, fmt);
66 	vlog(pri, fmt, ap);
67 	va_end(ap);
68 }
69 
70 void
71 vlog(int pri, const char *fmt, va_list ap)
72 {
73 	char	*nfmt;
74 
75 	if (debug) {
76 		/* best effort in out of mem situations */
77 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
78 			vfprintf(stderr, fmt, ap);
79 			fprintf(stderr, "\n");
80 		} else {
81 			vfprintf(stderr, nfmt, ap);
82 			free(nfmt);
83 		}
84 		fflush(stderr);
85 	} else
86 		vsyslog(pri, fmt, ap);
87 }
88 
89 
90 void
91 log_warn(const char *emsg, ...)
92 {
93 	char	*nfmt;
94 	va_list	 ap;
95 
96 	/* best effort to even work in out of memory situations */
97 	if (emsg == NULL)
98 		logit(LOG_CRIT, "%s", strerror(errno));
99 	else {
100 		va_start(ap, emsg);
101 
102 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
103 			/* we tried it... */
104 			vlog(LOG_CRIT, emsg, ap);
105 			logit(LOG_CRIT, "%s", strerror(errno));
106 		} else {
107 			vlog(LOG_CRIT, nfmt, ap);
108 			free(nfmt);
109 		}
110 		va_end(ap);
111 	}
112 }
113 
114 void
115 log_warnx(const char *emsg, ...)
116 {
117 	va_list	 ap;
118 
119 	va_start(ap, emsg);
120 	vlog(LOG_CRIT, emsg, ap);
121 	va_end(ap);
122 }
123 
124 void
125 log_info(const char *emsg, ...)
126 {
127 	va_list	 ap;
128 
129 	va_start(ap, emsg);
130 	vlog(LOG_INFO, emsg, ap);
131 	va_end(ap);
132 }
133 
134 void
135 log_debug(const char *emsg, ...)
136 {
137 	va_list	 ap;
138 
139 	if (verbose > 1) {
140 		va_start(ap, emsg);
141 		vlog(LOG_DEBUG, emsg, ap);
142 		va_end(ap);
143 	}
144 }
145 
146 void
147 print_debug(const char *emsg, ...)
148 {
149 	va_list	 ap;
150 
151 	if (debug && verbose > 2) {
152 		va_start(ap, emsg);
153 		vfprintf(stderr, emsg, ap);
154 		va_end(ap);
155 	}
156 }
157 
158 void
159 print_verbose(const char *emsg, ...)
160 {
161 	va_list	 ap;
162 
163 	if (verbose) {
164 		va_start(ap, emsg);
165 		vfprintf(stderr, emsg, ap);
166 		va_end(ap);
167 	}
168 }
169 
170 void
171 fatal(const char *emsg)
172 {
173 	if (emsg == NULL)
174 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
175 	else {
176 		if (errno)
177 			logit(LOG_CRIT, "fatal: %s: %s",
178 			    emsg, strerror(errno));
179 		else
180 			logit(LOG_CRIT, "fatal: %s", emsg);
181 	}
182 
183 	exit(1);
184 }
185 
186 void
187 fatalx(const char *emsg)
188 {
189 	errno = 0;
190 	fatal(emsg);
191 }
192