Added dynamic log-levels:
[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.9 2004-11-03 14:25:06 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, 510);
156     newname[510]='\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                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
262             level -= mask_names[i].mask;
263         }
264     va_start(ap, fmt);
265 #ifdef WIN32
266     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
267 #else
268 /* !WIN32 */
269 #if HAVE_VSNPRINTF
270     vsnprintf(buf, sizeof(buf), fmt, ap);
271 #else
272     vsprintf(buf, fmt, ap);
273 #endif
274 #endif
275 /* WIN32 */
276     if (o_level & LOG_ERRNO)
277     {
278         strcat(buf, " [");
279         yaz_strerror(buf+strlen(buf), 2048);
280         strcat(buf, "]");
281     }
282     va_end (ap);
283     if (start_hook_func)
284         (*start_hook_func)(o_level, buf, start_hook_info);
285     ti = time(0);
286     tim = localtime(&ti);
287     if (l_level & LOG_NOTIME)
288       tbuf[0]='\0';
289     else
290       strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
291     tbuf[TIMEFORMAT_LEN-1]='\0';
292     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
293             l_prefix2, buf);
294     if (l_level & (LOG_FLUSH|LOG_DEBUG) )
295         fflush(l_file);
296     if (end_hook_func)
297         (*end_hook_func)(o_level, buf, end_hook_info);
298 }
299
300 void yaz_log_time_format(const char *fmt)
301 {
302     if ( !fmt || !*fmt) 
303     { /* no format, default to new */
304         l_actual_format = l_new_default_format;
305         return; 
306     }
307     if (0==strcmp(fmt,"old"))
308     { /* force the old format */
309         l_actual_format = l_old_default_format;
310         return; 
311     }
312     /* else use custom format */
313     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
314     l_custom_format[TIMEFORMAT_LEN-1]='\0';
315     l_actual_format = l_custom_format;
316 }
317
318 /** cleans a loglevel name from leading paths and suffixes */
319 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
320 {
321     char *p;
322     char *start;
323     if (buflen <len)
324         len=buflen; 
325     strncpy(namebuf, name, len);
326     namebuf[len]='\0';
327     start=p=namebuf;
328     while ((p=index(start,'/')))
329         start=p+1;
330     if ((p=rindex(start,'.')))
331         *p='\0';
332     return start;
333
334 }
335
336 static int define_module_bit(const char *name)
337 {
338     int i;
339     for (i=0;mask_names[i].name;i++)
340         ;
341     if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
342     {
343         yaz_log(LOG_WARN,"No more log bits left, not logging '%s'", name);
344         return 0;
345     }
346     mask_names[i].mask= next_log_bit;
347     next_log_bit= next_log_bit<<1;
348     mask_names[i].name=xstrdup(name);
349     mask_names[i+1].name=NULL;
350     mask_names[i+1].mask=0;
351     return mask_names[i].mask;
352 }
353
354 int yaz_log_module_level(const char *name)
355 {
356     int i;
357     char clean[255];
358     char *n=clean_name(name, strlen(name), clean, sizeof(clean));
359     for (i=0;mask_names[i].name;i++)
360         if (0==strcmp(n,mask_names[i].name))
361             return mask_names[i].mask;
362     return 0;
363 }
364
365 int yaz_log_mask_str (const char *str)
366 {
367     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
368 }
369
370 int yaz_log_mask_str_x (const char *str, int level)
371 {
372     const char *p;
373     int i;
374     int found;
375     char clean[255];
376     char *n;
377
378     while (*str)
379     {
380         found=0;
381         for (p = str; *p && *p != ','; p++)
382             ;
383         if (*str == '-' || isdigit(*str))
384         {
385             level = atoi (str);
386             found=1;
387         }
388         else
389             n=clean_name(str, p-str, clean, sizeof(clean));
390             for (i = 0; mask_names[i].name; i++)
391                 /*if (strlen (mask_names[i].name) == (size_t) (p-str) &&
392                     memcmp (mask_names[i].name, str, p-str) == 0)*/
393                 if (0==strcmp (mask_names[i].name,n))
394                 {
395                     if (mask_names[i].mask)
396                         level |= mask_names[i].mask;
397                     else
398                         level = 0;
399                     found=1;
400                 }
401         if (!found)
402             level |= define_module_bit(n);
403         if (*p == ',')
404             p++;
405         str = p;
406     }
407     return level;
408 }