2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.31 2006-03-21 12:31:48 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 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 to 1 GB */
85 static const int l_def_max_size = 1024*1024*1024;
86 static int l_max_size = 1024*1024*1024;
88 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
92 } mask_names[MAX_MASK_NAMES] =
94 { YLOG_FATAL, "fatal"},
95 { YLOG_DEBUG, "debug"},
96 { YLOG_WARN, "warn" },
99 { YLOG_MALLOC, "malloc"},
101 { YLOG_NOTIME, "notime" },
102 { YLOG_APP2, "app2" },
103 { YLOG_APP3, "app3" },
105 { YLOG_FLUSH, "flush" },
106 { YLOG_LOGLVL, "loglevel" },
109 /* the rest will be filled in if the user defines dynamic modules*/
112 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
114 static void init_mutex()
118 nmem_mutex_create(&log_mutex);
122 FILE *yaz_log_file(void)
125 switch(yaz_file_type)
127 case use_stderr: f = stderr; break;
128 case use_none: f = 0; break;
129 case use_file: f = yaz_global_log_file; break;
136 if (yaz_file_type == use_file && yaz_global_log_file)
138 fclose(yaz_global_log_file);
139 yaz_global_log_file = 0;
143 void yaz_log_init_file(const char *fname)
151 yaz_file_type = use_stderr; /* empty name; use stderr */
153 yaz_file_type = use_file;
154 strncpy(l_fname, fname, sizeof(l_fname)-1);
155 l_fname[sizeof(l_fname)-1] = '\0';
159 yaz_file_type = use_none; /* NULL name; use no file at all */
165 static void rotate_log(const char *cur_fname)
168 strncpy(newname, cur_fname, 509);
169 newname[509] = '\0'; /* make sure it is terminated */
170 strcat(newname, ".1");
172 /* windows can't rename a file if it is open */
175 MoveFileEx(cur_fname, newname, MOVEFILE_REPLACE_EXISTING);
177 rename(cur_fname, newname);
181 void yaz_log_init_level(int level)
184 if (l_level < 0) l_level = default_log_level();
185 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
188 yaz_log_reopen(); /* make sure we set buffering right */
191 if (l_level & YLOG_LOGLVL)
192 { /* dump the log level bits */
193 const char *bittype = "Static ";
196 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
198 /* determine size of mask_names (locked) */
199 nmem_mutex_enter(log_mutex);
200 for (sz = 0; mask_names[sz].name; sz++)
202 nmem_mutex_leave(log_mutex);
203 /* second pass without lock */
204 for (i = 0; i < sz; i++)
205 if (mask_names[i].mask && *mask_names[i].name)
206 if (strcmp(mask_names[i].name, "all") != 0)
208 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
209 bittype, mask_names[i].mask, mask_names[i].name,
210 (level & mask_names[i].mask)? "ON": "off");
211 if (mask_names[i].mask > YLOG_LAST_BIT)
217 void yaz_log_init_prefix(const char *prefix)
219 if (prefix && *prefix)
220 sprintf(l_prefix, "%.511s ", prefix);
225 void yaz_log_init_prefix2(const char *prefix)
227 if (prefix && *prefix)
228 sprintf(l_prefix2, "%.511s ", prefix);
233 void yaz_log_init(int level, const char *prefix, const char *fname)
236 yaz_log_init_level(level);
237 yaz_log_init_prefix(prefix);
239 yaz_log_init_file(fname);
242 void yaz_log_init_max_size(int mx)
245 l_max_size = l_def_max_size;
250 static void (*start_hook_func)(int, const char *, void *) = NULL;
251 static void *start_hook_info;
252 static void (*end_hook_func)(int, const char *, void *) = NULL;
253 static void *end_hook_info;
255 void log_event_start(void (*func)(int, const char *, void *), void *info)
257 start_hook_func = func;
258 start_hook_info = info;
261 void log_event_end(void (*func)(int, const char *, void *), void *info)
263 end_hook_func = func;
264 end_hook_info = info;
267 static void yaz_log_open_check(struct tm *tm, int force)
269 char new_filename[512];
270 static char cur_filename[512] = "";
272 if (l_fname && *l_fname)
274 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
275 if (strcmp(new_filename, cur_filename))
277 strcpy(cur_filename, new_filename);
282 if (l_max_size > 0 && yaz_global_log_file && yaz_file_type == use_file)
284 long flen = ftell(yaz_global_log_file);
285 if (flen > l_max_size)
287 rotate_log(cur_filename);
291 if (force && yaz_file_type == use_file && *cur_filename)
294 yaz_global_log_file = fopen(cur_filename, "a");
295 if (l_level < 0) l_level = default_log_level();
296 if (l_level & YLOG_FLUSH)
297 setvbuf(yaz_global_log_file, 0, _IONBF, 0);
301 void yaz_log_reopen()
303 time_t cur_time = time(0);
305 struct tm tm0, *tm = &tm0;
310 nmem_mutex_enter(log_mutex);
312 localtime_r(&cur_time, tm);
314 tm = localtime(&cur_time);
316 yaz_log_open_check(tm, 1);
317 nmem_mutex_leave(log_mutex);
320 static void yaz_log_to_file(int level, FILE *file, const char *buf)
325 struct tm tm0, *tm = &tm0;
332 nmem_mutex_enter(log_mutex);
335 localtime_r(&ti, tm);
340 yaz_log_open_check(tm, 0);
341 file = yaz_log_file(); /* file may change in yaz_log_open_check */
345 char tbuf[TIMEFORMAT_LEN];
349 for (i = 0; level && mask_names[i].name; i++)
350 if ( mask_names[i].mask & level)
352 if (*mask_names[i].name && mask_names[i].mask &&
353 mask_names[i].mask != YLOG_ALL)
355 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
356 level &= ~mask_names[i].mask;
360 if (l_level & YLOG_NOTIME)
363 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
364 tbuf[TIMEFORMAT_LEN-1] = '\0';
366 fprintf(file, "%s %s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2, buf);
367 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
370 nmem_mutex_leave(log_mutex);
373 void yaz_log(int level, const char *fmt, ...)
381 l_level = default_log_level();
382 if (!(level & l_level))
386 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
390 vsnprintf(buf, sizeof(buf), fmt, ap);
392 vsprintf(buf, fmt, ap);
396 if (o_level & YLOG_ERRNO)
399 yaz_strerror(buf+strlen(buf), 2048);
404 (*start_hook_func)(o_level, buf, start_hook_info);
406 file = yaz_log_file();
408 yaz_log_to_file(level, file, buf);
410 (*end_hook_func)(o_level, buf, end_hook_info);
413 void yaz_log_time_format(const char *fmt)
416 { /* no format, default to new */
417 l_actual_format = l_new_default_format;
420 if (0==strcmp(fmt, "old"))
421 { /* force the old format */
422 l_actual_format = l_old_default_format;
425 /* else use custom format */
426 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
427 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
428 l_actual_format = l_custom_format;
431 /** cleans a loglevel name from leading paths and suffixes */
432 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
435 char *start = namebuf;
438 strncpy(namebuf, name, len);
440 while ((p = strchr(start, '/')))
442 if ((p = strrchr(start, '.')))
447 static int define_module_bit(const char *name)
451 nmem_mutex_enter(log_mutex);
452 for (i = 0; mask_names[i].name; i++)
453 if (0 == strcmp(mask_names[i].name, name))
455 nmem_mutex_leave(log_mutex);
456 return mask_names[i].mask;
458 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
460 nmem_mutex_leave(log_mutex);
461 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
464 mask_names[i].mask = next_log_bit;
465 next_log_bit = next_log_bit<<1;
466 mask_names[i].name = malloc(strlen(name)+1);
467 strcpy(mask_names[i].name, name);
468 mask_names[i+1].name = NULL;
469 mask_names[i+1].mask = 0;
470 nmem_mutex_leave(log_mutex);
471 return mask_names[i].mask;
474 int yaz_log_module_level(const char *name)
478 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
481 nmem_mutex_enter(log_mutex);
482 for (i = 0; mask_names[i].name; i++)
483 if (0==strcmp(n, mask_names[i].name))
485 nmem_mutex_leave(log_mutex);
486 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
487 mask_names[i].mask, n,
488 strcmp(n,name) ? name : "");
489 return mask_names[i].mask;
491 nmem_mutex_leave(log_mutex);
492 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
493 strcmp(n, name) ? name : "" );
497 int yaz_log_mask_str(const char *str)
499 return yaz_log_mask_str_x(str, default_log_level());
502 int yaz_log_mask_str_x(const char *str, int level)
509 for (p = str; *p && *p != ','; p++)
516 if (isdigit(*(unsigned char *) str))
523 char *n = clean_name(str, p-str, clean, sizeof(clean));
524 int mask = define_module_bit(n);
526 level = 0; /* 'none' clears them all */
541 * indent-tabs-mode: nil
543 * vim: shiftwidth=4 tabstop=8 expandtab