2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.8 2004-11-02 15:47:31 heikki Exp $
10 * \brief Implements logging utility
40 static NMEM_MUTEX log_mutex=0;
41 static 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 int l_max_size=1024*1024*1024;*/
72 static int l_max_size=1024; /* while testing */
79 { LOG_FATAL, "fatal"},
80 { LOG_DEBUG, "debug"},
84 { LOG_MALLOC, "malloc"},
87 { LOG_APP2 , "app2" },
88 { LOG_APP3 , "app3" },
90 { LOG_FLUSH, "flush" },
95 static void init_mutex()
99 nmem_mutex_create (&log_mutex);
104 FILE *yaz_log_file(void)
111 void yaz_log_init_file (const char *fname)
113 if (!mutex_init_flag)
117 strncpy(l_fname, fname, sizeof(l_fname)-1);
118 l_fname[sizeof(l_fname)-1] = '\0';
125 void yaz_log_reopen(void)
128 if (!mutex_init_flag)
134 else if (!(new_file = fopen(l_fname, "a")))
136 if (l_file != stderr)
140 if (l_level & LOG_FLUSH)
141 setvbuf(new_file, 0, _IONBF, 0);
145 static void rotate_log()
149 return; /* can't rotate that */
151 return; /* hmm, no name, can't rotate */
152 strncpy(newname, l_fname, 510);
153 newname[510]='\0'; /* make sure it is terminated */
154 strcat(newname,".1");
159 rename(l_fname, newname);
164 void yaz_log_init_level (int level)
166 if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
169 yaz_log_reopen(); /* make sure we set buffering right */
174 void yaz_log_init_prefix (const char *prefix)
176 if (prefix && *prefix)
177 sprintf(l_prefix, "%.511s ", prefix);
182 void yaz_log_init_prefix2 (const char *prefix)
184 if (prefix && *prefix)
185 sprintf(l_prefix2, "%.511s ", prefix);
190 void yaz_log_init(int level, const char *prefix, const char *fname)
192 if (!mutex_init_flag)
194 yaz_log_init_level (level);
195 yaz_log_init_prefix (prefix);
197 yaz_log_init_file (fname);
200 static void (*start_hook_func)(int, const char *, void *) = NULL;
201 static void *start_hook_info;
202 static void (*end_hook_func)(int, const char *, void *) = NULL;
203 static void *end_hook_info;
205 void log_event_start (void (*func)(int, const char *, void *), void *info)
207 start_hook_func = func;
208 start_hook_info = info;
211 void log_event_end (void (*func)(int, const char *, void *), void *info)
213 end_hook_func = func;
214 end_hook_info = info;
217 void yaz_log(int level, const char *fmt, ...)
220 char buf[4096], flags[1024];
224 char tbuf[TIMEFORMAT_LEN]="";
228 if (!(level & l_level))
230 if (!mutex_init_flag)
235 if (l_file != stderr)
237 nmem_mutex_enter (log_mutex);
241 nmem_mutex_leave (log_mutex);
245 for (i = 0; level && mask_names[i].name; i++)
246 if (mask_names[i].mask & level)
248 if (*mask_names[i].name)
249 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
250 level -= mask_names[i].mask;
254 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
258 vsnprintf(buf, sizeof(buf), fmt, ap);
260 vsprintf(buf, fmt, ap);
264 if (o_level & LOG_ERRNO)
267 yaz_strerror(buf+strlen(buf), 2048);
272 (*start_hook_func)(o_level, buf, start_hook_info);
274 tim = localtime(&ti);
275 if (l_level & LOG_NOTIME)
278 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
279 tbuf[TIMEFORMAT_LEN-1]='\0';
280 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
282 if (l_level & (LOG_FLUSH|LOG_DEBUG) )
285 (*end_hook_func)(o_level, buf, end_hook_info);
288 void yaz_log_time_format(const char *fmt)
291 { /* no format, default to new */
292 l_actual_format = l_new_default_format;
295 if (0==strcmp(fmt,"old"))
296 { /* force the old format */
297 l_actual_format = l_old_default_format;
300 /* else use custom format */
301 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
302 l_custom_format[TIMEFORMAT_LEN-1]='\0';
303 l_actual_format = l_custom_format;
306 int yaz_log_mask_str (const char *str)
308 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
311 int yaz_log_mask_str_x (const char *str, int level)
318 for (p = str; *p && *p != ','; p++)
320 if (*str == '-' || isdigit(*str))
323 for (i = 0; mask_names[i].name; i++)
324 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
325 memcmp (mask_names[i].name, str, p-str) == 0)
327 if (mask_names[i].mask)
328 level |= mask_names[i].mask;