xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/log.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: log.c,v 1.6 2024/08/18 20:47:21 christos Exp $	*/
28585484eSchristos 
38585484eSchristos /*	$OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $	*/
48585484eSchristos 
58585484eSchristos /*
68585484eSchristos  * log.c
78585484eSchristos  *
88585484eSchristos  * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
98585484eSchristos  *
108585484eSchristos  * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
118585484eSchristos  *
128585484eSchristos  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
138585484eSchristos  *
148585484eSchristos  * Copyright (c) 1993
158585484eSchristos  *	The Regents of the University of California.  All rights reserved.
168585484eSchristos  *
178585484eSchristos  * Redistribution and use in source and binary forms, with or without
188585484eSchristos  * modification, are permitted provided that the following conditions
198585484eSchristos  * are met:
208585484eSchristos  * 1. Redistributions of source code must retain the above copyright
218585484eSchristos  *    notice, this list of conditions and the following disclaimer.
228585484eSchristos  * 2. Redistributions in binary form must reproduce the above copyright
238585484eSchristos  *    notice, this list of conditions and the following disclaimer in the
248585484eSchristos  *    documentation and/or other materials provided with the distribution.
258585484eSchristos  * 3. Neither the name of the University nor the names of its contributors
268585484eSchristos  *    may be used to endorse or promote products derived from this software
278585484eSchristos  *    without specific prior written permission.
288585484eSchristos  *
298585484eSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
308585484eSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
318585484eSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
328585484eSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
338585484eSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
348585484eSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
358585484eSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
368585484eSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
378585484eSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
388585484eSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
398585484eSchristos  * SUCH DAMAGE.
408585484eSchristos  */
418585484eSchristos 
428585484eSchristos #include "event2/event-config.h"
438585484eSchristos #include "evconfig-private.h"
448585484eSchristos 
458585484eSchristos #ifdef _WIN32
468585484eSchristos #include <winsock2.h>
478585484eSchristos #define WIN32_LEAN_AND_MEAN
488585484eSchristos #include <windows.h>
498585484eSchristos #undef WIN32_LEAN_AND_MEAN
508585484eSchristos #endif
518585484eSchristos #include <sys/types.h>
528585484eSchristos #include <stdio.h>
538585484eSchristos #include <stdlib.h>
548585484eSchristos #include <stdarg.h>
558585484eSchristos #include <string.h>
568585484eSchristos #include <errno.h>
578585484eSchristos #include "event2/event.h"
588585484eSchristos #include "event2/util.h"
598585484eSchristos 
608585484eSchristos #include "log-internal.h"
618585484eSchristos 
628585484eSchristos static void event_log(int severity, const char *msg);
638585484eSchristos static void event_exit(int errcode) EV_NORETURN;
648585484eSchristos 
658585484eSchristos static event_fatal_cb fatal_fn = NULL;
668585484eSchristos 
678585484eSchristos #ifdef EVENT_DEBUG_LOGGING_ENABLED
688585484eSchristos #ifdef USE_DEBUG
698585484eSchristos #define DEFAULT_MASK EVENT_DBG_ALL
708585484eSchristos #else
718585484eSchristos #define DEFAULT_MASK 0
728585484eSchristos #endif
738585484eSchristos 
74*eabc0478Schristos EVENT2_EXPORT_SYMBOL ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
758585484eSchristos #endif /* EVENT_DEBUG_LOGGING_ENABLED */
768585484eSchristos 
778585484eSchristos void
788585484eSchristos event_enable_debug_logging(ev_uint32_t which)
798585484eSchristos {
808585484eSchristos #ifdef EVENT_DEBUG_LOGGING_ENABLED
818585484eSchristos 	event_debug_logging_mask_ = which;
828585484eSchristos #endif
838585484eSchristos }
848585484eSchristos 
858585484eSchristos void
868585484eSchristos event_set_fatal_callback(event_fatal_cb cb)
878585484eSchristos {
888585484eSchristos 	fatal_fn = cb;
898585484eSchristos }
908585484eSchristos 
918585484eSchristos static void
928585484eSchristos event_exit(int errcode)
938585484eSchristos {
948585484eSchristos 	if (fatal_fn) {
958585484eSchristos 		fatal_fn(errcode);
968585484eSchristos 		exit(errcode); /* should never be reached */
978585484eSchristos 	} else if (errcode == EVENT_ERR_ABORT_)
988585484eSchristos 		abort();
998585484eSchristos 	else
1008585484eSchristos 		exit(errcode);
1018585484eSchristos }
1028585484eSchristos 
1038585484eSchristos void
1048585484eSchristos event_err(int eval, const char *fmt, ...)
1058585484eSchristos {
1068585484eSchristos 	va_list ap;
1078585484eSchristos 
1088585484eSchristos 	va_start(ap, fmt);
109b8ecfcfeSchristos 	event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap);
1108585484eSchristos 	va_end(ap);
1118585484eSchristos 	event_exit(eval);
1128585484eSchristos }
1138585484eSchristos 
1148585484eSchristos void
1158585484eSchristos event_warn(const char *fmt, ...)
1168585484eSchristos {
1178585484eSchristos 	va_list ap;
1188585484eSchristos 
1198585484eSchristos 	va_start(ap, fmt);
120b8ecfcfeSchristos 	event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap);
1218585484eSchristos 	va_end(ap);
1228585484eSchristos }
1238585484eSchristos 
1248585484eSchristos void
1258585484eSchristos event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
1268585484eSchristos {
1278585484eSchristos 	va_list ap;
1288585484eSchristos 	int err = evutil_socket_geterror(sock);
1298585484eSchristos 
1308585484eSchristos 	va_start(ap, fmt);
131b8ecfcfeSchristos 	event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
1328585484eSchristos 	va_end(ap);
1338585484eSchristos 	event_exit(eval);
1348585484eSchristos }
1358585484eSchristos 
1368585484eSchristos void
1378585484eSchristos event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
1388585484eSchristos {
1398585484eSchristos 	va_list ap;
1408585484eSchristos 	int err = evutil_socket_geterror(sock);
1418585484eSchristos 
1428585484eSchristos 	va_start(ap, fmt);
143b8ecfcfeSchristos 	event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
1448585484eSchristos 	va_end(ap);
1458585484eSchristos }
1468585484eSchristos 
1478585484eSchristos void
1488585484eSchristos event_errx(int eval, const char *fmt, ...)
1498585484eSchristos {
1508585484eSchristos 	va_list ap;
1518585484eSchristos 
1528585484eSchristos 	va_start(ap, fmt);
153b8ecfcfeSchristos 	event_logv_(EVENT_LOG_ERR, NULL, fmt, ap);
1548585484eSchristos 	va_end(ap);
1558585484eSchristos 	event_exit(eval);
1568585484eSchristos }
1578585484eSchristos 
1588585484eSchristos void
1598585484eSchristos event_warnx(const char *fmt, ...)
1608585484eSchristos {
1618585484eSchristos 	va_list ap;
1628585484eSchristos 
1638585484eSchristos 	va_start(ap, fmt);
164b8ecfcfeSchristos 	event_logv_(EVENT_LOG_WARN, NULL, fmt, ap);
1658585484eSchristos 	va_end(ap);
1668585484eSchristos }
1678585484eSchristos 
1688585484eSchristos void
1698585484eSchristos event_msgx(const char *fmt, ...)
1708585484eSchristos {
1718585484eSchristos 	va_list ap;
1728585484eSchristos 
1738585484eSchristos 	va_start(ap, fmt);
174b8ecfcfeSchristos 	event_logv_(EVENT_LOG_MSG, NULL, fmt, ap);
1758585484eSchristos 	va_end(ap);
1768585484eSchristos }
1778585484eSchristos 
1788585484eSchristos void
1798585484eSchristos event_debugx_(const char *fmt, ...)
1808585484eSchristos {
1818585484eSchristos 	va_list ap;
1828585484eSchristos 
1838585484eSchristos 	va_start(ap, fmt);
184b8ecfcfeSchristos 	event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap);
1858585484eSchristos 	va_end(ap);
1868585484eSchristos }
1878585484eSchristos 
188b8ecfcfeSchristos void
189b8ecfcfeSchristos event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
1908585484eSchristos {
1918585484eSchristos 	char buf[1024];
1928585484eSchristos 	size_t len;
1938585484eSchristos 
194b8ecfcfeSchristos 	if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_())
195b8ecfcfeSchristos 		return;
196b8ecfcfeSchristos 
1978585484eSchristos 	if (fmt != NULL)
1988585484eSchristos 		evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
1998585484eSchristos 	else
2008585484eSchristos 		buf[0] = '\0';
2018585484eSchristos 
2028585484eSchristos 	if (errstr) {
2038585484eSchristos 		len = strlen(buf);
2048585484eSchristos 		if (len < sizeof(buf) - 3) {
2058585484eSchristos 			evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
2068585484eSchristos 		}
2078585484eSchristos 	}
2088585484eSchristos 
2098585484eSchristos 	event_log(severity, buf);
2108585484eSchristos }
2118585484eSchristos 
2128585484eSchristos static event_log_cb log_fn = NULL;
2138585484eSchristos 
2148585484eSchristos void
2158585484eSchristos event_set_log_callback(event_log_cb cb)
2168585484eSchristos {
2178585484eSchristos 	log_fn = cb;
2188585484eSchristos }
2198585484eSchristos 
2208585484eSchristos static void
2218585484eSchristos event_log(int severity, const char *msg)
2228585484eSchristos {
2238585484eSchristos 	if (log_fn)
2248585484eSchristos 		log_fn(severity, msg);
2258585484eSchristos 	else {
2268585484eSchristos 		const char *severity_str;
2278585484eSchristos 		switch (severity) {
2288585484eSchristos 		case EVENT_LOG_DEBUG:
2298585484eSchristos 			severity_str = "debug";
2308585484eSchristos 			break;
2318585484eSchristos 		case EVENT_LOG_MSG:
2328585484eSchristos 			severity_str = "msg";
2338585484eSchristos 			break;
2348585484eSchristos 		case EVENT_LOG_WARN:
2358585484eSchristos 			severity_str = "warn";
2368585484eSchristos 			break;
2378585484eSchristos 		case EVENT_LOG_ERR:
2388585484eSchristos 			severity_str = "err";
2398585484eSchristos 			break;
2408585484eSchristos 		default:
2418585484eSchristos 			severity_str = "???";
2428585484eSchristos 			break;
2438585484eSchristos 		}
2448585484eSchristos 		(void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
2458585484eSchristos 	}
2468585484eSchristos }
247