1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
8 * \brief Logging utility
29 #include <yaz/yaz-iconv.h>
30 #include <yaz/errno.h>
31 #include <yaz/thread_id.h>
33 #include <yaz/mutex.h>
34 #include <yaz/snprintf.h>
35 #include <yaz/xmalloc.h>
40 static int l_level = YLOG_DEFAULT_LEVEL;
42 enum l_file_type { use_stderr, use_none, use_file };
45 enum l_file_type type;
51 use_stderr, 0, "", "", ""
54 static void (*start_hook_func)(int, const char *, void *) = NULL;
55 static void *start_hook_info;
57 static void (*end_hook_func)(int, const char *, void *) = NULL;
58 static void *end_hook_info;
60 static void (*hook_func)(int, const char *, void *) = NULL;
61 static void *hook_info;
63 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
64 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
65 #define TIMEFORMAT_LEN 50
67 static char l_custom_format[TIMEFORMAT_LEN] = "";
68 static char *l_actual_format = l_old_default_format;
70 /** l_max_size tells when to rotate the log. The default value is
71 0 which means DISABLED. This is to be preffered if YAZ runs
72 as a server using logrotate etc.
73 A positive size specifies the file size in bytes when a log rotate
74 will occur. Note that in order for this to work YAZ must have
77 static int l_max_size = 0;
79 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
83 } mask_names[MAX_MASK_NAMES] =
85 { YLOG_FATAL, "fatal"},
86 { YLOG_DEBUG, "debug"},
87 { YLOG_WARN, "warn" },
90 { YLOG_MALLOC, "malloc"},
93 { YLOG_NOTIME, "notime" },
94 { YLOG_APP2, "app2" },
95 { YLOG_APP3, "app3" },
97 { YLOG_FLUSH, "flush" },
98 { YLOG_LOGLVL, "loglevel" },
101 /* the rest will be filled in if the user defines dynamic modules*/
104 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
106 static int yaz_log_reopen_flag = 0;
108 static YAZ_MUTEX log_mutex = 0;
110 static void yaz_log_open(void);
112 void yaz_log_lock(void)
114 yaz_mutex_enter(log_mutex);
117 void yaz_log_unlock(void)
119 yaz_mutex_leave(log_mutex);
122 void yaz_log_init_globals(void)
127 yaz_mutex_create(&log_mutex);
128 #if YAZ_POSIX_THREADS
129 pthread_atfork(yaz_log_lock, yaz_log_unlock, yaz_log_unlock);
131 env = getenv("YAZ_LOG");
133 l_level = yaz_log_mask_str_x(env, l_level);
136 FILE *yaz_log_file(void)
139 switch (yaz_log_info.type)
141 case use_stderr: f = stderr; break;
142 case use_none: f = 0; break;
143 case use_file: f = yaz_log_info.log_file; break;
148 void yaz_log_close(void)
150 if (yaz_log_info.type == use_file && yaz_log_info.log_file)
152 fclose(yaz_log_info.log_file);
153 yaz_log_info.log_file = 0;
157 void yaz_log_init_file(const char *fname)
165 yaz_log_info.type = use_stderr; /* empty name; use stderr */
167 yaz_log_info.type = use_file;
168 strncpy(yaz_log_info.l_fname, fname, sizeof(yaz_log_info.l_fname)-1);
169 yaz_log_info.l_fname[sizeof(yaz_log_info.l_fname)-1] = '\0';
173 yaz_log_info.type = use_none; /* NULL name; use no file at all */
174 yaz_log_info.l_fname[0] = '\0';
179 static void rotate_log(const char *cur_fname)
184 /* windows can't rename a file if it is open */
187 for (i = 0; i<9; i++)
189 char fname_str[FILENAME_MAX];
190 struct stat stat_buf;
192 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
193 if (stat(fname_str, &stat_buf) != 0)
198 char fname_str[2][FILENAME_MAX];
201 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
202 "%s.%d", cur_fname, i-1);
204 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
206 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
207 "%s.%d", cur_fname, i);
209 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
211 rename(fname_str[0], fname_str[1]);
217 void yaz_log_init_level(int level)
220 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
223 yaz_log_open(); /* make sure we set buffering right */
228 if (l_level & YLOG_LOGLVL)
229 { /* dump the log level bits */
230 const char *bittype = "Static ";
233 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
235 /* determine size of mask_names (locked) */
236 for (sz = 0; mask_names[sz].name; sz++)
238 /* second pass without lock */
239 for (i = 0; i < sz; i++)
240 if (mask_names[i].mask && *mask_names[i].name)
241 if (strcmp(mask_names[i].name, "all") != 0)
243 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
244 bittype, mask_names[i].mask, mask_names[i].name,
245 (level & mask_names[i].mask)? "ON": "off");
246 if (mask_names[i].mask > YLOG_LAST_BIT)
252 void yaz_log_init_prefix(const char *prefix)
254 if (prefix && *prefix)
255 yaz_snprintf(yaz_log_info.l_prefix,
256 sizeof(yaz_log_info.l_prefix), "%s ", prefix);
258 *yaz_log_info.l_prefix = 0;
261 void yaz_log_init_prefix2(const char *prefix)
263 if (prefix && *prefix)
264 yaz_snprintf(yaz_log_info.l_prefix2,
265 sizeof(yaz_log_info.l_prefix2), "%s ", prefix);
267 *yaz_log_info.l_prefix2 = 0;
270 void yaz_log_init(int level, const char *prefix, const char *fname)
273 yaz_log_init_level(level);
274 yaz_log_init_prefix(prefix);
276 yaz_log_init_file(fname);
279 void yaz_log_init_max_size(int mx)
287 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
293 void log_event_start(void (*func)(int, const char *, void *), void *info)
295 start_hook_func = func;
296 start_hook_info = info;
299 void log_event_end(void (*func)(int, const char *, void *), void *info)
301 end_hook_func = func;
302 end_hook_info = info;
305 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
307 char new_filename[512];
308 static char cur_filename[512] = "";
310 if (yaz_log_info.type != use_file)
313 if (yaz_log_reopen_flag)
316 yaz_log_reopen_flag = 0;
318 if (*yaz_log_info.l_fname)
320 strftime(new_filename, sizeof(new_filename)-1, yaz_log_info.l_fname,
322 if (strcmp(new_filename, cur_filename))
324 strcpy(cur_filename, new_filename);
329 if (l_max_size > 0 && yaz_log_info.log_file)
331 long flen = ftell(yaz_log_info.log_file);
332 if (flen > l_max_size)
334 rotate_log(cur_filename);
338 if (force && *cur_filename)
344 if (!strncmp(cur_filename, "fd=", 3))
345 new_file = fdopen(atoi(cur_filename + 3), filemode);
347 new_file = fopen(cur_filename, filemode);
351 yaz_log_info.log_file = new_file;
355 /* disable log rotate */
361 static void yaz_log_do_reopen(const char *filemode)
363 time_t cur_time = time(0);
365 struct tm tm0, *tm = &tm0;
372 localtime_r(&cur_time, tm);
374 tm = localtime(&cur_time);
376 yaz_log_open_check(tm, 1, filemode);
380 void yaz_log_reopen()
382 yaz_log_reopen_flag = 1;
385 static void yaz_log_open()
387 yaz_log_do_reopen("a");
392 yaz_log_do_reopen("w");
395 static void yaz_strftime(char *dst, size_t sz,
396 const char *fmt, const struct tm *tm)
398 strftime(dst, sz, fmt, tm);
401 static void yaz_log_to_file(int level, const char *fmt, va_list ap,
402 const char *error_cp)
407 struct tm tm0, *tm = &tm0;
414 localtime_r(&ti, tm);
419 yaz_log_open_check(tm, 0, "a");
420 file = yaz_log_file(); /* file may change in yaz_log_open_check */
424 char tbuf[TIMEFORMAT_LEN];
430 for (i = 0; level && mask_names[i].name; i++)
431 if ( mask_names[i].mask & level)
433 if (*mask_names[i].name && mask_names[i].mask &&
434 mask_names[i].mask != YLOG_ALL)
436 if (strlen(flags) + strlen(mask_names[i].name)
440 strcat(flags, mask_names[i].name);
443 level &= ~mask_names[i].mask;
448 if (!(l_level & YLOG_NOTIME))
450 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
451 tbuf[TIMEFORMAT_LEN-2] = '\0';
457 if (l_level & YLOG_TID)
459 yaz_thread_id_cstr(tid, sizeof(tid)-1);
464 fprintf(file, "%s%s%s%s %s", tbuf, yaz_log_info.l_prefix,
465 tid, flags, yaz_log_info.l_prefix2);
466 vfprintf(file, fmt, ap);
468 fprintf(file, " [%s]", error_cp);
470 if (l_level & YLOG_FLUSH)
476 void yaz_log(int level, const char *fmt, ...)
481 char *error_cp = 0, error_buf[128];
483 if (o_level & YLOG_ERRNO)
485 yaz_strerror(error_buf, sizeof(error_buf));
486 error_cp = error_buf;
489 if (!(level & l_level))
493 file = yaz_log_file();
494 if (start_hook_func || hook_func || end_hook_func)
497 /* 30 is enough for our 'rest of output' message */
498 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
499 if (strlen(buf) >= sizeof(buf)-31)
500 strcat(buf, " [rest of output omitted]");
502 (*start_hook_func)(o_level, buf, start_hook_info);
504 (*hook_func)(o_level, buf, hook_info);
506 yaz_log_to_file(level, fmt, ap, error_cp);
508 (*end_hook_func)(o_level, buf, end_hook_info);
513 yaz_log_to_file(level, fmt, ap, error_cp);
518 void yaz_log_time_format(const char *fmt)
521 { /* no format, default to new */
522 l_actual_format = l_new_default_format;
525 if (0==strcmp(fmt, "old"))
526 { /* force the old format */
527 l_actual_format = l_old_default_format;
530 /* else use custom format */
531 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
532 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
533 l_actual_format = l_custom_format;
536 /** cleans a loglevel name from leading paths and suffixes */
537 static char *clean_name(const char *name, size_t len, char *namebuf, size_t buflen)
540 char *start = namebuf;
543 strncpy(namebuf, name, len);
545 while ((p = strchr(start, '/')))
547 if ((p = strrchr(start, '.')))
552 static int define_module_bit(const char *name)
556 for (i = 0; mask_names[i].name; i++)
557 if (0 == strcmp(mask_names[i].name, name))
559 return mask_names[i].mask;
561 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1U<<31) ))
563 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
566 mask_names[i].mask = (int) next_log_bit; /* next_log_bit can hold int */
567 next_log_bit = next_log_bit<<1;
568 mask_names[i].name = (char *) malloc(strlen(name)+1);
569 strcpy(mask_names[i].name, name);
570 mask_names[i+1].name = NULL;
571 mask_names[i+1].mask = 0;
572 return mask_names[i].mask;
575 int yaz_log_module_level(const char *name)
579 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
582 for (i = 0; mask_names[i].name; i++)
583 if (0==strcmp(n, mask_names[i].name))
585 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
586 mask_names[i].mask, n,
587 strcmp(n,name) ? name : "");
588 return mask_names[i].mask;
590 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
591 strcmp(n, name) ? name : "" );
595 int yaz_log_mask_str(const char *str)
597 yaz_init_globals(); /* since l_level may be affected */
598 return yaz_log_mask_str_x(str, l_level);
601 /* this function is called by yaz_log_init_globals & yaz_init_globals
602 and, thus, may not call any of them indirectly */
603 int yaz_log_mask_str_x(const char *str, int level)
610 for (p = str; *p && *p != ','; p++)
617 if (yaz_isdigit(*str))
624 char *n = clean_name(str, (size_t) (p - str), clean, sizeof(clean));
625 int mask = define_module_bit(n);
627 level = 0; /* 'none' clears them all */
642 * c-file-style: "Stroustrup"
643 * indent-tabs-mode: nil
645 * vim: shiftwidth=4 tabstop=8 expandtab