blob: e8ae9fdc310e0fb13b75ff778ecae21014cf6578 [file] [log] [blame]
Christopher Wileye8679812015-07-01 13:36:18 -07001/* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
2
3/*
4 * log.c
5 *
6 * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
7 *
8 * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
9 *
10 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
11 *
12 * Copyright (c) 1993
13 * The Regents of the University of California. All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 */
39
40#include "event2/event-config.h"
Narayan Kamathfc74cb42017-09-13 12:53:52 +010041#include "evconfig-private.h"
Christopher Wileye8679812015-07-01 13:36:18 -070042
Narayan Kamathfc74cb42017-09-13 12:53:52 +010043#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -070044#include <winsock2.h>
45#define WIN32_LEAN_AND_MEAN
46#include <windows.h>
47#undef WIN32_LEAN_AND_MEAN
48#endif
49#include <sys/types.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <stdarg.h>
53#include <string.h>
54#include <errno.h>
55#include "event2/event.h"
56#include "event2/util.h"
57
58#include "log-internal.h"
59
Christopher Wileye8679812015-07-01 13:36:18 -070060static void event_log(int severity, const char *msg);
61static void event_exit(int errcode) EV_NORETURN;
62
63static event_fatal_cb fatal_fn = NULL;
64
Narayan Kamathfc74cb42017-09-13 12:53:52 +010065#ifdef EVENT_DEBUG_LOGGING_ENABLED
66#ifdef USE_DEBUG
67#define DEFAULT_MASK EVENT_DBG_ALL
68#else
69#define DEFAULT_MASK 0
70#endif
71
72#ifdef USE_GLOBAL_FOR_DEBUG_LOGGING
73ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
74#else
75static ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
76ev_uint32_t
77event_debug_get_logging_mask_(void)
78{
79 return event_debug_logging_mask_;
80}
81#endif
82#endif /* EVENT_DEBUG_LOGGING_ENABLED */
83
84void
85event_enable_debug_logging(ev_uint32_t which)
86{
87#ifdef EVENT_DEBUG_LOGGING_ENABLED
88 event_debug_logging_mask_ = which;
89#endif
90}
91
Christopher Wileye8679812015-07-01 13:36:18 -070092void
93event_set_fatal_callback(event_fatal_cb cb)
94{
95 fatal_fn = cb;
96}
97
98static void
99event_exit(int errcode)
100{
101 if (fatal_fn) {
102 fatal_fn(errcode);
103 exit(errcode); /* should never be reached */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100104 } else if (errcode == EVENT_ERR_ABORT_)
Christopher Wileye8679812015-07-01 13:36:18 -0700105 abort();
106 else
107 exit(errcode);
108}
109
110void
111event_err(int eval, const char *fmt, ...)
112{
113 va_list ap;
114
115 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100116 event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700117 va_end(ap);
118 event_exit(eval);
119}
120
121void
122event_warn(const char *fmt, ...)
123{
124 va_list ap;
125
126 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100127 event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700128 va_end(ap);
129}
130
131void
132event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
133{
134 va_list ap;
135 int err = evutil_socket_geterror(sock);
136
137 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100138 event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700139 va_end(ap);
140 event_exit(eval);
141}
142
143void
144event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
145{
146 va_list ap;
147 int err = evutil_socket_geterror(sock);
148
149 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100150 event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700151 va_end(ap);
152}
153
154void
155event_errx(int eval, const char *fmt, ...)
156{
157 va_list ap;
158
159 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100160 event_logv_(EVENT_LOG_ERR, NULL, fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700161 va_end(ap);
162 event_exit(eval);
163}
164
165void
166event_warnx(const char *fmt, ...)
167{
168 va_list ap;
169
170 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100171 event_logv_(EVENT_LOG_WARN, NULL, fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700172 va_end(ap);
173}
174
175void
176event_msgx(const char *fmt, ...)
177{
178 va_list ap;
179
180 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100181 event_logv_(EVENT_LOG_MSG, NULL, fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700182 va_end(ap);
183}
184
185void
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100186event_debugx_(const char *fmt, ...)
Christopher Wileye8679812015-07-01 13:36:18 -0700187{
188 va_list ap;
189
190 va_start(ap, fmt);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100191 event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap);
Christopher Wileye8679812015-07-01 13:36:18 -0700192 va_end(ap);
193}
194
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100195void
196event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
Christopher Wileye8679812015-07-01 13:36:18 -0700197{
198 char buf[1024];
199 size_t len;
200
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100201 if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_())
202 return;
203
Christopher Wileye8679812015-07-01 13:36:18 -0700204 if (fmt != NULL)
205 evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
206 else
207 buf[0] = '\0';
208
209 if (errstr) {
210 len = strlen(buf);
211 if (len < sizeof(buf) - 3) {
212 evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
213 }
214 }
215
216 event_log(severity, buf);
217}
218
219static event_log_cb log_fn = NULL;
220
221void
222event_set_log_callback(event_log_cb cb)
223{
224 log_fn = cb;
225}
226
227static void
228event_log(int severity, const char *msg)
229{
230 if (log_fn)
231 log_fn(severity, msg);
232 else {
233 const char *severity_str;
234 switch (severity) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100235 case EVENT_LOG_DEBUG:
Christopher Wileye8679812015-07-01 13:36:18 -0700236 severity_str = "debug";
237 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100238 case EVENT_LOG_MSG:
Christopher Wileye8679812015-07-01 13:36:18 -0700239 severity_str = "msg";
240 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100241 case EVENT_LOG_WARN:
Christopher Wileye8679812015-07-01 13:36:18 -0700242 severity_str = "warn";
243 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100244 case EVENT_LOG_ERR:
Christopher Wileye8679812015-07-01 13:36:18 -0700245 severity_str = "err";
246 break;
247 default:
248 severity_str = "???";
249 break;
250 }
251 (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
252 }
253}