Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / lib / log.c
blobe3dc8079ef310fbb136c7028d6eedd9375eb1e66
1 /* $NetBSD: log.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
3 #ifndef lint
4 static char *rcsid = "Id: log.c,v 1.1 2003/06/04 00:25:53 marka Exp ";
5 #endif
7 /*
8 * Copyright (c) 2000 Japan Network Information Center. All rights reserved.
9 *
10 * By using this file, you agree to the terms and conditions set forth bellow.
12 * LICENSE TERMS AND CONDITIONS
14 * The following License Terms and Conditions apply, unless a different
15 * license is obtained from Japan Network Information Center ("JPNIC"),
16 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
17 * Chiyoda-ku, Tokyo 101-0047, Japan.
19 * 1. Use, Modification and Redistribution (including distribution of any
20 * modified or derived work) in source and/or binary forms is permitted
21 * under this License Terms and Conditions.
23 * 2. Redistribution of source code must retain the copyright notices as they
24 * appear in each source code file, this License Terms and Conditions.
26 * 3. Redistribution in binary form must reproduce the Copyright Notice,
27 * this License Terms and Conditions, in the documentation and/or other
28 * materials provided with the distribution. For the purposes of binary
29 * distribution the "Copyright Notice" refers to the following language:
30 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
32 * 4. The name of JPNIC may not be used to endorse or promote products
33 * derived from this Software without specific prior written approval of
34 * JPNIC.
36 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
46 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
49 #include <config.h>
51 #include <stdio.h>
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
58 #include <idn/log.h>
60 #define LOGLEVEL_ENV "IDN_LOG_LEVEL"
62 #ifdef DEBUG
63 #define DEFAULT_LOG_LEVEL idn_log_level_info
64 #else
65 #define DEFAULT_LOG_LEVEL idn_log_level_error
66 #endif
68 static int log_level = -1;
69 static idn_log_proc_t log_proc;
71 static void initialize(void);
72 static void log(int level, const char *fmt, va_list args);
73 static void log_to_stderr(int level, const char *buf);
75 void
76 idn_log_fatal(const char *fmt, ...) {
77 va_list args;
79 va_start(args, fmt);
80 log(idn_log_level_fatal, fmt, args);
81 va_end(args);
82 exit(1);
85 void
86 idn_log_error(const char *fmt, ...) {
87 va_list args;
89 va_start(args, fmt);
90 log(idn_log_level_error, fmt, args);
91 va_end(args);
94 void
95 idn_log_warning(const char *fmt, ...) {
96 va_list args;
98 va_start(args, fmt);
99 log(idn_log_level_warning, fmt, args);
100 va_end(args);
103 void
104 idn_log_info(const char *fmt, ...) {
105 va_list args;
107 va_start(args, fmt);
108 log(idn_log_level_info, fmt, args);
109 va_end(args);
112 void
113 idn_log_trace(const char *fmt, ...) {
114 va_list args;
116 va_start(args, fmt);
117 log(idn_log_level_trace, fmt, args);
118 va_end(args);
121 void
122 idn_log_dump(const char *fmt, ...) {
123 va_list args;
125 va_start(args, fmt);
126 log(idn_log_level_dump, fmt, args);
127 va_end(args);
130 void
131 idn_log_setlevel(int level) {
132 if (level >= 0)
133 log_level = level;
137 idn_log_getlevel(void) {
138 if (log_level < 0)
139 initialize();
140 return log_level;
143 void
144 idn_log_setproc(idn_log_proc_t proc) {
145 if (proc == NULL)
146 log_proc = log_to_stderr;
147 else
148 log_proc = proc;
151 static void
152 initialize(void) {
153 char *s;
155 if (log_level < 0) {
156 if ((s = getenv(LOGLEVEL_ENV)) != NULL) {
157 int level = atoi(s);
158 if (level >= 0)
159 log_level = level;
161 if (log_level < 0)
162 log_level = DEFAULT_LOG_LEVEL;
165 if (log_proc == NULL)
166 log_proc = log_to_stderr;
169 static void
170 log(int level, const char *fmt, va_list args) {
171 char buf[1024];
173 initialize();
175 if (log_level < level)
176 return;
178 #if HAVE_VSNPRINTF
179 (void)vsnprintf(buf, sizeof(buf), fmt, args);
180 #else
181 /* Let's hope 1024 is enough.. */
182 (void)vsprintf(buf, fmt, args);
183 #endif
184 (*log_proc)(level, buf);
187 static void
188 log_to_stderr(int level, const char *buf) {
189 char *title;
190 char tmp[20];
192 switch (level) {
193 case idn_log_level_fatal:
194 title = "FATAL";
195 break;
196 case idn_log_level_error:
197 title = "ERROR";
198 break;
199 case idn_log_level_warning:
200 title = "WARNING";
201 break;
202 case idn_log_level_info:
203 title = "INFO";
204 break;
205 case idn_log_level_trace:
206 title = "TRACE";
207 break;
208 case idn_log_level_dump:
209 title = "DUMP";
210 break;
211 default:
212 (void)sprintf(tmp, "LEVEL%d", level);
213 title = tmp;
214 break;
216 fprintf(stderr, "%u: [%s] %s", (unsigned int)getpid(), title, buf);