2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.43 2006-11-27 14:09:32 adam Exp $
10 * \brief Logging utility
39 #include <yaz/xmalloc.h>
41 static NMEM_MUTEX log_mutex = 0;
43 #define HAS_STRERROR 1
51 extern char *sys_errlist[];
52 return sys_errlist[n];
58 static int l_level = YLOG_DEFAULT_LEVEL;
60 enum l_file_type { use_stderr, use_none, use_file };
61 static enum l_file_type yaz_file_type = use_stderr;
62 static FILE *yaz_global_log_file = NULL;
64 static void (*start_hook_func)(int, const char *, void *) = NULL;
65 static void *start_hook_info;
67 static void (*end_hook_func)(int, const char *, void *) = NULL;
68 static void *end_hook_info;
70 static void (*hook_func)(int, const char *, void *) = NULL;
71 static void *hook_info;
73 static char l_prefix[512] = "";
74 static char l_prefix2[512] = "";
75 static char l_fname[512] = "";
78 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
79 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
80 #define TIMEFORMAT_LEN 50
81 static char l_custom_format[TIMEFORMAT_LEN] = "";
82 static char *l_actual_format = l_old_default_format;
84 /** l_max_size tells when to rotate the log. Default is 1 GB
85 This is almost the same as never, but it saves applications in the
86 case of 2 or 4 GB file size limits..
88 static int l_max_size = 1024*1024*1024;
90 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
94 } mask_names[MAX_MASK_NAMES] =
96 { YLOG_FATAL, "fatal"},
97 { YLOG_DEBUG, "debug"},
98 { YLOG_WARN, "warn" },
101 { YLOG_MALLOC, "malloc"},
103 { YLOG_NOTIME, "notime" },
104 { YLOG_APP2, "app2" },
105 { YLOG_APP3, "app3" },
107 { YLOG_FLUSH, "flush" },
108 { YLOG_LOGLVL, "loglevel" },
111 /* the rest will be filled in if the user defines dynamic modules*/
114 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
116 static void internal_log_init(void)
118 static int mutex_init_flag = 0; /* not yet initialized */
123 mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */
125 nmem_mutex_create(&log_mutex);
127 env = getenv("YAZ_LOG");
129 l_level = yaz_log_mask_str_x(env, l_level);
133 FILE *yaz_log_file(void)
136 switch(yaz_file_type)
138 case use_stderr: f = stderr; break;
139 case use_none: f = 0; break;
140 case use_file: f = yaz_global_log_file; break;
145 void yaz_log_close(void)
147 if (yaz_file_type == use_file && yaz_global_log_file)
149 fclose(yaz_global_log_file);
150 yaz_global_log_file = 0;
154 void yaz_log_init_file(const char *fname)
162 yaz_file_type = use_stderr; /* empty name; use stderr */
164 yaz_file_type = use_file;
165 strncpy(l_fname, fname, sizeof(l_fname)-1);
166 l_fname[sizeof(l_fname)-1] = '\0';
170 yaz_file_type = use_none; /* NULL name; use no file at all */
176 static void rotate_log(const char *cur_fname)
180 /* windows can't rename a file if it is open */
181 fclose(yaz_global_log_file);
182 yaz_global_log_file = 0;
184 for (i = 0; i<9; i++)
186 char fname_str[FILENAME_MAX];
187 struct stat stat_buf;
189 sprintf(fname_str, "%s.%d", cur_fname, i);
190 if (stat(fname_str, &stat_buf) != 0)
195 char fname_str[2][FILENAME_MAX];
198 sprintf(fname_str[0], "%s.%d", cur_fname, i-1);
200 sprintf(fname_str[0], "%s", cur_fname);
201 sprintf(fname_str[1], "%s.%d", cur_fname, i);
203 MoveFileEx(fname_str[0], fname_str[1], MOVEFILE_REPLACE_EXISTING);
205 rename(fname_str[0], fname_str[1]);
211 void yaz_log_init_level(int level)
214 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
217 yaz_log_reopen(); /* make sure we set buffering right */
222 if (l_level & YLOG_LOGLVL)
223 { /* dump the log level bits */
224 const char *bittype = "Static ";
227 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
229 /* determine size of mask_names (locked) */
230 nmem_mutex_enter(log_mutex);
231 for (sz = 0; mask_names[sz].name; sz++)
233 nmem_mutex_leave(log_mutex);
234 /* second pass without lock */
235 for (i = 0; i < sz; i++)
236 if (mask_names[i].mask && *mask_names[i].name)
237 if (strcmp(mask_names[i].name, "all") != 0)
239 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
240 bittype, mask_names[i].mask, mask_names[i].name,
241 (level & mask_names[i].mask)? "ON": "off");
242 if (mask_names[i].mask > YLOG_LAST_BIT)
248 void yaz_log_init_prefix(const char *prefix)
250 if (prefix && *prefix)
251 sprintf(l_prefix, "%.511s ", prefix);
256 void yaz_log_init_prefix2(const char *prefix)
258 if (prefix && *prefix)
259 sprintf(l_prefix2, "%.511s ", prefix);
264 void yaz_log_init(int level, const char *prefix, const char *fname)
267 yaz_log_init_level(level);
268 yaz_log_init_prefix(prefix);
270 yaz_log_init_file(fname);
273 void yaz_log_init_max_size(int mx)
281 void yaz_log_set_handler(void (*func)(int, const char *, void *), void *info)
287 void log_event_start(void (*func)(int, const char *, void *), void *info)
289 start_hook_func = func;
290 start_hook_info = info;
293 void log_event_end(void (*func)(int, const char *, void *), void *info)
295 end_hook_func = func;
296 end_hook_info = info;
299 static void yaz_log_open_check(struct tm *tm, int force, const char *filemode)
301 char new_filename[512];
302 static char cur_filename[512] = "";
304 if (l_fname && *l_fname)
306 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
307 if (strcmp(new_filename, cur_filename))
309 strcpy(cur_filename, new_filename);
314 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
316 long flen = ftell(yaz_global_log_file);
317 if (flen > l_max_size)
319 rotate_log(cur_filename);
323 if (force && yaz_file_type == use_file && *cur_filename)
326 yaz_global_log_file = fopen(cur_filename, filemode);
327 if (l_level & YLOG_FLUSH)
328 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
332 static void yaz_log_do_reopen(const char *filemode)
334 time_t cur_time = time(0);
336 struct tm tm0, *tm = &tm0;
341 nmem_mutex_enter(log_mutex);
343 localtime_r(&cur_time, tm);
345 tm = localtime(&cur_time);
347 yaz_log_open_check(tm, 1, filemode);
348 nmem_mutex_leave(log_mutex);
352 void yaz_log_reopen()
354 yaz_log_do_reopen("a");
359 yaz_log_do_reopen("w");
364 static void yaz_strftime(char *dst, size_t sz,
365 const char *fmt, const struct tm *tm)
367 const char *cp = strstr(fmt, "%!");
368 if (cp && strlen(fmt) < 60)
373 DWORD tid = GetCurrentThreadId();
376 #if YAZ_POSIX_THREADS
377 tid = pthread_self();
380 memcpy(fmt2, fmt, cp-fmt);
382 sprintf(tpidstr, "%08lx", (long) tid);
383 strcat(fmt2, tpidstr);
385 strftime(dst, sz, fmt2, tm);
388 strftime(dst, sz, fmt, tm);
391 static void yaz_log_to_file(int level, const char *log_message)
396 struct tm tm0, *tm = &tm0;
403 nmem_mutex_enter(log_mutex);
406 localtime_r(&ti, tm);
411 yaz_log_open_check(tm, 0, "a");
412 file = yaz_log_file(); /* file may change in yaz_log_open_check */
416 char tbuf[TIMEFORMAT_LEN];
421 for (i = 0; level && mask_names[i].name; i++)
422 if ( mask_names[i].mask & level)
424 if (*mask_names[i].name && mask_names[i].mask &&
425 mask_names[i].mask != YLOG_ALL)
427 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
428 level &= ~mask_names[i].mask;
432 if (l_level & YLOG_NOTIME)
435 yaz_strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
436 tbuf[TIMEFORMAT_LEN-1] = '\0';
438 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2,
440 if (l_level & YLOG_FLUSH)
443 nmem_mutex_leave(log_mutex);
446 void yaz_log(int level, const char *fmt, ...)
454 if (!(level & l_level))
458 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
462 vsnprintf(buf, sizeof(buf), fmt, ap);
464 vsprintf(buf, fmt, ap);
468 if (o_level & YLOG_ERRNO)
471 yaz_strerror(buf+strlen(buf), 2048);
476 (*start_hook_func)(o_level, buf, start_hook_info);
478 (*hook_func)(o_level, buf, hook_info);
479 file = yaz_log_file();
481 yaz_log_to_file(level, buf);
483 (*end_hook_func)(o_level, buf, end_hook_info);
486 void yaz_log_time_format(const char *fmt)
489 { /* no format, default to new */
490 l_actual_format = l_new_default_format;
493 if (0==strcmp(fmt, "old"))
494 { /* force the old format */
495 l_actual_format = l_old_default_format;
498 /* else use custom format */
499 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
500 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
501 l_actual_format = l_custom_format;
504 /** cleans a loglevel name from leading paths and suffixes */
505 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
508 char *start = namebuf;
511 strncpy(namebuf, name, len);
513 while ((p = strchr(start, '/')))
515 if ((p = strrchr(start, '.')))
520 static int define_module_bit(const char *name)
524 nmem_mutex_enter(log_mutex);
525 for (i = 0; mask_names[i].name; i++)
526 if (0 == strcmp(mask_names[i].name, name))
528 nmem_mutex_leave(log_mutex);
529 return mask_names[i].mask;
531 if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) ))
533 nmem_mutex_leave(log_mutex);
534 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
537 mask_names[i].mask = next_log_bit;
538 next_log_bit = next_log_bit<<1;
539 mask_names[i].name = malloc(strlen(name)+1);
540 strcpy(mask_names[i].name, name);
541 mask_names[i+1].name = NULL;
542 mask_names[i+1].mask = 0;
543 nmem_mutex_leave(log_mutex);
544 return mask_names[i].mask;
547 int yaz_log_module_level(const char *name)
551 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
554 nmem_mutex_enter(log_mutex);
555 for (i = 0; mask_names[i].name; i++)
556 if (0==strcmp(n, mask_names[i].name))
558 nmem_mutex_leave(log_mutex);
559 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
560 mask_names[i].mask, n,
561 strcmp(n,name) ? name : "");
562 return mask_names[i].mask;
564 nmem_mutex_leave(log_mutex);
565 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
566 strcmp(n, name) ? name : "" );
570 int yaz_log_mask_str(const char *str)
572 internal_log_init(); /* since l_level may be affected */
573 return yaz_log_mask_str_x(str, l_level);
576 int yaz_log_mask_str_x(const char *str, int level)
584 for (p = str; *p && *p != ','; p++)
591 if (isdigit(*(unsigned char *) str))
598 char *n = clean_name(str, p-str, clean, sizeof(clean));
599 int mask = define_module_bit(n);
601 level = 0; /* 'none' clears them all */
616 * indent-tabs-mode: nil
618 * vim: shiftwidth=4 tabstop=8 expandtab