1 /*	$OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $	*/
2 
3 /*
4  * log.c
5  *
6  * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
7  *
8  * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
9  *
10  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
11  *
12  * Copyright (c) 1993
13  *	The Regents of the University of California.  All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include "event2/event-config.h"
41 
42 #ifdef WIN32
43 #include <winsock2.h>
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #undef WIN32_LEAN_AND_MEAN
47 #endif
48 #include <sys/types.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <stdarg.h>
52 #include <string.h>
53 #include <errno.h>
54 #include "event2/event.h"
55 #include "event2/util.h"
56 
57 #include "log-internal.h"
58 
59 static void _warn_helper(int severity, const char *errstr, const char *fmt,
60     va_list ap);
61 static void event_log(int severity, const char *msg);
62 static void event_exit(int errcode) EV_NORETURN;
63 
64 static event_fatal_cb fatal_fn = NULL;
65 
66 void
event_set_fatal_callback(event_fatal_cb cb)67 event_set_fatal_callback(event_fatal_cb cb)
68 {
69 	fatal_fn = cb;
70 }
71 
72 static void
event_exit(int errcode)73 event_exit(int errcode)
74 {
75 	if (fatal_fn) {
76 		fatal_fn(errcode);
77 		exit(errcode); /* should never be reached */
78 	} else if (errcode == _EVENT_ERR_ABORT)
79 		abort();
80 	else
81 		exit(errcode);
82 }
83 
84 void
event_err(int eval,const char * fmt,...)85 event_err(int eval, const char *fmt, ...)
86 {
87 	va_list ap;
88 
89 	va_start(ap, fmt);
90 	_warn_helper(_EVENT_LOG_ERR, strerror(errno), fmt, ap);
91 	va_end(ap);
92 	event_exit(eval);
93 }
94 
95 void
event_warn(const char * fmt,...)96 event_warn(const char *fmt, ...)
97 {
98 	va_list ap;
99 
100 	va_start(ap, fmt);
101 	_warn_helper(_EVENT_LOG_WARN, strerror(errno), fmt, ap);
102 	va_end(ap);
103 }
104 
105 void
event_sock_err(int eval,evutil_socket_t sock,const char * fmt,...)106 event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
107 {
108 	va_list ap;
109 	int err = evutil_socket_geterror(sock);
110 
111 	va_start(ap, fmt);
112 	_warn_helper(_EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
113 	va_end(ap);
114 	event_exit(eval);
115 }
116 
117 void
event_sock_warn(evutil_socket_t sock,const char * fmt,...)118 event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
119 {
120 	va_list ap;
121 	int err = evutil_socket_geterror(sock);
122 
123 	va_start(ap, fmt);
124 	_warn_helper(_EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
125 	va_end(ap);
126 }
127 
128 void
event_errx(int eval,const char * fmt,...)129 event_errx(int eval, const char *fmt, ...)
130 {
131 	va_list ap;
132 
133 	va_start(ap, fmt);
134 	_warn_helper(_EVENT_LOG_ERR, NULL, fmt, ap);
135 	va_end(ap);
136 	event_exit(eval);
137 }
138 
139 void
event_warnx(const char * fmt,...)140 event_warnx(const char *fmt, ...)
141 {
142 	va_list ap;
143 
144 	va_start(ap, fmt);
145 	_warn_helper(_EVENT_LOG_WARN, NULL, fmt, ap);
146 	va_end(ap);
147 }
148 
149 void
event_msgx(const char * fmt,...)150 event_msgx(const char *fmt, ...)
151 {
152 	va_list ap;
153 
154 	va_start(ap, fmt);
155 	_warn_helper(_EVENT_LOG_MSG, NULL, fmt, ap);
156 	va_end(ap);
157 }
158 
159 void
_event_debugx(const char * fmt,...)160 _event_debugx(const char *fmt, ...)
161 {
162 	va_list ap;
163 
164 	va_start(ap, fmt);
165 	_warn_helper(_EVENT_LOG_DEBUG, NULL, fmt, ap);
166 	va_end(ap);
167 }
168 
169 static void
_warn_helper(int severity,const char * errstr,const char * fmt,va_list ap)170 _warn_helper(int severity, const char *errstr, const char *fmt, va_list ap)
171 {
172 	char buf[1024];
173 	size_t len;
174 
175 	if (fmt != NULL)
176 		evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
177 	else
178 		buf[0] = '\0';
179 
180 	if (errstr) {
181 		len = strlen(buf);
182 		if (len < sizeof(buf) - 3) {
183 			evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
184 		}
185 	}
186 
187 	event_log(severity, buf);
188 }
189 
190 static event_log_cb log_fn = NULL;
191 
192 void
event_set_log_callback(event_log_cb cb)193 event_set_log_callback(event_log_cb cb)
194 {
195 	log_fn = cb;
196 }
197 
198 static void
event_log(int severity,const char * msg)199 event_log(int severity, const char *msg)
200 {
201 	if (log_fn)
202 		log_fn(severity, msg);
203 	else {
204 		const char *severity_str;
205 		switch (severity) {
206 		case _EVENT_LOG_DEBUG:
207 			severity_str = "debug";
208 			break;
209 		case _EVENT_LOG_MSG:
210 			severity_str = "msg";
211 			break;
212 		case _EVENT_LOG_WARN:
213 			severity_str = "warn";
214 			break;
215 		case _EVENT_LOG_ERR:
216 			severity_str = "err";
217 			break;
218 		default:
219 			severity_str = "???";
220 			break;
221 		}
222 		(void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
223 	}
224 }
225