| * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code. |
| 7 | * |
| 8 | * Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net> |
| 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 "config.h" |
| 41 |
| 42 | #ifdef WIN32 |
| 43 | #define WIN32_LEAN_AND_MEAN |
| 44 | #include <windows.h> |
| 45 | #undef WIN32_LEAN_AND_MEAN |
| 46 | #endif |
| 47 | #include <sys/types.h> |
| 48 | #ifdef HAVE_SYS_TIME_H |
| 49 | #include <sys/time.h> |
| 50 | #else |
| 51 | #include <sys/_libevent_time.h> |
| 52 | #endif |
| 53 | #include <stdio.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <stdarg.h> |
| 56 | #include <string.h> |
| 57 | #include <errno.h> |
| 58 | #include "event.h" |
| 59 |
| 60 | #include "log.h" |
| 61 | #include "evutil.h" |
| 62 |
| 63 | static void _warn_helper(int severity, int log_errno, const char *fmt, |
| 64 | va_list ap); |
| 65 | static void event_log(int severity, const char *msg); |
| 66 |
| 67 | void |
| 68 | event_err(int eval, const char *fmt, ...) |
| 69 | { |
| 70 | va_list ap; |
| 71 |
| 72 | va_start(ap, fmt); |
| 73 | _warn_helper(_EVENT_LOG_ERR, errno, fmt, ap); |
| 74 | va_end(ap); |
| 75 | exit(eval); |
| 76 | } |
| 77 |
| 78 | void |
| 79 | event_warn(const char *fmt, ...) |
| 80 | { |
| 81 | va_list ap; |
| 82 |
| 83 | va_start(ap, fmt); |
| 84 | _warn_helper(_EVENT_LOG_WARN, errno, fmt, ap); |
| 85 | va_end(ap); |
| 86 | } |
| 87 |
| 88 | void |
| 89 | event_errx(int eval, const char *fmt, ...) |
| 90 | { |
| 91 | va_list ap; |
| 92 |
| 93 | va_start(ap, fmt); |
| 94 | _warn_helper(_EVENT_LOG_ERR, -1, fmt, ap); |
| 95 | va_end(ap); |
| 96 | exit(eval); |
| 97 | } |
| 98 |
| 99 | void |
| 100 | event_warnx(const char *fmt, ...) |
| 101 | { |
| 102 | va_list ap; |
| 103 |
| 104 | va_start(ap, fmt); |
| 105 | _warn_helper(_EVENT_LOG_WARN, -1, fmt, ap); |
| 106 | va_end(ap); |
| 107 | } |
| 108 |
| 109 | void |
| 110 | event_msgx(const char *fmt, ...) |
| 111 | { |
| 112 | va_list ap; |
| 113 |
| 114 | va_start(ap, fmt); |
| 115 | _warn_helper(_EVENT_LOG_MSG, -1, fmt, ap); |
| 116 | va_end(ap); |
| 117 | } |
| 118 |
| 119 | void |
| 120 | _event_debugx(const char *fmt, ...) |
| 121 | { |
| 122 | va_list ap; |
| 123 |
| 124 | va_start(ap, fmt); |
| 125 | _warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap); |
| 126 | va_end(ap); |
| 127 | } |
| 128 |
| 129 | static void |
| 130 | _warn_helper(int severity, int log_errno, const char *fmt, va_list ap) |
| 131 | { |
| 132 | char buf[1024]; |
| 133 | size_t len; |
| 134 |
| 135 | if (fmt != NULL) |
| 136 | evutil_vsnprintf(buf, sizeof(buf), fmt, ap); |
| 137 | else |
| 138 | buf[0] = '\0'; |
| 139 |
| 140 | if (log_errno >= 0) { |
| 141 | len = strlen(buf); |
| 142 | if (len < sizeof(buf) - 3) { |
| 143 | evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", |
| 144 | strerror(log_errno)); |
| 145 | } |
| 146 | } |
| 147 |
| 148 | event_log(severity, buf); |
| 149 | } |
| 150 |
| 151 | static event_log_cb log_fn = NULL; |
| 152 |
| 153 | void |
| 154 | event_set_log_callback(event_log_cb cb) |
| 155 | { |
| 156 | log_fn = cb; |
| 157 | } |
| 158 |
| 159 | static void |
| 160 | event_log(int severity, const char *msg) |
| 161 | { |
| 162 | if (log_fn) |
| 163 | log_fn(severity, msg); |
| 164 | else { |
| 165 | const char *severity_str; |
| 166 | switch (severity) { |
| 167 | case _EVENT_LOG_DEBUG: |
| 168 | severity_str = "debug"; |
| 169 | break; |
| 170 | case _EVENT_LOG_MSG: |
| 171 | severity_str = "msg"; |
| 172 | break; |
| 173 | case _EVENT_LOG_WARN: |
| 174 | severity_str = "warn"; |
| 175 | break; |
| 176 | case _EVENT_LOG_ERR: |
| 177 | severity_str = "err"; |
| 178 | break; |
| 179 | default: |
| 180 | severity_str = "???"; |
| 181 | break; |
| 182 | } |
| 183 | (void)fprintf(stderr, "[%s] %s\n", severity_str, msg); |
| 184 | } |
| 185 | } |
| 186 |