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