e8aa1a83e5e36865c6bf345a2d5b7a57da3cfb4f
[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.19 2004-12-30 00:11:00 adam 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/xmalloc.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 = YLOG_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     { YLOG_FATAL,  "fatal"},
81     { YLOG_DEBUG,  "debug"},
82     { YLOG_WARN,   "warn" },
83     { YLOG_LOG,    "log"  },
84     { YLOG_ERRNO,  ""},
85     { YLOG_MALLOC, "malloc"},
86  /*   { YLOG_APP,    "app"  }, */
87     { YLOG_NOTIME, "notime" },
88  /*   { YLOG_APP2,   "app2" }, */
89  /*   { YLOG_APP3,   "app3" }, */
90     { YLOG_ALL,    "all"  },
91     { YLOG_FLUSH,  "flush" },
92     { YLOG_LOGLVL, "loglevel" }, 
93     { 0,          "none" },
94     { 0, NULL }
95     /* the rest will be filled in if the user defines dynamic modules*/
96 };  
97 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
98
99 static void init_mutex()
100 {
101     if (mutex_init_flag)
102         return;
103     nmem_mutex_create (&log_mutex);
104     mutex_init_flag = 1;
105 }
106
107
108 FILE *yaz_log_file(void)
109 {
110     if (!l_file)
111         l_file = stderr;
112     return l_file;
113 }
114
115 void yaz_log_init_file (const char *fname)
116 {
117     if (!mutex_init_flag)
118         init_mutex();
119     if (fname)
120     {
121         strncpy(l_fname, fname, sizeof(l_fname)-1);
122         l_fname[sizeof(l_fname)-1] = '\0';
123     }
124     else
125         l_fname[0] = '\0';
126     yaz_log_reopen();
127 }
128
129 void yaz_log_reopen(void)
130 {
131     FILE *new_file;
132     if (!mutex_init_flag)
133         init_mutex();
134     if (!l_file)
135         l_file = stderr;
136     if (!*l_fname)
137         new_file = stderr;
138     else if (!(new_file = fopen(l_fname, "a")))
139         return;
140     if (l_file != stderr)
141     {
142         fclose (l_file);
143     }
144     if (l_level & YLOG_FLUSH)
145         setvbuf(new_file, 0, _IONBF, 0);
146     l_file = new_file;
147 }
148
149 static void rotate_log()
150 {
151     char newname[512];
152     if (l_file==stderr)
153         return; /* can't rotate that */
154     if (!*l_fname)
155         return; /* hmm, no name, can't rotate */
156     strncpy(newname, l_fname, 509);
157     newname[509] = '\0'; /* make sure it is terminated */
158     strcat(newname,".1");
159 #ifdef WIN32
160     /* windows can't rename a file if it is open */
161     fclose(l_file);
162     l_file = stderr;
163 #endif
164     rename(l_fname, newname);
165     yaz_log_reopen();
166 }
167
168
169 void yaz_log_init_level (int level)
170 {
171     if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
172     {
173         l_level = level;
174         yaz_log_reopen(); /* make sure we set buffering right */
175     } else
176         l_level = level;
177     if (l_level  & YLOG_LOGLVL)
178     {  /* dump the log level bits */
179         char *bittype="Static ";
180         int i;
181         yaz_log(YLOG_LOGLVL,"Setting log level to %d = 0x%08x",l_level,l_level);
182         for (i = 0; mask_names[i].name; i++)
183             if (mask_names[i].mask && *mask_names[i].name)
184                 if (strcmp(mask_names[i].name,"all")!=0)
185                 {
186                     yaz_log(YLOG_LOGLVL,"%s log bit %08x '%s' is %s",
187                         bittype, mask_names[i].mask, mask_names[i].name,
188                         (level & mask_names[i].mask)?  "ON": "off");
189                 if (mask_names[i].mask>YLOG_LAST_BIT)
190                     bittype="Dynamic";
191                 }
192     }
193 }
194
195 void yaz_log_init_prefix (const char *prefix)
196 {
197     if (prefix && *prefix)
198         sprintf(l_prefix, "%.511s ", prefix);
199     else
200         *l_prefix = 0;
201 }
202
203 void yaz_log_init_prefix2 (const char *prefix)
204 {
205     if (prefix && *prefix)
206         sprintf(l_prefix2, "%.511s ", prefix);
207     else
208         *l_prefix2 = 0;
209 }
210
211 void yaz_log_init(int level, const char *prefix, const char *fname)
212 {
213     if (!mutex_init_flag)
214         init_mutex();
215     yaz_log_init_level (level);
216     yaz_log_init_prefix (prefix);
217     if (fname && *fname)
218         yaz_log_init_file (fname);
219 }
220
221 void yaz_log_init_max_size(int mx)
222 {
223     if (mx <0)
224         l_max_size = l_def_max_size;
225     else
226         l_max_size = mx;
227 }
228
229 static void (*start_hook_func)(int, const char *, void *) = NULL;
230 static void *start_hook_info;
231 static void (*end_hook_func)(int, const char *, void *) = NULL;
232 static void *end_hook_info;
233
234 void log_event_start (void (*func)(int, const char *, void *), void *info)
235 {
236     start_hook_func = func;
237     start_hook_info = info;
238 }
239
240 void log_event_end (void (*func)(int, const char *, void *), void *info)
241 {
242     end_hook_func = func;
243     end_hook_info = info;
244 }
245
246 void yaz_log(int level, const char *fmt, ...)
247 {
248     va_list ap;
249     char buf[4096], flags[1024];
250     int i;
251     time_t ti;
252     struct tm *tim;
253     char tbuf[TIMEFORMAT_LEN] = "";
254     int o_level = level;
255     int flen; 
256
257     if (!(level & l_level))
258         return;
259     if (!mutex_init_flag)
260         init_mutex();
261     if (!l_file)
262         l_file = stderr;
263     
264     if ((l_file != stderr) && (l_max_size>0))
265     {
266         nmem_mutex_enter (log_mutex);
267         flen = ftell(l_file);
268         if (flen>l_max_size) 
269             rotate_log();
270         nmem_mutex_leave (log_mutex);
271     }
272
273     *flags = '\0';
274     for (i = 0; level && mask_names[i].name; i++)
275         if ( mask_names[i].mask & level)
276         {
277             if (*mask_names[i].name && mask_names[i].mask && 
278                  mask_names[i].mask != YLOG_ALL)
279             {
280                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
281                 level &= ~mask_names[i].mask;
282             }
283         }
284     va_start(ap, fmt);
285 #ifdef WIN32
286     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
287 #else
288 /* !WIN32 */
289 #if HAVE_VSNPRINTF
290     vsnprintf(buf, sizeof(buf), fmt, ap);
291 #else
292     vsprintf(buf, fmt, ap);
293 #endif
294 #endif
295 /* WIN32 */
296     if (o_level & YLOG_ERRNO)
297     {
298         strcat(buf, " [");
299         yaz_strerror(buf+strlen(buf), 2048);
300         strcat(buf, "]");
301     }
302     va_end (ap);
303     if (start_hook_func)
304         (*start_hook_func)(o_level, buf, start_hook_info);
305     ti = time(0);
306     tim = localtime(&ti);
307     if (l_level & YLOG_NOTIME)
308         tbuf[0] = '\0';
309     else
310         strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
311     tbuf[TIMEFORMAT_LEN-1] = '\0';
312     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
313             l_prefix2, buf);
314     if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
315         fflush(l_file);
316     if (end_hook_func)
317         (*end_hook_func)(o_level, buf, end_hook_info);
318 }
319
320 void yaz_log_time_format(const char *fmt)
321 {
322     if ( !fmt || !*fmt) 
323     { /* no format, default to new */
324         l_actual_format = l_new_default_format;
325         return; 
326     }
327     if (0==strcmp(fmt,"old"))
328     { /* force the old format */
329         l_actual_format = l_old_default_format;
330         return; 
331     }
332     /* else use custom format */
333     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
334     l_custom_format[TIMEFORMAT_LEN-1] = '\0';
335     l_actual_format = l_custom_format;
336 }
337
338 /** cleans a loglevel name from leading paths and suffixes */
339 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
340 {
341     char *p = namebuf;
342     char *start = namebuf;
343     if (buflen <len)
344         len = buflen; 
345     strncpy(namebuf, name, len);
346     namebuf[len] = '\0';
347     while ((p = strchr(start,'/')))
348         start = p+1;
349     if ((p = strrchr(start,'.')))
350         *p = '\0';
351     return start;
352
353 }
354
355 static int define_module_bit(const char *name)
356 {
357     int i;
358     for (i = 0; mask_names[i].name; i++)
359         ;
360     if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
361     {
362         yaz_log(YLOG_WARN,"No more log bits left, not logging '%s'", name);
363         return 0;
364     }
365     mask_names[i].mask = next_log_bit;
366     next_log_bit = next_log_bit<<1;
367     mask_names[i].name = xstrdup(name);
368     mask_names[i+1].name = NULL;
369     mask_names[i+1].mask = 0;
370     return mask_names[i].mask;
371 }
372
373 int yaz_log_module_level(const char *name)
374 {
375     int i;
376     char clean[255];
377     char *n = clean_name(name, strlen(name), clean, sizeof(clean));
378     for (i = 0; mask_names[i].name; i++)
379         if (0==strcmp(n,mask_names[i].name))
380         {
381             yaz_log(YLOG_LOGLVL,"returning log bit 0x%x for '%s' %s",
382                     mask_names[i].mask, n, 
383                     strcmp(n,name)?name:"" );
384             return mask_names[i].mask;
385         }
386     yaz_log(YLOG_LOGLVL,"returning NO log bit for '%s' %s", n, 
387                     strcmp(n,name)?name:"" );
388     return 0;
389 }
390
391 int yaz_log_mask_str (const char *str)
392 {
393     return yaz_log_mask_str_x (str, YLOG_DEFAULT_LEVEL);
394 }
395
396 int yaz_log_mask_str_x (const char *str, int level)
397 {
398     const char *p;
399     int i;
400     char clean[255] = "";
401     char *n = clean;
402
403     while (*str)
404     {
405         int found = 0;
406         int negated = 0;
407         for (p = str; *p && *p != ','; p++)
408             ;
409         if (*str=='-')
410         {
411             negated=1;
412             str++;
413         }
414         if (isdigit(*(unsigned char *) str))
415         {
416             level = atoi (str);
417             found = 1;
418         }
419         else 
420         {
421             n = clean_name(str, p-str, clean, sizeof(clean));
422             for (i = 0; mask_names[i].name; i++)
423                 if (0==strcmp (mask_names[i].name,n))
424                 {
425                     if (mask_names[i].mask)
426                         if (negated)
427                             level &= ~mask_names[i].mask;
428                         else
429                             level |= mask_names[i].mask;
430                     else
431                         level = 0; /* 'none' clears them all */
432                     found = 1;
433                 }
434         }
435         if (!found)
436             level |= define_module_bit(n);
437         if (*p == ',')
438             p++;
439         str = p;
440     }
441     return level;
442 }