2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.51 2007-05-06 20:12:20 adam Exp $
10 * \brief Logging utility
31 #include <yaz/snprintf.h>
32 #include <yaz/xmalloc.h>
34 #define HAS_STRERROR 1
42 extern char *sys_errlist[];
43 return sys_errlist[n];
49 static int l_level = YLOG_DEFAULT_LEVEL;
51 enum l_file_type { use_stderr, use_none, use_file };
52 static enum l_file_type yaz_file_type = use_stderr;
53 static FILE *yaz_global_log_file = NULL;
55 static void (*start_hook_func)(int, const char *, void *) = NULL;
56 static void *start_hook_info;
58 static void (*end_hook_func)(int, const char *, void *) = NULL;
59 static void *end_hook_info;
61 static void (*hook_func)(int, const char *, void *) = NULL;
62 static void *hook_info;
64 static char l_prefix[512] = "";
65 static char l_prefix2[512] = "";
66 static char l_fname[512] = "";
69 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
70 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
71 #define TIMEFORMAT_LEN 50
72 static char l_custom_format[TIMEFORMAT_LEN] = "";
73 static char *l_actual_format = l_old_default_format;
75 /** l_max_size tells when to rotate the log. Default is 1 GB
76 This is almost the same as never, but it saves applications in the
77 case of 2 or 4 GB file size limits..
79 static int l_max_size = 1024*1024*1024;
81 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
85 } mask_names[MAX_MASK_NAMES] =
87 { YLOG_FATAL, "fatal"},
88 { YLOG_DEBUG, "debug"},
89 { YLOG_WARN, "warn" },
92 { YLOG_MALLOC, "malloc"},
94 { YLOG_NOTIME, "notime" },
95 { YLOG_APP2, "app2" },
96 { YLOG_APP3, "app3" },
98 { YLOG_FLUSH, "flush" },
99 { YLOG_LOGLVL, "loglevel" },
102 /* the rest will be filled in if the user defines dynamic modules*/
105 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
107 static void internal_log_init(void)
109 static int mutex_init_flag = 0; /* not yet initialized */
114 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
116 env = getenv("YAZ_LOG");
118 l_level = yaz_log_mask_str_x(env, l_level);
122 FILE *yaz_log_file(void)
125 switch(yaz_file_type)
127 case use_stderr: f = stderr; break;
128 case use_none: f = 0; break;
129 case use_file: f = yaz_global_log_file; break;
134 void yaz_log_close(void)
136 if (yaz_file_type == use_file && yaz_global_log_file)
138 fclose(yaz_global_log_file);
139 yaz_global_log_file = 0;
143 void yaz_log_init_file(const char *fname)
151 yaz_file_type = use_stderr; /* empty name; use stderr */
153 yaz_file_type = use_file;
154 strncpy(l_fname, fname, sizeof(l_fname)-1);
155 l_fname[sizeof(l_fname)-1] = '\0';
159 yaz_file_type = use_none; /* NULL name; use no file at all */
165 static void rotate_log(const char *cur_fname)
170 /* windows can't rename a file if it is open */
173 for (i = 0; i<9; i++)
175 char fname_str[FILENAME_MAX];
176 struct stat stat_buf;
178 sprintf(fname_str, "%s.%d", cur_fname, i);
179 if (stat(fname_str, &stat_buf) != 0)
184 char fname_str[2][FILENAME_MAX];
187 sprintf(fname_str[0], "%s.%d", cur_fname, i-1);
189 sprintf(fname_str[0], "%s", cur_fname);
190 sprintf(fname_str[1], "%s.%d", cur_fname, i);
192 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
194 rename(fname_str[0], fname_str[1]);
200 void yaz_log_init_level(int level)
203 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
206 yaz_log_reopen(); /* make sure we set buffering right */
211 if (l_level & YLOG_LOGLVL)
212 { /* dump the log level bits */
213 const char *bittype = "Static ";
216 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
218 /* determine size of mask_names (locked) */
219 for (sz = 0; mask_names[sz].name; sz++)
221 /* second pass without lock */
222 for (i = 0; i < sz; i++)
223 if (mask_names[i].mask && *mask_names[i].name)
224 if (strcmp(mask_names[i].name, "all") != 0)
226 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
227 bittype, mask_names[i].mask, mask_names[i].name,
228 (level & mask_names[i].mask)? "ON": "off");
229 if (mask_names[i].mask > YLOG_LAST_BIT)
235 void yaz_log_init_prefix(const char *prefix)
237 if (prefix && *prefix)
238 sprintf(l_prefix, "%.511s ", prefix);
243 void yaz_log_init_prefix2(const char *prefix)
245 if (prefix && *prefix)
246 sprintf(l_prefix2, "%.511s ", prefix);
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_file_type != use_file)
294 if (l_fname && *l_fname)
296 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
297 if (strcmp(new_filename, cur_filename))
299 strcpy(cur_filename, new_filename);
304 if (l_max_size > 0 && yaz_global_log_file)
306 long flen = ftell(yaz_global_log_file);
307 if (flen > l_max_size)
309 rotate_log(cur_filename);
313 if (force && *cur_filename)
319 new_file = fopen(cur_filename, filemode);
323 yaz_global_log_file = new_file;
324 if (l_level & YLOG_FLUSH)
325 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
329 /* disable log rotate */
335 static void yaz_log_do_reopen(const char *filemode)
337 time_t cur_time = time(0);
339 struct tm tm0, *tm = &tm0;
345 localtime_r(&cur_time, tm);
347 tm = localtime(&cur_time);
349 yaz_log_open_check(tm, 1, filemode);
353 void yaz_log_reopen()
355 yaz_log_do_reopen("a");
360 yaz_log_do_reopen("w");
363 static void yaz_strftime(char *dst, size_t sz,
364 const char *fmt, const struct tm *tm)
366 strftime(dst, sz, fmt, tm);
369 static void yaz_log_to_file(int level, const char *log_message)
374 struct tm tm0, *tm = &tm0;
382 localtime_r(&ti, tm);
387 yaz_log_open_check(tm, 0, "a");
388 file = yaz_log_file(); /* file may change in yaz_log_open_check */
392 char tbuf[TIMEFORMAT_LEN];
397 for (i = 0; level && mask_names[i].name; i++)
398 if ( mask_names[i].mask & level)
400 if (*mask_names[i].name && mask_names[i].mask &&
401 mask_names[i].mask != YLOG_ALL)
403 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
404 level &= ~mask_names[i].mask;
409 if (!(l_level & YLOG_NOTIME))
411 yaz_strftime(tbuf, TIMEFORMAT_LEN-2, l_actual_format, tm);
412 tbuf[TIMEFORMAT_LEN-2] = '\0';
416 fprintf(file, "%s%s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
418 if (l_level & YLOG_FLUSH)
423 void yaz_log(int level, const char *fmt, ...)
431 if (!(level & l_level))
435 /* 30 is enough for our 'rest of output' message */
436 yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
437 if (strlen(buf) >= sizeof(buf)-31)
438 strcat(buf, " [rest of output omitted]");
440 if (o_level & YLOG_ERRNO)
443 yaz_strerror(buf+strlen(buf), 2048);
448 (*start_hook_func)(o_level, buf, start_hook_info);
450 (*hook_func)(o_level, buf, hook_info);
451 file = yaz_log_file();
453 yaz_log_to_file(level, buf);
455 (*end_hook_func)(o_level, buf, end_hook_info);
458 void yaz_log_time_format(const char *fmt)
461 { /* no format, default to new */
462 l_actual_format = l_new_default_format;
465 if (0==strcmp(fmt, "old"))
466 { /* force the old format */
467 l_actual_format = l_old_default_format;
470 /* else use custom format */
471 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
472 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
473 l_actual_format = l_custom_format;
476 /** cleans a loglevel name from leading paths and suffixes */
477 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
480 char *start = namebuf;
483 strncpy(namebuf, name, len);
485 while ((p = strchr(start, '/')))
487 if ((p = strrchr(start, '.')))
492 static int define_module_bit(const char *name)
496 for (i = 0; mask_names[i].name; i++)
497 if (0 == strcmp(mask_names[i].name, name))
499 return mask_names[i].mask;
501 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) ))
503 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
506 mask_names[i].mask = next_log_bit;
507 next_log_bit = next_log_bit<<1;
508 mask_names[i].name = (char *) malloc(strlen(name)+1);
509 strcpy(mask_names[i].name, name);
510 mask_names[i+1].name = NULL;
511 mask_names[i+1].mask = 0;
512 return mask_names[i].mask;
515 int yaz_log_module_level(const char *name)
519 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
522 for (i = 0; mask_names[i].name; i++)
523 if (0==strcmp(n, mask_names[i].name))
525 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
526 mask_names[i].mask, n,
527 strcmp(n,name) ? name : "");
528 return mask_names[i].mask;
530 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
531 strcmp(n, name) ? name : "" );
535 int yaz_log_mask_str(const char *str)
537 internal_log_init(); /* since l_level may be affected */
538 return yaz_log_mask_str_x(str, l_level);
541 int yaz_log_mask_str_x(const char *str, int level)
549 for (p = str; *p && *p != ','; p++)
556 if (isdigit(*(unsigned char *) str))
563 char *n = clean_name(str, p-str, clean, sizeof(clean));
564 int mask = define_module_bit(n);
566 level = 0; /* 'none' clears them all */
581 * indent-tabs-mode: nil
583 * vim: shiftwidth=4 tabstop=8 expandtab