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