2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.31 2002-11-26 16:56:39 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 DWORD err = GetLastError();
171 FORMAT_MESSAGE_FROM_SYSTEM,
174 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
175 (LPTSTR) buf + strlen(buf),
181 sprintf(buf + strlen(buf), " [%s]", strerror(yaz_errno()));
186 (*start_hook_func)(o_level, buf, start_hook_info);
188 tim = localtime(&ti);
189 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
190 fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
194 (*end_hook_func)(o_level, buf, end_hook_info);
197 int yaz_log_mask_str (const char *str)
199 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
202 int yaz_log_mask_str_x (const char *str, int level)
209 for (p = str; *p && *p != ','; p++)
211 if (*str == '-' || isdigit(*str))
214 for (i = 0; mask_names[i].name; i++)
215 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
216 memcmp (mask_names[i].name, str, p-str) == 0)
218 if (mask_names[i].mask)
219 level |= mask_names[i].mask;