2 * Copyright (c) 1995-2000, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 2002-01-03 10:23:33 adam
8 * Using _vsnprintf on WIN32.
10 * Revision 1.25 2001/08/23 09:02:46 adam
11 * WIN32 fixes: Socket not re-used for bind. yaz_log logs WIN32 error
14 * Revision 1.24 2000/09/04 08:58:15 adam
15 * Added prefix yaz_ for most logging utility functions.
17 * Revision 1.23 2000/03/14 09:06:11 adam
18 * Added POSIX threads support for frontend server.
20 * Revision 1.22 2000/02/29 13:44:55 adam
21 * Check for config.h (currently not generated).
23 * Revision 1.21 2000/02/28 11:20:06 adam
24 * Using autoconf. New definitions: YAZ_BEGIN_CDECL/YAZ_END_CDECL.
26 * Revision 1.20 1999/11/30 13:47:12 adam
27 * Improved installation. Moved header files to include/yaz.
29 * Revision 1.19 1999/08/27 09:40:32 adam
30 * Renamed logf function to yaz_log. Removed VC++ project files.
32 * Revision 1.18 1998/10/28 10:27:00 adam
33 * New functions log_init_file, log_init_level, log_init_prefix.
35 * Revision 1.17 1997/12/09 16:11:02 adam
36 * Assume strerror is defined on Unixes as well. It's standard ANSI.
38 * Revision 1.16 1997/10/06 08:55:07 adam
39 * Changed log_init so that previous (if any) is closed.
41 * Revision 1.15 1997/09/29 07:13:13 adam
44 * Revision 1.14 1997/09/18 08:48:09 adam
45 * Fixed minor bug that caused log_init to ignore filename.
47 * Revision 1.13 1997/09/01 08:54:13 adam
48 * New windows NT/95 port using MSV5.0. Made prefix query handling
49 * thread safe. The function options ignores empty arguments when met.
51 * Revision 1.12 1997/05/01 15:08:14 adam
52 * Added log_mask_str_x routine.
54 * Revision 1.11 1996/02/05 12:24:32 adam
55 * Implemented log_event_{start,end}-functions.
57 * Revision 1.10 1995/12/06 09:51:27 quinn
58 * Fixed the log-prefix buffer - it was too small and the setup code lacked
61 * Revision 1.9 1995/09/29 17:12:34 quinn
64 * Revision 1.8 1995/09/27 15:03:02 quinn
65 * Modified function heads & prototypes.
67 * Revision 1.7 1995/06/19 12:40:18 quinn
70 * Revision 1.6 1995/06/15 15:45:03 quinn
73 * Revision 1.5 1995/05/16 08:51:11 quinn
74 * License, documentation, and memory fixes
76 * Revision 1.4 1995/05/15 11:56:55 quinn
77 * Debuggng & adjustments.
79 * Revision 1.3 1995/04/10 10:23:51 quinn
82 * Revision 1.2 1995/03/31 10:16:55 quinn
85 * Revision 1.1 1995/03/30 10:26:53 quinn
88 * Revision 1.9 1994/12/12 12:09:02 quinn
91 * Revision 1.8 1994/11/22 13:15:38 quinn
94 * Revision 1.7 1994/10/05 10:16:11 quinn
95 * Added xrealloc. Fixed bug in log.
97 * Revision 1.6 1994/10/04 14:02:19 quinn
100 * Revision 1.5 1994/09/28 13:07:41 adam
101 * Implemented log_mask_str.
103 * Revision 1.4 1994/09/27 20:04:13 quinn
106 * Revision 1.3 1994/08/18 08:18:48 quinn
107 * Added prefix to log_init.
109 * Revision 1.2 1994/08/17 14:27:53 quinn
112 * Revision 1.1 1994/08/17 13:23:15 quinn
135 #define HAS_STRERROR 1
140 char *strerror(int n)
142 extern char *sys_errlist[];
143 return sys_errlist[n];
148 static int l_level = LOG_DEFAULT_LEVEL;
149 static FILE *l_file = NULL;
150 static char l_prefix[512] = "log";
157 { LOG_FATAL, "fatal"},
158 { LOG_DEBUG, "debug"},
159 { LOG_WARN, "warn" },
167 FILE *yaz_log_file(void)
174 void yaz_log_init_file (const char *fname)
179 if (!fname || !*fname)
181 if (!(new_file = fopen(fname, "a")))
183 if (l_file != stderr)
187 setvbuf(new_file, 0, _IONBF, 0);
191 void yaz_log_init_level (int level)
196 void yaz_log_init_prefix (const char *prefix)
198 if (prefix && *prefix)
199 sprintf(l_prefix, "%.512s", prefix);
202 void yaz_log_init(int level, const char *prefix, const char *fname)
204 yaz_log_init_level (level);
205 yaz_log_init_prefix (prefix);
206 yaz_log_init_file (fname);
209 static void (*start_hook_func)(int, const char *, void *) = NULL;
210 static void *start_hook_info;
211 static void (*end_hook_func)(int, const char *, void *) = NULL;
212 static void *end_hook_info;
214 void log_event_start (void (*func)(int, const char *, void *), void *info)
216 start_hook_func = func;
217 start_hook_info = info;
220 void log_event_end (void (*func)(int, const char *, void *), void *info)
222 end_hook_func = func;
223 end_hook_info = info;
226 void yaz_log(int level, const char *fmt, ...)
229 char buf[4096], flags[1024];
236 if (!(level & l_level))
241 for (i = 0; level && mask_names[i].name; i++)
242 if (mask_names[i].mask & level)
244 if (*mask_names[i].name)
245 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
246 level -= mask_names[i].mask;
250 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
254 vsnprintf(buf, sizeof(buf), fmt, ap);
256 vsprintf(buf, fmt, ap);
260 if (o_level & LOG_ERRNO)
263 DWORD err = GetLastError();
268 FORMAT_MESSAGE_FROM_SYSTEM,
271 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
272 (LPTSTR) buf + strlen(buf),
278 sprintf(buf + strlen(buf), " [%s]", strerror(errno));
282 (*start_hook_func)(o_level, buf, start_hook_info);
284 tim = localtime(&ti);
285 strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
286 fprintf(l_file, "%s: %s: %s %s\n", tbuf, l_prefix, flags, buf);
289 (*end_hook_func)(o_level, buf, end_hook_info);
292 int yaz_log_mask_str (const char *str)
294 return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
297 int yaz_log_mask_str_x (const char *str, int level)
304 for (p = str; *p && *p != ','; p++)
306 if (*str == '-' || isdigit(*str))
309 for (i = 0; mask_names[i].name; i++)
310 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
311 memcmp (mask_names[i].name, str, p-str) == 0)
313 if (mask_names[i].mask)
314 level |= mask_names[i].mask;