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