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