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