2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.41 2006-10-04 07:42:13 adam Exp $
10 * \brief Logging utility
39 #include <yaz/xmalloc.h>
41 static NMEM_MUTEX log_mutex = 0;
42 static int mutex_init_flag = 0; /* not yet initialized */
44 #define HAS_STRERROR 1
52 extern char *sys_errlist[];
53 return sys_errlist[n];
59 static int default_log_level(void)
61 char *env = getenv("YAZ_LOG");
63 return yaz_log_mask_str_x(env, YLOG_DEFAULT_LEVEL);
65 return YLOG_DEFAULT_LEVEL;
69 static int l_level = -1; /* will be set from default_log_level() */
71 enum l_file_type { use_stderr, use_none, use_file };
72 static enum l_file_type yaz_file_type = use_stderr;
73 static FILE *yaz_global_log_file = NULL;
75 static void (*start_hook_func)(int, const char *, void *) = NULL;
76 static void *start_hook_info;
78 static void (*end_hook_func)(int, const char *, void *) = NULL;
79 static void *end_hook_info;
81 static void (*hook_func)(int, const char *, void *) = NULL;
82 static void *hook_info;
84 static char l_prefix[512] = "";
85 static char l_prefix2[512] = "";
86 static char l_fname[512] = "";
89 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
90 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
91 #define TIMEFORMAT_LEN 50
92 static char l_custom_format[TIMEFORMAT_LEN] = "";
93 static char *l_actual_format = l_old_default_format;
95 /** l_max_size tells when to rotate the log. Default is 1 GB
96 This is almost the same as never, but it saves applications in the
97 case of 2 or 4 GB file size limits..
99 static int l_max_size = 1024*1024*1024;
101 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
105 } mask_names[MAX_MASK_NAMES] =
107 { YLOG_FATAL, "fatal"},
108 { YLOG_DEBUG, "debug"},
109 { YLOG_WARN, "warn" },
112 { YLOG_MALLOC, "malloc"},
114 { YLOG_NOTIME, "notime" },
115 { YLOG_APP2, "app2" },
116 { YLOG_APP3, "app3" },
118 { YLOG_FLUSH, "flush" },
119 { YLOG_LOGLVL, "loglevel" },
122 /* the rest will be filled in if the user defines dynamic modules*/
125 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
127 static void init_mutex(void)
131 nmem_mutex_create(&log_mutex);
135 FILE *yaz_log_file(void)
138 switch(yaz_file_type)
140 case use_stderr: f = stderr; break;
141 case use_none: f = 0; break;
142 case use_file: f = yaz_global_log_file; break;
147 void yaz_log_close(void)
149 if (yaz_file_type == use_file && yaz_global_log_file)
151 fclose(yaz_global_log_file);
152 yaz_global_log_file = 0;
156 void yaz_log_init_file(const char *fname)
164 yaz_file_type = use_stderr; /* empty name; use stderr */
166 yaz_file_type = use_file;
167 strncpy(l_fname, fname, sizeof(l_fname)-1);
168 l_fname[sizeof(l_fname)-1] = '\0';
172 yaz_file_type = use_none; /* NULL name; use no file at all */
178 static void rotate_log(const char *cur_fname)
182 /* windows can't rename a file if it is open */
183 fclose(yaz_global_log_file);
184 yaz_global_log_file = 0;
186 for (i = 0; i<9; i++)
188 char fname_str[FILENAME_MAX];
189 struct stat stat_buf;
191 sprintf(fname_str, "%s.%d", cur_fname, i);
192 if (stat(fname_str, &stat_buf) != 0)
197 char fname_str[2][FILENAME_MAX];
200 sprintf(fname_str[0], "%s.%d", cur_fname, i-1);
202 sprintf(fname_str[0], "%s", cur_fname);
203 sprintf(fname_str[1], "%s.%d", cur_fname, i);
205 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
207 rename(fname_str[0], fname_str[1]);
213 void yaz_log_init_level(int level)
217 l_level = default_log_level();
218 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
221 yaz_log_reopen(); /* make sure we set buffering right */
225 if (l_level & YLOG_LOGLVL)
226 { /* dump the log level bits */
227 const char *bittype = "Static ";
230 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
232 /* determine size of mask_names (locked) */
233 nmem_mutex_enter(log_mutex);
234 for (sz = 0; mask_names[sz].name; sz++)
236 nmem_mutex_leave(log_mutex);
237 /* second pass without lock */
238 for (i = 0; i < sz; i++)
239 if (mask_names[i].mask && *mask_names[i].name)
240 if (strcmp(mask_names[i].name, "all") != 0)
242 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
243 bittype, mask_names[i].mask, mask_names[i].name,
244 (level & mask_names[i].mask)? "ON": "off");
245 if (mask_names[i].mask > YLOG_LAST_BIT)
251 void yaz_log_init_prefix(const char *prefix)
253 if (prefix && *prefix)
254 sprintf(l_prefix, "%.511s ", prefix);
259 void yaz_log_init_prefix2(const char *prefix)
261 if (prefix && *prefix)
262 sprintf(l_prefix2, "%.511s ", prefix);
267 void yaz_log_init(int level, const char *prefix, const char *fname)
270 yaz_log_init_level(level);
271 yaz_log_init_prefix(prefix);
273 yaz_log_init_file(fname);
276 void yaz_log_init_max_size(int mx)
284 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
290 void log_event_start(void (*func)(int, const char *, void *), void *info)
292 start_hook_func = func;
293 start_hook_info = info;
296 void log_event_end(void (*func)(int, const char *, void *), void *info)
298 end_hook_func = func;
299 end_hook_info = info;
302 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
304 char new_filename[512];
305 static char cur_filename[512] = "";
307 if (l_fname && *l_fname)
309 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
310 if (strcmp(new_filename, cur_filename))
312 strcpy(cur_filename, new_filename);
317 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
319 long flen = ftell(yaz_global_log_file);
320 if (flen > l_max_size)
322 rotate_log(cur_filename);
326 if (force && yaz_file_type == use_file && *cur_filename)
329 yaz_global_log_file = fopen(cur_filename, filemode);
330 if (l_level < 0) l_level = default_log_level();
331 if (l_level & YLOG_FLUSH)
332 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
336 static void yaz_log_do_reopen(const char *filemode)
338 time_t cur_time = time(0);
340 struct tm tm0, *tm = &tm0;
345 nmem_mutex_enter(log_mutex);
347 localtime_r(&cur_time, tm);
349 tm = localtime(&cur_time);
351 yaz_log_open_check(tm, 1, filemode);
352 nmem_mutex_leave(log_mutex);
356 void yaz_log_reopen()
358 yaz_log_do_reopen("a");
363 yaz_log_do_reopen("w");
368 static void yaz_strftime(char *dst, size_t sz,
369 const char *fmt, const struct tm *tm)
371 const char *cp = strstr(fmt, "%!");
372 if (cp && strlen(fmt) < 60)
377 DWORD tid = GetCurrentThreadId();
380 #if YAZ_POSIX_THREADS
381 tid = pthread_self();
384 memcpy(fmt2, fmt, cp-fmt);
386 sprintf(tpidstr, "%08lx", (long) tid);
387 strcat(fmt2, tpidstr);
389 strftime(dst, sz, fmt2, tm);
392 strftime(dst, sz, fmt, tm);
395 static void yaz_log_to_file(int level, const char *log_message)
400 struct tm tm0, *tm = &tm0;
407 nmem_mutex_enter(log_mutex);
410 localtime_r(&ti, tm);
415 yaz_log_open_check(tm, 0, "a");
416 file = yaz_log_file(); /* file may change in yaz_log_open_check */
420 char tbuf[TIMEFORMAT_LEN];
425 for (i = 0; level && mask_names[i].name; i++)
426 if ( mask_names[i].mask & level)
428 if (*mask_names[i].name && mask_names[i].mask &&
429 mask_names[i].mask != YLOG_ALL)
431 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
432 level &= ~mask_names[i].mask;
436 if (l_level & YLOG_NOTIME)
439 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
440 tbuf[TIMEFORMAT_LEN-1] = '\0';
442 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
444 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
447 nmem_mutex_leave(log_mutex);
450 void yaz_log(int level, const char *fmt, ...)
458 l_level = default_log_level();
459 if (!(level & l_level))
463 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
467 vsnprintf(buf, sizeof(buf), fmt, ap);
469 vsprintf(buf, fmt, ap);
473 if (o_level & YLOG_ERRNO)
476 yaz_strerror(buf+strlen(buf), 2048);
481 (*start_hook_func)(o_level, buf, start_hook_info);
483 (*hook_func)(o_level, buf, hook_info);
484 file = yaz_log_file();
486 yaz_log_to_file(level, buf);
488 (*end_hook_func)(o_level, buf, end_hook_info);
491 void yaz_log_time_format(const char *fmt)
494 { /* no format, default to new */
495 l_actual_format = l_new_default_format;
498 if (0==strcmp(fmt, "old"))
499 { /* force the old format */
500 l_actual_format = l_old_default_format;
503 /* else use custom format */
504 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
505 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
506 l_actual_format = l_custom_format;
509 /** cleans a loglevel name from leading paths and suffixes */
510 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
513 char *start = namebuf;
516 strncpy(namebuf, name, len);
518 while ((p = strchr(start, '/')))
520 if ((p = strrchr(start, '.')))
525 static int define_module_bit(const char *name)
529 nmem_mutex_enter(log_mutex);
530 for (i = 0; mask_names[i].name; i++)
531 if (0 == strcmp(mask_names[i].name, name))
533 nmem_mutex_leave(log_mutex);
534 return mask_names[i].mask;
536 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
538 nmem_mutex_leave(log_mutex);
539 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
542 mask_names[i].mask = next_log_bit;
543 next_log_bit = next_log_bit<<1;
544 mask_names[i].name = malloc(strlen(name)+1);
545 strcpy(mask_names[i].name, name);
546 mask_names[i+1].name = NULL;
547 mask_names[i+1].mask = 0;
548 nmem_mutex_leave(log_mutex);
549 return mask_names[i].mask;
552 int yaz_log_module_level(const char *name)
556 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
559 nmem_mutex_enter(log_mutex);
560 for (i = 0; mask_names[i].name; i++)
561 if (0==strcmp(n, mask_names[i].name))
563 nmem_mutex_leave(log_mutex);
564 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
565 mask_names[i].mask, n,
566 strcmp(n,name) ? name : "");
567 return mask_names[i].mask;
569 nmem_mutex_leave(log_mutex);
570 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
571 strcmp(n, name) ? name : "" );
575 int yaz_log_mask_str(const char *str)
577 return yaz_log_mask_str_x(str, default_log_level());
580 int yaz_log_mask_str_x(const char *str, int level)
587 for (p = str; *p && *p != ','; p++)
594 if (isdigit(*(unsigned char *) str))
601 char *n = clean_name(str, p-str, clean, sizeof(clean));
602 int mask = define_module_bit(n);
604 level = 0; /* 'none' clears them all */
619 * indent-tabs-mode: nil
621 * vim: shiftwidth=4 tabstop=8 expandtab