ignore cql2pqf and cql2xcql
[yaz-moved-to-github.git] / util / log.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: log.c,v 1.37 2003-02-18 14:28:53 adam Exp $
6  */
7
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #ifdef WIN32
13 #include <windows.h>
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <ctype.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #include <errno.h>
22 #include <time.h>
23 #include <yaz/nmem.h>
24 #include <yaz/log.h>
25
26 #define HAS_STRERROR 1
27
28 #if HAS_STRERROR
29
30 #else
31 char *strerror(int n)
32 {
33         extern char *sys_errlist[];
34         return sys_errlist[n];
35 }
36
37 #endif
38
39 static int l_level = LOG_DEFAULT_LEVEL;
40 static FILE *l_file = NULL;
41 static char l_prefix[512] = "";
42 static char l_prefix2[512] = "";
43
44 static struct {
45     int mask;
46     char *name;
47 } mask_names[] =
48 {
49     { LOG_FATAL, "fatal"},
50     { LOG_DEBUG, "debug"},
51     { LOG_WARN,  "warn" },
52     { LOG_LOG,   "log"  },
53     { LOG_ERRNO, ""},
54     { LOG_MALLOC, "malloc"},
55     { LOG_APP,   "app"  },
56     { LOG_NOTIME, "" },
57     { LOG_ALL,   "all"  },
58     { 0,         "none" },
59     { 0, NULL }
60 };  
61
62 FILE *yaz_log_file(void)
63 {
64     if (!l_file)
65         l_file = stderr;
66     return l_file;
67 }
68
69 void yaz_log_init_file (const char *fname)
70 {
71     FILE *new_file;
72     if (!l_file)
73         l_file = stderr;
74     if (!fname || !*fname)
75         new_file=stderr;
76     else if (!(new_file = fopen(fname, "a")))
77         return;
78     if (l_file != stderr)
79     {
80         fclose (l_file);
81     }
82     setvbuf(new_file, 0, _IONBF, 0);
83     l_file = new_file;
84 }
85
86 void yaz_log_init_level (int level)
87 {
88     l_level = level;
89 }
90
91 void yaz_log_init_prefix (const char *prefix)
92 {
93     if (prefix && *prefix)
94         sprintf(l_prefix, "%.511s ", prefix);
95     else
96         *l_prefix = 0;
97 }
98
99 void yaz_log_init_prefix2 (const char *prefix)
100 {
101     if (prefix && *prefix)
102         sprintf(l_prefix2, "%.511s ", prefix);
103     else
104         *l_prefix2 = 0;
105 }
106
107 void yaz_log_init(int level, const char *prefix, const char *fname)
108 {
109     yaz_log_init_level (level);
110     yaz_log_init_prefix (prefix);
111     if (fname && *fname)
112         yaz_log_init_file (fname);
113 }
114
115 static void (*start_hook_func)(int, const char *, void *) = NULL;
116 static void *start_hook_info;
117 static void (*end_hook_func)(int, const char *, void *) = NULL;
118 static void *end_hook_info;
119
120 void log_event_start (void (*func)(int, const char *, void *), void *info)
121 {
122     start_hook_func = func;
123     start_hook_info = info;
124 }
125
126 void log_event_end (void (*func)(int, const char *, void *), void *info)
127 {
128     end_hook_func = func;
129     end_hook_info = info;
130 }
131
132 void yaz_log(int level, const char *fmt, ...)
133 {
134     va_list ap;
135     char buf[4096], flags[1024];
136     int i;
137     time_t ti;
138     struct tm *tim;
139     char tbuf[50]="";
140     int o_level = level;
141
142     if (!(level & l_level))
143         return;
144     if (!l_file)
145         l_file = stderr;
146     *flags = '\0';
147     for (i = 0; level && mask_names[i].name; i++)
148         if (mask_names[i].mask & level)
149         {
150             if (*mask_names[i].name)
151                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
152             level -= mask_names[i].mask;
153         }
154     va_start(ap, fmt);
155 #ifdef WIN32
156     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
157 #else
158 /* !WIN32 */
159 #if HAVE_VSNPRINTF
160     vsnprintf(buf, sizeof(buf), fmt, ap);
161 #else
162     vsprintf(buf, fmt, ap);
163 #endif
164 #endif
165 /* WIN32 */
166     if (o_level & LOG_ERRNO)
167     {
168         strcat(buf, " [");
169         yaz_strerror(buf+strlen(buf), 2048);
170         strcat(buf, "]");
171     }
172     va_end (ap);
173     if (start_hook_func)
174         (*start_hook_func)(o_level, buf, start_hook_info);
175     ti = time(0);
176     tim = localtime(&ti);
177     if (l_level & LOG_NOTIME)
178       tbuf[0]='\0';
179     else
180       strftime(tbuf, 50, "%H:%M:%S-%d/%m: ", tim);
181     fprintf(l_file, "%s%s%s %s%s\n", tbuf, l_prefix, flags,
182             l_prefix2, buf);
183     fflush(l_file);
184     if (end_hook_func)
185         (*end_hook_func)(o_level, buf, end_hook_info);
186 }
187
188 int yaz_log_mask_str (const char *str)
189 {
190     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
191 }
192
193 int yaz_log_mask_str_x (const char *str, int level)
194 {
195     const char *p;
196     int i;
197
198     while (*str)
199     {
200         for (p = str; *p && *p != ','; p++)
201             ;
202         if (*str == '-' || isdigit(*str))
203             level = atoi (str);
204         else
205             for (i = 0; mask_names[i].name; i++)
206                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
207                     memcmp (mask_names[i].name, str, p-str) == 0)
208                 {
209                     if (mask_names[i].mask)
210                         level |= mask_names[i].mask;
211                     else
212                         level = 0;
213                 }
214         if (*p == ',')
215             p++;
216         str = p;
217     }
218     return level;
219 }