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