2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: log.c,v 1.30 2002-10-22 10:04:51 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] = "";
41 static char l_prefix2[512] = "";
48 { LOG_FATAL, "fatal"},
49 { LOG_DEBUG, "debug"},
53 { LOG_MALLOC, "malloc"},
59 FILE *yaz_log_file(void)
66 void yaz_log_init_file (const char *fname)
71 if (!fname || !*fname)
73 if (!(new_file = fopen(fname, "a")))
79 setvbuf(new_file, 0, _IONBF, 0);
83 void yaz_log_init_level (int level)
88 void yaz_log_init_prefix (const char *prefix)
90 if (prefix && *prefix)
91 sprintf(l_prefix, "%.511s ", prefix);
96 void yaz_log_init_prefix2 (const char *prefix)
98 if (prefix && *prefix)
99 sprintf(l_prefix2, "%.511s ", prefix);
104 void yaz_log_init(int level, const char *prefix, const char *fname)
106 yaz_log_init_level (level);
107 yaz_log_init_prefix (prefix);
108 yaz_log_init_file (fname);
111 static void (*start_hook_func)(int, const char *, void *) = NULL;
112 static void *start_hook_info;
113 static void (*end_hook_func)(int, const char *, void *) = NULL;
114 static void *end_hook_info;
116 void log_event_start (void (*func)(int, const char *, void *), void *info)
118 start_hook_func = func;
119 start_hook_info = info;
122 void log_event_end (void (*func)(int, const char *, void *), void *info)
124 end_hook_func = func;
125 end_hook_info = info;
128 void yaz_log(int level, const char *fmt, ...)
131 char buf[4096], flags[1024];
138 if (!(level & l_level))
143 for (i = 0; level && mask_names[i].name; i++)
144 if (mask_names[i].mask & level)
146 if (*mask_names[i].name)
147 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
148 level -= mask_names[i].mask;
152 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
156 vsnprintf(buf, sizeof(buf), fmt, ap);
158 vsprintf(buf, fmt, ap);
162 if (o_level & LOG_ERRNO)
165 DWORD err = GetLastError();
170 FORMAT_MESSAGE_FROM_SYSTEM,
173 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
174 (LPTSTR) buf + strlen(buf),
180 sprintf(buf + strlen(buf), " [%s]", strerror(yaz_errno()));
185 (*start_hook_func)(o_level, buf, start_hook_info);
187 tim = localtime(&ti);
188 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
189 fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
193 (*end_hook_func)(o_level, buf, end_hook_info);
196 int yaz_log_mask_str (const char *str)
198 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
201 int yaz_log_mask_str_x (const char *str, int level)
208 for (p = str; *p && *p != ','; p++)
210 if (*str == '-' || isdigit(*str))
213 for (i = 0; mask_names[i].name; i++)
214 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
215 memcmp (mask_names[i].name, str, p-str) == 0)
217 if (mask_names[i].mask)
218 level |= mask_names[i].mask;