2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.1 2003-10-27 12:21:30 adam Exp $
26 #define HAS_STRERROR 1
33 extern char *sys_errlist[];
34 return sys_errlist[n];
39 static int l_level = LOG_DEFAULT_LEVEL;
40 static FILE *l_file = NULL;
41 static char l_prefix[512] = "";
42 static char l_prefix2[512] = "";
43 static char l_fname[512] = "";
50 { LOG_FATAL, "fatal"},
51 { LOG_DEBUG, "debug"},
55 { LOG_MALLOC, "malloc"},
58 { LOG_APP2 , "app2" },
59 { LOG_APP3 , "app3" },
65 FILE *yaz_log_file(void)
72 void yaz_log_init_file (const char *fname)
76 strncpy(l_fname, fname, sizeof(l_fname)-1);
77 l_fname[sizeof(l_fname)-1] = '\0';
84 void yaz_log_reopen(void)
92 else if (!(new_file = fopen(l_fname, "a")))
98 setvbuf(new_file, 0, _IONBF, 0);
102 void yaz_log_init_level (int level)
107 void yaz_log_init_prefix (const char *prefix)
109 if (prefix && *prefix)
110 sprintf(l_prefix, "%.511s ", prefix);
115 void yaz_log_init_prefix2 (const char *prefix)
117 if (prefix && *prefix)
118 sprintf(l_prefix2, "%.511s ", prefix);
123 void yaz_log_init(int level, const char *prefix, const char *fname)
125 yaz_log_init_level (level);
126 yaz_log_init_prefix (prefix);
128 yaz_log_init_file (fname);
131 static void (*start_hook_func)(int, const char *, void *) = NULL;
132 static void *start_hook_info;
133 static void (*end_hook_func)(int, const char *, void *) = NULL;
134 static void *end_hook_info;
136 void log_event_start (void (*func)(int, const char *, void *), void *info)
138 start_hook_func = func;
139 start_hook_info = info;
142 void log_event_end (void (*func)(int, const char *, void *), void *info)
144 end_hook_func = func;
145 end_hook_info = info;
148 void yaz_log(int level, const char *fmt, ...)
151 char buf[4096], flags[1024];
158 if (!(level & l_level))
163 for (i = 0; level && mask_names[i].name; i++)
164 if (mask_names[i].mask & level)
166 if (*mask_names[i].name)
167 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
168 level -= mask_names[i].mask;
172 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
176 vsnprintf(buf, sizeof(buf), fmt, ap);
178 vsprintf(buf, fmt, ap);
182 if (o_level & LOG_ERRNO)
185 yaz_strerror(buf+strlen(buf), 2048);
190 (*start_hook_func)(o_level, buf, start_hook_info);
192 tim = localtime(&ti);
193 if (l_level & LOG_NOTIME)
196 strftime(tbuf, 50, "%H:%M:%S-%d/%m: ", tim);
197 fprintf(l_file, "%s%s%s %s%s\n", tbuf, l_prefix, flags,
201 (*end_hook_func)(o_level, buf, end_hook_info);
204 int yaz_log_mask_str (const char *str)
206 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
209 int yaz_log_mask_str_x (const char *str, int level)
216 for (p = str; *p && *p != ','; p++)
218 if (*str == '-' || isdigit(*str))
221 for (i = 0; mask_names[i].name; i++)
222 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
223 memcmp (mask_names[i].name, str, p-str) == 0)
225 if (mask_names[i].mask)
226 level |= mask_names[i].mask;