2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.38 2003-05-22 13:15:08 heikki 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] = "";
49 { LOG_FATAL, "fatal"},
50 { LOG_DEBUG, "debug"},
54 { LOG_MALLOC, "malloc"},
57 { LOG_APP2 , "app2" },
58 { LOG_APP3 , "app3" },
64 FILE *yaz_log_file(void)
71 void yaz_log_init_file (const char *fname)
76 if (!fname || !*fname)
78 else if (!(new_file = fopen(fname, "a")))
84 setvbuf(new_file, 0, _IONBF, 0);
88 void yaz_log_init_level (int level)
93 void yaz_log_init_prefix (const char *prefix)
95 if (prefix && *prefix)
96 sprintf(l_prefix, "%.511s ", prefix);
101 void yaz_log_init_prefix2 (const char *prefix)
103 if (prefix && *prefix)
104 sprintf(l_prefix2, "%.511s ", prefix);
109 void yaz_log_init(int level, const char *prefix, const char *fname)
111 yaz_log_init_level (level);
112 yaz_log_init_prefix (prefix);
114 yaz_log_init_file (fname);
117 static void (*start_hook_func)(int, const char *, void *) = NULL;
118 static void *start_hook_info;
119 static void (*end_hook_func)(int, const char *, void *) = NULL;
120 static void *end_hook_info;
122 void log_event_start (void (*func)(int, const char *, void *), void *info)
124 start_hook_func = func;
125 start_hook_info = info;
128 void log_event_end (void (*func)(int, const char *, void *), void *info)
130 end_hook_func = func;
131 end_hook_info = info;
134 void yaz_log(int level, const char *fmt, ...)
137 char buf[4096], flags[1024];
144 if (!(level & l_level))
149 for (i = 0; level && mask_names[i].name; i++)
150 if (mask_names[i].mask & level)
152 if (*mask_names[i].name)
153 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
154 level -= mask_names[i].mask;
158 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
162 vsnprintf(buf, sizeof(buf), fmt, ap);
164 vsprintf(buf, fmt, ap);
168 if (o_level & LOG_ERRNO)
171 yaz_strerror(buf+strlen(buf), 2048);
176 (*start_hook_func)(o_level, buf, start_hook_info);
178 tim = localtime(&ti);
179 if (l_level & LOG_NOTIME)
182 strftime(tbuf, 50, "%H:%M:%S-%d/%m: ", tim);
183 fprintf(l_file, "%s%s%s %s%s\n", tbuf, l_prefix, flags,
187 (*end_hook_func)(o_level, buf, end_hook_info);
190 int yaz_log_mask_str (const char *str)
192 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
195 int yaz_log_mask_str_x (const char *str, int level)
202 for (p = str; *p && *p != ','; p++)
204 if (*str == '-' || isdigit(*str))
207 for (i = 0; mask_names[i].name; i++)
208 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
209 memcmp (mask_names[i].name, str, p-str) == 0)
211 if (mask_names[i].mask)
212 level |= mask_names[i].mask;