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