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 YAZ_MUTEX log_mutex = 0;
108 static void yaz_log_lock(void)
110 yaz_mutex_enter(log_mutex);
113 static void yaz_log_unlock(void)
115 yaz_mutex_leave(log_mutex);
118 void yaz_log_init_globals(void)
123 yaz_mutex_create(&log_mutex);
124 #if YAZ_POSIX_THREADS
125 pthread_atfork(yaz_log_lock, yaz_log_unlock, yaz_log_unlock);
127 env = getenv("YAZ_LOG");
129 l_level = yaz_log_mask_str_x(env, l_level);
132 FILE *yaz_log_file(void)
135 switch (yaz_log_info.type)
137 case use_stderr: f = stderr; break;
138 case use_none: f = 0; break;
139 case use_file: f = yaz_log_info.log_file; break;
144 void yaz_log_close(void)
146 if (yaz_log_info.type == use_file && yaz_log_info.log_file)
148 fclose(yaz_log_info.log_file);
149 yaz_log_info.log_file = 0;
153 void yaz_log_init_file(const char *fname)
161 yaz_log_info.type = use_stderr; /* empty name; use stderr */
163 yaz_log_info.type = use_file;
164 strncpy(yaz_log_info.l_fname, fname, sizeof(yaz_log_info.l_fname)-1);
165 yaz_log_info.l_fname[sizeof(yaz_log_info.l_fname)-1] = '\0';
169 yaz_log_info.type = use_none; /* NULL name; use no file at all */
170 yaz_log_info.l_fname[0] = '\0';
175 static void rotate_log(const char *cur_fname)
180 /* windows can't rename a file if it is open */
183 for (i = 0; i<9; i++)
185 char fname_str[FILENAME_MAX];
186 struct stat stat_buf;
188 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
189 if (stat(fname_str, &stat_buf) != 0)
194 char fname_str[2][FILENAME_MAX];
197 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
198 "%s.%d", cur_fname, i-1);
200 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
202 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
203 "%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)
216 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
219 yaz_log_reopen(); /* make sure we set buffering right */
224 if (l_level & YLOG_LOGLVL)
225 { /* dump the log level bits */
226 const char *bittype = "Static ";
229 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
231 /* determine size of mask_names (locked) */
232 for (sz = 0; mask_names[sz].name; sz++)
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 yaz_snprintf(yaz_log_info.l_prefix,
252 sizeof(yaz_log_info.l_prefix), "%s ", prefix);
254 *yaz_log_info.l_prefix = 0;
257 void yaz_log_init_prefix2(const char *prefix)
259 if (prefix && *prefix)
260 yaz_snprintf(yaz_log_info.l_prefix2,
261 sizeof(yaz_log_info.l_prefix2), "%s ", prefix);
263 *yaz_log_info.l_prefix2 = 0;
266 void yaz_log_init(int level, const char *prefix, const char *fname)
269 yaz_log_init_level(level);
270 yaz_log_init_prefix(prefix);
272 yaz_log_init_file(fname);
275 void yaz_log_init_max_size(int mx)
283 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
289 void log_event_start(void (*func)(int, const char *, void *), void *info)
291 start_hook_func = func;
292 start_hook_info = info;
295 void log_event_end(void (*func)(int, const char *, void *), void *info)
297 end_hook_func = func;
298 end_hook_info = info;
301 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
303 char new_filename[512];
304 static char cur_filename[512] = "";
306 if (yaz_log_info.type != use_file)
309 if (*yaz_log_info.l_fname)
311 strftime(new_filename, sizeof(new_filename)-1, yaz_log_info.l_fname,
313 if (strcmp(new_filename, cur_filename))
315 strcpy(cur_filename, new_filename);
320 if (l_max_size > 0 && yaz_log_info.log_file)
322 long flen = ftell(yaz_log_info.log_file);
323 if (flen > l_max_size)
325 rotate_log(cur_filename);
329 if (force && *cur_filename)
335 if (!strncmp(cur_filename, "fd=", 3))
336 new_file = fdopen(atoi(cur_filename + 3), filemode);
338 new_file = fopen(cur_filename, filemode);
342 yaz_log_info.log_file = new_file;
343 if (l_level & YLOG_FLUSH)
344 setvbuf(yaz_log_info.log_file, 0, _IONBF, 0);
348 /* disable log rotate */
354 static void yaz_log_do_reopen(const char *filemode)
356 time_t cur_time = time(0);
358 struct tm tm0, *tm = &tm0;
365 localtime_r(&cur_time, tm);
367 tm = localtime(&cur_time);
369 yaz_log_open_check(tm, 1, filemode);
374 void yaz_log_reopen()
376 yaz_log_do_reopen("a");
381 yaz_log_do_reopen("w");
384 static void yaz_strftime(char *dst, size_t sz,
385 const char *fmt, const struct tm *tm)
387 strftime(dst, sz, fmt, tm);
390 static void yaz_log_to_file(int level, const char *fmt, va_list ap,
391 const char *error_cp)
396 struct tm tm0, *tm = &tm0;
403 localtime_r(&ti, tm);
408 yaz_log_open_check(tm, 0, "a");
409 file = yaz_log_file(); /* file may change in yaz_log_open_check */
413 char tbuf[TIMEFORMAT_LEN];
419 for (i = 0; level && mask_names[i].name; i++)
420 if ( mask_names[i].mask & level)
422 if (*mask_names[i].name && mask_names[i].mask &&
423 mask_names[i].mask != YLOG_ALL)
425 if (strlen(flags) + strlen(mask_names[i].name)
429 strcat(flags, mask_names[i].name);
432 level &= ~mask_names[i].mask;
437 if (!(l_level & YLOG_NOTIME))
439 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
440 tbuf[TIMEFORMAT_LEN-2] = '\0';
446 if (l_level & YLOG_TID)
448 yaz_thread_id_cstr(tid, sizeof(tid)-1);
453 fprintf(file, "%s%s%s%s %s", tbuf, yaz_log_info.l_prefix,
454 tid, flags, yaz_log_info.l_prefix2);
455 vfprintf(file, fmt, ap);
457 fprintf(file, " [%s]", error_cp);
459 if (l_level & YLOG_FLUSH)
465 void yaz_log(int level, const char *fmt, ...)
470 char *error_cp = 0, error_buf[128];
472 if (o_level & YLOG_ERRNO)
474 yaz_strerror(error_buf, sizeof(error_buf));
475 error_cp = error_buf;
478 if (!(level & l_level))
482 file = yaz_log_file();
483 if (start_hook_func || hook_func || end_hook_func)
486 /* 30 is enough for our 'rest of output' message */
487 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
488 if (strlen(buf) >= sizeof(buf)-31)
489 strcat(buf, " [rest of output omitted]");
491 (*start_hook_func)(o_level, buf, start_hook_info);
493 (*hook_func)(o_level, buf, hook_info);
495 yaz_log_to_file(level, fmt, ap, error_cp);
497 (*end_hook_func)(o_level, buf, end_hook_info);
502 yaz_log_to_file(level, fmt, ap, error_cp);
507 void yaz_log_time_format(const char *fmt)
510 { /* no format, default to new */
511 l_actual_format = l_new_default_format;
514 if (0==strcmp(fmt, "old"))
515 { /* force the old format */
516 l_actual_format = l_old_default_format;
519 /* else use custom format */
520 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
521 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
522 l_actual_format = l_custom_format;
525 /** cleans a loglevel name from leading paths and suffixes */
526 static char *clean_name(const char *name, size_t len, char *namebuf, size_t buflen)
529 char *start = namebuf;
532 strncpy(namebuf, name, len);
534 while ((p = strchr(start, '/')))
536 if ((p = strrchr(start, '.')))
541 static int define_module_bit(const char *name)
545 for (i = 0; mask_names[i].name; i++)
546 if (0 == strcmp(mask_names[i].name, name))
548 return mask_names[i].mask;
550 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1U<<31) ))
552 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
555 mask_names[i].mask = (int) next_log_bit; /* next_log_bit can hold int */
556 next_log_bit = next_log_bit<<1;
557 mask_names[i].name = (char *) malloc(strlen(name)+1);
558 strcpy(mask_names[i].name, name);
559 mask_names[i+1].name = NULL;
560 mask_names[i+1].mask = 0;
561 return mask_names[i].mask;
564 int yaz_log_module_level(const char *name)
568 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
571 for (i = 0; mask_names[i].name; i++)
572 if (0==strcmp(n, mask_names[i].name))
574 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
575 mask_names[i].mask, n,
576 strcmp(n,name) ? name : "");
577 return mask_names[i].mask;
579 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
580 strcmp(n, name) ? name : "" );
584 int yaz_log_mask_str(const char *str)
586 yaz_init_globals(); /* since l_level may be affected */
587 return yaz_log_mask_str_x(str, l_level);
590 /* this function is called by yaz_log_init_globals & yaz_init_globals
591 and, thus, may not call any of them indirectly */
592 int yaz_log_mask_str_x(const char *str, int level)
599 for (p = str; *p && *p != ','; p++)
606 if (yaz_isdigit(*str))
613 char *n = clean_name(str, (size_t) (p - str), clean, sizeof(clean));
614 int mask = define_module_bit(n);
616 level = 0; /* 'none' clears them all */
631 * c-file-style: "Stroustrup"
632 * indent-tabs-mode: nil
634 * vim: shiftwidth=4 tabstop=8 expandtab