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