Remove building with NOCRYPTO option
[minix.git] / external / bsd / libevent / dist / log.c
blob8430ce56786187f1725f7117b7126fba617fa871
1 /* $NetBSD: log.c,v 1.4 2015/01/30 00:07:06 joerg Exp $ */
2 /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 /*
5 * log.c
7 * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
9 * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
11 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
13 * Copyright (c) 1993
14 * The Regents of the University of California. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
41 #include "event2/event-config.h"
42 #include <sys/cdefs.h>
43 __RCSID("$NetBSD: log.c,v 1.4 2015/01/30 00:07:06 joerg Exp $");
45 #ifdef WIN32
46 #include <winsock2.h>
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
49 #undef WIN32_LEAN_AND_MEAN
50 #endif
51 #include <sys/types.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <stdarg.h>
55 #include <string.h>
56 #include <errno.h>
57 #include "event2/event.h"
58 #include "event2/util.h"
60 #include "log-internal.h"
62 static void _warn_helper(int severity, const char *errstr, const char *fmt,
63 va_list ap) __printflike(3, 0);
64 static void event_log(int severity, const char *msg);
65 static void event_exit(int errcode) EV_NORETURN;
67 static event_fatal_cb fatal_fn = NULL;
69 void
70 event_set_fatal_callback(event_fatal_cb cb)
72 fatal_fn = cb;
75 static void
76 event_exit(int errcode)
78 if (fatal_fn) {
79 fatal_fn(errcode);
80 exit(errcode); /* should never be reached */
81 } else if (errcode == _EVENT_ERR_ABORT)
82 abort();
83 else
84 exit(errcode);
87 void
88 event_err(int eval, const char *fmt, ...)
90 va_list ap;
92 va_start(ap, fmt);
93 _warn_helper(_EVENT_LOG_ERR, strerror(errno), fmt, ap);
94 va_end(ap);
95 event_exit(eval);
98 void
99 event_warn(const char *fmt, ...)
101 va_list ap;
103 va_start(ap, fmt);
104 _warn_helper(_EVENT_LOG_WARN, strerror(errno), fmt, ap);
105 va_end(ap);
108 void
109 event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
111 va_list ap;
112 int err = evutil_socket_geterror(sock);
114 va_start(ap, fmt);
115 _warn_helper(_EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
116 va_end(ap);
117 event_exit(eval);
120 void
121 event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
123 va_list ap;
124 int err = evutil_socket_geterror(sock);
126 va_start(ap, fmt);
127 _warn_helper(_EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
128 va_end(ap);
131 void
132 event_errx(int eval, const char *fmt, ...)
134 va_list ap;
136 va_start(ap, fmt);
137 _warn_helper(_EVENT_LOG_ERR, NULL, fmt, ap);
138 va_end(ap);
139 event_exit(eval);
142 void
143 event_warnx(const char *fmt, ...)
145 va_list ap;
147 va_start(ap, fmt);
148 _warn_helper(_EVENT_LOG_WARN, NULL, fmt, ap);
149 va_end(ap);
152 void
153 event_msgx(const char *fmt, ...)
155 va_list ap;
157 va_start(ap, fmt);
158 _warn_helper(_EVENT_LOG_MSG, NULL, fmt, ap);
159 va_end(ap);
162 void
163 _event_debugx(const char *fmt, ...)
165 va_list ap;
167 va_start(ap, fmt);
168 _warn_helper(_EVENT_LOG_DEBUG, NULL, fmt, ap);
169 va_end(ap);
172 static void
173 _warn_helper(int severity, const char *errstr, const char *fmt, va_list ap)
175 char buf[1024];
176 size_t len;
178 if (fmt != NULL)
179 evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
180 else
181 buf[0] = '\0';
183 if (errstr) {
184 len = strlen(buf);
185 if (len < sizeof(buf) - 3) {
186 evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
190 event_log(severity, buf);
193 static event_log_cb log_fn = NULL;
195 void
196 event_set_log_callback(event_log_cb cb)
198 log_fn = cb;
201 static void
202 event_log(int severity, const char *msg)
204 if (log_fn)
205 log_fn(severity, msg);
206 else {
207 const char *severity_str;
208 switch (severity) {
209 case _EVENT_LOG_DEBUG:
210 severity_str = "debug";
211 break;
212 case _EVENT_LOG_MSG:
213 severity_str = "msg";
214 break;
215 case _EVENT_LOG_WARN:
216 severity_str = "warn";
217 break;
218 case _EVENT_LOG_ERR:
219 severity_str = "err";
220 break;
221 default:
222 severity_str = "???";
223 break;
225 (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);