2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.12 2004-11-04 14:19:58 heikki Exp $
10 * \brief Implements logging utility
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 = LOG_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 { LOG_FATAL, "fatal"},
81 { LOG_DEBUG, "debug"},
85 { LOG_MALLOC, "malloc"},
91 { LOG_FLUSH, "flush" },
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");
162 rename(l_fname, newname);
167 void yaz_log_init_level (int level)
169 if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
172 yaz_log_reopen(); /* make sure we set buffering right */
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)
195 if (!mutex_init_flag)
197 yaz_log_init_level (level);
198 yaz_log_init_prefix (prefix);
200 yaz_log_init_file (fname);
203 void yaz_log_init_max_size(int mx)
206 l_max_size = l_def_max_size;
211 static void (*start_hook_func)(int, const char *, void *) = NULL;
212 static void *start_hook_info;
213 static void (*end_hook_func)(int, const char *, void *) = NULL;
214 static void *end_hook_info;
216 void log_event_start (void (*func)(int, const char *, void *), void *info)
218 start_hook_func = func;
219 start_hook_info = info;
222 void log_event_end (void (*func)(int, const char *, void *), void *info)
224 end_hook_func = func;
225 end_hook_info = info;
228 void yaz_log(int level, const char *fmt, ...)
231 char buf[4096], flags[1024];
235 char tbuf[TIMEFORMAT_LEN] = "";
239 if (!(level & l_level))
241 if (!mutex_init_flag)
246 if ((l_file != stderr) && (l_max_size>0))
248 nmem_mutex_enter (log_mutex);
249 flen = ftell(l_file);
252 nmem_mutex_leave (log_mutex);
256 for (i = 0; level && mask_names[i].name; i++)
257 if ( mask_names[i].mask & level)
259 if (*mask_names[i].name && mask_names[i].mask &&
260 mask_names[i].mask != LOG_ALL)
262 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
263 level &= ~mask_names[i].mask;
268 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
272 vsnprintf(buf, sizeof(buf), fmt, ap);
274 vsprintf(buf, fmt, ap);
278 if (o_level & LOG_ERRNO)
281 yaz_strerror(buf+strlen(buf), 2048);
286 (*start_hook_func)(o_level, buf, start_hook_info);
288 tim = localtime(&ti);
289 if (l_level & LOG_NOTIME)
292 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
293 tbuf[TIMEFORMAT_LEN-1] = '\0';
294 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
296 if (l_level & (LOG_FLUSH|LOG_DEBUG) )
299 (*end_hook_func)(o_level, buf, end_hook_info);
302 void yaz_log_time_format(const char *fmt)
305 { /* no format, default to new */
306 l_actual_format = l_new_default_format;
309 if (0==strcmp(fmt,"old"))
310 { /* force the old format */
311 l_actual_format = l_old_default_format;
314 /* else use custom format */
315 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
316 l_custom_format[TIMEFORMAT_LEN-1] = '\0';
317 l_actual_format = l_custom_format;
320 /** cleans a loglevel name from leading paths and suffixes */
321 static char *clean_name(const char *name, int len, char *namebuf, int buflen)
327 strncpy(namebuf, name, len);
331 while ((p = index(start,'/')))
333 if ((p = rindex(start,'.')))
335 /*logf(LOG_LOG,"cleaned '%.*s' to '%s' ", len,name, start); */
340 static int define_module_bit(const char *name)
343 for (i = 0; mask_names[i].name; i++)
345 if ( (i>=MAX_MASK_NAMES) || (next_log_bit >= 1<<31 ))
347 yaz_log(LOG_WARN,"No more log bits left, not logging '%s'", name);
350 mask_names[i].mask = next_log_bit;
351 next_log_bit = next_log_bit<<1;
352 mask_names[i].name = xstrdup(name);
353 mask_names[i+1].name = NULL;
354 mask_names[i+1].mask = 0;
355 return mask_names[i].mask;
358 int yaz_log_module_level(const char *name)
362 char *n = clean_name(name, strlen(name), clean, sizeof(clean));
363 for (i = 0; mask_names[i].name; i++)
364 if (0==strcmp(n,mask_names[i].name))
365 return mask_names[i].mask;
369 int yaz_log_mask_str (const char *str)
371 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
374 int yaz_log_mask_str_x (const char *str, int level)
379 char clean[255] = "";
385 for (p = str; *p && *p != ','; p++)
387 if (*str == '-' || isdigit(*str))
394 n = clean_name(str, p-str, clean, sizeof(clean));
395 for (i = 0; mask_names[i].name; i++)
396 /*if (strlen (mask_names[i].name) == (size_t) (p-str) &&
397 memcmp (mask_names[i].name, str, p-str) == 0)*/
398 if (0==strcmp (mask_names[i].name,n))
400 if (mask_names[i].mask)
401 level |= mask_names[i].mask;
408 level |= define_module_bit(n);