1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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/snprintf.h>
34 #include <yaz/xmalloc.h>
36 static int l_level = YLOG_DEFAULT_LEVEL;
38 enum l_file_type { use_stderr, use_none, use_file };
41 enum l_file_type type;
47 use_stderr, 0, "", "", ""
50 static void (*start_hook_func)(int, const char *, void *) = NULL;
51 static void *start_hook_info;
53 static void (*end_hook_func)(int, const char *, void *) = NULL;
54 static void *end_hook_info;
56 static void (*hook_func)(int, const char *, void *) = NULL;
57 static void *hook_info;
59 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
60 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
61 #define TIMEFORMAT_LEN 50
63 static char l_custom_format[TIMEFORMAT_LEN] = "";
64 static char *l_actual_format = l_old_default_format;
66 /** l_max_size tells when to rotate the log. The default value is
67 0 which means DISABLED. This is to be preffered if YAZ runs
68 as a server using logrotate etc.
69 A positive size specifies the file size in bytes when a log rotate
70 will occur. Note that in order for this to work YAZ must have
73 static int l_max_size = 0;
75 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
79 } mask_names[MAX_MASK_NAMES] =
81 { YLOG_FATAL, "fatal"},
82 { YLOG_DEBUG, "debug"},
83 { YLOG_WARN, "warn" },
86 { YLOG_MALLOC, "malloc"},
89 { YLOG_NOTIME, "notime" },
90 { YLOG_APP2, "app2" },
91 { YLOG_APP3, "app3" },
93 { YLOG_FLUSH, "flush" },
94 { YLOG_LOGLVL, "loglevel" },
97 /* the rest will be filled in if the user defines dynamic modules*/
100 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
102 static void internal_log_init(void)
104 static int mutex_init_flag = 0; /* not yet initialized */
109 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
111 env = getenv("YAZ_LOG");
113 l_level = yaz_log_mask_str_x(env, l_level);
117 FILE *yaz_log_file(void)
120 switch (yaz_log_info.type)
122 case use_stderr: f = stderr; break;
123 case use_none: f = 0; break;
124 case use_file: f = yaz_log_info.log_file; break;
129 void yaz_log_close(void)
131 if (yaz_log_info.type == use_file && yaz_log_info.log_file)
133 fclose(yaz_log_info.log_file);
134 yaz_log_info.log_file = 0;
138 void yaz_log_init_file(const char *fname)
146 yaz_log_info.type = use_stderr; /* empty name; use stderr */
148 yaz_log_info.type = use_file;
149 strncpy(yaz_log_info.l_fname, fname, sizeof(yaz_log_info.l_fname)-1);
150 yaz_log_info.l_fname[sizeof(yaz_log_info.l_fname)-1] = '\0';
154 yaz_log_info.type = use_none; /* NULL name; use no file at all */
155 yaz_log_info.l_fname[0] = '\0';
160 static void rotate_log(const char *cur_fname)
165 /* windows can't rename a file if it is open */
168 for (i = 0; i<9; i++)
170 char fname_str[FILENAME_MAX];
171 struct stat stat_buf;
173 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
174 if (stat(fname_str, &stat_buf) != 0)
179 char fname_str[2][FILENAME_MAX];
182 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
183 "%s.%d", cur_fname, i-1);
185 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
187 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
188 "%s.%d", cur_fname, i);
190 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
192 rename(fname_str[0], fname_str[1]);
198 void yaz_log_init_level(int level)
201 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
204 yaz_log_reopen(); /* make sure we set buffering right */
209 if (l_level & YLOG_LOGLVL)
210 { /* dump the log level bits */
211 const char *bittype = "Static ";
214 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
216 /* determine size of mask_names (locked) */
217 for (sz = 0; mask_names[sz].name; sz++)
219 /* second pass without lock */
220 for (i = 0; i < sz; i++)
221 if (mask_names[i].mask && *mask_names[i].name)
222 if (strcmp(mask_names[i].name, "all") != 0)
224 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
225 bittype, mask_names[i].mask, mask_names[i].name,
226 (level & mask_names[i].mask)? "ON": "off");
227 if (mask_names[i].mask > YLOG_LAST_BIT)
233 void yaz_log_init_prefix(const char *prefix)
235 if (prefix && *prefix)
236 yaz_snprintf(yaz_log_info.l_prefix,
237 sizeof(yaz_log_info.l_prefix), "%s ", prefix);
239 *yaz_log_info.l_prefix = 0;
242 void yaz_log_init_prefix2(const char *prefix)
244 if (prefix && *prefix)
245 yaz_snprintf(yaz_log_info.l_prefix2,
246 sizeof(yaz_log_info.l_prefix2), "%s ", prefix);
248 *yaz_log_info.l_prefix2 = 0;
251 void yaz_log_init(int level, const char *prefix, const char *fname)
254 yaz_log_init_level(level);
255 yaz_log_init_prefix(prefix);
257 yaz_log_init_file(fname);
260 void yaz_log_init_max_size(int mx)
268 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
274 void log_event_start(void (*func)(int, const char *, void *), void *info)
276 start_hook_func = func;
277 start_hook_info = info;
280 void log_event_end(void (*func)(int, const char *, void *), void *info)
282 end_hook_func = func;
283 end_hook_info = info;
286 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
288 char new_filename[512];
289 static char cur_filename[512] = "";
291 if (yaz_log_info.type != use_file)
294 if (*yaz_log_info.l_fname)
296 strftime(new_filename, sizeof(new_filename)-1, yaz_log_info.l_fname,
298 if (strcmp(new_filename, cur_filename))
300 strcpy(cur_filename, new_filename);
305 if (l_max_size > 0 && yaz_log_info.log_file)
307 long flen = ftell(yaz_log_info.log_file);
308 if (flen > l_max_size)
310 rotate_log(cur_filename);
314 if (force && *cur_filename)
320 new_file = fopen(cur_filename, filemode);
324 yaz_log_info.log_file = new_file;
325 if (l_level & YLOG_FLUSH)
326 setvbuf(yaz_log_info.log_file, 0, _IONBF, 0);
330 /* disable log rotate */
336 static void yaz_log_do_reopen(const char *filemode)
338 time_t cur_time = time(0);
340 struct tm tm0, *tm = &tm0;
346 localtime_r(&cur_time, tm);
348 tm = localtime(&cur_time);
350 yaz_log_open_check(tm, 1, filemode);
354 void yaz_log_reopen()
356 yaz_log_do_reopen("a");
361 yaz_log_do_reopen("w");
364 static void yaz_strftime(char *dst, size_t sz,
365 const char *fmt, const struct tm *tm)
367 strftime(dst, sz, fmt, tm);
370 static void yaz_log_to_file(int level, const char *log_message)
375 struct tm tm0, *tm = &tm0;
383 localtime_r(&ti, tm);
388 yaz_log_open_check(tm, 0, "a");
389 file = yaz_log_file(); /* file may change in yaz_log_open_check */
393 char tbuf[TIMEFORMAT_LEN];
399 for (i = 0; level && mask_names[i].name; i++)
400 if ( mask_names[i].mask & level)
402 if (*mask_names[i].name && mask_names[i].mask &&
403 mask_names[i].mask != YLOG_ALL)
405 if (strlen(flags) + strlen(mask_names[i].name)
409 strcat(flags, mask_names[i].name);
412 level &= ~mask_names[i].mask;
417 if (!(l_level & YLOG_NOTIME))
419 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
420 tbuf[TIMEFORMAT_LEN-2] = '\0';
426 if (l_level & YLOG_TID)
428 yaz_thread_id_cstr(tid, sizeof(tid)-1);
433 fprintf(file, "%s%s%s%s %s%s\n", tbuf, yaz_log_info.l_prefix,
434 tid, flags, yaz_log_info.l_prefix2,
436 if (l_level & YLOG_FLUSH)
441 void yaz_log(int level, const char *fmt, ...)
449 if (!(level & l_level))
453 /* 30 is enough for our 'rest of output' message */
454 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
455 if (strlen(buf) >= sizeof(buf)-31)
456 strcat(buf, " [rest of output omitted]");
458 if (o_level & YLOG_ERRNO)
460 size_t remain = sizeof(buf) - strlen(buf);
461 if (remain > 100) /* reasonable minimum space for error */
464 yaz_strerror(buf+strlen(buf), remain-5); /* 5 due to extra [] */
470 (*start_hook_func)(o_level, buf, start_hook_info);
472 (*hook_func)(o_level, buf, hook_info);
473 file = yaz_log_file();
475 yaz_log_to_file(level, buf);
477 (*end_hook_func)(o_level, buf, end_hook_info);
480 void yaz_log_time_format(const char *fmt)
483 { /* no format, default to new */
484 l_actual_format = l_new_default_format;
487 if (0==strcmp(fmt, "old"))
488 { /* force the old format */
489 l_actual_format = l_old_default_format;
492 /* else use custom format */
493 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
494 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
495 l_actual_format = l_custom_format;
498 /** cleans a loglevel name from leading paths and suffixes */
499 static char *clean_name(const char *name, size_t len, char *namebuf, size_t buflen)
502 char *start = namebuf;
505 strncpy(namebuf, name, len);
507 while ((p = strchr(start, '/')))
509 if ((p = strrchr(start, '.')))
514 static int define_module_bit(const char *name)
518 for (i = 0; mask_names[i].name; i++)
519 if (0 == strcmp(mask_names[i].name, name))
521 return mask_names[i].mask;
523 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1U<<31) ))
525 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
528 mask_names[i].mask = (int) next_log_bit; /* next_log_bit can hold int */
529 next_log_bit = next_log_bit<<1;
530 mask_names[i].name = (char *) malloc(strlen(name)+1);
531 strcpy(mask_names[i].name, name);
532 mask_names[i+1].name = NULL;
533 mask_names[i+1].mask = 0;
534 return mask_names[i].mask;
537 int yaz_log_module_level(const char *name)
541 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
544 for (i = 0; mask_names[i].name; i++)
545 if (0==strcmp(n, mask_names[i].name))
547 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
548 mask_names[i].mask, n,
549 strcmp(n,name) ? name : "");
550 return mask_names[i].mask;
552 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
553 strcmp(n, name) ? name : "" );
557 int yaz_log_mask_str(const char *str)
559 internal_log_init(); /* since l_level may be affected */
560 return yaz_log_mask_str_x(str, l_level);
563 int yaz_log_mask_str_x(const char *str, int level)
571 for (p = str; *p && *p != ','; p++)
578 if (yaz_isdigit(*str))
585 char *n = clean_name(str, (size_t) (p - str), clean, sizeof(clean));
586 int mask = define_module_bit(n);
588 level = 0; /* 'none' clears them all */
603 * c-file-style: "Stroustrup"
604 * indent-tabs-mode: nil
606 * vim: shiftwidth=4 tabstop=8 expandtab