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