Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libevent / dist / log.c
blob5956c0d30792e9240925131aa61aea3e2218b41d
1 /* $NetBSD$ */
2 /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 /*
5 * log.c
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>
13 * Copyright (c) 1993
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
18 * are met:
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
38 * SUCH DAMAGE.
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
45 #ifdef WIN32
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
48 #undef WIN32_LEAN_AND_MEAN
49 #endif
50 #include <sys/types.h>
51 #ifdef HAVE_SYS_TIME_H
52 #include <sys/time.h>
53 #else
54 #include <sys/_time.h>
55 #endif
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <stdarg.h>
59 #include <string.h>
60 #include <errno.h>
61 #include "event.h"
63 #include "log.h"
64 #include "evutil.h"
66 static void _warn_helper(int severity, int log_errno, const char *fmt,
67 va_list ap);
68 static void event_log(int severity, const char *msg);
70 void
71 event_err(int eval, const char *fmt, ...)
73 va_list ap;
75 va_start(ap, fmt);
76 _warn_helper(_EVENT_LOG_ERR, errno, fmt, ap);
77 va_end(ap);
78 exit(eval);
81 void
82 event_warn(const char *fmt, ...)
84 va_list ap;
86 va_start(ap, fmt);
87 _warn_helper(_EVENT_LOG_WARN, errno, fmt, ap);
88 va_end(ap);
91 void
92 event_errx(int eval, const char *fmt, ...)
94 va_list ap;
96 va_start(ap, fmt);
97 _warn_helper(_EVENT_LOG_ERR, -1, fmt, ap);
98 va_end(ap);
99 exit(eval);
102 void
103 event_warnx(const char *fmt, ...)
105 va_list ap;
107 va_start(ap, fmt);
108 _warn_helper(_EVENT_LOG_WARN, -1, fmt, ap);
109 va_end(ap);
112 void
113 event_msgx(const char *fmt, ...)
115 va_list ap;
117 va_start(ap, fmt);
118 _warn_helper(_EVENT_LOG_MSG, -1, fmt, ap);
119 va_end(ap);
122 void
123 _event_debugx(const char *fmt, ...)
125 va_list ap;
127 va_start(ap, fmt);
128 _warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap);
129 va_end(ap);
132 static void
133 _warn_helper(int severity, int log_errno, const char *fmt, va_list ap)
135 char buf[1024];
136 size_t len;
138 if (fmt != NULL)
139 evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
140 else
141 buf[0] = '\0';
143 if (log_errno >= 0) {
144 len = strlen(buf);
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;
156 void
157 event_set_log_callback(event_log_cb cb)
159 log_fn = cb;
162 static void
163 event_log(int severity, const char *msg)
165 if (log_fn)
166 log_fn(severity, msg);
167 else {
168 const char *severity_str;
169 switch (severity) {
170 case _EVENT_LOG_DEBUG:
171 severity_str = "debug";
172 break;
173 case _EVENT_LOG_MSG:
174 severity_str = "msg";
175 break;
176 case _EVENT_LOG_WARN:
177 severity_str = "warn";
178 break;
179 case _EVENT_LOG_ERR:
180 severity_str = "err";
181 break;
182 default:
183 severity_str = "???";
184 break;
186 (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);