2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.36 2006-06-30 11:09:44 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);
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_strftime(char *dst, size_t sz,
331 const char *fmt, const struct tm *tm)
333 const char *cp = strstr(fmt, "%!");
334 if (cp && strlen(fmt) < 60)
339 DWORD tid = GetCurrentThreadId();
342 #if YAZ_POSIX_THREADS
343 tid = pthread_self();
346 memcpy(fmt2, fmt, cp-fmt);
348 sprintf(tpidstr, "%08lx", (long) tid);
349 strcat(fmt2, tpidstr);
351 strftime(dst, sz, fmt2, tm);
354 strftime(dst, sz, fmt, tm);
357 static void yaz_log_to_file(int level, FILE *file, const char *buf)
362 struct tm tm0, *tm = &tm0;
369 nmem_mutex_enter(log_mutex);
372 localtime_r(&ti, tm);
377 yaz_log_open_check(tm, 0);
378 file = yaz_log_file(); /* file may change in yaz_log_open_check */
382 char tbuf[TIMEFORMAT_LEN];
386 for (i = 0; level && mask_names[i].name; i++)
387 if ( mask_names[i].mask & level)
389 if (*mask_names[i].name && mask_names[i].mask &&
390 mask_names[i].mask != YLOG_ALL)
392 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
393 level &= ~mask_names[i].mask;
397 if (l_level & YLOG_NOTIME)
400 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
401 tbuf[TIMEFORMAT_LEN-1] = '\0';
403 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2, buf);
404 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
407 nmem_mutex_leave(log_mutex);
410 void yaz_log(int level, const char *fmt, ...)
418 l_level = default_log_level();
419 if (!(level & l_level))
423 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
427 vsnprintf(buf, sizeof(buf), fmt, ap);
429 vsprintf(buf, fmt, ap);
433 if (o_level & YLOG_ERRNO)
436 yaz_strerror(buf+strlen(buf), 2048);
441 (*start_hook_func)(o_level, buf, start_hook_info);
443 (*hook_func)(o_level, buf, hook_info);
444 file = yaz_log_file();
446 yaz_log_to_file(level, file, buf);
448 (*end_hook_func)(o_level, buf, end_hook_info);
451 void yaz_log_time_format(const char *fmt)
454 { /* no format, default to new */
455 l_actual_format = l_new_default_format;
458 if (0==strcmp(fmt, "old"))
459 { /* force the old format */
460 l_actual_format = l_old_default_format;
463 /* else use custom format */
464 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
465 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
466 l_actual_format = l_custom_format;
469 /** cleans a loglevel name from leading paths and suffixes */
470 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
473 char *start = namebuf;
476 strncpy(namebuf, name, len);
478 while ((p = strchr(start, '/')))
480 if ((p = strrchr(start, '.')))
485 static int define_module_bit(const char *name)
489 nmem_mutex_enter(log_mutex);
490 for (i = 0; mask_names[i].name; i++)
491 if (0 == strcmp(mask_names[i].name, name))
493 nmem_mutex_leave(log_mutex);
494 return mask_names[i].mask;
496 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
498 nmem_mutex_leave(log_mutex);
499 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
502 mask_names[i].mask = next_log_bit;
503 next_log_bit = next_log_bit<<1;
504 mask_names[i].name = malloc(strlen(name)+1);
505 strcpy(mask_names[i].name, name);
506 mask_names[i+1].name = NULL;
507 mask_names[i+1].mask = 0;
508 nmem_mutex_leave(log_mutex);
509 return mask_names[i].mask;
512 int yaz_log_module_level(const char *name)
516 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
519 nmem_mutex_enter(log_mutex);
520 for (i = 0; mask_names[i].name; i++)
521 if (0==strcmp(n, mask_names[i].name))
523 nmem_mutex_leave(log_mutex);
524 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
525 mask_names[i].mask, n,
526 strcmp(n,name) ? name : "");
527 return mask_names[i].mask;
529 nmem_mutex_leave(log_mutex);
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 return yaz_log_mask_str_x(str, default_log_level());
540 int yaz_log_mask_str_x(const char *str, int level)
547 for (p = str; *p && *p != ','; p++)
554 if (isdigit(*(unsigned char *) str))
561 char *n = clean_name(str, p-str, clean, sizeof(clean));
562 int mask = define_module_bit(n);
564 level = 0; /* 'none' clears them all */
579 * indent-tabs-mode: nil
581 * vim: shiftwidth=4 tabstop=8 expandtab