1 /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
6 * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
8 * Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net>
10 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
13 * The Regents of the University of California. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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
45 #define WIN32_LEAN_AND_MEAN
47 #undef WIN32_LEAN_AND_MEAN
50 #include <sys/types.h>
52 #ifdef HAVE_SYS_TIME_H
55 #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_vsnprintf(char *str
, size_t size
, const char *format
, va_list args
)
77 r
= _vsnprintf(str
, size
, format
, args
);
79 r
= vsnprintf(str
, size
, format
, args
);
82 if (r
< 0 || ((size_t)r
) >= size
) {
83 /* different platforms behave differently on overflow;
84 * handle both kinds. */
91 event_snprintf(char *str
, size_t size
, const char *format
, ...)
96 r
= event_vsnprintf(str
, size
, format
, ap
);
102 event_err(int eval
, const char *fmt
, ...)
107 _warn_helper(_EVENT_LOG_ERR
, errno
, fmt
, ap
);
113 event_warn(const char *fmt
, ...)
118 _warn_helper(_EVENT_LOG_WARN
, errno
, fmt
, ap
);
123 event_errx(int eval
, const char *fmt
, ...)
128 _warn_helper(_EVENT_LOG_ERR
, -1, fmt
, ap
);
134 event_warnx(const char *fmt
, ...)
139 _warn_helper(_EVENT_LOG_WARN
, -1, fmt
, ap
);
144 event_msgx(const char *fmt
, ...)
149 _warn_helper(_EVENT_LOG_MSG
, -1, fmt
, ap
);
154 _event_debugx(const char *fmt
, ...)
159 _warn_helper(_EVENT_LOG_DEBUG
, -1, fmt
, ap
);
164 _warn_helper(int severity
, int log_errno
, const char *fmt
, va_list ap
)
170 event_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
174 if (log_errno
>= 0) {
176 if (len
< sizeof(buf
) - 3) {
177 event_snprintf(buf
+ len
, sizeof(buf
) - len
, ": %s",
178 strerror(log_errno
));
182 event_log(severity
, buf
);
185 static event_log_cb log_fn
= NULL
;
188 event_set_log_callback(event_log_cb cb
)
194 event_log(int severity
, const char *msg
)
197 log_fn(severity
, msg
);
199 const char *severity_str
;
201 case _EVENT_LOG_DEBUG
:
202 severity_str
= "debug";
205 severity_str
= "msg";
207 case _EVENT_LOG_WARN
:
208 severity_str
= "warn";
211 severity_str
= "err";
214 severity_str
= "???";
217 (void)fprintf(stderr
, "[%s] %s\n", severity_str
, msg
);