xref: /netbsd-src/external/bsd/libevent/dist/log.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: log.c,v 1.1.1.1 2009/11/02 10:01:01 plunky Exp $	*/
2 /*	$OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $	*/
3 
4 /*
5  * log.c
6  *
7  * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
8  *
9  * Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net>
10  *
11  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
12  *
13  * Copyright (c) 1993
14  *	The Regents of the University of California.  All rights reserved.
15  *
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.
27  *
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.
39  */
40 
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
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"
62 
63 #include "log.h"
64 #include "evutil.h"
65 
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);
69 
70 void
71 event_err(int eval, const char *fmt, ...)
72 {
73 	va_list ap;
74 
75 	va_start(ap, fmt);
76 	_warn_helper(_EVENT_LOG_ERR, errno, fmt, ap);
77 	va_end(ap);
78 	exit(eval);
79 }
80 
81 void
82 event_warn(const char *fmt, ...)
83 {
84 	va_list ap;
85 
86 	va_start(ap, fmt);
87 	_warn_helper(_EVENT_LOG_WARN, errno, fmt, ap);
88 	va_end(ap);
89 }
90 
91 void
92 event_errx(int eval, const char *fmt, ...)
93 {
94 	va_list ap;
95 
96 	va_start(ap, fmt);
97 	_warn_helper(_EVENT_LOG_ERR, -1, fmt, ap);
98 	va_end(ap);
99 	exit(eval);
100 }
101 
102 void
103 event_warnx(const char *fmt, ...)
104 {
105 	va_list ap;
106 
107 	va_start(ap, fmt);
108 	_warn_helper(_EVENT_LOG_WARN, -1, fmt, ap);
109 	va_end(ap);
110 }
111 
112 void
113 event_msgx(const char *fmt, ...)
114 {
115 	va_list ap;
116 
117 	va_start(ap, fmt);
118 	_warn_helper(_EVENT_LOG_MSG, -1, fmt, ap);
119 	va_end(ap);
120 }
121 
122 void
123 _event_debugx(const char *fmt, ...)
124 {
125 	va_list ap;
126 
127 	va_start(ap, fmt);
128 	_warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap);
129 	va_end(ap);
130 }
131 
132 static void
133 _warn_helper(int severity, int log_errno, const char *fmt, va_list ap)
134 {
135 	char buf[1024];
136 	size_t len;
137 
138 	if (fmt != NULL)
139 		evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
140 	else
141 		buf[0] = '\0';
142 
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));
148 		}
149 	}
150 
151 	event_log(severity, buf);
152 }
153 
154 static event_log_cb log_fn = NULL;
155 
156 void
157 event_set_log_callback(event_log_cb cb)
158 {
159 	log_fn = cb;
160 }
161 
162 static void
163 event_log(int severity, const char *msg)
164 {
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;
185 		}
186 		(void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
187 	}
188 }
189