2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.37 2006-07-06 13:10:31 heikki 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)
194 if (l_level < 0) l_level = default_log_level();
195 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
198 yaz_log_reopen(); /* make sure we set buffering right */
201 if (l_level & YLOG_LOGLVL)
202 { /* dump the log level bits */
203 const char *bittype = "Static ";
206 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
208 /* determine size of mask_names (locked) */
209 nmem_mutex_enter(log_mutex);
210 for (sz = 0; mask_names[sz].name; sz++)
212 nmem_mutex_leave(log_mutex);
213 /* second pass without lock */
214 for (i = 0; i < sz; i++)
215 if (mask_names[i].mask && *mask_names[i].name)
216 if (strcmp(mask_names[i].name, "all") != 0)
218 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
219 bittype, mask_names[i].mask, mask_names[i].name,
220 (level & mask_names[i].mask)? "ON": "off");
221 if (mask_names[i].mask > YLOG_LAST_BIT)
227 void yaz_log_init_prefix(const char *prefix)
229 if (prefix && *prefix)
230 sprintf(l_prefix, "%.511s ", prefix);
235 void yaz_log_init_prefix2(const char *prefix)
237 if (prefix && *prefix)
238 sprintf(l_prefix2, "%.511s ", prefix);
243 void yaz_log_init(int level, const char *prefix, const char *fname)
246 yaz_log_init_level(level);
247 yaz_log_init_prefix(prefix);
249 yaz_log_init_file(fname);
252 void yaz_log_init_max_size(int mx)
255 l_max_size = l_def_max_size;
260 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
266 void log_event_start(void (*func)(int, const char *, void *), void *info)
268 start_hook_func = func;
269 start_hook_info = info;
272 void log_event_end(void (*func)(int, const char *, void *), void *info)
274 end_hook_func = func;
275 end_hook_info = info;
278 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
280 char new_filename[512];
281 static char cur_filename[512] = "";
283 if (l_fname && *l_fname)
285 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
286 if (strcmp(new_filename, cur_filename))
288 strcpy(cur_filename, new_filename);
293 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
295 long flen = ftell(yaz_global_log_file);
296 if (flen > l_max_size)
298 rotate_log(cur_filename);
302 if (force && yaz_file_type == use_file && *cur_filename)
305 yaz_global_log_file = fopen(cur_filename, filemode);
306 if (l_level < 0) l_level = default_log_level();
307 if (l_level & YLOG_FLUSH)
308 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
312 static void yaz_log_do_reopen(const char *filemode)
314 time_t cur_time = time(0);
316 struct tm tm0, *tm = &tm0;
321 nmem_mutex_enter(log_mutex);
323 localtime_r(&cur_time, tm);
325 tm = localtime(&cur_time);
327 yaz_log_open_check(tm, 1, filemode );
328 nmem_mutex_leave(log_mutex);
332 void yaz_log_reopen()
334 yaz_log_do_reopen("a");
339 yaz_log_do_reopen("w");
344 static void yaz_strftime(char *dst, size_t sz,
345 const char *fmt, const struct tm *tm)
347 const char *cp = strstr(fmt, "%!");
348 if (cp && strlen(fmt) < 60)
353 DWORD tid = GetCurrentThreadId();
356 #if YAZ_POSIX_THREADS
357 tid = pthread_self();
360 memcpy(fmt2, fmt, cp-fmt);
362 sprintf(tpidstr, "%08lx", (long) tid);
363 strcat(fmt2, tpidstr);
365 strftime(dst, sz, fmt2, tm);
368 strftime(dst, sz, fmt, tm);
371 static void yaz_log_to_file(int level, FILE *file, const char *buf)
376 struct tm tm0, *tm = &tm0;
383 nmem_mutex_enter(log_mutex);
386 localtime_r(&ti, tm);
391 yaz_log_open_check(tm, 0, "a");
392 file = yaz_log_file(); /* file may change in yaz_log_open_check */
396 char tbuf[TIMEFORMAT_LEN];
400 for (i = 0; level && mask_names[i].name; i++)
401 if ( mask_names[i].mask & level)
403 if (*mask_names[i].name && mask_names[i].mask &&
404 mask_names[i].mask != YLOG_ALL)
406 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
407 level &= ~mask_names[i].mask;
411 if (l_level & YLOG_NOTIME)
414 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
415 tbuf[TIMEFORMAT_LEN-1] = '\0';
417 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2, buf);
418 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
421 nmem_mutex_leave(log_mutex);
424 void yaz_log(int level, const char *fmt, ...)
432 l_level = default_log_level();
433 if (!(level & l_level))
437 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
441 vsnprintf(buf, sizeof(buf), fmt, ap);
443 vsprintf(buf, fmt, ap);
447 if (o_level & YLOG_ERRNO)
450 yaz_strerror(buf+strlen(buf), 2048);
455 (*start_hook_func)(o_level, buf, start_hook_info);
457 (*hook_func)(o_level, buf, hook_info);
458 file = yaz_log_file();
460 yaz_log_to_file(level, file, buf);
462 (*end_hook_func)(o_level, buf, end_hook_info);
465 void yaz_log_time_format(const char *fmt)
468 { /* no format, default to new */
469 l_actual_format = l_new_default_format;
472 if (0==strcmp(fmt, "old"))
473 { /* force the old format */
474 l_actual_format = l_old_default_format;
477 /* else use custom format */
478 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
479 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
480 l_actual_format = l_custom_format;
483 /** cleans a loglevel name from leading paths and suffixes */
484 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
487 char *start = namebuf;
490 strncpy(namebuf, name, len);
492 while ((p = strchr(start, '/')))
494 if ((p = strrchr(start, '.')))
499 static int define_module_bit(const char *name)
503 nmem_mutex_enter(log_mutex);
504 for (i = 0; mask_names[i].name; i++)
505 if (0 == strcmp(mask_names[i].name, name))
507 nmem_mutex_leave(log_mutex);
508 return mask_names[i].mask;
510 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
512 nmem_mutex_leave(log_mutex);
513 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
516 mask_names[i].mask = next_log_bit;
517 next_log_bit = next_log_bit<<1;
518 mask_names[i].name = malloc(strlen(name)+1);
519 strcpy(mask_names[i].name, name);
520 mask_names[i+1].name = NULL;
521 mask_names[i+1].mask = 0;
522 nmem_mutex_leave(log_mutex);
523 return mask_names[i].mask;
526 int yaz_log_module_level(const char *name)
530 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
533 nmem_mutex_enter(log_mutex);
534 for (i = 0; mask_names[i].name; i++)
535 if (0==strcmp(n, mask_names[i].name))
537 nmem_mutex_leave(log_mutex);
538 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
539 mask_names[i].mask, n,
540 strcmp(n,name) ? name : "");
541 return mask_names[i].mask;
543 nmem_mutex_leave(log_mutex);
544 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
545 strcmp(n, name) ? name : "" );
549 int yaz_log_mask_str(const char *str)
551 return yaz_log_mask_str_x(str, default_log_level());
554 int yaz_log_mask_str_x(const char *str, int level)
561 for (p = str; *p && *p != ','; p++)
568 if (isdigit(*(unsigned char *) str))
575 char *n = clean_name(str, p-str, clean, sizeof(clean));
576 int mask = define_module_bit(n);
578 level = 0; /* 'none' clears them all */
593 * indent-tabs-mode: nil
595 * vim: shiftwidth=4 tabstop=8 expandtab