2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.33 2006-03-21 13:58:50 adam Exp $
10 * \brief Implements logging utility
38 #include <yaz/xmalloc.h>
40 static NMEM_MUTEX log_mutex = 0;
41 static int mutex_init_flag = 0; /* not yet initialized */
43 #define HAS_STRERROR 1
51 extern char *sys_errlist[];
52 return sys_errlist[n];
58 static int default_log_level() {
59 char *env = getenv("YAZ_LOG");
61 return yaz_log_mask_str_x(env, YLOG_DEFAULT_LEVEL);
63 return YLOG_DEFAULT_LEVEL;
67 static int l_level = -1; /* will be set from default_log_level() */
69 enum l_file_type { use_stderr, use_none, use_file };
70 static enum l_file_type yaz_file_type = use_stderr;
71 static FILE *yaz_global_log_file = NULL;
73 static void (*start_hook_func)(int, const char *, void *) = NULL;
74 static void *start_hook_info;
76 static void (*end_hook_func)(int, const char *, void *) = NULL;
77 static void *end_hook_info;
79 static void (*hook_func)(int, const char *, void *) = NULL;
80 static void *hook_info;
82 static char l_prefix[512] = "";
83 static char l_prefix2[512] = "";
84 static char l_fname[512] = "";
87 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
88 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
89 #define TIMEFORMAT_LEN 50
90 static char l_custom_format[TIMEFORMAT_LEN] = "";
91 static char *l_actual_format = l_old_default_format;
93 /** l_max_size tells when to rotate the log. Default to 1 GB */
94 static const int l_def_max_size = 1024*1024*1024;
95 static int l_max_size = 1024*1024*1024;
97 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
101 } mask_names[MAX_MASK_NAMES] =
103 { YLOG_FATAL, "fatal"},
104 { YLOG_DEBUG, "debug"},
105 { YLOG_WARN, "warn" },
108 { YLOG_MALLOC, "malloc"},
110 { YLOG_NOTIME, "notime" },
111 { YLOG_APP2, "app2" },
112 { YLOG_APP3, "app3" },
114 { YLOG_FLUSH, "flush" },
115 { YLOG_LOGLVL, "loglevel" },
118 /* the rest will be filled in if the user defines dynamic modules*/
121 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
123 static void init_mutex()
127 nmem_mutex_create(&log_mutex);
131 FILE *yaz_log_file(void)
134 switch(yaz_file_type)
136 case use_stderr: f = stderr; break;
137 case use_none: f = 0; break;
138 case use_file: f = yaz_global_log_file; break;
145 if (yaz_file_type == use_file && yaz_global_log_file)
147 fclose(yaz_global_log_file);
148 yaz_global_log_file = 0;
152 void yaz_log_init_file(const char *fname)
160 yaz_file_type = use_stderr; /* empty name; use stderr */
162 yaz_file_type = use_file;
163 strncpy(l_fname, fname, sizeof(l_fname)-1);
164 l_fname[sizeof(l_fname)-1] = '\0';
168 yaz_file_type = use_none; /* NULL name; use no file at all */
174 static void rotate_log(const char *cur_fname)
177 strncpy(newname, cur_fname, 509);
178 newname[509] = '\0'; /* make sure it is terminated */
179 strcat(newname, ".1");
181 /* windows can't rename a file if it is open */
184 MoveFileEx(cur_fname, newname, MOVEFILE_REPLACE_EXISTING);
186 rename(cur_fname, newname);
190 void yaz_log_init_level(int level)
193 if (l_level < 0) l_level = default_log_level();
194 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
197 yaz_log_reopen(); /* make sure we set buffering right */
200 if (l_level & YLOG_LOGLVL)
201 { /* dump the log level bits */
202 const char *bittype = "Static ";
205 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
207 /* determine size of mask_names (locked) */
208 nmem_mutex_enter(log_mutex);
209 for (sz = 0; mask_names[sz].name; sz++)
211 nmem_mutex_leave(log_mutex);
212 /* second pass without lock */
213 for (i = 0; i < sz; i++)
214 if (mask_names[i].mask && *mask_names[i].name)
215 if (strcmp(mask_names[i].name, "all") != 0)
217 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
218 bittype, mask_names[i].mask, mask_names[i].name,
219 (level & mask_names[i].mask)? "ON": "off");
220 if (mask_names[i].mask > YLOG_LAST_BIT)
226 void yaz_log_init_prefix(const char *prefix)
228 if (prefix && *prefix)
229 sprintf(l_prefix, "%.511s ", prefix);
234 void yaz_log_init_prefix2(const char *prefix)
236 if (prefix && *prefix)
237 sprintf(l_prefix2, "%.511s ", prefix);
242 void yaz_log_init(int level, const char *prefix, const char *fname)
245 yaz_log_init_level(level);
246 yaz_log_init_prefix(prefix);
248 yaz_log_init_file(fname);
251 void yaz_log_init_max_size(int mx)
254 l_max_size = l_def_max_size;
259 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
265 void log_event_start(void (*func)(int, const char *, void *), void *info)
267 start_hook_func = func;
268 start_hook_info = info;
271 void log_event_end(void (*func)(int, const char *, void *), void *info)
273 end_hook_func = func;
274 end_hook_info = info;
277 static void yaz_log_open_check(struct tm *tm, int force)
279 char new_filename[512];
280 static char cur_filename[512] = "";
282 if (l_fname && *l_fname)
284 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
285 if (strcmp(new_filename, cur_filename))
287 strcpy(cur_filename, new_filename);
292 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
294 long flen = ftell(yaz_global_log_file);
295 if (flen > l_max_size)
297 rotate_log(cur_filename);
301 if (force && yaz_file_type == use_file && *cur_filename)
304 yaz_global_log_file = fopen(cur_filename, "a");
305 if (l_level < 0) l_level = default_log_level();
306 if (l_level & YLOG_FLUSH)
307 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
311 void yaz_log_reopen()
313 time_t cur_time = time(0);
315 struct tm tm0, *tm = &tm0;
320 nmem_mutex_enter(log_mutex);
322 localtime_r(&cur_time, tm);
324 tm = localtime(&cur_time);
326 yaz_log_open_check(tm, 1);
327 nmem_mutex_leave(log_mutex);
330 static void yaz_log_to_file(int level, FILE *file, const char *buf)
335 struct tm tm0, *tm = &tm0;
342 nmem_mutex_enter(log_mutex);
345 localtime_r(&ti, tm);
350 yaz_log_open_check(tm, 0);
351 file = yaz_log_file(); /* file may change in yaz_log_open_check */
355 char tbuf[TIMEFORMAT_LEN];
359 for (i = 0; level && mask_names[i].name; i++)
360 if ( mask_names[i].mask & level)
362 if (*mask_names[i].name && mask_names[i].mask &&
363 mask_names[i].mask != YLOG_ALL)
365 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
366 level &= ~mask_names[i].mask;
370 if (l_level & YLOG_NOTIME)
373 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
374 tbuf[TIMEFORMAT_LEN-1] = '\0';
376 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2, buf);
377 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
380 nmem_mutex_leave(log_mutex);
383 void yaz_log(int level, const char *fmt, ...)
391 l_level = default_log_level();
392 if (!(level & l_level))
396 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
400 vsnprintf(buf, sizeof(buf), fmt, ap);
402 vsprintf(buf, fmt, ap);
406 if (o_level & YLOG_ERRNO)
409 yaz_strerror(buf+strlen(buf), 2048);
414 (*start_hook_func)(o_level, buf, start_hook_info);
416 (*hook_func)(o_level, buf, hook_info);
417 file = yaz_log_file();
419 yaz_log_to_file(level, file, buf);
421 (*end_hook_func)(o_level, buf, end_hook_info);
424 void yaz_log_time_format(const char *fmt)
427 { /* no format, default to new */
428 l_actual_format = l_new_default_format;
431 if (0==strcmp(fmt, "old"))
432 { /* force the old format */
433 l_actual_format = l_old_default_format;
436 /* else use custom format */
437 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
438 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
439 l_actual_format = l_custom_format;
442 /** cleans a loglevel name from leading paths and suffixes */
443 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
446 char *start = namebuf;
449 strncpy(namebuf, name, len);
451 while ((p = strchr(start, '/')))
453 if ((p = strrchr(start, '.')))
458 static int define_module_bit(const char *name)
462 nmem_mutex_enter(log_mutex);
463 for (i = 0; mask_names[i].name; i++)
464 if (0 == strcmp(mask_names[i].name, name))
466 nmem_mutex_leave(log_mutex);
467 return mask_names[i].mask;
469 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
471 nmem_mutex_leave(log_mutex);
472 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
475 mask_names[i].mask = next_log_bit;
476 next_log_bit = next_log_bit<<1;
477 mask_names[i].name = malloc(strlen(name)+1);
478 strcpy(mask_names[i].name, name);
479 mask_names[i+1].name = NULL;
480 mask_names[i+1].mask = 0;
481 nmem_mutex_leave(log_mutex);
482 return mask_names[i].mask;
485 int yaz_log_module_level(const char *name)
489 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
492 nmem_mutex_enter(log_mutex);
493 for (i = 0; mask_names[i].name; i++)
494 if (0==strcmp(n, mask_names[i].name))
496 nmem_mutex_leave(log_mutex);
497 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
498 mask_names[i].mask, n,
499 strcmp(n,name) ? name : "");
500 return mask_names[i].mask;
502 nmem_mutex_leave(log_mutex);
503 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
504 strcmp(n, name) ? name : "" );
508 int yaz_log_mask_str(const char *str)
510 return yaz_log_mask_str_x(str, default_log_level());
513 int yaz_log_mask_str_x(const char *str, int level)
520 for (p = str; *p && *p != ','; p++)
527 if (isdigit(*(unsigned char *) str))
534 char *n = clean_name(str, p-str, clean, sizeof(clean));
535 int mask = define_module_bit(n);
537 level = 0; /* 'none' clears them all */
552 * indent-tabs-mode: nil
554 * vim: shiftwidth=4 tabstop=8 expandtab