1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Logging utility
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 };
39 static enum l_file_type yaz_file_type = use_stderr;
40 static FILE *yaz_global_log_file = NULL;
42 static void (*start_hook_func)(int, const char *, void *) = NULL;
43 static void *start_hook_info;
45 static void (*end_hook_func)(int, const char *, void *) = NULL;
46 static void *end_hook_info;
48 static void (*hook_func)(int, const char *, void *) = NULL;
49 static void *hook_info;
51 static char l_prefix[512] = "";
52 static char l_prefix2[512] = "";
53 static char l_fname[512] = "";
56 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
57 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
58 #define TIMEFORMAT_LEN 50
60 static char l_custom_format[TIMEFORMAT_LEN] = "";
61 static char *l_actual_format = l_old_default_format;
63 /** l_max_size tells when to rotate the log. The default value is
64 0 which means DISABLED. This is to be preffered if YAZ runs
65 as a server using logrotate etc.
66 A positive size specifies the file size in bytes when a log rotate
67 will occur. Note that in order for this to work YAZ must have
70 static int l_max_size = 0;
72 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
76 } mask_names[MAX_MASK_NAMES] =
78 { YLOG_FATAL, "fatal"},
79 { YLOG_DEBUG, "debug"},
80 { YLOG_WARN, "warn" },
83 { YLOG_MALLOC, "malloc"},
86 { YLOG_NOTIME, "notime" },
87 { YLOG_APP2, "app2" },
88 { YLOG_APP3, "app3" },
90 { YLOG_FLUSH, "flush" },
91 { YLOG_LOGLVL, "loglevel" },
94 /* the rest will be filled in if the user defines dynamic modules*/
97 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
99 static void internal_log_init(void)
101 static int mutex_init_flag = 0; /* not yet initialized */
106 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
108 env = getenv("YAZ_LOG");
110 l_level = yaz_log_mask_str_x(env, l_level);
114 FILE *yaz_log_file(void)
117 switch(yaz_file_type)
119 case use_stderr: f = stderr; break;
120 case use_none: f = 0; break;
121 case use_file: f = yaz_global_log_file; break;
126 void yaz_log_close(void)
128 if (yaz_file_type == use_file && yaz_global_log_file)
130 fclose(yaz_global_log_file);
131 yaz_global_log_file = 0;
135 void yaz_log_init_file(const char *fname)
143 yaz_file_type = use_stderr; /* empty name; use stderr */
145 yaz_file_type = use_file;
146 strncpy(l_fname, fname, sizeof(l_fname)-1);
147 l_fname[sizeof(l_fname)-1] = '\0';
151 yaz_file_type = use_none; /* NULL name; use no file at all */
157 static void rotate_log(const char *cur_fname)
162 /* windows can't rename a file if it is open */
165 for (i = 0; i<9; i++)
167 char fname_str[FILENAME_MAX];
168 struct stat stat_buf;
170 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
171 if (stat(fname_str, &stat_buf) != 0)
176 char fname_str[2][FILENAME_MAX];
179 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
180 "%s.%d", cur_fname, i-1);
182 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
184 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
185 "%s.%d", cur_fname, i);
187 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
189 rename(fname_str[0], fname_str[1]);
195 void yaz_log_init_level(int level)
198 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
201 yaz_log_reopen(); /* make sure we set buffering right */
206 if (l_level & YLOG_LOGLVL)
207 { /* dump the log level bits */
208 const char *bittype = "Static ";
211 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
213 /* determine size of mask_names (locked) */
214 for (sz = 0; mask_names[sz].name; sz++)
216 /* second pass without lock */
217 for (i = 0; i < sz; i++)
218 if (mask_names[i].mask && *mask_names[i].name)
219 if (strcmp(mask_names[i].name, "all") != 0)
221 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
222 bittype, mask_names[i].mask, mask_names[i].name,
223 (level & mask_names[i].mask)? "ON": "off");
224 if (mask_names[i].mask > YLOG_LAST_BIT)
230 void yaz_log_init_prefix(const char *prefix)
232 if (prefix && *prefix)
233 yaz_snprintf(l_prefix, sizeof(l_prefix), "%s ", prefix);
238 void yaz_log_init_prefix2(const char *prefix)
240 if (prefix && *prefix)
241 yaz_snprintf(l_prefix2, sizeof(l_prefix2), "%s ", prefix);
246 void yaz_log_init(int level, const char *prefix, const char *fname)
249 yaz_log_init_level(level);
250 yaz_log_init_prefix(prefix);
252 yaz_log_init_file(fname);
255 void yaz_log_init_max_size(int mx)
263 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
269 void log_event_start(void (*func)(int, const char *, void *), void *info)
271 start_hook_func = func;
272 start_hook_info = info;
275 void log_event_end(void (*func)(int, const char *, void *), void *info)
277 end_hook_func = func;
278 end_hook_info = info;
281 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
283 char new_filename[512];
284 static char cur_filename[512] = "";
286 if (yaz_file_type != use_file)
291 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
292 if (strcmp(new_filename, cur_filename))
294 strcpy(cur_filename, new_filename);
299 if (l_max_size > 0 && yaz_global_log_file)
301 long flen = ftell(yaz_global_log_file);
302 if (flen > l_max_size)
304 rotate_log(cur_filename);
308 if (force && *cur_filename)
314 new_file = fopen(cur_filename, filemode);
318 yaz_global_log_file = new_file;
319 if (l_level & YLOG_FLUSH)
320 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
324 /* disable log rotate */
330 static void yaz_log_do_reopen(const char *filemode)
332 time_t cur_time = time(0);
334 struct tm tm0, *tm = &tm0;
340 localtime_r(&cur_time, tm);
342 tm = localtime(&cur_time);
344 yaz_log_open_check(tm, 1, filemode);
348 void yaz_log_reopen()
350 yaz_log_do_reopen("a");
355 yaz_log_do_reopen("w");
358 static void yaz_strftime(char *dst, size_t sz,
359 const char *fmt, const struct tm *tm)
361 strftime(dst, sz, fmt, tm);
364 static void yaz_log_to_file(int level, const char *log_message)
369 struct tm tm0, *tm = &tm0;
377 localtime_r(&ti, tm);
382 yaz_log_open_check(tm, 0, "a");
383 file = yaz_log_file(); /* file may change in yaz_log_open_check */
387 char tbuf[TIMEFORMAT_LEN];
393 for (i = 0; level && mask_names[i].name; i++)
394 if ( mask_names[i].mask & level)
396 if (*mask_names[i].name && mask_names[i].mask &&
397 mask_names[i].mask != YLOG_ALL)
399 if (strlen(flags) + strlen(mask_names[i].name)
403 strcat(flags, mask_names[i].name);
406 level &= ~mask_names[i].mask;
411 if (!(l_level & YLOG_NOTIME))
413 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
414 tbuf[TIMEFORMAT_LEN-2] = '\0';
420 if (l_level & YLOG_TID)
422 yaz_thread_id_cstr(tid, sizeof(tid)-1);
427 fprintf(file, "%s%s%s%s %s%s\n", tbuf, l_prefix, tid, flags, l_prefix2,
429 if (l_level & YLOG_FLUSH)
434 void yaz_log(int level, const char *fmt, ...)
442 if (!(level & l_level))
446 /* 30 is enough for our 'rest of output' message */
447 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
448 if (strlen(buf) >= sizeof(buf)-31)
449 strcat(buf, " [rest of output omitted]");
451 if (o_level & YLOG_ERRNO)
453 size_t remain = sizeof(buf) - strlen(buf);
454 if (remain > 100) /* reasonable minimum space for error */
457 yaz_strerror(buf+strlen(buf), remain-5); /* 5 due to extra [] */
463 (*start_hook_func)(o_level, buf, start_hook_info);
465 (*hook_func)(o_level, buf, hook_info);
466 file = yaz_log_file();
468 yaz_log_to_file(level, buf);
470 (*end_hook_func)(o_level, buf, end_hook_info);
473 void yaz_log_time_format(const char *fmt)
476 { /* no format, default to new */
477 l_actual_format = l_new_default_format;
480 if (0==strcmp(fmt, "old"))
481 { /* force the old format */
482 l_actual_format = l_old_default_format;
485 /* else use custom format */
486 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
487 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
488 l_actual_format = l_custom_format;
491 /** cleans a loglevel name from leading paths and suffixes */
492 static char *clean_name(const char *name, size_t len, char *namebuf, size_t buflen)
495 char *start = namebuf;
498 strncpy(namebuf, name, len);
500 while ((p = strchr(start, '/')))
502 if ((p = strrchr(start, '.')))
507 static int define_module_bit(const char *name)
511 for (i = 0; mask_names[i].name; i++)
512 if (0 == strcmp(mask_names[i].name, name))
514 return mask_names[i].mask;
516 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1U<<31) ))
518 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
521 mask_names[i].mask = (int) next_log_bit; /* next_log_bit can hold int */
522 next_log_bit = next_log_bit<<1;
523 mask_names[i].name = (char *) malloc(strlen(name)+1);
524 strcpy(mask_names[i].name, name);
525 mask_names[i+1].name = NULL;
526 mask_names[i+1].mask = 0;
527 return mask_names[i].mask;
530 int yaz_log_module_level(const char *name)
534 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
537 for (i = 0; mask_names[i].name; i++)
538 if (0==strcmp(n, mask_names[i].name))
540 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
541 mask_names[i].mask, n,
542 strcmp(n,name) ? name : "");
543 return mask_names[i].mask;
545 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
546 strcmp(n, name) ? name : "" );
550 int yaz_log_mask_str(const char *str)
552 internal_log_init(); /* since l_level may be affected */
553 return yaz_log_mask_str_x(str, l_level);
556 int yaz_log_mask_str_x(const char *str, int level)
564 for (p = str; *p && *p != ','; p++)
571 if (isdigit(*(unsigned char *) str))
578 char *n = clean_name(str, (size_t) (p - str), clean, sizeof(clean));
579 int mask = define_module_bit(n);
581 level = 0; /* 'none' clears them all */
596 * c-file-style: "Stroustrup"
597 * indent-tabs-mode: nil
599 * vim: shiftwidth=4 tabstop=8 expandtab