2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.45 2007-01-03 08:42:15 adam Exp $
10 * \brief Logging utility
39 #include <yaz/xmalloc.h>
41 static NMEM_MUTEX log_mutex = 0;
43 #define HAS_STRERROR 1
51 extern char *sys_errlist[];
52 return sys_errlist[n];
58 static int l_level = YLOG_DEFAULT_LEVEL;
60 enum l_file_type { use_stderr, use_none, use_file };
61 static enum l_file_type yaz_file_type = use_stderr;
62 static FILE *yaz_global_log_file = NULL;
64 static void (*start_hook_func)(int, const char *, void *) = NULL;
65 static void *start_hook_info;
67 static void (*end_hook_func)(int, const char *, void *) = NULL;
68 static void *end_hook_info;
70 static void (*hook_func)(int, const char *, void *) = NULL;
71 static void *hook_info;
73 static char l_prefix[512] = "";
74 static char l_prefix2[512] = "";
75 static char l_fname[512] = "";
78 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
79 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
80 #define TIMEFORMAT_LEN 50
81 static char l_custom_format[TIMEFORMAT_LEN] = "";
82 static char *l_actual_format = l_old_default_format;
84 /** l_max_size tells when to rotate the log. Default is 1 GB
85 This is almost the same as never, but it saves applications in the
86 case of 2 or 4 GB file size limits..
88 static int l_max_size = 1024*1024*1024;
90 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
94 } mask_names[MAX_MASK_NAMES] =
96 { YLOG_FATAL, "fatal"},
97 { YLOG_DEBUG, "debug"},
98 { YLOG_WARN, "warn" },
101 { YLOG_MALLOC, "malloc"},
103 { YLOG_NOTIME, "notime" },
104 { YLOG_APP2, "app2" },
105 { YLOG_APP3, "app3" },
107 { YLOG_FLUSH, "flush" },
108 { YLOG_LOGLVL, "loglevel" },
111 /* the rest will be filled in if the user defines dynamic modules*/
114 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
116 static void internal_log_init(void)
118 static int mutex_init_flag = 0; /* not yet initialized */
123 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
125 nmem_mutex_create(&log_mutex);
127 env = getenv("YAZ_LOG");
129 l_level = yaz_log_mask_str_x(env, l_level);
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)
181 /* windows can't rename a file if it is open */
184 for (i = 0; i<9; 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)
214 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
217 yaz_log_reopen(); /* make sure we set buffering right */
222 if (l_level & YLOG_LOGLVL)
223 { /* dump the log level bits */
224 const char *bittype = "Static ";
227 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
229 /* determine size of mask_names (locked) */
230 nmem_mutex_enter(log_mutex);
231 for (sz = 0; mask_names[sz].name; sz++)
233 nmem_mutex_leave(log_mutex);
234 /* second pass without lock */
235 for (i = 0; i < sz; i++)
236 if (mask_names[i].mask && *mask_names[i].name)
237 if (strcmp(mask_names[i].name, "all") != 0)
239 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
240 bittype, mask_names[i].mask, mask_names[i].name,
241 (level & mask_names[i].mask)? "ON": "off");
242 if (mask_names[i].mask > YLOG_LAST_BIT)
248 void yaz_log_init_prefix(const char *prefix)
250 if (prefix && *prefix)
251 sprintf(l_prefix, "%.511s ", prefix);
256 void yaz_log_init_prefix2(const char *prefix)
258 if (prefix && *prefix)
259 sprintf(l_prefix2, "%.511s ", prefix);
264 void yaz_log_init(int level, const char *prefix, const char *fname)
267 yaz_log_init_level(level);
268 yaz_log_init_prefix(prefix);
270 yaz_log_init_file(fname);
273 void yaz_log_init_max_size(int mx)
281 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
287 void log_event_start(void (*func)(int, const char *, void *), void *info)
289 start_hook_func = func;
290 start_hook_info = info;
293 void log_event_end(void (*func)(int, const char *, void *), void *info)
295 end_hook_func = func;
296 end_hook_info = info;
299 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
301 char new_filename[512];
302 static char cur_filename[512] = "";
304 if (yaz_file_type != use_file)
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)
319 long flen = ftell(yaz_global_log_file);
320 if (flen > l_max_size)
322 rotate_log(cur_filename);
326 if (force && *cur_filename)
332 new_file = fopen(cur_filename, filemode);
336 yaz_global_log_file = new_file;
337 if (l_level & YLOG_FLUSH)
338 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
342 /* disable log rotate */
348 static void yaz_log_do_reopen(const char *filemode)
350 time_t cur_time = time(0);
352 struct tm tm0, *tm = &tm0;
357 nmem_mutex_enter(log_mutex);
359 localtime_r(&cur_time, tm);
361 tm = localtime(&cur_time);
363 yaz_log_open_check(tm, 1, filemode);
364 nmem_mutex_leave(log_mutex);
368 void yaz_log_reopen()
370 yaz_log_do_reopen("a");
375 yaz_log_do_reopen("w");
380 static void yaz_strftime(char *dst, size_t sz,
381 const char *fmt, const struct tm *tm)
383 const char *cp = strstr(fmt, "%!");
384 if (cp && strlen(fmt) < 60)
389 DWORD tid = GetCurrentThreadId();
392 #if YAZ_POSIX_THREADS
393 tid = pthread_self();
396 memcpy(fmt2, fmt, cp-fmt);
398 sprintf(tpidstr, "%08lx", (long) tid);
399 strcat(fmt2, tpidstr);
401 strftime(dst, sz, fmt2, tm);
404 strftime(dst, sz, fmt, tm);
407 static void yaz_log_to_file(int level, const char *log_message)
412 struct tm tm0, *tm = &tm0;
419 nmem_mutex_enter(log_mutex);
422 localtime_r(&ti, tm);
427 yaz_log_open_check(tm, 0, "a");
428 file = yaz_log_file(); /* file may change in yaz_log_open_check */
432 char tbuf[TIMEFORMAT_LEN];
437 for (i = 0; level && mask_names[i].name; i++)
438 if ( mask_names[i].mask & level)
440 if (*mask_names[i].name && mask_names[i].mask &&
441 mask_names[i].mask != YLOG_ALL)
443 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
444 level &= ~mask_names[i].mask;
448 if (l_level & YLOG_NOTIME)
451 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
452 tbuf[TIMEFORMAT_LEN-1] = '\0';
454 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
456 if (l_level & YLOG_FLUSH)
459 nmem_mutex_leave(log_mutex);
462 void yaz_log(int level, const char *fmt, ...)
470 if (!(level & l_level))
474 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
478 vsnprintf(buf, sizeof(buf), fmt, ap);
480 vsprintf(buf, fmt, ap);
484 if (o_level & YLOG_ERRNO)
487 yaz_strerror(buf+strlen(buf), 2048);
492 (*start_hook_func)(o_level, buf, start_hook_info);
494 (*hook_func)(o_level, buf, hook_info);
495 file = yaz_log_file();
497 yaz_log_to_file(level, buf);
499 (*end_hook_func)(o_level, buf, end_hook_info);
502 void yaz_log_time_format(const char *fmt)
505 { /* no format, default to new */
506 l_actual_format = l_new_default_format;
509 if (0==strcmp(fmt, "old"))
510 { /* force the old format */
511 l_actual_format = l_old_default_format;
514 /* else use custom format */
515 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
516 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
517 l_actual_format = l_custom_format;
520 /** cleans a loglevel name from leading paths and suffixes */
521 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
524 char *start = namebuf;
527 strncpy(namebuf, name, len);
529 while ((p = strchr(start, '/')))
531 if ((p = strrchr(start, '.')))
536 static int define_module_bit(const char *name)
540 nmem_mutex_enter(log_mutex);
541 for (i = 0; mask_names[i].name; i++)
542 if (0 == strcmp(mask_names[i].name, name))
544 nmem_mutex_leave(log_mutex);
545 return mask_names[i].mask;
547 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) ))
549 nmem_mutex_leave(log_mutex);
550 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
553 mask_names[i].mask = next_log_bit;
554 next_log_bit = next_log_bit<<1;
555 mask_names[i].name = malloc(strlen(name)+1);
556 strcpy(mask_names[i].name, name);
557 mask_names[i+1].name = NULL;
558 mask_names[i+1].mask = 0;
559 nmem_mutex_leave(log_mutex);
560 return mask_names[i].mask;
563 int yaz_log_module_level(const char *name)
567 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
570 nmem_mutex_enter(log_mutex);
571 for (i = 0; mask_names[i].name; i++)
572 if (0==strcmp(n, mask_names[i].name))
574 nmem_mutex_leave(log_mutex);
575 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
576 mask_names[i].mask, n,
577 strcmp(n,name) ? name : "");
578 return mask_names[i].mask;
580 nmem_mutex_leave(log_mutex);
581 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
582 strcmp(n, name) ? name : "" );
586 int yaz_log_mask_str(const char *str)
588 internal_log_init(); /* since l_level may be affected */
589 return yaz_log_mask_str_x(str, l_level);
592 int yaz_log_mask_str_x(const char *str, int level)
600 for (p = str; *p && *p != ','; p++)
607 if (isdigit(*(unsigned char *) str))
614 char *n = clean_name(str, p-str, clean, sizeof(clean));
615 int mask = define_module_bit(n);
617 level = 0; /* 'none' clears them all */
632 * indent-tabs-mode: nil
634 * vim: shiftwidth=4 tabstop=8 expandtab