1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
8 * \brief Logging utility
30 #include <yaz/errno.h>
32 #include <yaz/snprintf.h>
33 #include <yaz/xmalloc.h>
35 #define HAS_STRERROR 1
43 extern char *sys_errlist[];
44 return sys_errlist[n];
50 static int l_level = YLOG_DEFAULT_LEVEL;
52 enum l_file_type { use_stderr, use_none, use_file };
53 static enum l_file_type yaz_file_type = use_stderr;
54 static FILE *yaz_global_log_file = NULL;
56 static void (*start_hook_func)(int, const char *, void *) = NULL;
57 static void *start_hook_info;
59 static void (*end_hook_func)(int, const char *, void *) = NULL;
60 static void *end_hook_info;
62 static void (*hook_func)(int, const char *, void *) = NULL;
63 static void *hook_info;
65 static char l_prefix[512] = "";
66 static char l_prefix2[512] = "";
67 static char l_fname[512] = "";
70 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
71 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
72 #define TIMEFORMAT_LEN 50
73 static char l_custom_format[TIMEFORMAT_LEN] = "";
74 static char *l_actual_format = l_old_default_format;
76 /** l_max_size tells when to rotate the log. The default value is
77 0 which means DISABLED. This is to be preffered if YAZ runs
78 as a server using logrotate etc.
79 A positive size specifies the file size in bytes when a log rotate
80 will occur. Note that in order for this to work YAZ must have
83 static int l_max_size = 0;
85 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
89 } mask_names[MAX_MASK_NAMES] =
91 { YLOG_FATAL, "fatal"},
92 { YLOG_DEBUG, "debug"},
93 { YLOG_WARN, "warn" },
96 { YLOG_MALLOC, "malloc"},
98 { YLOG_NOTIME, "notime" },
99 { YLOG_APP2, "app2" },
100 { YLOG_APP3, "app3" },
102 { YLOG_FLUSH, "flush" },
103 { YLOG_LOGLVL, "loglevel" },
106 /* the rest will be filled in if the user defines dynamic modules*/
109 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
111 static void internal_log_init(void)
113 static int mutex_init_flag = 0; /* not yet initialized */
118 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
120 env = getenv("YAZ_LOG");
122 l_level = yaz_log_mask_str_x(env, l_level);
126 FILE *yaz_log_file(void)
129 switch(yaz_file_type)
131 case use_stderr: f = stderr; break;
132 case use_none: f = 0; break;
133 case use_file: f = yaz_global_log_file; break;
138 void yaz_log_close(void)
140 if (yaz_file_type == use_file && yaz_global_log_file)
142 fclose(yaz_global_log_file);
143 yaz_global_log_file = 0;
147 void yaz_log_init_file(const char *fname)
155 yaz_file_type = use_stderr; /* empty name; use stderr */
157 yaz_file_type = use_file;
158 strncpy(l_fname, fname, sizeof(l_fname)-1);
159 l_fname[sizeof(l_fname)-1] = '\0';
163 yaz_file_type = use_none; /* NULL name; use no file at all */
169 static void rotate_log(const char *cur_fname)
174 /* windows can't rename a file if it is open */
177 for (i = 0; i<9; i++)
179 char fname_str[FILENAME_MAX];
180 struct stat stat_buf;
182 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
183 if (stat(fname_str, &stat_buf) != 0)
188 char fname_str[2][FILENAME_MAX];
191 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
192 "%s.%d", cur_fname, i-1);
194 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
196 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
197 "%s.%d", cur_fname, i);
199 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
201 rename(fname_str[0], fname_str[1]);
207 void yaz_log_init_level(int level)
210 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
213 yaz_log_reopen(); /* make sure we set buffering right */
218 if (l_level & YLOG_LOGLVL)
219 { /* dump the log level bits */
220 const char *bittype = "Static ";
223 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
225 /* determine size of mask_names (locked) */
226 for (sz = 0; mask_names[sz].name; sz++)
228 /* second pass without lock */
229 for (i = 0; i < sz; i++)
230 if (mask_names[i].mask && *mask_names[i].name)
231 if (strcmp(mask_names[i].name, "all") != 0)
233 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
234 bittype, mask_names[i].mask, mask_names[i].name,
235 (level & mask_names[i].mask)? "ON": "off");
236 if (mask_names[i].mask > YLOG_LAST_BIT)
242 void yaz_log_init_prefix(const char *prefix)
244 if (prefix && *prefix)
245 yaz_snprintf(l_prefix, sizeof(l_prefix), "%s ", prefix);
250 void yaz_log_init_prefix2(const char *prefix)
252 if (prefix && *prefix)
253 yaz_snprintf(l_prefix2, sizeof(l_prefix2), "%s ", prefix);
258 void yaz_log_init(int level, const char *prefix, const char *fname)
261 yaz_log_init_level(level);
262 yaz_log_init_prefix(prefix);
264 yaz_log_init_file(fname);
267 void yaz_log_init_max_size(int mx)
275 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
281 void log_event_start(void (*func)(int, const char *, void *), void *info)
283 start_hook_func = func;
284 start_hook_info = info;
287 void log_event_end(void (*func)(int, const char *, void *), void *info)
289 end_hook_func = func;
290 end_hook_info = info;
293 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
295 char new_filename[512];
296 static char cur_filename[512] = "";
298 if (yaz_file_type != use_file)
303 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
304 if (strcmp(new_filename, cur_filename))
306 strcpy(cur_filename, new_filename);
311 if (l_max_size > 0 && yaz_global_log_file)
313 long flen = ftell(yaz_global_log_file);
314 if (flen > l_max_size)
316 rotate_log(cur_filename);
320 if (force && *cur_filename)
326 new_file = fopen(cur_filename, filemode);
330 yaz_global_log_file = new_file;
331 if (l_level & YLOG_FLUSH)
332 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
336 /* disable log rotate */
342 static void yaz_log_do_reopen(const char *filemode)
344 time_t cur_time = time(0);
346 struct tm tm0, *tm = &tm0;
352 localtime_r(&cur_time, tm);
354 tm = localtime(&cur_time);
356 yaz_log_open_check(tm, 1, filemode);
360 void yaz_log_reopen()
362 yaz_log_do_reopen("a");
367 yaz_log_do_reopen("w");
370 static void yaz_strftime(char *dst, size_t sz,
371 const char *fmt, const struct tm *tm)
373 strftime(dst, sz, fmt, tm);
376 static void yaz_log_to_file(int level, const char *log_message)
381 struct tm tm0, *tm = &tm0;
389 localtime_r(&ti, tm);
394 yaz_log_open_check(tm, 0, "a");
395 file = yaz_log_file(); /* file may change in yaz_log_open_check */
399 char tbuf[TIMEFORMAT_LEN];
404 for (i = 0; level && mask_names[i].name; i++)
405 if ( mask_names[i].mask & level)
407 if (*mask_names[i].name && mask_names[i].mask &&
408 mask_names[i].mask != YLOG_ALL)
410 if (strlen(flags) + strlen(mask_names[i].name)
414 strcat(flags, mask_names[i].name);
417 level &= ~mask_names[i].mask;
422 if (!(l_level & YLOG_NOTIME))
424 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
425 tbuf[TIMEFORMAT_LEN-2] = '\0';
429 fprintf(file, "%s%s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
431 if (l_level & YLOG_FLUSH)
436 void yaz_log(int level, const char *fmt, ...)
444 if (!(level & l_level))
448 /* 30 is enough for our 'rest of output' message */
449 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
450 if (strlen(buf) >= sizeof(buf)-31)
451 strcat(buf, " [rest of output omitted]");
453 if (o_level & YLOG_ERRNO)
455 int remain = sizeof(buf) - strlen(buf);
456 if (remain > 100) /* reasonable minimum space for error */
459 yaz_strerror(buf+strlen(buf), remain-5); /* 5 due to extra [] */
465 (*start_hook_func)(o_level, buf, start_hook_info);
467 (*hook_func)(o_level, buf, hook_info);
468 file = yaz_log_file();
470 yaz_log_to_file(level, buf);
472 (*end_hook_func)(o_level, buf, end_hook_info);
475 void yaz_log_time_format(const char *fmt)
478 { /* no format, default to new */
479 l_actual_format = l_new_default_format;
482 if (0==strcmp(fmt, "old"))
483 { /* force the old format */
484 l_actual_format = l_old_default_format;
487 /* else use custom format */
488 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
489 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
490 l_actual_format = l_custom_format;
493 /** cleans a loglevel name from leading paths and suffixes */
494 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
497 char *start = namebuf;
500 strncpy(namebuf, name, len);
502 while ((p = strchr(start, '/')))
504 if ((p = strrchr(start, '.')))
509 static int define_module_bit(const char *name)
513 for (i = 0; mask_names[i].name; i++)
514 if (0 == strcmp(mask_names[i].name, name))
516 return mask_names[i].mask;
518 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) ))
520 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
523 mask_names[i].mask = next_log_bit;
524 next_log_bit = next_log_bit<<1;
525 mask_names[i].name = (char *) malloc(strlen(name)+1);
526 strcpy(mask_names[i].name, name);
527 mask_names[i+1].name = NULL;
528 mask_names[i+1].mask = 0;
529 return mask_names[i].mask;
532 int yaz_log_module_level(const char *name)
536 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
539 for (i = 0; mask_names[i].name; i++)
540 if (0==strcmp(n, mask_names[i].name))
542 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
543 mask_names[i].mask, n,
544 strcmp(n,name) ? name : "");
545 return mask_names[i].mask;
547 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
548 strcmp(n, name) ? name : "" );
552 int yaz_log_mask_str(const char *str)
554 internal_log_init(); /* since l_level may be affected */
555 return yaz_log_mask_str_x(str, l_level);
558 int yaz_log_mask_str_x(const char *str, int level)
566 for (p = str; *p && *p != ','; p++)
573 if (isdigit(*(unsigned char *) str))
580 char *n = clean_name(str, p-str, clean, sizeof(clean));
581 int mask = define_module_bit(n);
583 level = 0; /* 'none' clears them all */
598 * c-file-style: "Stroustrup"
599 * indent-tabs-mode: nil
601 * vim: shiftwidth=4 tabstop=8 expandtab