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