2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.33 2003-01-06 08:20:28 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] = "";
49 { LOG_FATAL, "fatal"},
50 { LOG_DEBUG, "debug"},
54 { LOG_MALLOC, "malloc"},
60 FILE *yaz_log_file(void)
67 void yaz_log_init_file (const char *fname)
72 if (!fname || !*fname)
74 if (!(new_file = fopen(fname, "a")))
80 setvbuf(new_file, 0, _IONBF, 0);
84 void yaz_log_init_level (int level)
89 void yaz_log_init_prefix (const char *prefix)
91 if (prefix && *prefix)
92 sprintf(l_prefix, "%.511s ", prefix);
97 void yaz_log_init_prefix2 (const char *prefix)
99 if (prefix && *prefix)
100 sprintf(l_prefix2, "%.511s ", prefix);
105 void yaz_log_init(int level, const char *prefix, const char *fname)
107 yaz_log_init_level (level);
108 yaz_log_init_prefix (prefix);
109 yaz_log_init_file (fname);
112 static void (*start_hook_func)(int, const char *, void *) = NULL;
113 static void *start_hook_info;
114 static void (*end_hook_func)(int, const char *, void *) = NULL;
115 static void *end_hook_info;
117 void log_event_start (void (*func)(int, const char *, void *), void *info)
119 start_hook_func = func;
120 start_hook_info = info;
123 void log_event_end (void (*func)(int, const char *, void *), void *info)
125 end_hook_func = func;
126 end_hook_info = info;
129 void yaz_log(int level, const char *fmt, ...)
132 char buf[4096], flags[1024];
139 if (!(level & l_level))
144 for (i = 0; level && mask_names[i].name; i++)
145 if (mask_names[i].mask & level)
147 if (*mask_names[i].name)
148 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
149 level -= mask_names[i].mask;
153 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
157 vsnprintf(buf, sizeof(buf), fmt, ap);
159 vsprintf(buf, fmt, ap);
163 if (o_level & LOG_ERRNO)
166 yaz_strerror(buf+strlen(buf), 2048);
171 (*start_hook_func)(o_level, buf, start_hook_info);
173 tim = localtime(&ti);
174 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
175 fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
179 (*end_hook_func)(o_level, buf, end_hook_info);
182 int yaz_log_mask_str (const char *str)
184 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
187 int yaz_log_mask_str_x (const char *str, int level)
194 for (p = str; *p && *p != ','; p++)
196 if (*str == '-' || isdigit(*str))
199 for (i = 0; mask_names[i].name; i++)
200 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
201 memcmp (mask_names[i].name, str, p-str) == 0)
203 if (mask_names[i].mask)
204 level |= mask_names[i].mask;