2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.5 2004-11-02 12:57:54 heikki Exp $
10 * \brief Implements logging utility
31 #define HAS_STRERROR 1
38 extern char *sys_errlist[];
39 return sys_errlist[n];
45 static int l_level = LOG_DEFAULT_LEVEL;
46 static FILE *l_file = NULL;
47 static char l_prefix[512] = "";
48 static char l_prefix2[512] = "";
49 static char l_fname[512] = "";
50 static char l_old_default_format[]="%H:%M:%S-%d/%m";
51 static char l_new_default_format[]="%Y%m%d-%H%M%S";
52 #define TIMEFORMAT_LEN 50
53 static char l_custom_format[TIMEFORMAT_LEN]="";
54 static char *l_actual_format=l_old_default_format;
61 { LOG_FATAL, "fatal"},
62 { LOG_DEBUG, "debug"},
66 { LOG_MALLOC, "malloc"},
69 { LOG_APP2 , "app2" },
70 { LOG_APP3 , "app3" },
72 { LOG_FLUSH, "flush" },
77 FILE *yaz_log_file(void)
84 void yaz_log_init_file (const char *fname)
88 strncpy(l_fname, fname, sizeof(l_fname)-1);
89 l_fname[sizeof(l_fname)-1] = '\0';
96 void yaz_log_reopen(void)
104 else if (!(new_file = fopen(l_fname, "a")))
106 if (l_file != stderr)
110 setvbuf(new_file, 0, _IONBF, 0);
114 void yaz_log_init_level (int level)
119 void yaz_log_init_prefix (const char *prefix)
121 if (prefix && *prefix)
122 sprintf(l_prefix, "%.511s ", prefix);
127 void yaz_log_init_prefix2 (const char *prefix)
129 if (prefix && *prefix)
130 sprintf(l_prefix2, "%.511s ", prefix);
135 void yaz_log_init(int level, const char *prefix, const char *fname)
137 yaz_log_init_level (level);
138 yaz_log_init_prefix (prefix);
140 yaz_log_init_file (fname);
143 static void (*start_hook_func)(int, const char *, void *) = NULL;
144 static void *start_hook_info;
145 static void (*end_hook_func)(int, const char *, void *) = NULL;
146 static void *end_hook_info;
148 void log_event_start (void (*func)(int, const char *, void *), void *info)
150 start_hook_func = func;
151 start_hook_info = info;
154 void log_event_end (void (*func)(int, const char *, void *), void *info)
156 end_hook_func = func;
157 end_hook_info = info;
160 void yaz_log(int level, const char *fmt, ...)
163 char buf[4096], flags[1024];
167 char tbuf[TIMEFORMAT_LEN]="";
170 if (!(level & l_level))
175 for (i = 0; level && mask_names[i].name; i++)
176 if (mask_names[i].mask & level)
178 if (*mask_names[i].name)
179 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
180 level -= mask_names[i].mask;
184 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
188 vsnprintf(buf, sizeof(buf), fmt, ap);
190 vsprintf(buf, fmt, ap);
194 if (o_level & LOG_ERRNO)
197 yaz_strerror(buf+strlen(buf), 2048);
202 (*start_hook_func)(o_level, buf, start_hook_info);
204 tim = localtime(&ti);
205 if (l_level & LOG_NOTIME)
208 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
209 tbuf[TIMEFORMAT_LEN-1]='\0';
210 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
212 if (l_level & (LOG_FLUSH|LOG_DEBUG) )
215 (*end_hook_func)(o_level, buf, end_hook_info);
218 void yaz_log_time_format(const char *fmt)
221 { /* no format, default to new */
222 l_actual_format = l_new_default_format;
225 if (0==strcmp(fmt,"old"))
226 { /* force the old format */
227 l_actual_format = l_old_default_format;
230 /* else use custom format */
231 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
232 l_custom_format[TIMEFORMAT_LEN-1]='\0';
233 l_actual_format = l_custom_format;
236 int yaz_log_mask_str (const char *str)
238 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
241 int yaz_log_mask_str_x (const char *str, int level)
248 for (p = str; *p && *p != ','; p++)
250 if (*str == '-' || isdigit(*str))
253 for (i = 0; mask_names[i].name; i++)
254 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
255 memcmp (mask_names[i].name, str, p-str) == 0)
257 if (mask_names[i].mask)
258 level |= mask_names[i].mask;