1 /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
6 * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
8 * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
10 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
13 * The Regents of the University of California. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include "event2/event-config.h"
41 #include "evconfig-private.h"
45 #define WIN32_LEAN_AND_MEAN
47 #undef WIN32_LEAN_AND_MEAN
49 #include <sys/types.h>
55 #include "event2/event.h"
56 #include "event2/util.h"
58 #include "log-internal.h"
60 static void event_log(int severity, const char *msg);
61 static void event_exit(int errcode) EV_NORETURN;
63 static event_fatal_cb fatal_fn = NULL;
65 #ifdef EVENT_DEBUG_LOGGING_ENABLED
67 #define DEFAULT_MASK EVENT_DBG_ALL
69 #define DEFAULT_MASK 0
72 EVENT2_EXPORT_SYMBOL ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
73 #endif /* EVENT_DEBUG_LOGGING_ENABLED */
76 event_enable_debug_logging(ev_uint32_t which)
78 #ifdef EVENT_DEBUG_LOGGING_ENABLED
79 event_debug_logging_mask_ = which;
84 event_set_fatal_callback(event_fatal_cb cb)
90 event_exit(int errcode)
94 exit(errcode); /* should never be reached */
95 } else if (errcode == EVENT_ERR_ABORT_)
102 event_err(int eval, const char *fmt, ...)
107 event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap);
113 event_warn(const char *fmt, ...)
118 event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap);
123 event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
126 int err = evutil_socket_geterror(sock);
129 event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
135 event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
138 int err = evutil_socket_geterror(sock);
141 event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
146 event_errx(int eval, const char *fmt, ...)
151 event_logv_(EVENT_LOG_ERR, NULL, fmt, ap);
157 event_warnx(const char *fmt, ...)
162 event_logv_(EVENT_LOG_WARN, NULL, fmt, ap);
167 event_msgx(const char *fmt, ...)
172 event_logv_(EVENT_LOG_MSG, NULL, fmt, ap);
177 event_debugx_(const char *fmt, ...)
182 event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap);
187 event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
192 if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_())
196 evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
202 if (len < sizeof(buf) - 3) {
203 evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
207 event_log(severity, buf);
210 static event_log_cb log_fn = NULL;
213 event_set_log_callback(event_log_cb cb)
219 event_log(int severity, const char *msg)
222 log_fn(severity, msg);
224 const char *severity_str;
226 case EVENT_LOG_DEBUG:
227 severity_str = "debug";
230 severity_str = "msg";
233 severity_str = "warn";
236 severity_str = "err";
239 severity_str = "???";
242 (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);