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
29 #include <yaz/snprintf.h>
30 #include <yaz/xmalloc.h>
32 #define HAS_STRERROR 1
40 extern char *sys_errlist[];
41 return sys_errlist[n];
47 static int l_level = YLOG_DEFAULT_LEVEL;
49 enum l_file_type { use_stderr, use_none, use_file };
50 static enum l_file_type yaz_file_type = use_stderr;
51 static FILE *yaz_global_log_file = NULL;
53 static void (*start_hook_func)(int, const char *, void *) = NULL;
54 static void *start_hook_info;
56 static void (*end_hook_func)(int, const char *, void *) = NULL;
57 static void *end_hook_info;
59 static void (*hook_func)(int, const char *, void *) = NULL;
60 static void *hook_info;
62 static char l_prefix[512] = "";
63 static char l_prefix2[512] = "";
64 static char l_fname[512] = "";
67 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
68 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
69 #define TIMEFORMAT_LEN 50
70 static char l_custom_format[TIMEFORMAT_LEN] = "";
71 static char *l_actual_format = l_old_default_format;
73 /** l_max_size tells when to rotate the log. Default is 1 GB
74 This is almost the same as never, but it saves applications in the
75 case of 2 or 4 GB file size limits..
77 static int l_max_size = 1024*1024*1024;
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"},
92 { YLOG_NOTIME, "notime" },
93 { YLOG_APP2, "app2" },
94 { YLOG_APP3, "app3" },
96 { YLOG_FLUSH, "flush" },
97 { YLOG_LOGLVL, "loglevel" },
100 /* the rest will be filled in if the user defines dynamic modules*/
103 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
105 static void internal_log_init(void)
107 static int mutex_init_flag = 0; /* not yet initialized */
112 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
114 env = getenv("YAZ_LOG");
116 l_level = yaz_log_mask_str_x(env, l_level);
120 FILE *yaz_log_file(void)
123 switch(yaz_file_type)
125 case use_stderr: f = stderr; break;
126 case use_none: f = 0; break;
127 case use_file: f = yaz_global_log_file; break;
132 void yaz_log_close(void)
134 if (yaz_file_type == use_file && yaz_global_log_file)
136 fclose(yaz_global_log_file);
137 yaz_global_log_file = 0;
141 void yaz_log_init_file(const char *fname)
149 yaz_file_type = use_stderr; /* empty name; use stderr */
151 yaz_file_type = use_file;
152 strncpy(l_fname, fname, sizeof(l_fname)-1);
153 l_fname[sizeof(l_fname)-1] = '\0';
157 yaz_file_type = use_none; /* NULL name; use no file at all */
163 static void rotate_log(const char *cur_fname)
168 /* windows can't rename a file if it is open */
171 for (i = 0; i<9; i++)
173 char fname_str[FILENAME_MAX];
174 struct stat stat_buf;
176 yaz_snprintf(fname_str, sizeof(fname_str), "%s.%d", cur_fname, i);
177 if (stat(fname_str, &stat_buf) != 0)
182 char fname_str[2][FILENAME_MAX];
185 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
186 "%s.%d", cur_fname, i-1);
188 yaz_snprintf(fname_str[0], sizeof(fname_str[0]),
190 yaz_snprintf(fname_str[1], sizeof(fname_str[1]),
191 "%s.%d", cur_fname, i);
193 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
195 rename(fname_str[0], fname_str[1]);
201 void yaz_log_init_level(int level)
204 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
207 yaz_log_reopen(); /* make sure we set buffering right */
212 if (l_level & YLOG_LOGLVL)
213 { /* dump the log level bits */
214 const char *bittype = "Static ";
217 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
219 /* determine size of mask_names (locked) */
220 for (sz = 0; mask_names[sz].name; sz++)
222 /* second pass without lock */
223 for (i = 0; i < sz; i++)
224 if (mask_names[i].mask && *mask_names[i].name)
225 if (strcmp(mask_names[i].name, "all") != 0)
227 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
228 bittype, mask_names[i].mask, mask_names[i].name,
229 (level & mask_names[i].mask)? "ON": "off");
230 if (mask_names[i].mask > YLOG_LAST_BIT)
236 void yaz_log_init_prefix(const char *prefix)
238 if (prefix && *prefix)
239 yaz_snprintf(l_prefix, sizeof(l_prefix), "%s ", prefix);
244 void yaz_log_init_prefix2(const char *prefix)
246 if (prefix && *prefix)
247 yaz_snprintf(l_prefix2, sizeof(l_prefix2), "%s ", prefix);
252 void yaz_log_init(int level, const char *prefix, const char *fname)
255 yaz_log_init_level(level);
256 yaz_log_init_prefix(prefix);
258 yaz_log_init_file(fname);
261 void yaz_log_init_max_size(int mx)
269 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
275 void log_event_start(void (*func)(int, const char *, void *), void *info)
277 start_hook_func = func;
278 start_hook_info = info;
281 void log_event_end(void (*func)(int, const char *, void *), void *info)
283 end_hook_func = func;
284 end_hook_info = info;
287 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
289 char new_filename[512];
290 static char cur_filename[512] = "";
292 if (yaz_file_type != use_file)
297 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
298 if (strcmp(new_filename, cur_filename))
300 strcpy(cur_filename, new_filename);
305 if (l_max_size > 0 && yaz_global_log_file)
307 long flen = ftell(yaz_global_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_global_log_file = new_file;
325 if (l_level & YLOG_FLUSH)
326 setvbuf(yaz_global_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];
398 for (i = 0; level && mask_names[i].name; i++)
399 if ( mask_names[i].mask & level)
401 if (*mask_names[i].name && mask_names[i].mask &&
402 mask_names[i].mask != YLOG_ALL)
404 if (strlen(flags) + strlen(mask_names[i].name)
408 strcat(flags, mask_names[i].name);
411 level &= ~mask_names[i].mask;
416 if (!(l_level & YLOG_NOTIME))
418 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
419 tbuf[TIMEFORMAT_LEN-2] = '\0';
423 fprintf(file, "%s%s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
425 if (l_level & YLOG_FLUSH)
430 void yaz_log(int level, const char *fmt, ...)
438 if (!(level & l_level))
442 /* 30 is enough for our 'rest of output' message */
443 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
444 if (strlen(buf) >= sizeof(buf)-31)
445 strcat(buf, " [rest of output omitted]");
447 if (o_level & YLOG_ERRNO)
449 int remain = sizeof(buf) - strlen(buf);
450 if (remain > 100) /* reasonable minimum space for error */
453 yaz_strerror(buf+strlen(buf), remain-5); /* 5 due to extra [] */
459 (*start_hook_func)(o_level, buf, start_hook_info);
461 (*hook_func)(o_level, buf, hook_info);
462 file = yaz_log_file();
464 yaz_log_to_file(level, buf);
466 (*end_hook_func)(o_level, buf, end_hook_info);
469 void yaz_log_time_format(const char *fmt)
472 { /* no format, default to new */
473 l_actual_format = l_new_default_format;
476 if (0==strcmp(fmt, "old"))
477 { /* force the old format */
478 l_actual_format = l_old_default_format;
481 /* else use custom format */
482 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
483 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
484 l_actual_format = l_custom_format;
487 /** cleans a loglevel name from leading paths and suffixes */
488 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
491 char *start = namebuf;
494 strncpy(namebuf, name, len);
496 while ((p = strchr(start, '/')))
498 if ((p = strrchr(start, '.')))
503 static int define_module_bit(const char *name)
507 for (i = 0; mask_names[i].name; i++)
508 if (0 == strcmp(mask_names[i].name, name))
510 return mask_names[i].mask;
512 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) ))
514 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
517 mask_names[i].mask = next_log_bit;
518 next_log_bit = next_log_bit<<1;
519 mask_names[i].name = (char *) malloc(strlen(name)+1);
520 strcpy(mask_names[i].name, name);
521 mask_names[i+1].name = NULL;
522 mask_names[i+1].mask = 0;
523 return mask_names[i].mask;
526 int yaz_log_module_level(const char *name)
530 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
533 for (i = 0; mask_names[i].name; i++)
534 if (0==strcmp(n, mask_names[i].name))
536 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
537 mask_names[i].mask, n,
538 strcmp(n,name) ? name : "");
539 return mask_names[i].mask;
541 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
542 strcmp(n, name) ? name : "" );
546 int yaz_log_mask_str(const char *str)
548 internal_log_init(); /* since l_level may be affected */
549 return yaz_log_mask_str_x(str, l_level);
552 int yaz_log_mask_str_x(const char *str, int level)
560 for (p = str; *p && *p != ','; p++)
567 if (isdigit(*(unsigned char *) str))
574 char *n = clean_name(str, p-str, clean, sizeof(clean));
575 int mask = define_module_bit(n);
577 level = 0; /* 'none' clears them all */
592 * indent-tabs-mode: nil
594 * vim: shiftwidth=4 tabstop=8 expandtab