xref: /freebsd/contrib/ntp/sntp/libevent/log.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*	$OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $	*/
22b15cb3dSCy Schubert 
32b15cb3dSCy Schubert /*
42b15cb3dSCy Schubert  * log.c
52b15cb3dSCy Schubert  *
62b15cb3dSCy Schubert  * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
72b15cb3dSCy Schubert  *
82b15cb3dSCy Schubert  * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
92b15cb3dSCy Schubert  *
102b15cb3dSCy Schubert  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
112b15cb3dSCy Schubert  *
122b15cb3dSCy Schubert  * Copyright (c) 1993
132b15cb3dSCy Schubert  *	The Regents of the University of California.  All rights reserved.
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
162b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
172b15cb3dSCy Schubert  * are met:
182b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
192b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
202b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
212b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
222b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
232b15cb3dSCy Schubert  * 3. Neither the name of the University nor the names of its contributors
242b15cb3dSCy Schubert  *    may be used to endorse or promote products derived from this software
252b15cb3dSCy Schubert  *    without specific prior written permission.
262b15cb3dSCy Schubert  *
272b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
282b15cb3dSCy Schubert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
292b15cb3dSCy Schubert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
302b15cb3dSCy Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
312b15cb3dSCy Schubert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
322b15cb3dSCy Schubert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
332b15cb3dSCy Schubert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
342b15cb3dSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
352b15cb3dSCy Schubert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
362b15cb3dSCy Schubert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
372b15cb3dSCy Schubert  * SUCH DAMAGE.
382b15cb3dSCy Schubert  */
392b15cb3dSCy Schubert 
402b15cb3dSCy Schubert #include "event2/event-config.h"
412b15cb3dSCy Schubert #include "evconfig-private.h"
422b15cb3dSCy Schubert 
432b15cb3dSCy Schubert #ifdef _WIN32
442b15cb3dSCy Schubert #include <winsock2.h>
452b15cb3dSCy Schubert #define WIN32_LEAN_AND_MEAN
462b15cb3dSCy Schubert #include <windows.h>
472b15cb3dSCy Schubert #undef WIN32_LEAN_AND_MEAN
482b15cb3dSCy Schubert #endif
492b15cb3dSCy Schubert #include <sys/types.h>
502b15cb3dSCy Schubert #include <stdio.h>
512b15cb3dSCy Schubert #include <stdlib.h>
522b15cb3dSCy Schubert #include <stdarg.h>
532b15cb3dSCy Schubert #include <string.h>
542b15cb3dSCy Schubert #include <errno.h>
552b15cb3dSCy Schubert #include "event2/event.h"
562b15cb3dSCy Schubert #include "event2/util.h"
572b15cb3dSCy Schubert 
582b15cb3dSCy Schubert #include "log-internal.h"
592b15cb3dSCy Schubert 
602b15cb3dSCy Schubert static void event_log(int severity, const char *msg);
612b15cb3dSCy Schubert static void event_exit(int errcode) EV_NORETURN;
622b15cb3dSCy Schubert 
632b15cb3dSCy Schubert static event_fatal_cb fatal_fn = NULL;
642b15cb3dSCy Schubert 
652b15cb3dSCy Schubert #ifdef EVENT_DEBUG_LOGGING_ENABLED
662b15cb3dSCy Schubert #ifdef USE_DEBUG
672b15cb3dSCy Schubert #define DEFAULT_MASK EVENT_DBG_ALL
682b15cb3dSCy Schubert #else
692b15cb3dSCy Schubert #define DEFAULT_MASK 0
702b15cb3dSCy Schubert #endif
712b15cb3dSCy Schubert 
72*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
732b15cb3dSCy Schubert #endif /* EVENT_DEBUG_LOGGING_ENABLED */
742b15cb3dSCy Schubert 
752b15cb3dSCy Schubert void
event_enable_debug_logging(ev_uint32_t which)762b15cb3dSCy Schubert event_enable_debug_logging(ev_uint32_t which)
772b15cb3dSCy Schubert {
782b15cb3dSCy Schubert #ifdef EVENT_DEBUG_LOGGING_ENABLED
792b15cb3dSCy Schubert 	event_debug_logging_mask_ = which;
802b15cb3dSCy Schubert #endif
812b15cb3dSCy Schubert }
822b15cb3dSCy Schubert 
832b15cb3dSCy Schubert void
event_set_fatal_callback(event_fatal_cb cb)842b15cb3dSCy Schubert event_set_fatal_callback(event_fatal_cb cb)
852b15cb3dSCy Schubert {
862b15cb3dSCy Schubert 	fatal_fn = cb;
872b15cb3dSCy Schubert }
882b15cb3dSCy Schubert 
892b15cb3dSCy Schubert static void
event_exit(int errcode)902b15cb3dSCy Schubert event_exit(int errcode)
912b15cb3dSCy Schubert {
922b15cb3dSCy Schubert 	if (fatal_fn) {
932b15cb3dSCy Schubert 		fatal_fn(errcode);
942b15cb3dSCy Schubert 		exit(errcode); /* should never be reached */
952b15cb3dSCy Schubert 	} else if (errcode == EVENT_ERR_ABORT_)
962b15cb3dSCy Schubert 		abort();
972b15cb3dSCy Schubert 	else
982b15cb3dSCy Schubert 		exit(errcode);
992b15cb3dSCy Schubert }
1002b15cb3dSCy Schubert 
1012b15cb3dSCy Schubert void
event_err(int eval,const char * fmt,...)1022b15cb3dSCy Schubert event_err(int eval, const char *fmt, ...)
1032b15cb3dSCy Schubert {
1042b15cb3dSCy Schubert 	va_list ap;
1052b15cb3dSCy Schubert 
1062b15cb3dSCy Schubert 	va_start(ap, fmt);
1072b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap);
1082b15cb3dSCy Schubert 	va_end(ap);
1092b15cb3dSCy Schubert 	event_exit(eval);
1102b15cb3dSCy Schubert }
1112b15cb3dSCy Schubert 
1122b15cb3dSCy Schubert void
event_warn(const char * fmt,...)1132b15cb3dSCy Schubert event_warn(const char *fmt, ...)
1142b15cb3dSCy Schubert {
1152b15cb3dSCy Schubert 	va_list ap;
1162b15cb3dSCy Schubert 
1172b15cb3dSCy Schubert 	va_start(ap, fmt);
1182b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap);
1192b15cb3dSCy Schubert 	va_end(ap);
1202b15cb3dSCy Schubert }
1212b15cb3dSCy Schubert 
1222b15cb3dSCy Schubert void
event_sock_err(int eval,evutil_socket_t sock,const char * fmt,...)1232b15cb3dSCy Schubert event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
1242b15cb3dSCy Schubert {
1252b15cb3dSCy Schubert 	va_list ap;
1262b15cb3dSCy Schubert 	int err = evutil_socket_geterror(sock);
1272b15cb3dSCy Schubert 
1282b15cb3dSCy Schubert 	va_start(ap, fmt);
1292b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
1302b15cb3dSCy Schubert 	va_end(ap);
1312b15cb3dSCy Schubert 	event_exit(eval);
1322b15cb3dSCy Schubert }
1332b15cb3dSCy Schubert 
1342b15cb3dSCy Schubert void
event_sock_warn(evutil_socket_t sock,const char * fmt,...)1352b15cb3dSCy Schubert event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
1362b15cb3dSCy Schubert {
1372b15cb3dSCy Schubert 	va_list ap;
1382b15cb3dSCy Schubert 	int err = evutil_socket_geterror(sock);
1392b15cb3dSCy Schubert 
1402b15cb3dSCy Schubert 	va_start(ap, fmt);
1412b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
1422b15cb3dSCy Schubert 	va_end(ap);
1432b15cb3dSCy Schubert }
1442b15cb3dSCy Schubert 
1452b15cb3dSCy Schubert void
event_errx(int eval,const char * fmt,...)1462b15cb3dSCy Schubert event_errx(int eval, const char *fmt, ...)
1472b15cb3dSCy Schubert {
1482b15cb3dSCy Schubert 	va_list ap;
1492b15cb3dSCy Schubert 
1502b15cb3dSCy Schubert 	va_start(ap, fmt);
1512b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_ERR, NULL, fmt, ap);
1522b15cb3dSCy Schubert 	va_end(ap);
1532b15cb3dSCy Schubert 	event_exit(eval);
1542b15cb3dSCy Schubert }
1552b15cb3dSCy Schubert 
1562b15cb3dSCy Schubert void
event_warnx(const char * fmt,...)1572b15cb3dSCy Schubert event_warnx(const char *fmt, ...)
1582b15cb3dSCy Schubert {
1592b15cb3dSCy Schubert 	va_list ap;
1602b15cb3dSCy Schubert 
1612b15cb3dSCy Schubert 	va_start(ap, fmt);
1622b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_WARN, NULL, fmt, ap);
1632b15cb3dSCy Schubert 	va_end(ap);
1642b15cb3dSCy Schubert }
1652b15cb3dSCy Schubert 
1662b15cb3dSCy Schubert void
event_msgx(const char * fmt,...)1672b15cb3dSCy Schubert event_msgx(const char *fmt, ...)
1682b15cb3dSCy Schubert {
1692b15cb3dSCy Schubert 	va_list ap;
1702b15cb3dSCy Schubert 
1712b15cb3dSCy Schubert 	va_start(ap, fmt);
1722b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_MSG, NULL, fmt, ap);
1732b15cb3dSCy Schubert 	va_end(ap);
1742b15cb3dSCy Schubert }
1752b15cb3dSCy Schubert 
1762b15cb3dSCy Schubert void
event_debugx_(const char * fmt,...)1772b15cb3dSCy Schubert event_debugx_(const char *fmt, ...)
1782b15cb3dSCy Schubert {
1792b15cb3dSCy Schubert 	va_list ap;
1802b15cb3dSCy Schubert 
1812b15cb3dSCy Schubert 	va_start(ap, fmt);
1822b15cb3dSCy Schubert 	event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap);
1832b15cb3dSCy Schubert 	va_end(ap);
1842b15cb3dSCy Schubert }
1852b15cb3dSCy Schubert 
1862b15cb3dSCy Schubert void
event_logv_(int severity,const char * errstr,const char * fmt,va_list ap)1872b15cb3dSCy Schubert event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
1882b15cb3dSCy Schubert {
1892b15cb3dSCy Schubert 	char buf[1024];
1902b15cb3dSCy Schubert 	size_t len;
1912b15cb3dSCy Schubert 
1922b15cb3dSCy Schubert 	if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_())
1932b15cb3dSCy Schubert 		return;
1942b15cb3dSCy Schubert 
1952b15cb3dSCy Schubert 	if (fmt != NULL)
1962b15cb3dSCy Schubert 		evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
1972b15cb3dSCy Schubert 	else
1982b15cb3dSCy Schubert 		buf[0] = '\0';
1992b15cb3dSCy Schubert 
2002b15cb3dSCy Schubert 	if (errstr) {
2012b15cb3dSCy Schubert 		len = strlen(buf);
2022b15cb3dSCy Schubert 		if (len < sizeof(buf) - 3) {
2032b15cb3dSCy Schubert 			evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
2042b15cb3dSCy Schubert 		}
2052b15cb3dSCy Schubert 	}
2062b15cb3dSCy Schubert 
2072b15cb3dSCy Schubert 	event_log(severity, buf);
2082b15cb3dSCy Schubert }
2092b15cb3dSCy Schubert 
2102b15cb3dSCy Schubert static event_log_cb log_fn = NULL;
2112b15cb3dSCy Schubert 
2122b15cb3dSCy Schubert void
event_set_log_callback(event_log_cb cb)2132b15cb3dSCy Schubert event_set_log_callback(event_log_cb cb)
2142b15cb3dSCy Schubert {
2152b15cb3dSCy Schubert 	log_fn = cb;
2162b15cb3dSCy Schubert }
2172b15cb3dSCy Schubert 
2182b15cb3dSCy Schubert static void
event_log(int severity,const char * msg)2192b15cb3dSCy Schubert event_log(int severity, const char *msg)
2202b15cb3dSCy Schubert {
2212b15cb3dSCy Schubert 	if (log_fn)
2222b15cb3dSCy Schubert 		log_fn(severity, msg);
2232b15cb3dSCy Schubert 	else {
2242b15cb3dSCy Schubert 		const char *severity_str;
2252b15cb3dSCy Schubert 		switch (severity) {
2262b15cb3dSCy Schubert 		case EVENT_LOG_DEBUG:
2272b15cb3dSCy Schubert 			severity_str = "debug";
2282b15cb3dSCy Schubert 			break;
2292b15cb3dSCy Schubert 		case EVENT_LOG_MSG:
2302b15cb3dSCy Schubert 			severity_str = "msg";
2312b15cb3dSCy Schubert 			break;
2322b15cb3dSCy Schubert 		case EVENT_LOG_WARN:
2332b15cb3dSCy Schubert 			severity_str = "warn";
2342b15cb3dSCy Schubert 			break;
2352b15cb3dSCy Schubert 		case EVENT_LOG_ERR:
2362b15cb3dSCy Schubert 			severity_str = "err";
2372b15cb3dSCy Schubert 			break;
2382b15cb3dSCy Schubert 		default:
2392b15cb3dSCy Schubert 			severity_str = "???";
2402b15cb3dSCy Schubert 			break;
2412b15cb3dSCy Schubert 		}
2422b15cb3dSCy Schubert 		(void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
2432b15cb3dSCy Schubert 	}
2442b15cb3dSCy Schubert }
245