michael@0: /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ michael@0: michael@0: /* michael@0: * log.c michael@0: * michael@0: * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code. michael@0: * michael@0: * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Copyright (c) 2000 Dug Song michael@0: * michael@0: * Copyright (c) 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: #undef WIN32_LEAN_AND_MEAN michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "event2/event.h" michael@0: #include "event2/util.h" michael@0: michael@0: #include "log-internal.h" michael@0: michael@0: static void _warn_helper(int severity, const char *errstr, const char *fmt, michael@0: va_list ap); michael@0: static void event_log(int severity, const char *msg); michael@0: static void event_exit(int errcode) EV_NORETURN; michael@0: michael@0: static event_fatal_cb fatal_fn = NULL; michael@0: michael@0: void michael@0: event_set_fatal_callback(event_fatal_cb cb) michael@0: { michael@0: fatal_fn = cb; michael@0: } michael@0: michael@0: static void michael@0: event_exit(int errcode) michael@0: { michael@0: if (fatal_fn) { michael@0: fatal_fn(errcode); michael@0: exit(errcode); /* should never be reached */ michael@0: } else if (errcode == _EVENT_ERR_ABORT) michael@0: abort(); michael@0: else michael@0: exit(errcode); michael@0: } michael@0: michael@0: void michael@0: event_err(int eval, const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_ERR, strerror(errno), fmt, ap); michael@0: va_end(ap); michael@0: event_exit(eval); michael@0: } michael@0: michael@0: void michael@0: event_warn(const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_WARN, strerror(errno), fmt, ap); michael@0: va_end(ap); michael@0: } michael@0: michael@0: void michael@0: event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: int err = evutil_socket_geterror(sock); michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap); michael@0: va_end(ap); michael@0: event_exit(eval); michael@0: } michael@0: michael@0: void michael@0: event_sock_warn(evutil_socket_t sock, const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: int err = evutil_socket_geterror(sock); michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap); michael@0: va_end(ap); michael@0: } michael@0: michael@0: void michael@0: event_errx(int eval, const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_ERR, NULL, fmt, ap); michael@0: va_end(ap); michael@0: event_exit(eval); michael@0: } michael@0: michael@0: void michael@0: event_warnx(const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_WARN, NULL, fmt, ap); michael@0: va_end(ap); michael@0: } michael@0: michael@0: void michael@0: event_msgx(const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_MSG, NULL, fmt, ap); michael@0: va_end(ap); michael@0: } michael@0: michael@0: void michael@0: _event_debugx(const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: michael@0: va_start(ap, fmt); michael@0: _warn_helper(_EVENT_LOG_DEBUG, NULL, fmt, ap); michael@0: va_end(ap); michael@0: } michael@0: michael@0: static void michael@0: _warn_helper(int severity, const char *errstr, const char *fmt, va_list ap) michael@0: { michael@0: char buf[1024]; michael@0: size_t len; michael@0: michael@0: if (fmt != NULL) michael@0: evutil_vsnprintf(buf, sizeof(buf), fmt, ap); michael@0: else michael@0: buf[0] = '\0'; michael@0: michael@0: if (errstr) { michael@0: len = strlen(buf); michael@0: if (len < sizeof(buf) - 3) { michael@0: evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr); michael@0: } michael@0: } michael@0: michael@0: event_log(severity, buf); michael@0: } michael@0: michael@0: static event_log_cb log_fn = NULL; michael@0: michael@0: void michael@0: event_set_log_callback(event_log_cb cb) michael@0: { michael@0: log_fn = cb; michael@0: } michael@0: michael@0: static void michael@0: event_log(int severity, const char *msg) michael@0: { michael@0: if (log_fn) michael@0: log_fn(severity, msg); michael@0: else { michael@0: const char *severity_str; michael@0: switch (severity) { michael@0: case _EVENT_LOG_DEBUG: michael@0: severity_str = "debug"; michael@0: break; michael@0: case _EVENT_LOG_MSG: michael@0: severity_str = "msg"; michael@0: break; michael@0: case _EVENT_LOG_WARN: michael@0: severity_str = "warn"; michael@0: break; michael@0: case _EVENT_LOG_ERR: michael@0: severity_str = "err"; michael@0: break; michael@0: default: michael@0: severity_str = "???"; michael@0: break; michael@0: } michael@0: (void)fprintf(stderr, "[%s] %s\n", severity_str, msg); michael@0: } michael@0: }