2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.29 2005-09-16 21:46:24 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
50 extern char *sys_errlist[];
51 return sys_errlist[n];
56 static int l_level = YLOG_DEFAULT_LEVEL;
57 static FILE *l_file = NULL;
58 static char l_prefix[512] = "";
59 static char l_prefix2[512] = "";
60 static char l_fname[512] = "";
62 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
63 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
64 #define TIMEFORMAT_LEN 50
65 static char l_custom_format[TIMEFORMAT_LEN] = "";
66 static char *l_actual_format = l_old_default_format;
68 /** l_max_size tells when to rotate the log. Default to 1 GB */
69 static const int l_def_max_size = 1024*1024*1024;
70 static int l_max_size = 1024*1024*1024;
72 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
76 } mask_names[MAX_MASK_NAMES] =
78 { YLOG_FATAL, "fatal"},
79 { YLOG_DEBUG, "debug"},
80 { YLOG_WARN, "warn" },
83 { YLOG_MALLOC, "malloc"},
85 { YLOG_NOTIME, "notime" },
86 { YLOG_APP2, "app2" },
87 { YLOG_APP3, "app3" },
89 { YLOG_FLUSH, "flush" },
90 { YLOG_LOGLVL, "loglevel" },
93 /* the rest will be filled in if the user defines dynamic modules*/
96 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
98 static void init_mutex()
102 nmem_mutex_create(&log_mutex);
106 FILE *yaz_log_file(void)
113 void yaz_log_init_file(const char *fname)
118 strncpy(l_fname, fname, sizeof(l_fname)-1);
119 l_fname[sizeof(l_fname)-1] = '\0';
126 static void rotate_log(const char *cur_fname)
129 strncpy(newname, cur_fname, 509);
130 newname[509] = '\0'; /* make sure it is terminated */
131 strcat(newname, ".1");
133 /* windows can't rename a file if it is open */
136 MoveFileEx(cur_fname, newname, MOVEFILE_REPLACE_EXISTING);
138 rename(cur_fname, newname);
142 void yaz_log_init_level(int level)
145 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
148 yaz_log_reopen(); /* make sure we set buffering right */
151 if (l_level & YLOG_LOGLVL)
152 { /* dump the log level bits */
153 const char *bittype = "Static ";
156 yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x",
158 /* determine size of mask_names (locked) */
159 nmem_mutex_enter(log_mutex);
160 for (sz = 0; mask_names[sz].name; sz++)
162 nmem_mutex_leave(log_mutex);
163 /* second pass without lock */
164 for (i = 0; i < sz; i++)
165 if (mask_names[i].mask && *mask_names[i].name)
166 if (strcmp(mask_names[i].name, "all") != 0)
168 yaz_log(YLOG_LOGLVL, "%s log bit %08x '%s' is %s",
169 bittype, mask_names[i].mask, mask_names[i].name,
170 (level & mask_names[i].mask)? "ON": "off");
171 if (mask_names[i].mask > YLOG_LAST_BIT)
177 void yaz_log_init_prefix(const char *prefix)
179 if (prefix && *prefix)
180 sprintf(l_prefix, "%.511s ", prefix);
185 void yaz_log_init_prefix2(const char *prefix)
187 if (prefix && *prefix)
188 sprintf(l_prefix2, "%.511s ", prefix);
193 void yaz_log_init(int level, const char *prefix, const char *fname)
196 yaz_log_init_level(level);
197 yaz_log_init_prefix(prefix);
199 yaz_log_init_file(fname);
202 void yaz_log_init_max_size(int mx)
205 l_max_size = l_def_max_size;
210 static void (*start_hook_func)(int, const char *, void *) = NULL;
211 static void *start_hook_info;
212 static void (*end_hook_func)(int, const char *, void *) = NULL;
213 static void *end_hook_info;
215 void log_event_start(void (*func)(int, const char *, void *), void *info)
217 start_hook_func = func;
218 start_hook_info = info;
221 void log_event_end(void (*func)(int, const char *, void *), void *info)
223 end_hook_func = func;
224 end_hook_info = info;
227 static void yaz_log_open_check(struct tm *tm, int force)
229 char new_filename[512];
230 static char cur_filename[512] = "";
235 if (l_fname && *l_fname)
237 strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
238 if (strcmp(new_filename, cur_filename))
240 strcpy(cur_filename, new_filename);
243 if (l_max_size > 0 && (l_file && l_file != stderr))
245 long flen = ftell(l_file);
246 if (flen > l_max_size)
248 rotate_log(cur_filename);
254 if (l_file && l_file != stderr)
256 l_file = fopen(cur_filename, "a");
257 if (l_level & YLOG_FLUSH)
258 setvbuf(l_file, 0, _IONBF, 0);
263 void yaz_log_reopen()
265 time_t cur_time = time(0);
267 struct tm tm0, *tm = &tm0;
272 nmem_mutex_enter(log_mutex);
274 localtime_r(&cur_time, tm);
276 tm = localtime(&cur_time);
278 yaz_log_open_check(tm, 1);
279 nmem_mutex_leave(log_mutex);
282 void yaz_log(int level, const char *fmt, ...)
285 char buf[4096], flags[1024];
289 struct tm tm0, *tm = &tm0;
293 char tbuf[TIMEFORMAT_LEN] = "";
296 if (!(level & l_level))
300 nmem_mutex_enter(log_mutex);
303 localtime_r(&ti, tm);
308 yaz_log_open_check(tm, 0);
314 for (i = 0; level && mask_names[i].name; i++)
315 if ( mask_names[i].mask & level)
317 if (*mask_names[i].name && mask_names[i].mask &&
318 mask_names[i].mask != YLOG_ALL)
320 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
321 level &= ~mask_names[i].mask;
327 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
331 vsnprintf(buf, sizeof(buf), fmt, ap);
333 vsprintf(buf, fmt, ap);
337 if (o_level & YLOG_ERRNO)
340 yaz_strerror(buf+strlen(buf), 2048);
345 (*start_hook_func)(o_level, buf, start_hook_info);
346 if (l_level & YLOG_NOTIME)
349 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
350 tbuf[TIMEFORMAT_LEN-1] = '\0';
351 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
353 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
356 (*end_hook_func)(o_level, buf, end_hook_info);
357 nmem_mutex_leave(log_mutex);
360 void yaz_log_time_format(const char *fmt)
363 { /* no format, default to new */
364 l_actual_format = l_new_default_format;
367 if (0==strcmp(fmt, "old"))
368 { /* force the old format */
369 l_actual_format = l_old_default_format;
372 /* else use custom format */
373 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
374 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
375 l_actual_format = l_custom_format;
378 /** cleans a loglevel name from leading paths and suffixes */
379 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
382 char *start = namebuf;
385 strncpy(namebuf, name, len);
387 while ((p = strchr(start, '/')))
389 if ((p = strrchr(start, '.')))
394 static int define_module_bit(const char *name)
398 nmem_mutex_enter(log_mutex);
399 for (i = 0; mask_names[i].name; i++)
400 if (0 == strcmp(mask_names[i].name, name))
402 nmem_mutex_leave(log_mutex);
403 return mask_names[i].mask;
405 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
407 nmem_mutex_leave(log_mutex);
408 yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name);
411 mask_names[i].mask = next_log_bit;
412 next_log_bit = next_log_bit<<1;
413 mask_names[i].name = malloc(strlen(name)+1);
414 strcpy(mask_names[i].name, name);
415 mask_names[i+1].name = NULL;
416 mask_names[i+1].mask = 0;
417 nmem_mutex_leave(log_mutex);
418 return mask_names[i].mask;
421 int yaz_log_module_level(const char *name)
425 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
428 nmem_mutex_enter(log_mutex);
429 for (i = 0; mask_names[i].name; i++)
430 if (0==strcmp(n, mask_names[i].name))
432 nmem_mutex_leave(log_mutex);
433 yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s",
434 mask_names[i].mask, n,
435 strcmp(n,name) ? name : "");
436 return mask_names[i].mask;
438 nmem_mutex_leave(log_mutex);
439 yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n,
440 strcmp(n, name) ? name : "" );
444 int yaz_log_mask_str(const char *str)
446 return yaz_log_mask_str_x(str, YLOG_DEFAULT_LEVEL);
449 int yaz_log_mask_str_x(const char *str, int level)
456 for (p = str; *p && *p != ','; p++)
463 if (isdigit(*(unsigned char *) str))
470 char *n = clean_name(str, p-str, clean, sizeof(clean));
471 int mask = define_module_bit(n);
473 level = 0; /* 'none' clears them all */
488 * indent-tabs-mode: nil
490 * vim: shiftwidth=4 tabstop=8 expandtab