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