Check for config.h (currently not generated).
[yaz-moved-to-github.git] / util / log.c
1 /*
2  * Copyright (c) 1995-2000, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: log.c,v $
7  * Revision 1.22  2000-02-29 13:44:55  adam
8  * Check for config.h (currently not generated).
9  *
10  * Revision 1.21  2000/02/28 11:20:06  adam
11  * Using autoconf. New definitions: YAZ_BEGIN_CDECL/YAZ_END_CDECL.
12  *
13  * Revision 1.20  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.19  1999/08/27 09:40:32  adam
17  * Renamed logf function to yaz_log. Removed VC++ project files.
18  *
19  * Revision 1.18  1998/10/28 10:27:00  adam
20  * New functions log_init_file, log_init_level, log_init_prefix.
21  *
22  * Revision 1.17  1997/12/09 16:11:02  adam
23  * Assume strerror is defined on Unixes as well. It's standard ANSI.
24  *
25  * Revision 1.16  1997/10/06 08:55:07  adam
26  * Changed log_init so that previous (if any) is closed.
27  *
28  * Revision 1.15  1997/09/29 07:13:13  adam
29  * Minor changes.
30  *
31  * Revision 1.14  1997/09/18 08:48:09  adam
32  * Fixed minor bug that caused log_init to ignore filename.
33  *
34  * Revision 1.13  1997/09/01 08:54:13  adam
35  * New windows NT/95 port using MSV5.0. Made prefix query handling
36  * thread safe. The function options ignores empty arguments when met.
37  *
38  * Revision 1.12  1997/05/01 15:08:14  adam
39  * Added log_mask_str_x routine.
40  *
41  * Revision 1.11  1996/02/05 12:24:32  adam
42  * Implemented log_event_{start,end}-functions.
43  *
44  * Revision 1.10  1995/12/06  09:51:27  quinn
45  * Fixed the log-prefix buffer - it was too small and the setup code lacked
46  * a bounds-check.
47  *
48  * Revision 1.9  1995/09/29  17:12:34  quinn
49  * Smallish
50  *
51  * Revision 1.8  1995/09/27  15:03:02  quinn
52  * Modified function heads & prototypes.
53  *
54  * Revision 1.7  1995/06/19  12:40:18  quinn
55  * Added log_file()
56  *
57  * Revision 1.6  1995/06/15  15:45:03  quinn
58  * Added date info.
59  *
60  * Revision 1.5  1995/05/16  08:51:11  quinn
61  * License, documentation, and memory fixes
62  *
63  * Revision 1.4  1995/05/15  11:56:55  quinn
64  * Debuggng & adjustments.
65  *
66  * Revision 1.3  1995/04/10  10:23:51  quinn
67  * Fixes.
68  *
69  * Revision 1.2  1995/03/31  10:16:55  quinn
70  * Fixed logging.
71  *
72  * Revision 1.1  1995/03/30  10:26:53  quinn
73  * Logging system
74  *
75  * Revision 1.9  1994/12/12  12:09:02  quinn
76  * Changes
77  *
78  * Revision 1.8  1994/11/22  13:15:38  quinn
79  * Simple
80  *
81  * Revision 1.7  1994/10/05  10:16:11  quinn
82  * Added xrealloc. Fixed bug in log.
83  *
84  * Revision 1.6  1994/10/04  14:02:19  quinn
85  * Fixed log_init
86  *
87  * Revision 1.5  1994/09/28  13:07:41  adam
88  * Implemented log_mask_str.
89  *
90  * Revision 1.4  1994/09/27  20:04:13  quinn
91  * Added fflush.
92  *
93  * Revision 1.3  1994/08/18  08:18:48  quinn
94  * Added prefix to log_init.
95  *
96  * Revision 1.2  1994/08/17  14:27:53  quinn
97  * added LOG_ERRNO
98  *
99  * Revision 1.1  1994/08/17  13:23:15  quinn
100  * First version
101  * Added log.c
102  *
103  */
104
105 #if HAVE_CONFIG_H
106 #include <config.h>
107 #endif
108
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <ctype.h>
112 #include <string.h>
113 #include <stdarg.h>
114 #include <errno.h>
115 #include <time.h>
116 #include <yaz/log.h>
117
118 #define HAS_STRERROR 1
119
120 #if HAS_STRERROR
121
122 #else
123 char *strerror(int n)
124 {
125         extern char *sys_errlist[];
126         return sys_errlist[n];
127 }
128
129 #endif
130
131 static int l_level = LOG_DEFAULT_LEVEL;
132 static FILE *l_file = NULL;
133 static char l_prefix[512] = "log";
134
135 static struct {
136     int mask;
137     char *name;
138 } mask_names[] =
139 {
140     { LOG_FATAL, "fatal"},
141     { LOG_DEBUG, "debug"},
142     { LOG_WARN,  "warn" },
143     { LOG_LOG,   "log"  },
144     { LOG_ERRNO, ""},
145     { LOG_ALL,   "all"  },
146     { 0,         "none" },
147     { 0, NULL }
148 };  
149
150 FILE *log_file(void)
151 {
152     if (!l_file)
153         l_file = stderr;
154     return l_file;
155 }
156
157 void log_init_file (const char *fname)
158 {
159     FILE *new_file;
160     if (!l_file)
161         l_file = stderr;
162     if (!fname || !*fname)
163         return;
164     if (!(new_file = fopen(fname, "a")))
165         return;
166     if (l_file != stderr)
167     {
168         fclose (l_file);
169     }
170     setvbuf(new_file, 0, _IONBF, 0);
171     l_file = new_file;
172 }
173
174 void log_init_level (int level)
175 {
176     l_level = level;
177 }
178
179 void log_init_prefix (const char *prefix)
180 {
181     if (prefix && *prefix)
182         sprintf(l_prefix, "%.512s", prefix);
183 }
184
185 void log_init(int level, const char *prefix, const char *fname)
186 {
187     log_init_level (level);
188     log_init_prefix (prefix);
189     log_init_file (fname);
190 }
191
192 static void (*start_hook_func)(int, const char *, void *) = NULL;
193 static void *start_hook_info;
194 static void (*end_hook_func)(int, const char *, void *) = NULL;
195 static void *end_hook_info;
196
197 void log_event_start (void (*func)(int, const char *, void *), void *info)
198 {
199     start_hook_func = func;
200     start_hook_info = info;
201 }
202
203 void log_event_end (void (*func)(int, const char *, void *), void *info)
204 {
205     end_hook_func = func;
206     end_hook_info = info;
207 }
208
209 void yaz_log(int level, const char *fmt, ...)
210 {
211     va_list ap;
212     char buf[4096], flags[1024];
213     int i;
214     time_t ti;
215     struct tm *tim;
216     char tbuf[50];
217     int o_level = level;
218
219     if (!(level & l_level))
220         return;
221     if (!l_file)
222         l_file = stderr;
223     *flags = '\0';
224     for (i = 0; level && mask_names[i].name; i++)
225         if (mask_names[i].mask & level)
226         {
227             if (*mask_names[i].name)
228                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
229             level -= mask_names[i].mask;
230         }
231     va_start(ap, fmt);
232     vsprintf(buf, fmt, ap);
233     if (o_level & LOG_ERRNO)
234         sprintf(buf + strlen(buf), " [%s]", strerror(errno));
235     if (start_hook_func)
236         (*start_hook_func)(o_level, buf, start_hook_info);
237     ti = time(0);
238     tim = localtime(&ti);
239     strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
240     fprintf(l_file, "%s: %s: %s %s\n", tbuf, l_prefix, flags, buf);
241     fflush(l_file);
242     if (end_hook_func)
243         (*end_hook_func)(o_level, buf, end_hook_info);
244 }
245
246 int log_mask_str (const char *str)
247 {
248     return log_mask_str_x (str, LOG_DEFAULT_LEVEL);
249 }
250
251 int log_mask_str_x (const char *str, int level)
252 {
253     const char *p;
254     int i;
255
256     while (*str)
257     {
258         for (p = str; *p && *p != ','; p++)
259             ;
260         if (*str == '-' || isdigit(*str))
261             level = atoi (str);
262         else
263             for (i = 0; mask_names[i].name; i++)
264                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
265                     memcmp (mask_names[i].name, str, p-str) == 0)
266                 {
267                     if (mask_names[i].mask)
268                         level |= mask_names[i].mask;
269                     else
270                         level = 0;
271                 }
272         if (*p == ',')
273             p++;
274         str = p;
275     }
276     return level;
277 }