Fix URL of SRU diagnostics list.
[yaz-moved-to-github.git] / src / log.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: log.c,v 1.29 2005-09-16 21:46:24 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 static int l_level = YLOG_DEFAULT_LEVEL;
57 static FILE *l_file = NULL;
58 static char l_prefix[512] = "";
59 static char l_prefix2[512] = "";
60 static char l_fname[512] = "";
61
62 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
63 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
64 #define TIMEFORMAT_LEN 50
65 static char l_custom_format[TIMEFORMAT_LEN] = "";
66 static char *l_actual_format = l_old_default_format;
67
68 /** l_max_size tells when to rotate the log. Default to 1 GB */
69 static const int l_def_max_size = 1024*1024*1024;
70 static int l_max_size = 1024*1024*1024;
71
72 #define MAX_MASK_NAMES 35   /* 32 bits plus a few combo names */
73 static struct {
74     int mask;
75     char *name;
76 } mask_names[MAX_MASK_NAMES] =
77 {
78     { YLOG_FATAL,  "fatal"},
79     { YLOG_DEBUG,  "debug"},
80     { YLOG_WARN,   "warn" },
81     { YLOG_LOG,    "log"  },
82     { YLOG_ERRNO,  ""},
83     { YLOG_MALLOC, "malloc"},
84     { YLOG_APP,    "app"  },
85     { YLOG_NOTIME, "notime" },
86     { YLOG_APP2,   "app2" }, 
87     { YLOG_APP3,   "app3" },
88     { YLOG_ALL,    "all"  },
89     { YLOG_FLUSH,  "flush" },
90     { YLOG_LOGLVL, "loglevel" }, 
91     { 0,           "none" },
92     { 0, NULL }
93     /* the rest will be filled in if the user defines dynamic modules*/
94 };  
95
96 static unsigned int next_log_bit = YLOG_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 FILE *yaz_log_file(void)
107 {
108     if (!l_file)
109         l_file = stderr;
110     return l_file;
111 }
112
113 void yaz_log_init_file(const char *fname)
114 {
115     init_mutex();
116     if (fname)
117     {
118         strncpy(l_fname, fname, sizeof(l_fname)-1);
119         l_fname[sizeof(l_fname)-1] = '\0';
120     }
121     else
122         l_fname[0] = '\0';
123     yaz_log_reopen();
124 }
125
126 static void rotate_log(const char *cur_fname)
127 {
128     char newname[512];
129     strncpy(newname, cur_fname, 509);
130     newname[509] = '\0'; /* make sure it is terminated */
131     strcat(newname, ".1");
132 #ifdef WIN32
133     /* windows can't rename a file if it is open */
134     fclose(l_file);
135     l_file = 0;
136     MoveFileEx(cur_fname, newname, MOVEFILE_REPLACE_EXISTING);
137 #else
138     rename(cur_fname, newname);
139 #endif
140 }
141
142 void yaz_log_init_level(int level)
143 {
144     init_mutex();
145     if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
146     {
147         l_level = level;
148         yaz_log_reopen(); /* make sure we set buffering right */
149     } else
150         l_level = level;
151     if (l_level  & YLOG_LOGLVL)
152     {  /* dump the log level bits */
153         const char *bittype = "Static ";
154         int i, sz;
155
156         yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
157                 l_level, l_level);
158         /* determine size of mask_names (locked) */
159         nmem_mutex_enter(log_mutex);
160         for (sz = 0; mask_names[sz].name; sz++)
161             ;
162         nmem_mutex_leave(log_mutex);
163         /* second pass without lock */
164         for (i = 0; i < sz; i++)
165             if (mask_names[i].mask && *mask_names[i].name)
166                 if (strcmp(mask_names[i].name, "all") != 0)
167                 {
168                     yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
169                             bittype, mask_names[i].mask, mask_names[i].name,
170                             (level & mask_names[i].mask)?  "ON": "off");
171                     if (mask_names[i].mask > YLOG_LAST_BIT)
172                         bittype = "Dynamic";
173                 }
174     }
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     init_mutex();
196     yaz_log_init_level(level);
197     yaz_log_init_prefix(prefix);
198     if (fname && *fname)
199         yaz_log_init_file(fname);
200 }
201
202 void yaz_log_init_max_size(int mx)
203 {
204     if (mx <0)
205         l_max_size = l_def_max_size;
206     else
207         l_max_size = mx;
208 }
209
210 static void (*start_hook_func)(int, const char *, void *) = NULL;
211 static void *start_hook_info;
212 static void (*end_hook_func)(int, const char *, void *) = NULL;
213 static void *end_hook_info;
214
215 void log_event_start(void (*func)(int, const char *, void *), void *info)
216 {
217     start_hook_func = func;
218     start_hook_info = info;
219 }
220
221 void log_event_end(void (*func)(int, const char *, void *), void *info)
222 {
223     end_hook_func = func;
224     end_hook_info = info;
225 }
226
227 static void yaz_log_open_check(struct tm *tm, int force)
228 {
229     char new_filename[512];
230     static char cur_filename[512] = "";
231
232     if (!l_file)
233         l_file = stderr;
234
235     if (l_fname && *l_fname)
236     {
237         strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
238         if (strcmp(new_filename, cur_filename))
239         {
240             strcpy(cur_filename, new_filename);
241             force = 1;
242         }
243         if (l_max_size > 0 && (l_file && l_file != stderr))
244         {
245             long flen = ftell(l_file);
246             if (flen > l_max_size)
247             {
248                 rotate_log(cur_filename);
249                 force = 1;
250             }
251         }
252         if (force)
253         {
254             if (l_file && l_file != stderr)
255                 fclose(l_file);
256             l_file = fopen(cur_filename, "a");
257             if (l_level & YLOG_FLUSH)
258                 setvbuf(l_file, 0, _IONBF, 0);
259         }
260     }
261 }
262
263 void yaz_log_reopen()
264 {
265     time_t cur_time = time(0);
266 #if HAVE_LOCALTIME_R
267     struct tm tm0, *tm = &tm0;
268 #else
269     struct tm *tm;
270 #endif
271
272     nmem_mutex_enter(log_mutex);
273 #if HAVE_LOCALTIME_R
274     localtime_r(&cur_time, tm);
275 #else
276     tm = localtime(&cur_time);
277 #endif
278     yaz_log_open_check(tm, 1);
279     nmem_mutex_leave(log_mutex);
280 }
281
282 void yaz_log(int level, const char *fmt, ...)
283 {
284     va_list ap;
285     char buf[4096], flags[1024];
286     int i;
287     time_t ti;
288 #if HAVE_LOCALTIME_R
289     struct tm tm0, *tm = &tm0;
290 #else
291     struct tm *tm;
292 #endif
293     char tbuf[TIMEFORMAT_LEN] = "";
294     int o_level = level;
295
296     if (!(level & l_level))
297         return;
298     init_mutex();
299
300     nmem_mutex_enter(log_mutex);
301     ti = time(0);
302 #if HAVE_LOCALTIME_R
303     localtime_r(&ti, tm);
304 #else
305     tm = localtime(&ti);
306 #endif
307
308     yaz_log_open_check(tm, 0);
309
310     if (!l_file)
311         l_file = stderr;
312
313     *flags = '\0';
314     for (i = 0; level && mask_names[i].name; i++)
315         if ( mask_names[i].mask & level)
316         {
317             if (*mask_names[i].name && mask_names[i].mask && 
318                  mask_names[i].mask != YLOG_ALL)
319             {
320                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
321                 level &= ~mask_names[i].mask;
322             }
323         }
324
325     va_start(ap, fmt);
326 #ifdef WIN32
327     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
328 #else
329 /* !WIN32 */
330 #if HAVE_VSNPRINTF
331     vsnprintf(buf, sizeof(buf), fmt, ap);
332 #else
333     vsprintf(buf, fmt, ap);
334 #endif
335 #endif
336 /* WIN32 */
337     if (o_level & YLOG_ERRNO)
338     {
339         strcat(buf, " [");
340         yaz_strerror(buf+strlen(buf), 2048);
341         strcat(buf, "]");
342     }
343     va_end (ap);
344     if (start_hook_func)
345         (*start_hook_func)(o_level, buf, start_hook_info);
346     if (l_level & YLOG_NOTIME)
347         tbuf[0] = '\0';
348     else
349         strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
350     tbuf[TIMEFORMAT_LEN-1] = '\0';
351     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
352             l_prefix2, buf);
353     if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
354         fflush(l_file);
355     if (end_hook_func)
356         (*end_hook_func)(o_level, buf, end_hook_info);
357     nmem_mutex_leave(log_mutex);
358 }
359
360 void yaz_log_time_format(const char *fmt)
361 {
362     if ( !fmt || !*fmt) 
363     { /* no format, default to new */
364         l_actual_format = l_new_default_format;
365         return; 
366     }
367     if (0==strcmp(fmt, "old"))
368     { /* force the old format */
369         l_actual_format = l_old_default_format;
370         return; 
371     }
372     /* else use custom format */
373     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
374     l_custom_format[TIMEFORMAT_LEN-1] = '\0';
375     l_actual_format = l_custom_format;
376 }
377
378 /** cleans a loglevel name from leading paths and suffixes */
379 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
380 {
381     char *p = namebuf;
382     char *start = namebuf;
383     if (buflen <= len)
384         len = buflen-1; 
385     strncpy(namebuf, name, len);
386     namebuf[len] = '\0';
387     while ((p = strchr(start, '/')))
388         start = p+1;
389     if ((p = strrchr(start, '.')))
390         *p = '\0';
391     return start;
392 }
393
394 static int define_module_bit(const char *name)
395 {
396     int i;
397     init_mutex();
398     nmem_mutex_enter(log_mutex);
399     for (i = 0; mask_names[i].name; i++)
400         if (0 == strcmp(mask_names[i].name, name))
401         {
402             nmem_mutex_leave(log_mutex);
403             return mask_names[i].mask;
404         }
405     if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
406     {
407         nmem_mutex_leave(log_mutex);
408         yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
409         return 0;
410     }
411     mask_names[i].mask = next_log_bit;
412     next_log_bit = next_log_bit<<1;
413     mask_names[i].name = malloc(strlen(name)+1);
414     strcpy(mask_names[i].name, name);
415     mask_names[i+1].name = NULL;
416     mask_names[i+1].mask = 0;
417     nmem_mutex_leave(log_mutex);
418     return mask_names[i].mask;
419 }
420
421 int yaz_log_module_level(const char *name)
422 {
423     int i;
424     char clean[255];
425     char *n = clean_name(name, strlen(name), clean, sizeof(clean));
426     init_mutex();
427     
428     nmem_mutex_enter(log_mutex);
429     for (i = 0; mask_names[i].name; i++)
430         if (0==strcmp(n, mask_names[i].name))
431         {
432             nmem_mutex_leave(log_mutex);
433             yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
434                     mask_names[i].mask, n, 
435                     strcmp(n,name) ? name : "");
436             return mask_names[i].mask;
437         }
438     nmem_mutex_leave(log_mutex);
439     yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n, 
440                     strcmp(n, name) ? name : "" );
441     return 0;
442 }
443
444 int yaz_log_mask_str(const char *str)
445 {
446     return yaz_log_mask_str_x(str, YLOG_DEFAULT_LEVEL);
447 }
448
449 int yaz_log_mask_str_x(const char *str, int level)
450 {
451     const char *p;
452
453     while (*str)
454     {
455         int negated = 0;
456         for (p = str; *p && *p != ','; p++)
457             ;
458         if (*str=='-')
459         {
460             negated = 1;
461             str++;
462         }
463         if (isdigit(*(unsigned char *) str))
464         {
465             level = atoi(str);
466         }
467         else 
468         {
469             char clean[509];
470             char *n = clean_name(str, p-str, clean, sizeof(clean));
471             int mask = define_module_bit(n);
472             if (!mask)
473                 level = 0;  /* 'none' clears them all */
474             else if (negated)
475                 level &= ~mask;
476             else
477                 level |= mask;
478         }
479         if (*p == ',')
480             p++;
481         str = p;
482     }
483     return level;
484 }
485 /*
486  * Local variables:
487  * c-basic-offset: 4
488  * indent-tabs-mode: nil
489  * End:
490  * vim: shiftwidth=4 tabstop=8 expandtab
491  */
492