2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.39 2006-09-27 11:39:02 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(void)
60 char *env = getenv("YAZ_LOG");
62 return yaz_log_mask_str_x(env, YLOG_DEFAULT_LEVEL);
64 return YLOG_DEFAULT_LEVEL;
68 static int l_level = -1; /* will be set from default_log_level() */
70 enum l_file_type { use_stderr, use_none, use_file };
71 static enum l_file_type yaz_file_type = use_stderr;
72 static FILE *yaz_global_log_file = NULL;
74 static void (*start_hook_func)(int, const char *, void *) = NULL;
75 static void *start_hook_info;
77 static void (*end_hook_func)(int, const char *, void *) = NULL;
78 static void *end_hook_info;
80 static void (*hook_func)(int, const char *, void *) = NULL;
81 static void *hook_info;
83 static char l_prefix[512] = "";
84 static char l_prefix2[512] = "";
85 static char l_fname[512] = "";
88 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
89 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
90 #define TIMEFORMAT_LEN 50
91 static char l_custom_format[TIMEFORMAT_LEN] = "";
92 static char *l_actual_format = l_old_default_format;
94 /** l_max_size tells when to rotate the log. Default to 1 GB */
95 static const int l_def_max_size = 1024*1024*1024;
96 static int l_max_size = 1024*1024*1024;
98 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
102 } mask_names[MAX_MASK_NAMES] =
104 { YLOG_FATAL, "fatal"},
105 { YLOG_DEBUG, "debug"},
106 { YLOG_WARN, "warn" },
109 { YLOG_MALLOC, "malloc"},
111 { YLOG_NOTIME, "notime" },
112 { YLOG_APP2, "app2" },
113 { YLOG_APP3, "app3" },
115 { YLOG_FLUSH, "flush" },
116 { YLOG_LOGLVL, "loglevel" },
119 /* the rest will be filled in if the user defines dynamic modules*/
122 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
124 static void init_mutex(void)
128 nmem_mutex_create(&log_mutex);
132 FILE *yaz_log_file(void)
135 switch(yaz_file_type)
137 case use_stderr: f = stderr; break;
138 case use_none: f = 0; break;
139 case use_file: f = yaz_global_log_file; break;
144 void yaz_log_close(void)
146 if (yaz_file_type == use_file && yaz_global_log_file)
148 fclose(yaz_global_log_file);
149 yaz_global_log_file = 0;
153 void yaz_log_init_file(const char *fname)
161 yaz_file_type = use_stderr; /* empty name; use stderr */
163 yaz_file_type = use_file;
164 strncpy(l_fname, fname, sizeof(l_fname)-1);
165 l_fname[sizeof(l_fname)-1] = '\0';
169 yaz_file_type = use_none; /* NULL name; use no file at all */
175 static void rotate_log(const char *cur_fname)
178 strncpy(newname, cur_fname, 509);
179 newname[509] = '\0'; /* make sure it is terminated */
180 strcat(newname, ".1");
182 /* windows can't rename a file if it is open */
183 fclose(yaz_global_log_file);
184 yaz_global_log_file = 0;
185 MoveFileEx(cur_fname, newname, MOVEFILE_REPLACE_EXISTING);
187 rename(cur_fname, newname);
192 void yaz_log_init_level(int level)
196 l_level = default_log_level();
197 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
200 yaz_log_reopen(); /* make sure we set buffering right */
204 if (l_level & YLOG_LOGLVL)
205 { /* dump the log level bits */
206 const char *bittype = "Static ";
209 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
211 /* determine size of mask_names (locked) */
212 nmem_mutex_enter(log_mutex);
213 for (sz = 0; mask_names[sz].name; sz++)
215 nmem_mutex_leave(log_mutex);
216 /* second pass without lock */
217 for (i = 0; i < sz; i++)
218 if (mask_names[i].mask && *mask_names[i].name)
219 if (strcmp(mask_names[i].name, "all") != 0)
221 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
222 bittype, mask_names[i].mask, mask_names[i].name,
223 (level & mask_names[i].mask)? "ON": "off");
224 if (mask_names[i].mask > YLOG_LAST_BIT)
230 void yaz_log_init_prefix(const char *prefix)
232 if (prefix && *prefix)
233 sprintf(l_prefix, "%.511s ", prefix);
238 void yaz_log_init_prefix2(const char *prefix)
240 if (prefix && *prefix)
241 sprintf(l_prefix2, "%.511s ", prefix);
246 void yaz_log_init(int level, const char *prefix, const char *fname)
249 yaz_log_init_level(level);
250 yaz_log_init_prefix(prefix);
252 yaz_log_init_file(fname);
255 void yaz_log_init_max_size(int mx)
258 l_max_size = l_def_max_size;
263 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
269 void log_event_start(void (*func)(int, const char *, void *), void *info)
271 start_hook_func = func;
272 start_hook_info = info;
275 void log_event_end(void (*func)(int, const char *, void *), void *info)
277 end_hook_func = func;
278 end_hook_info = info;
281 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
283 char new_filename[512];
284 static char cur_filename[512] = "";
286 if (l_fname && *l_fname)
288 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
289 if (strcmp(new_filename, cur_filename))
291 strcpy(cur_filename, new_filename);
296 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
298 long flen = ftell(yaz_global_log_file);
299 if (flen > l_max_size)
301 rotate_log(cur_filename);
305 if (force && yaz_file_type == use_file && *cur_filename)
308 yaz_global_log_file = fopen(cur_filename, filemode);
309 if (l_level < 0) l_level = default_log_level();
310 if (l_level & YLOG_FLUSH)
311 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
315 static void yaz_log_do_reopen(const char *filemode)
317 time_t cur_time = time(0);
319 struct tm tm0, *tm = &tm0;
324 nmem_mutex_enter(log_mutex);
326 localtime_r(&cur_time, tm);
328 tm = localtime(&cur_time);
330 yaz_log_open_check(tm, 1, filemode);
331 nmem_mutex_leave(log_mutex);
335 void yaz_log_reopen()
337 yaz_log_do_reopen("a");
342 yaz_log_do_reopen("w");
347 static void yaz_strftime(char *dst, size_t sz,
348 const char *fmt, const struct tm *tm)
350 const char *cp = strstr(fmt, "%!");
351 if (cp && strlen(fmt) < 60)
356 DWORD tid = GetCurrentThreadId();
359 #if YAZ_POSIX_THREADS
360 tid = pthread_self();
363 memcpy(fmt2, fmt, cp-fmt);
365 sprintf(tpidstr, "%08lx", (long) tid);
366 strcat(fmt2, tpidstr);
368 strftime(dst, sz, fmt2, tm);
371 strftime(dst, sz, fmt, tm);
374 static void yaz_log_to_file(int level, const char *log_message)
379 struct tm tm0, *tm = &tm0;
386 nmem_mutex_enter(log_mutex);
389 localtime_r(&ti, tm);
394 yaz_log_open_check(tm, 0, "a");
395 file = yaz_log_file(); /* file may change in yaz_log_open_check */
399 char tbuf[TIMEFORMAT_LEN];
404 for (i = 0; level && mask_names[i].name; i++)
405 if ( mask_names[i].mask & level)
407 if (*mask_names[i].name && mask_names[i].mask &&
408 mask_names[i].mask != YLOG_ALL)
410 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
411 level &= ~mask_names[i].mask;
415 if (l_level & YLOG_NOTIME)
418 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
419 tbuf[TIMEFORMAT_LEN-1] = '\0';
421 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
423 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
426 nmem_mutex_leave(log_mutex);
429 void yaz_log(int level, const char *fmt, ...)
437 l_level = default_log_level();
438 if (!(level & l_level))
442 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
446 vsnprintf(buf, sizeof(buf), fmt, ap);
448 vsprintf(buf, fmt, ap);
452 if (o_level & YLOG_ERRNO)
455 yaz_strerror(buf+strlen(buf), 2048);
460 (*start_hook_func)(o_level, buf, start_hook_info);
462 (*hook_func)(o_level, buf, hook_info);
463 file = yaz_log_file();
465 yaz_log_to_file(level, buf);
467 (*end_hook_func)(o_level, buf, end_hook_info);
470 void yaz_log_time_format(const char *fmt)
473 { /* no format, default to new */
474 l_actual_format = l_new_default_format;
477 if (0==strcmp(fmt, "old"))
478 { /* force the old format */
479 l_actual_format = l_old_default_format;
482 /* else use custom format */
483 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
484 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
485 l_actual_format = l_custom_format;
488 /** cleans a loglevel name from leading paths and suffixes */
489 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
492 char *start = namebuf;
495 strncpy(namebuf, name, len);
497 while ((p = strchr(start, '/')))
499 if ((p = strrchr(start, '.')))
504 static int define_module_bit(const char *name)
508 nmem_mutex_enter(log_mutex);
509 for (i = 0; mask_names[i].name; i++)
510 if (0 == strcmp(mask_names[i].name, name))
512 nmem_mutex_leave(log_mutex);
513 return mask_names[i].mask;
515 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
517 nmem_mutex_leave(log_mutex);
518 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
521 mask_names[i].mask = next_log_bit;
522 next_log_bit = next_log_bit<<1;
523 mask_names[i].name = malloc(strlen(name)+1);
524 strcpy(mask_names[i].name, name);
525 mask_names[i+1].name = NULL;
526 mask_names[i+1].mask = 0;
527 nmem_mutex_leave(log_mutex);
528 return mask_names[i].mask;
531 int yaz_log_module_level(const char *name)
535 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
538 nmem_mutex_enter(log_mutex);
539 for (i = 0; mask_names[i].name; i++)
540 if (0==strcmp(n, mask_names[i].name))
542 nmem_mutex_leave(log_mutex);
543 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
544 mask_names[i].mask, n,
545 strcmp(n,name) ? name : "");
546 return mask_names[i].mask;
548 nmem_mutex_leave(log_mutex);
549 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
550 strcmp(n, name) ? name : "" );
554 int yaz_log_mask_str(const char *str)
556 return yaz_log_mask_str_x(str, default_log_level());
559 int yaz_log_mask_str_x(const char *str, int level)
566 for (p = str; *p && *p != ','; p++)
573 if (isdigit(*(unsigned char *) str))
580 char *n = clean_name(str, p-str, clean, sizeof(clean));
581 int mask = define_module_bit(n);
583 level = 0; /* 'none' clears them all */
598 * indent-tabs-mode: nil
600 * vim: shiftwidth=4 tabstop=8 expandtab