2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.14 2004-11-16 17:08:11 heikki Exp $
10 * \brief Implements logging utility
39 static NMEM_MUTEX log_mutex = 0;
40 static int mutex_init_flag = 0; /* not yet initialized */
42 #define HAS_STRERROR 1
49 extern char *sys_errlist[];
50 return sys_errlist[n];
57 static int l_level = LOG_DEFAULT_LEVEL;
58 static FILE *l_file = NULL;
59 static char l_prefix[512] = "";
60 static char l_prefix2[512] = "";
61 static char l_fname[512] = "";
63 static char l_old_default_format[] = "%H:%M:%S-%d/%m";
64 static char l_new_default_format[] = "%Y%m%d-%H%M%S";
65 #define TIMEFORMAT_LEN 50
66 static char l_custom_format[TIMEFORMAT_LEN] = "";
67 static char *l_actual_format = l_old_default_format;
69 /** l_max_size tells when to rotate the log. Default to 1 GB */
70 static const int l_def_max_size = 1024*1024*1024;
71 static int l_max_size = 1024*1024*1024;
73 #define MAX_MASK_NAMES 35 /* 32 bits plus a few combo names */
77 } mask_names[MAX_MASK_NAMES] =
79 { LOG_FATAL, "fatal"},
80 { LOG_DEBUG, "debug"},
84 { LOG_MALLOC, "malloc"},
90 { LOG_FLUSH, "flush" },
91 { LOG_LOGLVL, "loglevel" },
94 /* the rest will be filled in if the user defines dynamic modules*/
96 static unsigned int next_log_bit = LOG_LAST_BIT<<1; /* first dynamic bit */
98 static void init_mutex()
102 nmem_mutex_create (&log_mutex);
107 FILE *yaz_log_file(void)
114 void yaz_log_init_file (const char *fname)
116 if (!mutex_init_flag)
120 strncpy(l_fname, fname, sizeof(l_fname)-1);
121 l_fname[sizeof(l_fname)-1] = '\0';
128 void yaz_log_reopen(void)
131 if (!mutex_init_flag)
137 else if (!(new_file = fopen(l_fname, "a")))
139 if (l_file != stderr)
143 if (l_level & LOG_FLUSH)
144 setvbuf(new_file, 0, _IONBF, 0);
148 static void rotate_log()
152 return; /* can't rotate that */
154 return; /* hmm, no name, can't rotate */
155 strncpy(newname, l_fname, 509);
156 newname[509] = '\0'; /* make sure it is terminated */
157 strcat(newname,".1");
159 /* windows can't rename a file if it is open */
163 rename(l_fname, newname);
168 void yaz_log_init_level (int level)
170 if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
173 yaz_log_reopen(); /* make sure we set buffering right */
176 if (l_level & LOG_LOGLVL)
177 { /* dump the log level bits */
178 char *bittype="Static ";
180 yaz_log(LOG_LOGLVL,"Setting log level to %d = 0x%08x",l_level,l_level);
181 for (i = 0; mask_names[i].name; i++)
182 if (mask_names[i].mask && *mask_names[i].name)
183 if (strcmp(mask_names[i].name,"all")!=0)
185 yaz_log(LOG_LOGLVL,"%s log bit %08x '%s' is %s",
186 bittype, mask_names[i].mask, mask_names[i].name,
187 (level & mask_names[i].mask)? "ON": "off");
188 if (mask_names[i].mask>LOG_LAST_BIT)
194 void yaz_log_init_prefix (const char *prefix)
196 if (prefix && *prefix)
197 sprintf(l_prefix, "%.511s ", prefix);
202 void yaz_log_init_prefix2 (const char *prefix)
204 if (prefix && *prefix)
205 sprintf(l_prefix2, "%.511s ", prefix);
210 void yaz_log_init(int level, const char *prefix, const char *fname)
212 if (!mutex_init_flag)
214 yaz_log_init_level (level);
215 yaz_log_init_prefix (prefix);
217 yaz_log_init_file (fname);
220 void yaz_log_init_max_size(int mx)
223 l_max_size = l_def_max_size;
228 static void (*start_hook_func)(int, const char *, void *) = NULL;
229 static void *start_hook_info;
230 static void (*end_hook_func)(int, const char *, void *) = NULL;
231 static void *end_hook_info;
233 void log_event_start (void (*func)(int, const char *, void *), void *info)
235 start_hook_func = func;
236 start_hook_info = info;
239 void log_event_end (void (*func)(int, const char *, void *), void *info)
241 end_hook_func = func;
242 end_hook_info = info;
245 void yaz_log(int level, const char *fmt, ...)
248 char buf[4096], flags[1024];
252 char tbuf[TIMEFORMAT_LEN] = "";
256 if (!(level & l_level))
258 if (!mutex_init_flag)
263 if ((l_file != stderr) && (l_max_size>0))
265 nmem_mutex_enter (log_mutex);
266 flen = ftell(l_file);
269 nmem_mutex_leave (log_mutex);
273 for (i = 0; level && mask_names[i].name; i++)
274 if ( mask_names[i].mask & level)
276 if (*mask_names[i].name && mask_names[i].mask &&
277 mask_names[i].mask != LOG_ALL)
279 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
280 level &= ~mask_names[i].mask;
285 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
289 vsnprintf(buf, sizeof(buf), fmt, ap);
291 vsprintf(buf, fmt, ap);
295 if (o_level & LOG_ERRNO)
298 yaz_strerror(buf+strlen(buf), 2048);
303 (*start_hook_func)(o_level, buf, start_hook_info);
305 tim = localtime(&ti);
306 if (l_level & LOG_NOTIME)
309 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
310 tbuf[TIMEFORMAT_LEN-1] = '\0';
311 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
313 if (l_level & (LOG_FLUSH|LOG_DEBUG) )
316 (*end_hook_func)(o_level, buf, end_hook_info);
319 void yaz_log_time_format(const char *fmt)
322 { /* no format, default to new */
323 l_actual_format = l_new_default_format;
326 if (0==strcmp(fmt,"old"))
327 { /* force the old format */
328 l_actual_format = l_old_default_format;
331 /* else use custom format */
332 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
333 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
334 l_actual_format = l_custom_format;
337 /** cleans a loglevel name from leading paths and suffixes */
338 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
341 char *start = namebuf;
344 strncpy(namebuf, name, len);
346 while ((p = strchr(start,'/')))
348 if ((p = strrchr(start,'.')))
354 static int define_module_bit(const char *name)
357 for (i = 0; mask_names[i].name; i++)
359 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
361 yaz_log(LOG_WARN,"No more log bits left, not logging '%s'", name);
364 mask_names[i].mask = next_log_bit;
365 next_log_bit = next_log_bit<<1;
366 mask_names[i].name = xstrdup(name);
367 mask_names[i+1].name = NULL;
368 mask_names[i+1].mask = 0;
369 return mask_names[i].mask;
372 int yaz_log_module_level(const char *name)
376 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
377 for (i = 0; mask_names[i].name; i++)
378 if (0==strcmp(n,mask_names[i].name))
380 yaz_log(LOG_LOGLVL,"returning log bit 0x%x for '%s' %s",
381 mask_names[i].mask, n,
382 strcmp(n,name)?name:"" );
383 return mask_names[i].mask;
385 yaz_log(LOG_LOGLVL,"returning NO log bit for '%s' %s", n,
386 strcmp(n,name)?name:"" );
390 int yaz_log_mask_str (const char *str)
392 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
395 int yaz_log_mask_str_x (const char *str, int level)
400 char clean[255] = "";
406 for (p = str; *p && *p != ','; p++)
408 if (*str == '-' || isdigit(*str))
415 n = clean_name(str, p-str, clean, sizeof(clean));
416 for (i = 0; mask_names[i].name; i++)
417 if (0==strcmp (mask_names[i].name,n))
419 if (mask_names[i].mask)
420 level |= mask_names[i].mask;
422 level = 0; /* 'none' clears them all */
427 level |= define_module_bit(n);