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