2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.27 2002-06-18 21:30:39 adam Exp $
25 #define HAS_STRERROR 1
32 extern char *sys_errlist[];
33 return sys_errlist[n];
38 static int l_level = LOG_DEFAULT_LEVEL;
39 static FILE *l_file = NULL;
40 static char l_prefix[512] = "";
47 { LOG_FATAL, "fatal"},
48 { LOG_DEBUG, "debug"},
52 { LOG_MALLOC, "malloc"},
58 FILE *yaz_log_file(void)
65 void yaz_log_init_file (const char *fname)
70 if (!fname || !*fname)
72 if (!(new_file = fopen(fname, "a")))
78 setvbuf(new_file, 0, _IONBF, 0);
82 void yaz_log_init_level (int level)
87 void yaz_log_init_prefix (const char *prefix)
89 if (prefix && *prefix)
90 sprintf(l_prefix, "%.511s ", prefix);
95 void yaz_log_init(int level, const char *prefix, const char *fname)
97 yaz_log_init_level (level);
98 yaz_log_init_prefix (prefix);
99 yaz_log_init_file (fname);
102 static void (*start_hook_func)(int, const char *, void *) = NULL;
103 static void *start_hook_info;
104 static void (*end_hook_func)(int, const char *, void *) = NULL;
105 static void *end_hook_info;
107 void log_event_start (void (*func)(int, const char *, void *), void *info)
109 start_hook_func = func;
110 start_hook_info = info;
113 void log_event_end (void (*func)(int, const char *, void *), void *info)
115 end_hook_func = func;
116 end_hook_info = info;
119 void yaz_log(int level, const char *fmt, ...)
122 char buf[4096], flags[1024];
129 if (!(level & l_level))
134 for (i = 0; level && mask_names[i].name; i++)
135 if (mask_names[i].mask & level)
137 if (*mask_names[i].name)
138 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
139 level -= mask_names[i].mask;
143 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
147 vsnprintf(buf, sizeof(buf), fmt, ap);
149 vsprintf(buf, fmt, ap);
153 if (o_level & LOG_ERRNO)
156 DWORD err = GetLastError();
161 FORMAT_MESSAGE_FROM_SYSTEM,
164 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
165 (LPTSTR) buf + strlen(buf),
171 sprintf(buf + strlen(buf), " [%s]", strerror(errno));
175 (*start_hook_func)(o_level, buf, start_hook_info);
177 tim = localtime(&ti);
178 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
179 fprintf(l_file, "%s: %s%s %s\n", tbuf, l_prefix, flags, buf);
182 (*end_hook_func)(o_level, buf, end_hook_info);
185 int yaz_log_mask_str (const char *str)
187 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
190 int yaz_log_mask_str_x (const char *str, int level)
197 for (p = str; *p && *p != ','; p++)
199 if (*str == '-' || isdigit(*str))
202 for (i = 0; mask_names[i].name; i++)
203 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
204 memcmp (mask_names[i].name, str, p-str) == 0)
206 if (mask_names[i].mask)
207 level |= mask_names[i].mask;