2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.34 2003-02-07 14:39:00 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"},
60 FILE *yaz_log_file(void)
67 void yaz_log_init_file (const char *fname)
72 if (!fname || !*fname)
74 else 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);
110 yaz_log_init_file (fname);
113 static void (*start_hook_func)(int, const char *, void *) = NULL;
114 static void *start_hook_info;
115 static void (*end_hook_func)(int, const char *, void *) = NULL;
116 static void *end_hook_info;
118 void log_event_start (void (*func)(int, const char *, void *), void *info)
120 start_hook_func = func;
121 start_hook_info = info;
124 void log_event_end (void (*func)(int, const char *, void *), void *info)
126 end_hook_func = func;
127 end_hook_info = info;
130 void yaz_log(int level, const char *fmt, ...)
133 char buf[4096], flags[1024];
140 if (!(level & l_level))
145 for (i = 0; level && mask_names[i].name; i++)
146 if (mask_names[i].mask & level)
148 if (*mask_names[i].name)
149 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
150 level -= mask_names[i].mask;
154 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
158 vsnprintf(buf, sizeof(buf), fmt, ap);
160 vsprintf(buf, fmt, ap);
164 if (o_level & LOG_ERRNO)
167 yaz_strerror(buf+strlen(buf), 2048);
172 (*start_hook_func)(o_level, buf, start_hook_info);
174 tim = localtime(&ti);
175 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
176 fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
180 (*end_hook_func)(o_level, buf, end_hook_info);
183 int yaz_log_mask_str (const char *str)
185 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
188 int yaz_log_mask_str_x (const char *str, int level)
195 for (p = str; *p && *p != ','; p++)
197 if (*str == '-' || isdigit(*str))
200 for (i = 0; mask_names[i].name; i++)
201 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
202 memcmp (mask_names[i].name, str, p-str) == 0)
204 if (mask_names[i].mask)
205 level |= mask_names[i].mask;