2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.38 2006-07-31 10:05:04 adam Exp $
10 * \brief 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 */
182 fclose(yaz_global_log_file);
183 yaz_global_log_file = 0;
184 MoveFileEx(cur_fname, newname, MOVEFILE_REPLACE_EXISTING);
186 rename(cur_fname, newname);
191 void yaz_log_init_level(int level)
195 l_level = default_log_level();
196 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
199 yaz_log_reopen(); /* make sure we set buffering right */
203 if (l_level & YLOG_LOGLVL)
204 { /* dump the log level bits */
205 const char *bittype = "Static ";
208 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
210 /* determine size of mask_names (locked) */
211 nmem_mutex_enter(log_mutex);
212 for (sz = 0; mask_names[sz].name; sz++)
214 nmem_mutex_leave(log_mutex);
215 /* second pass without lock */
216 for (i = 0; i < sz; i++)
217 if (mask_names[i].mask && *mask_names[i].name)
218 if (strcmp(mask_names[i].name, "all") != 0)
220 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
221 bittype, mask_names[i].mask, mask_names[i].name,
222 (level & mask_names[i].mask)? "ON": "off");
223 if (mask_names[i].mask > YLOG_LAST_BIT)
229 void yaz_log_init_prefix(const char *prefix)
231 if (prefix && *prefix)
232 sprintf(l_prefix, "%.511s ", prefix);
237 void yaz_log_init_prefix2(const char *prefix)
239 if (prefix && *prefix)
240 sprintf(l_prefix2, "%.511s ", prefix);
245 void yaz_log_init(int level, const char *prefix, const char *fname)
248 yaz_log_init_level(level);
249 yaz_log_init_prefix(prefix);
251 yaz_log_init_file(fname);
254 void yaz_log_init_max_size(int mx)
257 l_max_size = l_def_max_size;
262 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
268 void log_event_start(void (*func)(int, const char *, void *), void *info)
270 start_hook_func = func;
271 start_hook_info = info;
274 void log_event_end(void (*func)(int, const char *, void *), void *info)
276 end_hook_func = func;
277 end_hook_info = info;
280 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
282 char new_filename[512];
283 static char cur_filename[512] = "";
285 if (l_fname && *l_fname)
287 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
288 if (strcmp(new_filename, cur_filename))
290 strcpy(cur_filename, new_filename);
295 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
297 long flen = ftell(yaz_global_log_file);
298 if (flen > l_max_size)
300 rotate_log(cur_filename);
304 if (force && yaz_file_type == use_file && *cur_filename)
307 yaz_global_log_file = fopen(cur_filename, filemode);
308 if (l_level < 0) l_level = default_log_level();
309 if (l_level & YLOG_FLUSH)
310 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
314 static void yaz_log_do_reopen(const char *filemode)
316 time_t cur_time = time(0);
318 struct tm tm0, *tm = &tm0;
323 nmem_mutex_enter(log_mutex);
325 localtime_r(&cur_time, tm);
327 tm = localtime(&cur_time);
329 yaz_log_open_check(tm, 1, filemode);
330 nmem_mutex_leave(log_mutex);
334 void yaz_log_reopen()
336 yaz_log_do_reopen("a");
341 yaz_log_do_reopen("w");
346 static void yaz_strftime(char *dst, size_t sz,
347 const char *fmt, const struct tm *tm)
349 const char *cp = strstr(fmt, "%!");
350 if (cp && strlen(fmt) < 60)
355 DWORD tid = GetCurrentThreadId();
358 #if YAZ_POSIX_THREADS
359 tid = pthread_self();
362 memcpy(fmt2, fmt, cp-fmt);
364 sprintf(tpidstr, "%08lx", (long) tid);
365 strcat(fmt2, tpidstr);
367 strftime(dst, sz, fmt2, 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;
385 nmem_mutex_enter(log_mutex);
388 localtime_r(&ti, tm);
393 yaz_log_open_check(tm, 0, "a");
394 file = yaz_log_file(); /* file may change in yaz_log_open_check */
398 char tbuf[TIMEFORMAT_LEN];
403 for (i = 0; level && mask_names[i].name; i++)
404 if ( mask_names[i].mask & level)
406 if (*mask_names[i].name && mask_names[i].mask &&
407 mask_names[i].mask != YLOG_ALL)
409 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
410 level &= ~mask_names[i].mask;
414 if (l_level & YLOG_NOTIME)
417 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
418 tbuf[TIMEFORMAT_LEN-1] = '\0';
420 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
422 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
425 nmem_mutex_leave(log_mutex);
428 void yaz_log(int level, const char *fmt, ...)
436 l_level = default_log_level();
437 if (!(level & l_level))
441 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
445 vsnprintf(buf, sizeof(buf), fmt, ap);
447 vsprintf(buf, fmt, ap);
451 if (o_level & YLOG_ERRNO)
454 yaz_strerror(buf+strlen(buf), 2048);
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 nmem_mutex_enter(log_mutex);
508 for (i = 0; mask_names[i].name; i++)
509 if (0 == strcmp(mask_names[i].name, name))
511 nmem_mutex_leave(log_mutex);
512 return mask_names[i].mask;
514 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
516 nmem_mutex_leave(log_mutex);
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 = 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 nmem_mutex_leave(log_mutex);
527 return mask_names[i].mask;
530 int yaz_log_module_level(const char *name)
534 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
537 nmem_mutex_enter(log_mutex);
538 for (i = 0; mask_names[i].name; i++)
539 if (0==strcmp(n, mask_names[i].name))
541 nmem_mutex_leave(log_mutex);
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 nmem_mutex_leave(log_mutex);
548 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
549 strcmp(n, name) ? name : "" );
553 int yaz_log_mask_str(const char *str)
555 return yaz_log_mask_str_x(str, default_log_level());
558 int yaz_log_mask_str_x(const char *str, int level)
565 for (p = str; *p && *p != ','; p++)
572 if (isdigit(*(unsigned char *) str))
579 char *n = clean_name(str, p-str, clean, sizeof(clean));
580 int mask = define_module_bit(n);
582 level = 0; /* 'none' clears them all */
597 * indent-tabs-mode: nil
599 * vim: shiftwidth=4 tabstop=8 expandtab