2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.6 2004-11-02 13:52: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] = "";
51 static char l_old_default_format[]="%H:%M:%S-%d/%m";
52 static char l_new_default_format[]="%Y%m%d-%H%M%S";
53 #define TIMEFORMAT_LEN 50
54 static char l_custom_format[TIMEFORMAT_LEN]="";
55 static char *l_actual_format=l_old_default_format;
57 /** l_max_size tells when to rotate the log. Default to 1 GB */
58 /* static int l_max_size=1024*1024*1024; */
65 { LOG_FATAL, "fatal"},
66 { LOG_DEBUG, "debug"},
70 { LOG_MALLOC, "malloc"},
73 { LOG_APP2 , "app2" },
74 { LOG_APP3 , "app3" },
76 { LOG_FLUSH, "flush" },
81 FILE *yaz_log_file(void)
88 void yaz_log_init_file (const char *fname)
92 strncpy(l_fname, fname, sizeof(l_fname)-1);
93 l_fname[sizeof(l_fname)-1] = '\0';
100 void yaz_log_reopen(void)
108 else if (!(new_file = fopen(l_fname, "a")))
110 if (l_file != stderr)
114 if (l_level & LOG_FLUSH)
115 setvbuf(new_file, 0, _IONBF, 0);
119 void yaz_log_init_level (int level)
121 if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
124 yaz_log_reopen(); /* make sure we set buffering right */
129 void yaz_log_init_prefix (const char *prefix)
131 if (prefix && *prefix)
132 sprintf(l_prefix, "%.511s ", prefix);
137 void yaz_log_init_prefix2 (const char *prefix)
139 if (prefix && *prefix)
140 sprintf(l_prefix2, "%.511s ", prefix);
145 void yaz_log_init(int level, const char *prefix, const char *fname)
147 yaz_log_init_level (level);
148 yaz_log_init_prefix (prefix);
150 yaz_log_init_file (fname);
153 static void (*start_hook_func)(int, const char *, void *) = NULL;
154 static void *start_hook_info;
155 static void (*end_hook_func)(int, const char *, void *) = NULL;
156 static void *end_hook_info;
158 void log_event_start (void (*func)(int, const char *, void *), void *info)
160 start_hook_func = func;
161 start_hook_info = info;
164 void log_event_end (void (*func)(int, const char *, void *), void *info)
166 end_hook_func = func;
167 end_hook_info = info;
170 void yaz_log(int level, const char *fmt, ...)
173 char buf[4096], flags[1024];
177 char tbuf[TIMEFORMAT_LEN]="";
181 if (!(level & l_level))
192 for (i = 0; level && mask_names[i].name; i++)
193 if (mask_names[i].mask & level)
195 if (*mask_names[i].name)
196 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
197 level -= mask_names[i].mask;
201 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
205 vsnprintf(buf, sizeof(buf), fmt, ap);
207 vsprintf(buf, fmt, ap);
211 if (o_level & LOG_ERRNO)
214 yaz_strerror(buf+strlen(buf), 2048);
219 (*start_hook_func)(o_level, buf, start_hook_info);
221 tim = localtime(&ti);
222 if (l_level & LOG_NOTIME)
225 strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
226 tbuf[TIMEFORMAT_LEN-1]='\0';
227 fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
229 if (l_level & (LOG_FLUSH|LOG_DEBUG) )
232 (*end_hook_func)(o_level, buf, end_hook_info);
235 void yaz_log_time_format(const char *fmt)
238 { /* no format, default to new */
239 l_actual_format = l_new_default_format;
242 if (0==strcmp(fmt,"old"))
243 { /* force the old format */
244 l_actual_format = l_old_default_format;
247 /* else use custom format */
248 strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
249 l_custom_format[TIMEFORMAT_LEN-1]='\0';
250 l_actual_format = l_custom_format;
253 int yaz_log_mask_str (const char *str)
255 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
258 int yaz_log_mask_str_x (const char *str, int level)
265 for (p = str; *p && *p != ','; p++)
267 if (*str == '-' || isdigit(*str))
270 for (i = 0; mask_names[i].name; i++)
271 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
272 memcmp (mask_names[i].name, str, p-str) == 0)
274 if (mask_names[i].mask)
275 level |= mask_names[i].mask;