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