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