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