2 /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
7 * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
9 * Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net>
11 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
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
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
46 #define WIN32_LEAN_AND_MEAN
48 #undef WIN32_LEAN_AND_MEAN
50 #include <sys/types.h>
51 #ifdef HAVE_SYS_TIME_H
54 #include <sys/_time.h>
66 static void _warn_helper(int severity
, int log_errno
, const char *fmt
,
68 static void event_log(int severity
, const char *msg
);
71 event_err(int eval
, const char *fmt
, ...)
76 _warn_helper(_EVENT_LOG_ERR
, errno
, fmt
, ap
);
82 event_warn(const char *fmt
, ...)
87 _warn_helper(_EVENT_LOG_WARN
, errno
, fmt
, ap
);
92 event_errx(int eval
, const char *fmt
, ...)
97 _warn_helper(_EVENT_LOG_ERR
, -1, fmt
, ap
);
103 event_warnx(const char *fmt
, ...)
108 _warn_helper(_EVENT_LOG_WARN
, -1, fmt
, ap
);
113 event_msgx(const char *fmt
, ...)
118 _warn_helper(_EVENT_LOG_MSG
, -1, fmt
, ap
);
123 _event_debugx(const char *fmt
, ...)
128 _warn_helper(_EVENT_LOG_DEBUG
, -1, fmt
, ap
);
133 _warn_helper(int severity
, int log_errno
, const char *fmt
, va_list ap
)
139 evutil_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
143 if (log_errno
>= 0) {
145 if (len
< sizeof(buf
) - 3) {
146 evutil_snprintf(buf
+ len
, sizeof(buf
) - len
, ": %s",
147 strerror(log_errno
));
151 event_log(severity
, buf
);
154 static event_log_cb log_fn
= NULL
;
157 event_set_log_callback(event_log_cb cb
)
163 event_log(int severity
, const char *msg
)
166 log_fn(severity
, msg
);
168 const char *severity_str
;
170 case _EVENT_LOG_DEBUG
:
171 severity_str
= "debug";
174 severity_str
= "msg";
176 case _EVENT_LOG_WARN
:
177 severity_str
= "warn";
180 severity_str
= "err";
183 severity_str
= "???";
186 (void)fprintf(stderr
, "[%s] %s\n", severity_str
, msg
);