WIN32 fixes: Socket not re-used for bind. yaz_log logs WIN32 error
[yaz-moved-to-github.git] / util / log.c
1 /*
2  * Copyright (c) 1995-2000, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: log.c,v $
7  * Revision 1.25  2001-08-23 09:02:46  adam
8  * WIN32 fixes: Socket not re-used for bind. yaz_log logs WIN32 error
9  * message.
10  *
11  * Revision 1.24  2000/09/04 08:58:15  adam
12  * Added prefix yaz_ for most logging utility functions.
13  *
14  * Revision 1.23  2000/03/14 09:06:11  adam
15  * Added POSIX threads support for frontend server.
16  *
17  * Revision 1.22  2000/02/29 13:44:55  adam
18  * Check for config.h (currently not generated).
19  *
20  * Revision 1.21  2000/02/28 11:20:06  adam
21  * Using autoconf. New definitions: YAZ_BEGIN_CDECL/YAZ_END_CDECL.
22  *
23  * Revision 1.20  1999/11/30 13:47:12  adam
24  * Improved installation. Moved header files to include/yaz.
25  *
26  * Revision 1.19  1999/08/27 09:40:32  adam
27  * Renamed logf function to yaz_log. Removed VC++ project files.
28  *
29  * Revision 1.18  1998/10/28 10:27:00  adam
30  * New functions log_init_file, log_init_level, log_init_prefix.
31  *
32  * Revision 1.17  1997/12/09 16:11:02  adam
33  * Assume strerror is defined on Unixes as well. It's standard ANSI.
34  *
35  * Revision 1.16  1997/10/06 08:55:07  adam
36  * Changed log_init so that previous (if any) is closed.
37  *
38  * Revision 1.15  1997/09/29 07:13:13  adam
39  * Minor changes.
40  *
41  * Revision 1.14  1997/09/18 08:48:09  adam
42  * Fixed minor bug that caused log_init to ignore filename.
43  *
44  * Revision 1.13  1997/09/01 08:54:13  adam
45  * New windows NT/95 port using MSV5.0. Made prefix query handling
46  * thread safe. The function options ignores empty arguments when met.
47  *
48  * Revision 1.12  1997/05/01 15:08:14  adam
49  * Added log_mask_str_x routine.
50  *
51  * Revision 1.11  1996/02/05 12:24:32  adam
52  * Implemented log_event_{start,end}-functions.
53  *
54  * Revision 1.10  1995/12/06  09:51:27  quinn
55  * Fixed the log-prefix buffer - it was too small and the setup code lacked
56  * a bounds-check.
57  *
58  * Revision 1.9  1995/09/29  17:12:34  quinn
59  * Smallish
60  *
61  * Revision 1.8  1995/09/27  15:03:02  quinn
62  * Modified function heads & prototypes.
63  *
64  * Revision 1.7  1995/06/19  12:40:18  quinn
65  * Added log_file()
66  *
67  * Revision 1.6  1995/06/15  15:45:03  quinn
68  * Added date info.
69  *
70  * Revision 1.5  1995/05/16  08:51:11  quinn
71  * License, documentation, and memory fixes
72  *
73  * Revision 1.4  1995/05/15  11:56:55  quinn
74  * Debuggng & adjustments.
75  *
76  * Revision 1.3  1995/04/10  10:23:51  quinn
77  * Fixes.
78  *
79  * Revision 1.2  1995/03/31  10:16:55  quinn
80  * Fixed logging.
81  *
82  * Revision 1.1  1995/03/30  10:26:53  quinn
83  * Logging system
84  *
85  * Revision 1.9  1994/12/12  12:09:02  quinn
86  * Changes
87  *
88  * Revision 1.8  1994/11/22  13:15:38  quinn
89  * Simple
90  *
91  * Revision 1.7  1994/10/05  10:16:11  quinn
92  * Added xrealloc. Fixed bug in log.
93  *
94  * Revision 1.6  1994/10/04  14:02:19  quinn
95  * Fixed log_init
96  *
97  * Revision 1.5  1994/09/28  13:07:41  adam
98  * Implemented log_mask_str.
99  *
100  * Revision 1.4  1994/09/27  20:04:13  quinn
101  * Added fflush.
102  *
103  * Revision 1.3  1994/08/18  08:18:48  quinn
104  * Added prefix to log_init.
105  *
106  * Revision 1.2  1994/08/17  14:27:53  quinn
107  * added LOG_ERRNO
108  *
109  * Revision 1.1  1994/08/17  13:23:15  quinn
110  * First version
111  * Added log.c
112  *
113  */
114
115 #if HAVE_CONFIG_H
116 #include <config.h>
117 #endif
118
119 #ifdef WIN32
120 #include <windows.h>
121 #endif
122
123 #include <stdio.h>
124 #include <stdlib.h>
125 #include <ctype.h>
126 #include <string.h>
127 #include <stdarg.h>
128 #include <errno.h>
129 #include <time.h>
130 #include <yaz/log.h>
131
132 #define HAS_STRERROR 1
133
134 #if HAS_STRERROR
135
136 #else
137 char *strerror(int n)
138 {
139         extern char *sys_errlist[];
140         return sys_errlist[n];
141 }
142
143 #endif
144
145 static int l_level = LOG_DEFAULT_LEVEL;
146 static FILE *l_file = NULL;
147 static char l_prefix[512] = "log";
148
149 static struct {
150     int mask;
151     char *name;
152 } mask_names[] =
153 {
154     { LOG_FATAL, "fatal"},
155     { LOG_DEBUG, "debug"},
156     { LOG_WARN,  "warn" },
157     { LOG_LOG,   "log"  },
158     { LOG_ERRNO, ""},
159     { LOG_ALL,   "all"  },
160     { 0,         "none" },
161     { 0, NULL }
162 };  
163
164 FILE *yaz_log_file(void)
165 {
166     if (!l_file)
167         l_file = stderr;
168     return l_file;
169 }
170
171 void yaz_log_init_file (const char *fname)
172 {
173     FILE *new_file;
174     if (!l_file)
175         l_file = stderr;
176     if (!fname || !*fname)
177         return;
178     if (!(new_file = fopen(fname, "a")))
179         return;
180     if (l_file != stderr)
181     {
182         fclose (l_file);
183     }
184     setvbuf(new_file, 0, _IONBF, 0);
185     l_file = new_file;
186 }
187
188 void yaz_log_init_level (int level)
189 {
190     l_level = level;
191 }
192
193 void yaz_log_init_prefix (const char *prefix)
194 {
195     if (prefix && *prefix)
196         sprintf(l_prefix, "%.512s", prefix);
197 }
198
199 void yaz_log_init(int level, const char *prefix, const char *fname)
200 {
201     yaz_log_init_level (level);
202     yaz_log_init_prefix (prefix);
203     yaz_log_init_file (fname);
204 }
205
206 static void (*start_hook_func)(int, const char *, void *) = NULL;
207 static void *start_hook_info;
208 static void (*end_hook_func)(int, const char *, void *) = NULL;
209 static void *end_hook_info;
210
211 void log_event_start (void (*func)(int, const char *, void *), void *info)
212 {
213     start_hook_func = func;
214     start_hook_info = info;
215 }
216
217 void log_event_end (void (*func)(int, const char *, void *), void *info)
218 {
219     end_hook_func = func;
220     end_hook_info = info;
221 }
222
223 void yaz_log(int level, const char *fmt, ...)
224 {
225     va_list ap;
226     char buf[4096], flags[1024];
227     int i;
228     time_t ti;
229     struct tm *tim;
230     char tbuf[50];
231     int o_level = level;
232
233     if (!(level & l_level))
234         return;
235     if (!l_file)
236         l_file = stderr;
237     *flags = '\0';
238     for (i = 0; level && mask_names[i].name; i++)
239         if (mask_names[i].mask & level)
240         {
241             if (*mask_names[i].name)
242                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
243             level -= mask_names[i].mask;
244         }
245     va_start(ap, fmt);
246 #if HAVE_VSNPRINTF
247     vsnprintf(buf, sizeof(buf), fmt, ap);
248 #else
249     vsprintf(buf, fmt, ap);
250 #endif
251     if (o_level & LOG_ERRNO)
252     {
253 #ifdef WIN32
254         DWORD err = GetLastError();
255         if (err)
256         {
257             strcat(buf, " [");
258             FormatMessage(
259                 FORMAT_MESSAGE_FROM_SYSTEM,
260                 NULL,
261                 err,
262                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
263                 (LPTSTR) buf + strlen(buf),
264                 2048,
265                 NULL);
266             strcat(buf, "]");
267         }
268 #else
269         sprintf(buf + strlen(buf), " [%s]", strerror(errno));
270 #endif
271     }
272     if (start_hook_func)
273         (*start_hook_func)(o_level, buf, start_hook_info);
274     ti = time(0);
275     tim = localtime(&ti);
276     strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
277     fprintf(l_file, "%s: %s: %s %s\n", tbuf, l_prefix, flags, buf);
278     fflush(l_file);
279     if (end_hook_func)
280         (*end_hook_func)(o_level, buf, end_hook_info);
281 }
282
283 int yaz_log_mask_str (const char *str)
284 {
285     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
286 }
287
288 int yaz_log_mask_str_x (const char *str, int level)
289 {
290     const char *p;
291     int i;
292
293     while (*str)
294     {
295         for (p = str; *p && *p != ','; p++)
296             ;
297         if (*str == '-' || isdigit(*str))
298             level = atoi (str);
299         else
300             for (i = 0; mask_names[i].name; i++)
301                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
302                     memcmp (mask_names[i].name, str, p-str) == 0)
303                 {
304                     if (mask_names[i].mask)
305                         level |= mask_names[i].mask;
306                     else
307                         level = 0;
308                 }
309         if (*p == ',')
310             p++;
311         str = p;
312     }
313     return level;
314 }