Fixed a bug that caused extra [level] tags in the log
[yaz-moved-to-github.git] / src / log.c
1 /*
2  * Copyright (c) 1995-2004, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: log.c,v 1.12 2004-11-04 14:19:58 heikki Exp $
6  */
7
8 /**
9  * \file log.c
10  * \brief Implements logging utility
11  */
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #ifdef WIN32
18 #include <windows.h>
19 #endif
20
21 #if YAZ_POSIX_THREADS
22 #include <pthread.h>
23 #endif
24
25 #if YAZ_GNU_THREADS
26 #include <pth.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <yaz/nmem.h>
37 #include <yaz/log.h>
38 #include <yaz/nmem.h>
39
40 static NMEM_MUTEX log_mutex = 0;
41 static int mutex_init_flag = 0; /* not yet initialized */
42
43 #define HAS_STRERROR 1
44
45 #if HAS_STRERROR
46
47 #else
48 char *strerror(int n)
49 {
50     extern char *sys_errlist[];
51     return sys_errlist[n];
52 }
53
54 #endif
55
56
57
58 static int l_level = LOG_DEFAULT_LEVEL;
59 static FILE *l_file = NULL;
60 static char l_prefix[512] = "";
61 static char l_prefix2[512] = "";
62 static char l_fname[512] = "";
63
64 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
65 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
66 #define TIMEFORMAT_LEN 50
67 static char l_custom_format[TIMEFORMAT_LEN] = "";
68 static char *l_actual_format = l_old_default_format;
69
70 /** l_max_size tells when to rotate the log. Default to 1 GB */
71 static const int l_def_max_size = 1024*1024*1024;
72 static int l_max_size = 1024*1024*1024;
73
74 #define MAX_MASK_NAMES 35   /* 32 bits plus a few combo names */
75 static struct {
76     int mask;
77     char *name;
78 }  mask_names[MAX_MASK_NAMES] =
79 {
80     { LOG_FATAL,  "fatal"},
81     { LOG_DEBUG,  "debug"},
82     { LOG_WARN,   "warn" },
83     { LOG_LOG,    "log"  },
84     { LOG_ERRNO,  ""},
85     { LOG_MALLOC, "malloc"},
86     { LOG_APP,    "app"  },
87     { LOG_NOTIME, "" },
88     { LOG_APP2,   "app2" },
89     { LOG_APP3,   "app3" },
90     { LOG_ALL,    "all"  },
91     { LOG_FLUSH,  "flush" },
92     { 0,          "none" },
93     { 0, NULL }
94     /* the rest will be filled in if the user defines dynamic modules*/
95 };  
96 static unsigned int next_log_bit = LOG_LAST_BIT<<1; /* first dynamic bit */
97
98 static void init_mutex()
99 {
100     if (mutex_init_flag)
101         return;
102     nmem_mutex_create (&log_mutex);
103     mutex_init_flag = 1;
104 }
105
106
107 FILE *yaz_log_file(void)
108 {
109     if (!l_file)
110         l_file = stderr;
111     return l_file;
112 }
113
114 void yaz_log_init_file (const char *fname)
115 {
116     if (!mutex_init_flag)
117         init_mutex();
118     if (fname)
119     {
120         strncpy(l_fname, fname, sizeof(l_fname)-1);
121         l_fname[sizeof(l_fname)-1] = '\0';
122     }
123     else
124         l_fname[0] = '\0';
125     yaz_log_reopen();
126 }
127
128 void yaz_log_reopen(void)
129 {
130     FILE *new_file;
131     if (!mutex_init_flag)
132         init_mutex();
133     if (!l_file)
134         l_file = stderr;
135     if (!*l_fname)
136         new_file = stderr;
137     else if (!(new_file = fopen(l_fname, "a")))
138         return;
139     if (l_file != stderr)
140     {
141         fclose (l_file);
142     }
143     if (l_level & LOG_FLUSH)
144         setvbuf(new_file, 0, _IONBF, 0);
145     l_file = new_file;
146 }
147
148 static void rotate_log()
149 {
150     char newname[512];
151     if (l_file==stderr)
152         return; /* can't rotate that */
153     if (!*l_fname)
154         return; /* hmm, no name, can't rotate */
155     strncpy(newname, l_fname, 509);
156     newname[509] = '\0'; /* make sure it is terminated */
157     strcat(newname,".1");
158 #ifdef WIN32
159     fclose(l_file);
160     l_file = stderr;
161 #endif
162     rename(l_fname, newname);
163     yaz_log_reopen();
164 }
165
166
167 void yaz_log_init_level (int level)
168 {
169     if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
170     {
171         l_level = level;
172         yaz_log_reopen(); /* make sure we set buffering right */
173     } else
174         l_level = level;
175 }
176
177 void yaz_log_init_prefix (const char *prefix)
178 {
179     if (prefix && *prefix)
180         sprintf(l_prefix, "%.511s ", prefix);
181     else
182         *l_prefix = 0;
183 }
184
185 void yaz_log_init_prefix2 (const char *prefix)
186 {
187     if (prefix && *prefix)
188         sprintf(l_prefix2, "%.511s ", prefix);
189     else
190         *l_prefix2 = 0;
191 }
192
193 void yaz_log_init(int level, const char *prefix, const char *fname)
194 {
195     if (!mutex_init_flag)
196         init_mutex();
197     yaz_log_init_level (level);
198     yaz_log_init_prefix (prefix);
199     if (fname && *fname)
200         yaz_log_init_file (fname);
201 }
202
203 void yaz_log_init_max_size(int mx)
204 {
205     if (mx <0)
206         l_max_size = l_def_max_size;
207     else
208         l_max_size = mx;
209 }
210
211 static void (*start_hook_func)(int, const char *, void *) = NULL;
212 static void *start_hook_info;
213 static void (*end_hook_func)(int, const char *, void *) = NULL;
214 static void *end_hook_info;
215
216 void log_event_start (void (*func)(int, const char *, void *), void *info)
217 {
218     start_hook_func = func;
219     start_hook_info = info;
220 }
221
222 void log_event_end (void (*func)(int, const char *, void *), void *info)
223 {
224     end_hook_func = func;
225     end_hook_info = info;
226 }
227
228 void yaz_log(int level, const char *fmt, ...)
229 {
230     va_list ap;
231     char buf[4096], flags[1024];
232     int i;
233     time_t ti;
234     struct tm *tim;
235     char tbuf[TIMEFORMAT_LEN] = "";
236     int o_level = level;
237     int flen; 
238
239     if (!(level & l_level))
240         return;
241     if (!mutex_init_flag)
242         init_mutex();
243     if (!l_file)
244         l_file = stderr;
245     
246     if ((l_file != stderr) && (l_max_size>0))
247     {
248         nmem_mutex_enter (log_mutex);
249         flen = ftell(l_file);
250         if (flen>l_max_size) 
251             rotate_log();
252         nmem_mutex_leave (log_mutex);
253     }
254
255     *flags = '\0';
256     for (i = 0; level && mask_names[i].name; i++)
257         if ( mask_names[i].mask & level)
258         {
259             if (*mask_names[i].name && mask_names[i].mask && 
260                  mask_names[i].mask != LOG_ALL)
261             {
262                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
263                 level &= ~mask_names[i].mask;
264             }
265         }
266     va_start(ap, fmt);
267 #ifdef WIN32
268     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
269 #else
270 /* !WIN32 */
271 #if HAVE_VSNPRINTF
272     vsnprintf(buf, sizeof(buf), fmt, ap);
273 #else
274     vsprintf(buf, fmt, ap);
275 #endif
276 #endif
277 /* WIN32 */
278     if (o_level & LOG_ERRNO)
279     {
280         strcat(buf, " [");
281         yaz_strerror(buf+strlen(buf), 2048);
282         strcat(buf, "]");
283     }
284     va_end (ap);
285     if (start_hook_func)
286         (*start_hook_func)(o_level, buf, start_hook_info);
287     ti = time(0);
288     tim = localtime(&ti);
289     if (l_level & LOG_NOTIME)
290         tbuf[0] = '\0';
291     else
292         strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
293     tbuf[TIMEFORMAT_LEN-1] = '\0';
294     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
295             l_prefix2, buf);
296     if (l_level & (LOG_FLUSH|LOG_DEBUG) )
297         fflush(l_file);
298     if (end_hook_func)
299         (*end_hook_func)(o_level, buf, end_hook_info);
300 }
301
302 void yaz_log_time_format(const char *fmt)
303 {
304     if ( !fmt || !*fmt) 
305     { /* no format, default to new */
306         l_actual_format = l_new_default_format;
307         return; 
308     }
309     if (0==strcmp(fmt,"old"))
310     { /* force the old format */
311         l_actual_format = l_old_default_format;
312         return; 
313     }
314     /* else use custom format */
315     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
316     l_custom_format[TIMEFORMAT_LEN-1] = '\0';
317     l_actual_format = l_custom_format;
318 }
319
320 /** cleans a loglevel name from leading paths and suffixes */
321 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
322 {
323     char *p;
324     char *start;
325     if (buflen <len)
326         len = buflen; 
327     strncpy(namebuf, name, len);
328     namebuf[len] = '\0';
329     start = namebuf;
330     p = namebuf;
331     while ((p = index(start,'/')))
332         start = p+1;
333     if ((p = rindex(start,'.')))
334         *p = '\0';
335     /*logf(LOG_LOG,"cleaned '%.*s' to '%s' ", len,name, start); */ 
336     return start;
337
338 }
339
340 static int define_module_bit(const char *name)
341 {
342     int i;
343     for (i = 0; mask_names[i].name; i++)
344         ;
345     if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
346     {
347         yaz_log(LOG_WARN,"No more log bits left, not logging '%s'", name);
348         return 0;
349     }
350     mask_names[i].mask = next_log_bit;
351     next_log_bit = next_log_bit<<1;
352     mask_names[i].name = xstrdup(name);
353     mask_names[i+1].name = NULL;
354     mask_names[i+1].mask = 0;
355     return mask_names[i].mask;
356 }
357
358 int yaz_log_module_level(const char *name)
359 {
360     int i;
361     char clean[255];
362     char *n = clean_name(name, strlen(name), clean, sizeof(clean));
363     for (i = 0; mask_names[i].name; i++)
364         if (0==strcmp(n,mask_names[i].name))
365             return mask_names[i].mask;
366     return 0;
367 }
368
369 int yaz_log_mask_str (const char *str)
370 {
371     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
372 }
373
374 int yaz_log_mask_str_x (const char *str, int level)
375 {
376     const char *p;
377     int i;
378     int found;
379     char clean[255] = "";
380     char *n = clean;
381
382     while (*str)
383     {
384         found = 0;
385         for (p = str; *p && *p != ','; p++)
386             ;
387         if (*str == '-' || isdigit(*str))
388         {
389             level = atoi (str);
390             found = 1;
391         }
392         else 
393         {
394             n = clean_name(str, p-str, clean, sizeof(clean));
395             for (i = 0; mask_names[i].name; i++)
396                 /*if (strlen (mask_names[i].name) == (size_t) (p-str) &&
397                     memcmp (mask_names[i].name, str, p-str) == 0)*/
398                 if (0==strcmp (mask_names[i].name,n))
399                 {
400                     if (mask_names[i].mask)
401                         level |= mask_names[i].mask;
402                     else
403                         level = 0;
404                     found = 1;
405                 }
406         }
407         if (!found)
408             level |= define_module_bit(n);
409         if (*p == ',')
410             p++;
411         str = p;
412     }
413     return level;
414 }