1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
8 * \brief Logging utility
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. Default is 1 GB
77 This is almost the same as never, but it saves applications in the
78 case of 2 or 4 GB file size limits..
80 static int l_max_size = 1024*1024*1024;
82 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
86 } mask_names[MAX_MASK_NAMES] =
88 { YLOG_FATAL, "fatal"},
89 { YLOG_DEBUG, "debug"},
90 { YLOG_WARN, "warn" },
93 { YLOG_MALLOC, "malloc"},
95 { YLOG_NOTIME, "notime" },
96 { YLOG_APP2, "app2" },
97 { YLOG_APP3, "app3" },
99 { YLOG_FLUSH, "flush" },
100 { YLOG_LOGLVL, "loglevel" },
103 /* the rest will be filled in if the user defines dynamic modules*/
106 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
108 static void internal_log_init(void)
110 static int mutex_init_flag = 0; /* not yet initialized */
115 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
117 env = getenv("YAZ_LOG");
119 l_level = yaz_log_mask_str_x(env, l_level);
123 FILE *yaz_log_file(void)
126 switch(yaz_file_type)
128 case use_stderr: f = stderr; break;
129 case use_none: f = 0; break;
130 case use_file: f = yaz_global_log_file; break;
135 void yaz_log_close(void)
137 if (yaz_file_type == use_file && yaz_global_log_file)
139 fclose(yaz_global_log_file);
140 yaz_global_log_file = 0;
144 void yaz_log_init_file(const char *fname)
152 yaz_file_type = use_stderr; /* empty name; use stderr */
154 yaz_file_type = use_file;
155 strncpy(l_fname, fname, sizeof(l_fname)-1);
156 l_fname[sizeof(l_fname)-1] = '\0';
160 yaz_file_type = use_none; /* NULL name; use no file at all */
166 static void rotate_log(const char *cur_fname)
171 /* windows can't rename a file if it is open */
174 for (i = 0; i<9; i++)
176 char fname_str[FILENAME_MAX];
177 struct stat stat_buf;
179 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
180 if (stat(fname_str, &stat_buf) != 0)
185 char fname_str[2][FILENAME_MAX];
188 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
189 "%s.%d", cur_fname, i-1);
191 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
193 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
194 "%s.%d", cur_fname, i);
196 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
198 rename(fname_str[0], fname_str[1]);
204 void yaz_log_init_level(int level)
207 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
210 yaz_log_reopen(); /* make sure we set buffering right */
215 if (l_level & YLOG_LOGLVL)
216 { /* dump the log level bits */
217 const char *bittype = "Static ";
220 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
222 /* determine size of mask_names (locked) */
223 for (sz = 0; mask_names[sz].name; sz++)
225 /* second pass without lock */
226 for (i = 0; i < sz; i++)
227 if (mask_names[i].mask && *mask_names[i].name)
228 if (strcmp(mask_names[i].name, "all") != 0)
230 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
231 bittype, mask_names[i].mask, mask_names[i].name,
232 (level & mask_names[i].mask)? "ON": "off");
233 if (mask_names[i].mask > YLOG_LAST_BIT)
239 void yaz_log_init_prefix(const char *prefix)
241 if (prefix && *prefix)
242 yaz_snprintf(l_prefix, sizeof(l_prefix), "%s ", prefix);
247 void yaz_log_init_prefix2(const char *prefix)
249 if (prefix && *prefix)
250 yaz_snprintf(l_prefix2, sizeof(l_prefix2), "%s ", prefix);
255 void yaz_log_init(int level, const char *prefix, const char *fname)
258 yaz_log_init_level(level);
259 yaz_log_init_prefix(prefix);
261 yaz_log_init_file(fname);
264 void yaz_log_init_max_size(int mx)
272 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
278 void log_event_start(void (*func)(int, const char *, void *), void *info)
280 start_hook_func = func;
281 start_hook_info = info;
284 void log_event_end(void (*func)(int, const char *, void *), void *info)
286 end_hook_func = func;
287 end_hook_info = info;
290 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
292 char new_filename[512];
293 static char cur_filename[512] = "";
295 if (yaz_file_type != use_file)
300 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
301 if (strcmp(new_filename, cur_filename))
303 strcpy(cur_filename, new_filename);
308 if (l_max_size > 0 && yaz_global_log_file)
310 long flen = ftell(yaz_global_log_file);
311 if (flen > l_max_size)
313 rotate_log(cur_filename);
317 if (force && *cur_filename)
323 new_file = fopen(cur_filename, filemode);
327 yaz_global_log_file = new_file;
328 if (l_level & YLOG_FLUSH)
329 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
333 /* disable log rotate */
339 static void yaz_log_do_reopen(const char *filemode)
341 time_t cur_time = time(0);
343 struct tm tm0, *tm = &tm0;
349 localtime_r(&cur_time, tm);
351 tm = localtime(&cur_time);
353 yaz_log_open_check(tm, 1, filemode);
357 void yaz_log_reopen()
359 yaz_log_do_reopen("a");
364 yaz_log_do_reopen("w");
367 static void yaz_strftime(char *dst, size_t sz,
368 const char *fmt, const struct tm *tm)
370 strftime(dst, sz, fmt, tm);
373 static void yaz_log_to_file(int level, const char *log_message)
378 struct tm tm0, *tm = &tm0;
386 localtime_r(&ti, tm);
391 yaz_log_open_check(tm, 0, "a");
392 file = yaz_log_file(); /* file may change in yaz_log_open_check */
396 char tbuf[TIMEFORMAT_LEN];
401 for (i = 0; level && mask_names[i].name; i++)
402 if ( mask_names[i].mask & level)
404 if (*mask_names[i].name && mask_names[i].mask &&
405 mask_names[i].mask != YLOG_ALL)
407 if (strlen(flags) + strlen(mask_names[i].name)
411 strcat(flags, mask_names[i].name);
414 level &= ~mask_names[i].mask;
419 if (!(l_level & YLOG_NOTIME))
421 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
422 tbuf[TIMEFORMAT_LEN-2] = '\0';
426 fprintf(file, "%s%s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
428 if (l_level & YLOG_FLUSH)
433 void yaz_log(int level, const char *fmt, ...)
441 if (!(level & l_level))
445 /* 30 is enough for our 'rest of output' message */
446 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
447 if (strlen(buf) >= sizeof(buf)-31)
448 strcat(buf, " [rest of output omitted]");
450 if (o_level & YLOG_ERRNO)
452 int remain = sizeof(buf) - strlen(buf);
453 if (remain > 100) /* reasonable minimum space for error */
456 yaz_strerror(buf+strlen(buf), remain-5); /* 5 due to extra [] */
462 (*start_hook_func)(o_level, buf, start_hook_info);
464 (*hook_func)(o_level, buf, hook_info);
465 file = yaz_log_file();
467 yaz_log_to_file(level, buf);
469 (*end_hook_func)(o_level, buf, end_hook_info);
472 void yaz_log_time_format(const char *fmt)
475 { /* no format, default to new */
476 l_actual_format = l_new_default_format;
479 if (0==strcmp(fmt, "old"))
480 { /* force the old format */
481 l_actual_format = l_old_default_format;
484 /* else use custom format */
485 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
486 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
487 l_actual_format = l_custom_format;
490 /** cleans a loglevel name from leading paths and suffixes */
491 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
494 char *start = namebuf;
497 strncpy(namebuf, name, len);
499 while ((p = strchr(start, '/')))
501 if ((p = strrchr(start, '.')))
506 static int define_module_bit(const char *name)
510 for (i = 0; mask_names[i].name; i++)
511 if (0 == strcmp(mask_names[i].name, name))
513 return mask_names[i].mask;
515 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) ))
517 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
520 mask_names[i].mask = next_log_bit;
521 next_log_bit = next_log_bit<<1;
522 mask_names[i].name = (char *) malloc(strlen(name)+1);
523 strcpy(mask_names[i].name, name);
524 mask_names[i+1].name = NULL;
525 mask_names[i+1].mask = 0;
526 return mask_names[i].mask;
529 int yaz_log_module_level(const char *name)
533 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
536 for (i = 0; mask_names[i].name; i++)
537 if (0==strcmp(n, mask_names[i].name))
539 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
540 mask_names[i].mask, n,
541 strcmp(n,name) ? name : "");
542 return mask_names[i].mask;
544 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
545 strcmp(n, name) ? name : "" );
549 int yaz_log_mask_str(const char *str)
551 internal_log_init(); /* since l_level may be affected */
552 return yaz_log_mask_str_x(str, l_level);
555 int yaz_log_mask_str_x(const char *str, int level)
563 for (p = str; *p && *p != ','; p++)
570 if (isdigit(*(unsigned char *) str))
577 char *n = clean_name(str, p-str, clean, sizeof(clean));
578 int mask = define_module_bit(n);
580 level = 0; /* 'none' clears them all */
595 * indent-tabs-mode: nil
597 * vim: shiftwidth=4 tabstop=8 expandtab