2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.34 2006-03-24 13:58:43 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 */
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();
343 memcpy(fmt2, fmt, cp-fmt);
345 sprintf(tpidstr, "%08lx", (long) tid);
346 strcat(fmt2, tpidstr);
348 strftime(dst, sz, fmt2, tm);
351 strftime(dst, sz, fmt, tm);
354 static void yaz_log_to_file(int level, FILE *file, const char *buf)
359 struct tm tm0, *tm = &tm0;
366 nmem_mutex_enter(log_mutex);
369 localtime_r(&ti, tm);
374 yaz_log_open_check(tm, 0);
375 file = yaz_log_file(); /* file may change in yaz_log_open_check */
379 char tbuf[TIMEFORMAT_LEN];
383 for (i = 0; level && mask_names[i].name; i++)
384 if ( mask_names[i].mask & level)
386 if (*mask_names[i].name && mask_names[i].mask &&
387 mask_names[i].mask != YLOG_ALL)
389 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
390 level &= ~mask_names[i].mask;
394 if (l_level & YLOG_NOTIME)
397 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
398 tbuf[TIMEFORMAT_LEN-1] = '\0';
400 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2, buf);
401 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
404 nmem_mutex_leave(log_mutex);
407 void yaz_log(int level, const char *fmt, ...)
415 l_level = default_log_level();
416 if (!(level & l_level))
420 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
424 vsnprintf(buf, sizeof(buf), fmt, ap);
426 vsprintf(buf, fmt, ap);
430 if (o_level & YLOG_ERRNO)
433 yaz_strerror(buf+strlen(buf), 2048);
438 (*start_hook_func)(o_level, buf, start_hook_info);
440 (*hook_func)(o_level, buf, hook_info);
441 file = yaz_log_file();
443 yaz_log_to_file(level, file, buf);
445 (*end_hook_func)(o_level, buf, end_hook_info);
448 void yaz_log_time_format(const char *fmt)
451 { /* no format, default to new */
452 l_actual_format = l_new_default_format;
455 if (0==strcmp(fmt, "old"))
456 { /* force the old format */
457 l_actual_format = l_old_default_format;
460 /* else use custom format */
461 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
462 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
463 l_actual_format = l_custom_format;
466 /** cleans a loglevel name from leading paths and suffixes */
467 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
470 char *start = namebuf;
473 strncpy(namebuf, name, len);
475 while ((p = strchr(start, '/')))
477 if ((p = strrchr(start, '.')))
482 static int define_module_bit(const char *name)
486 nmem_mutex_enter(log_mutex);
487 for (i = 0; mask_names[i].name; i++)
488 if (0 == strcmp(mask_names[i].name, name))
490 nmem_mutex_leave(log_mutex);
491 return mask_names[i].mask;
493 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
495 nmem_mutex_leave(log_mutex);
496 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
499 mask_names[i].mask = next_log_bit;
500 next_log_bit = next_log_bit<<1;
501 mask_names[i].name = malloc(strlen(name)+1);
502 strcpy(mask_names[i].name, name);
503 mask_names[i+1].name = NULL;
504 mask_names[i+1].mask = 0;
505 nmem_mutex_leave(log_mutex);
506 return mask_names[i].mask;
509 int yaz_log_module_level(const char *name)
513 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
516 nmem_mutex_enter(log_mutex);
517 for (i = 0; mask_names[i].name; i++)
518 if (0==strcmp(n, mask_names[i].name))
520 nmem_mutex_leave(log_mutex);
521 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
522 mask_names[i].mask, n,
523 strcmp(n,name) ? name : "");
524 return mask_names[i].mask;
526 nmem_mutex_leave(log_mutex);
527 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
528 strcmp(n, name) ? name : "" );
532 int yaz_log_mask_str(const char *str)
534 return yaz_log_mask_str_x(str, default_log_level());
537 int yaz_log_mask_str_x(const char *str, int level)
544 for (p = str; *p && *p != ','; p++)
551 if (isdigit(*(unsigned char *) str))
558 char *n = clean_name(str, p-str, clean, sizeof(clean));
559 int mask = define_module_bit(n);
561 level = 0; /* 'none' clears them all */
576 * indent-tabs-mode: nil
578 * vim: shiftwidth=4 tabstop=8 expandtab