2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.40 2006-09-29 15:29:36 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 to 1 GB */
96 static const int l_def_max_size = 1024*1024*1024;
97 static int l_max_size = 1024*1024*1024;
99 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
103 } mask_names[MAX_MASK_NAMES] =
105 { YLOG_FATAL, "fatal"},
106 { YLOG_DEBUG, "debug"},
107 { YLOG_WARN, "warn" },
110 { YLOG_MALLOC, "malloc"},
112 { YLOG_NOTIME, "notime" },
113 { YLOG_APP2, "app2" },
114 { YLOG_APP3, "app3" },
116 { YLOG_FLUSH, "flush" },
117 { YLOG_LOGLVL, "loglevel" },
120 /* the rest will be filled in if the user defines dynamic modules*/
123 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
125 static void init_mutex(void)
129 nmem_mutex_create(&log_mutex);
133 FILE *yaz_log_file(void)
136 switch(yaz_file_type)
138 case use_stderr: f = stderr; break;
139 case use_none: f = 0; break;
140 case use_file: f = yaz_global_log_file; break;
145 void yaz_log_close(void)
147 if (yaz_file_type == use_file && yaz_global_log_file)
149 fclose(yaz_global_log_file);
150 yaz_global_log_file = 0;
154 void yaz_log_init_file(const char *fname)
162 yaz_file_type = use_stderr; /* empty name; use stderr */
164 yaz_file_type = use_file;
165 strncpy(l_fname, fname, sizeof(l_fname)-1);
166 l_fname[sizeof(l_fname)-1] = '\0';
170 yaz_file_type = use_none; /* NULL name; use no file at all */
176 static void rotate_log(const char *cur_fname)
180 /* windows can't rename a file if it is open */
181 fclose(yaz_global_log_file);
182 yaz_global_log_file = 0;
184 for (i = 0; i<100; i++)
186 char fname_str[FILENAME_MAX];
187 struct stat stat_buf;
189 sprintf(fname_str, "%s.%d", cur_fname, i);
190 if (stat(fname_str, &stat_buf) != 0)
195 char fname_str[2][FILENAME_MAX];
198 sprintf(fname_str[0], "%s.%d", cur_fname, i-1);
200 sprintf(fname_str[0], "%s", cur_fname);
201 sprintf(fname_str[1], "%s.%d", cur_fname, i);
203 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
205 rename(fname_str[0], fname_str[1]);
211 void yaz_log_init_level(int level)
215 l_level = default_log_level();
216 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
219 yaz_log_reopen(); /* make sure we set buffering right */
223 if (l_level & YLOG_LOGLVL)
224 { /* dump the log level bits */
225 const char *bittype = "Static ";
228 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
230 /* determine size of mask_names (locked) */
231 nmem_mutex_enter(log_mutex);
232 for (sz = 0; mask_names[sz].name; sz++)
234 nmem_mutex_leave(log_mutex);
235 /* second pass without lock */
236 for (i = 0; i < sz; i++)
237 if (mask_names[i].mask && *mask_names[i].name)
238 if (strcmp(mask_names[i].name, "all") != 0)
240 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
241 bittype, mask_names[i].mask, mask_names[i].name,
242 (level & mask_names[i].mask)? "ON": "off");
243 if (mask_names[i].mask > YLOG_LAST_BIT)
249 void yaz_log_init_prefix(const char *prefix)
251 if (prefix && *prefix)
252 sprintf(l_prefix, "%.511s ", prefix);
257 void yaz_log_init_prefix2(const char *prefix)
259 if (prefix && *prefix)
260 sprintf(l_prefix2, "%.511s ", prefix);
265 void yaz_log_init(int level, const char *prefix, const char *fname)
268 yaz_log_init_level(level);
269 yaz_log_init_prefix(prefix);
271 yaz_log_init_file(fname);
274 void yaz_log_init_max_size(int mx)
277 l_max_size = l_def_max_size;
282 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
288 void log_event_start(void (*func)(int, const char *, void *), void *info)
290 start_hook_func = func;
291 start_hook_info = info;
294 void log_event_end(void (*func)(int, const char *, void *), void *info)
296 end_hook_func = func;
297 end_hook_info = info;
300 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
302 char new_filename[512];
303 static char cur_filename[512] = "";
305 if (l_fname && *l_fname)
307 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
308 if (strcmp(new_filename, cur_filename))
310 strcpy(cur_filename, new_filename);
315 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
317 long flen = ftell(yaz_global_log_file);
318 if (flen > l_max_size)
320 rotate_log(cur_filename);
324 if (force && yaz_file_type == use_file && *cur_filename)
327 yaz_global_log_file = fopen(cur_filename, filemode);
328 if (l_level < 0) l_level = default_log_level();
329 if (l_level & YLOG_FLUSH)
330 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
334 static void yaz_log_do_reopen(const char *filemode)
336 time_t cur_time = time(0);
338 struct tm tm0, *tm = &tm0;
343 nmem_mutex_enter(log_mutex);
345 localtime_r(&cur_time, tm);
347 tm = localtime(&cur_time);
349 yaz_log_open_check(tm, 1, filemode);
350 nmem_mutex_leave(log_mutex);
354 void yaz_log_reopen()
356 yaz_log_do_reopen("a");
361 yaz_log_do_reopen("w");
366 static void yaz_strftime(char *dst, size_t sz,
367 const char *fmt, const struct tm *tm)
369 const char *cp = strstr(fmt, "%!");
370 if (cp && strlen(fmt) < 60)
375 DWORD tid = GetCurrentThreadId();
378 #if YAZ_POSIX_THREADS
379 tid = pthread_self();
382 memcpy(fmt2, fmt, cp-fmt);
384 sprintf(tpidstr, "%08lx", (long) tid);
385 strcat(fmt2, tpidstr);
387 strftime(dst, sz, fmt2, tm);
390 strftime(dst, sz, fmt, tm);
393 static void yaz_log_to_file(int level, const char *log_message)
398 struct tm tm0, *tm = &tm0;
405 nmem_mutex_enter(log_mutex);
408 localtime_r(&ti, tm);
413 yaz_log_open_check(tm, 0, "a");
414 file = yaz_log_file(); /* file may change in yaz_log_open_check */
418 char tbuf[TIMEFORMAT_LEN];
423 for (i = 0; level && mask_names[i].name; i++)
424 if ( mask_names[i].mask & level)
426 if (*mask_names[i].name && mask_names[i].mask &&
427 mask_names[i].mask != YLOG_ALL)
429 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
430 level &= ~mask_names[i].mask;
434 if (l_level & YLOG_NOTIME)
437 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
438 tbuf[TIMEFORMAT_LEN-1] = '\0';
440 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
442 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
445 nmem_mutex_leave(log_mutex);
448 void yaz_log(int level, const char *fmt, ...)
456 l_level = default_log_level();
457 if (!(level & l_level))
461 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
465 vsnprintf(buf, sizeof(buf), fmt, ap);
467 vsprintf(buf, fmt, ap);
471 if (o_level & YLOG_ERRNO)
474 yaz_strerror(buf+strlen(buf), 2048);
479 (*start_hook_func)(o_level, buf, start_hook_info);
481 (*hook_func)(o_level, buf, hook_info);
482 file = yaz_log_file();
484 yaz_log_to_file(level, buf);
486 (*end_hook_func)(o_level, buf, end_hook_info);
489 void yaz_log_time_format(const char *fmt)
492 { /* no format, default to new */
493 l_actual_format = l_new_default_format;
496 if (0==strcmp(fmt, "old"))
497 { /* force the old format */
498 l_actual_format = l_old_default_format;
501 /* else use custom format */
502 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
503 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
504 l_actual_format = l_custom_format;
507 /** cleans a loglevel name from leading paths and suffixes */
508 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
511 char *start = namebuf;
514 strncpy(namebuf, name, len);
516 while ((p = strchr(start, '/')))
518 if ((p = strrchr(start, '.')))
523 static int define_module_bit(const char *name)
527 nmem_mutex_enter(log_mutex);
528 for (i = 0; mask_names[i].name; i++)
529 if (0 == strcmp(mask_names[i].name, name))
531 nmem_mutex_leave(log_mutex);
532 return mask_names[i].mask;
534 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
536 nmem_mutex_leave(log_mutex);
537 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
540 mask_names[i].mask = next_log_bit;
541 next_log_bit = next_log_bit<<1;
542 mask_names[i].name = malloc(strlen(name)+1);
543 strcpy(mask_names[i].name, name);
544 mask_names[i+1].name = NULL;
545 mask_names[i+1].mask = 0;
546 nmem_mutex_leave(log_mutex);
547 return mask_names[i].mask;
550 int yaz_log_module_level(const char *name)
554 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
557 nmem_mutex_enter(log_mutex);
558 for (i = 0; mask_names[i].name; i++)
559 if (0==strcmp(n, mask_names[i].name))
561 nmem_mutex_leave(log_mutex);
562 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
563 mask_names[i].mask, n,
564 strcmp(n,name) ? name : "");
565 return mask_names[i].mask;
567 nmem_mutex_leave(log_mutex);
568 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
569 strcmp(n, name) ? name : "" );
573 int yaz_log_mask_str(const char *str)
575 return yaz_log_mask_str_x(str, default_log_level());
578 int yaz_log_mask_str_x(const char *str, int level)
585 for (p = str; *p && *p != ','; p++)
592 if (isdigit(*(unsigned char *) str))
599 char *n = clean_name(str, p-str, clean, sizeof(clean));
600 int mask = define_module_bit(n);
602 level = 0; /* 'none' clears them all */
617 * indent-tabs-mode: nil
619 * vim: shiftwidth=4 tabstop=8 expandtab