2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.18 2004-12-13 14:21:55 heikki 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];
58 static int l_level = YLOG_DEFAULT_LEVEL;
59 static FILE *l_file = NULL;
60 static char l_prefix[512] = "";
61 static char l_prefix2[512] = "";
62 static char l_fname[512] = "";
64 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
65 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
66 #define TIMEFORMAT_LEN 50
67 static char l_custom_format[TIMEFORMAT_LEN] = "";
68 static char *l_actual_format = l_old_default_format;
70 /** l_max_size tells when to rotate the log. Default to 1 GB */
71 static const int l_def_max_size = 1024*1024*1024;
72 static int l_max_size = 1024*1024*1024;
74 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
78 } mask_names[MAX_MASK_NAMES] =
80 { YLOG_FATAL, "fatal"},
81 { YLOG_DEBUG, "debug"},
82 { YLOG_WARN, "warn" },
85 { YLOG_MALLOC, "malloc"},
86 /* { YLOG_APP, "app" }, */
87 { YLOG_NOTIME, "notime" },
88 /* { YLOG_APP2, "app2" }, */
89 /* { YLOG_APP3, "app3" }, */
91 { YLOG_FLUSH, "flush" },
92 { YLOG_LOGLVL, "loglevel" },
95 /* the rest will be filled in if the user defines dynamic modules*/
97 static unsigned int next_log_bit = YLOG_LAST_BIT<<1; /* first dynamic bit */
99 static void init_mutex()
103 nmem_mutex_create (&log_mutex);
108 FILE *yaz_log_file(void)
115 void yaz_log_init_file (const char *fname)
117 if (!mutex_init_flag)
121 strncpy(l_fname, fname, sizeof(l_fname)-1);
122 l_fname[sizeof(l_fname)-1] = '\0';
129 void yaz_log_reopen(void)
132 if (!mutex_init_flag)
138 else if (!(new_file = fopen(l_fname, "a")))
140 if (l_file != stderr)
144 if (l_level & YLOG_FLUSH)
145 setvbuf(new_file, 0, _IONBF, 0);
149 static void rotate_log()
153 return; /* can't rotate that */
155 return; /* hmm, no name, can't rotate */
156 strncpy(newname, l_fname, 509);
157 newname[509] = '\0'; /* make sure it is terminated */
158 strcat(newname,".1");
160 /* windows can't rename a file if it is open */
164 rename(l_fname, newname);
169 void yaz_log_init_level (int level)
171 if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
174 yaz_log_reopen(); /* make sure we set buffering right */
177 if (l_level & YLOG_LOGLVL)
178 { /* dump the log level bits */
179 char *bittype="Static ";
181 yaz_log(YLOG_LOGLVL,"Setting log level to %d = 0x%08x",l_level,l_level);
182 for (i = 0; mask_names[i].name; i++)
183 if (mask_names[i].mask && *mask_names[i].name)
184 if (strcmp(mask_names[i].name,"all")!=0)
186 yaz_log(YLOG_LOGLVL,"%s log bit %08x '%s' is %s",
187 bittype, mask_names[i].mask, mask_names[i].name,
188 (level & mask_names[i].mask)? "ON": "off");
189 if (mask_names[i].mask>YLOG_LAST_BIT)
195 void yaz_log_init_prefix (const char *prefix)
197 if (prefix && *prefix)
198 sprintf(l_prefix, "%.511s ", prefix);
203 void yaz_log_init_prefix2 (const char *prefix)
205 if (prefix && *prefix)
206 sprintf(l_prefix2, "%.511s ", prefix);
211 void yaz_log_init(int level, const char *prefix, const char *fname)
213 if (!mutex_init_flag)
215 yaz_log_init_level (level);
216 yaz_log_init_prefix (prefix);
218 yaz_log_init_file (fname);
221 void yaz_log_init_max_size(int mx)
224 l_max_size = l_def_max_size;
229 static void (*start_hook_func)(int, const char *, void *) = NULL;
230 static void *start_hook_info;
231 static void (*end_hook_func)(int, const char *, void *) = NULL;
232 static void *end_hook_info;
234 void log_event_start (void (*func)(int, const char *, void *), void *info)
236 start_hook_func = func;
237 start_hook_info = info;
240 void log_event_end (void (*func)(int, const char *, void *), void *info)
242 end_hook_func = func;
243 end_hook_info = info;
246 void yaz_log(int level, const char *fmt, ...)
249 char buf[4096], flags[1024];
253 char tbuf[TIMEFORMAT_LEN] = "";
257 if (!(level & l_level))
259 if (!mutex_init_flag)
264 if ((l_file != stderr) && (l_max_size>0))
266 nmem_mutex_enter (log_mutex);
267 flen = ftell(l_file);
270 nmem_mutex_leave (log_mutex);
274 for (i = 0; level && mask_names[i].name; i++)
275 if ( mask_names[i].mask & level)
277 if (*mask_names[i].name && mask_names[i].mask &&
278 mask_names[i].mask != YLOG_ALL)
280 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
281 level &= ~mask_names[i].mask;
286 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
290 vsnprintf(buf, sizeof(buf), fmt, ap);
292 vsprintf(buf, fmt, ap);
296 if (o_level & YLOG_ERRNO)
299 yaz_strerror(buf+strlen(buf), 2048);
304 (*start_hook_func)(o_level, buf, start_hook_info);
306 tim = localtime(&ti);
307 if (l_level & YLOG_NOTIME)
310 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
311 tbuf[TIMEFORMAT_LEN-1] = '\0';
312 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
314 if (l_level & (YLOG_FLUSH|YLOG_DEBUG) )
317 (*end_hook_func)(o_level, buf, end_hook_info);
320 void yaz_log_time_format(const char *fmt)
323 { /* no format, default to new */
324 l_actual_format = l_new_default_format;
327 if (0==strcmp(fmt,"old"))
328 { /* force the old format */
329 l_actual_format = l_old_default_format;
332 /* else use custom format */
333 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
334 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
335 l_actual_format = l_custom_format;
338 /** cleans a loglevel name from leading paths and suffixes */
339 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
342 char *start = namebuf;
345 strncpy(namebuf, name, len);
347 while ((p = strchr(start,'/')))
349 if ((p = strrchr(start,'.')))
355 static int define_module_bit(const char *name)
358 for (i = 0; mask_names[i].name; i++)
360 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
362 yaz_log(YLOG_WARN,"No more log bits left, not logging '%s'", name);
365 mask_names[i].mask = next_log_bit;
366 next_log_bit = next_log_bit<<1;
367 mask_names[i].name = xstrdup(name);
368 mask_names[i+1].name = NULL;
369 mask_names[i+1].mask = 0;
370 return mask_names[i].mask;
373 int yaz_log_module_level(const char *name)
377 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
378 for (i = 0; mask_names[i].name; i++)
379 if (0==strcmp(n,mask_names[i].name))
381 yaz_log(YLOG_LOGLVL,"returning log bit 0x%x for '%s' %s",
382 mask_names[i].mask, n,
383 strcmp(n,name)?name:"" );
384 return mask_names[i].mask;
386 yaz_log(YLOG_LOGLVL,"returning NO log bit for '%s' %s", n,
387 strcmp(n,name)?name:"" );
391 int yaz_log_mask_str (const char *str)
393 return yaz_log_mask_str_x (str, YLOG_DEFAULT_LEVEL);
396 int yaz_log_mask_str_x (const char *str, int level)
402 char clean[255] = "";
409 for (p = str; *p && *p != ','; p++)
423 n = clean_name(str, p-str, clean, sizeof(clean));
424 for (i = 0; mask_names[i].name; i++)
425 if (0==strcmp (mask_names[i].name,n))
427 if (mask_names[i].mask)
429 level &= ~mask_names[i].mask;
431 level |= mask_names[i].mask;
433 level = 0; /* 'none' clears them all */
438 level |= define_module_bit(n);