2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.37 2003-02-18 14:28:53 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"},
62 FILE *yaz_log_file(void)
69 void yaz_log_init_file (const char *fname)
74 if (!fname || !*fname)
76 else if (!(new_file = fopen(fname, "a")))
82 setvbuf(new_file, 0, _IONBF, 0);
86 void yaz_log_init_level (int level)
91 void yaz_log_init_prefix (const char *prefix)
93 if (prefix && *prefix)
94 sprintf(l_prefix, "%.511s ", prefix);
99 void yaz_log_init_prefix2 (const char *prefix)
101 if (prefix && *prefix)
102 sprintf(l_prefix2, "%.511s ", prefix);
107 void yaz_log_init(int level, const char *prefix, const char *fname)
109 yaz_log_init_level (level);
110 yaz_log_init_prefix (prefix);
112 yaz_log_init_file (fname);
115 static void (*start_hook_func)(int, const char *, void *) = NULL;
116 static void *start_hook_info;
117 static void (*end_hook_func)(int, const char *, void *) = NULL;
118 static void *end_hook_info;
120 void log_event_start (void (*func)(int, const char *, void *), void *info)
122 start_hook_func = func;
123 start_hook_info = info;
126 void log_event_end (void (*func)(int, const char *, void *), void *info)
128 end_hook_func = func;
129 end_hook_info = info;
132 void yaz_log(int level, const char *fmt, ...)
135 char buf[4096], flags[1024];
142 if (!(level & l_level))
147 for (i = 0; level && mask_names[i].name; i++)
148 if (mask_names[i].mask & level)
150 if (*mask_names[i].name)
151 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
152 level -= mask_names[i].mask;
156 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
160 vsnprintf(buf, sizeof(buf), fmt, ap);
162 vsprintf(buf, fmt, ap);
166 if (o_level & LOG_ERRNO)
169 yaz_strerror(buf+strlen(buf), 2048);
174 (*start_hook_func)(o_level, buf, start_hook_info);
176 tim = localtime(&ti);
177 if (l_level & LOG_NOTIME)
180 strftime(tbuf, 50, "%H:%M:%S-%d/%m: ", tim);
181 fprintf(l_file, "%s%s%s %s%s\n", tbuf, l_prefix, flags,
185 (*end_hook_func)(o_level, buf, end_hook_info);
188 int yaz_log_mask_str (const char *str)
190 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
193 int yaz_log_mask_str_x (const char *str, int level)
200 for (p = str; *p && *p != ','; p++)
202 if (*str == '-' || isdigit(*str))
205 for (i = 0; mask_names[i].name; i++)
206 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
207 memcmp (mask_names[i].name, str, p-str) == 0)
209 if (mask_names[i].mask)
210 level |= mask_names[i].mask;