Line | Count | Source (jump to first uncovered line) |
1 | | /* $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" |
41 | | #include "evconfig-private.h" |
42 | | |
43 | | #ifdef _WIN32 |
44 | | #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 | | |
60 | | static void event_log(int severity, const char *msg); |
61 | | static void event_exit(int errcode) EV_NORETURN; |
62 | | |
63 | | static event_fatal_cb fatal_fn = NULL; |
64 | | |
65 | | #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 | | EVENT2_EXPORT_SYMBOL ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK; |
73 | | #endif /* EVENT_DEBUG_LOGGING_ENABLED */ |
74 | | |
75 | | void |
76 | | event_enable_debug_logging(ev_uint32_t which) |
77 | 0 | { |
78 | 0 | #ifdef EVENT_DEBUG_LOGGING_ENABLED |
79 | 0 | event_debug_logging_mask_ = which; |
80 | 0 | #endif |
81 | 0 | } |
82 | | |
83 | | void |
84 | | event_set_fatal_callback(event_fatal_cb cb) |
85 | 0 | { |
86 | 0 | fatal_fn = cb; |
87 | 0 | } |
88 | | |
89 | | static void |
90 | | event_exit(int errcode) |
91 | 0 | { |
92 | 0 | if (fatal_fn) { |
93 | 0 | fatal_fn(errcode); |
94 | 0 | exit(errcode); /* should never be reached */ |
95 | 0 | } else if (errcode == EVENT_ERR_ABORT_) |
96 | 0 | abort(); |
97 | 0 | else |
98 | 0 | exit(errcode); |
99 | 0 | } |
100 | | |
101 | | void |
102 | | event_err(int eval, const char *fmt, ...) |
103 | 0 | { |
104 | 0 | va_list ap; |
105 | 0 |
|
106 | 0 | va_start(ap, fmt); |
107 | 0 | event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap); |
108 | 0 | va_end(ap); |
109 | 0 | event_exit(eval); |
110 | 0 | } |
111 | | |
112 | | void |
113 | | event_warn(const char *fmt, ...) |
114 | 0 | { |
115 | 0 | va_list ap; |
116 | 0 |
|
117 | 0 | va_start(ap, fmt); |
118 | 0 | event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap); |
119 | 0 | va_end(ap); |
120 | 0 | } |
121 | | |
122 | | void |
123 | | event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...) |
124 | 0 | { |
125 | 0 | va_list ap; |
126 | 0 | int err = evutil_socket_geterror(sock); |
127 | 0 |
|
128 | 0 | va_start(ap, fmt); |
129 | 0 | event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap); |
130 | 0 | va_end(ap); |
131 | 0 | event_exit(eval); |
132 | 0 | } |
133 | | |
134 | | void |
135 | | event_sock_warn(evutil_socket_t sock, const char *fmt, ...) |
136 | 0 | { |
137 | 0 | va_list ap; |
138 | 0 | int err = evutil_socket_geterror(sock); |
139 | 0 |
|
140 | 0 | va_start(ap, fmt); |
141 | 0 | event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap); |
142 | 0 | va_end(ap); |
143 | 0 | } |
144 | | |
145 | | void |
146 | | event_errx(int eval, const char *fmt, ...) |
147 | 0 | { |
148 | 0 | va_list ap; |
149 | 0 |
|
150 | 0 | va_start(ap, fmt); |
151 | 0 | event_logv_(EVENT_LOG_ERR, NULL, fmt, ap); |
152 | 0 | va_end(ap); |
153 | 0 | event_exit(eval); |
154 | 0 | } |
155 | | |
156 | | void |
157 | | event_warnx(const char *fmt, ...) |
158 | 0 | { |
159 | 0 | va_list ap; |
160 | 0 |
|
161 | 0 | va_start(ap, fmt); |
162 | 0 | event_logv_(EVENT_LOG_WARN, NULL, fmt, ap); |
163 | 0 | va_end(ap); |
164 | 0 | } |
165 | | |
166 | | void |
167 | | event_msgx(const char *fmt, ...) |
168 | 0 | { |
169 | 0 | va_list ap; |
170 | 0 |
|
171 | 0 | va_start(ap, fmt); |
172 | 0 | event_logv_(EVENT_LOG_MSG, NULL, fmt, ap); |
173 | 0 | va_end(ap); |
174 | 0 | } |
175 | | |
176 | | void |
177 | | event_debugx_(const char *fmt, ...) |
178 | 0 | { |
179 | 0 | va_list ap; |
180 | 0 |
|
181 | 0 | va_start(ap, fmt); |
182 | 0 | event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap); |
183 | 0 | va_end(ap); |
184 | 0 | } |
185 | | |
186 | | void |
187 | | event_logv_(int severity, const char *errstr, const char *fmt, va_list ap) |
188 | 0 | { |
189 | 0 | char buf[1024]; |
190 | 0 | size_t len; |
191 | 0 |
|
192 | 0 | if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_()) |
193 | 0 | return; |
194 | 0 | |
195 | 0 | if (fmt != NULL) |
196 | 0 | evutil_vsnprintf(buf, sizeof(buf), fmt, ap); |
197 | 0 | else |
198 | 0 | buf[0] = '\0'; |
199 | 0 |
|
200 | 0 | if (errstr) { |
201 | 0 | len = strlen(buf); |
202 | 0 | if (len < sizeof(buf) - 3) { |
203 | 0 | evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr); |
204 | 0 | } |
205 | 0 | } |
206 | 0 |
|
207 | 0 | event_log(severity, buf); |
208 | 0 | } |
209 | | |
210 | | static event_log_cb log_fn = NULL; |
211 | | |
212 | | void |
213 | | event_set_log_callback(event_log_cb cb) |
214 | 0 | { |
215 | 0 | log_fn = cb; |
216 | 0 | } |
217 | | |
218 | | static void |
219 | | event_log(int severity, const char *msg) |
220 | 0 | { |
221 | 0 | if (log_fn) |
222 | 0 | log_fn(severity, msg); |
223 | 0 | else { |
224 | 0 | const char *severity_str; |
225 | 0 | switch (severity) { |
226 | 0 | case EVENT_LOG_DEBUG: |
227 | 0 | severity_str = "debug"; |
228 | 0 | break; |
229 | 0 | case EVENT_LOG_MSG: |
230 | 0 | severity_str = "msg"; |
231 | 0 | break; |
232 | 0 | case EVENT_LOG_WARN: |
233 | 0 | severity_str = "warn"; |
234 | 0 | break; |
235 | 0 | case EVENT_LOG_ERR: |
236 | 0 | severity_str = "err"; |
237 | 0 | break; |
238 | 0 | default: |
239 | 0 | severity_str = "???"; |
240 | 0 | break; |
241 | 0 | } |
242 | 0 | (void)fprintf(stderr, "[%s] %s\n", severity_str, msg); |
243 | 0 | } |
244 | 0 | } |