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