2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.35 2003-02-11 16:35:17 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"},
56 /* { LOG_ALL, "all" }, */
61 FILE *yaz_log_file(void)
68 void yaz_log_init_file (const char *fname)
73 if (!fname || !*fname)
75 else if (!(new_file = fopen(fname, "a")))
81 setvbuf(new_file, 0, _IONBF, 0);
85 void yaz_log_init_level (int level)
90 void yaz_log_init_prefix (const char *prefix)
92 if (prefix && *prefix)
93 sprintf(l_prefix, "%.511s ", prefix);
98 void yaz_log_init_prefix2 (const char *prefix)
100 if (prefix && *prefix)
101 sprintf(l_prefix2, "%.511s ", prefix);
106 void yaz_log_init(int level, const char *prefix, const char *fname)
108 yaz_log_init_level (level);
109 yaz_log_init_prefix (prefix);
111 yaz_log_init_file (fname);
114 static void (*start_hook_func)(int, const char *, void *) = NULL;
115 static void *start_hook_info;
116 static void (*end_hook_func)(int, const char *, void *) = NULL;
117 static void *end_hook_info;
119 void log_event_start (void (*func)(int, const char *, void *), void *info)
121 start_hook_func = func;
122 start_hook_info = info;
125 void log_event_end (void (*func)(int, const char *, void *), void *info)
127 end_hook_func = func;
128 end_hook_info = info;
131 void yaz_log(int level, const char *fmt, ...)
134 char buf[4096], flags[1024];
141 if (!(level & l_level))
146 for (i = 0; level && mask_names[i].name; i++)
147 if (mask_names[i].mask & level)
149 if (*mask_names[i].name)
150 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
151 level -= mask_names[i].mask;
155 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
159 vsnprintf(buf, sizeof(buf), fmt, ap);
161 vsprintf(buf, fmt, ap);
165 if (o_level & LOG_ERRNO)
168 yaz_strerror(buf+strlen(buf), 2048);
173 (*start_hook_func)(o_level, buf, start_hook_info);
175 tim = localtime(&ti);
176 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
177 fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
181 (*end_hook_func)(o_level, buf, end_hook_info);
184 int yaz_log_mask_str (const char *str)
186 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
189 int yaz_log_mask_str_x (const char *str, int level)
196 for (p = str; *p && *p != ','; p++)
198 if (*str == '-' || isdigit(*str))
201 for (i = 0; mask_names[i].name; i++)
202 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
203 memcmp (mask_names[i].name, str, p-str) == 0)
205 if (mask_names[i].mask)
206 level |= mask_names[i].mask;