Added a way to get log back to stderr
[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.34 2003-02-07 14:39:00 heikki 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_ALL,   "all"  },
56     { 0,         "none" },
57     { 0, NULL }
58 };  
59
60 FILE *yaz_log_file(void)
61 {
62     if (!l_file)
63         l_file = stderr;
64     return l_file;
65 }
66
67 void yaz_log_init_file (const char *fname)
68 {
69     FILE *new_file;
70     if (!l_file)
71         l_file = stderr;
72     if (!fname || !*fname)
73         new_file=stderr;
74     else if (!(new_file = fopen(fname, "a")))
75         return;
76     if (l_file != stderr)
77     {
78         fclose (l_file);
79     }
80     setvbuf(new_file, 0, _IONBF, 0);
81     l_file = new_file;
82 }
83
84 void yaz_log_init_level (int level)
85 {
86     l_level = level;
87 }
88
89 void yaz_log_init_prefix (const char *prefix)
90 {
91     if (prefix && *prefix)
92         sprintf(l_prefix, "%.511s ", prefix);
93     else
94         *l_prefix = 0;
95 }
96
97 void yaz_log_init_prefix2 (const char *prefix)
98 {
99     if (prefix && *prefix)
100         sprintf(l_prefix2, "%.511s ", prefix);
101     else
102         *l_prefix2 = 0;
103 }
104
105 void yaz_log_init(int level, const char *prefix, const char *fname)
106 {
107     yaz_log_init_level (level);
108     yaz_log_init_prefix (prefix);
109     if (fname && *fname)
110         yaz_log_init_file (fname);
111 }
112
113 static void (*start_hook_func)(int, const char *, void *) = NULL;
114 static void *start_hook_info;
115 static void (*end_hook_func)(int, const char *, void *) = NULL;
116 static void *end_hook_info;
117
118 void log_event_start (void (*func)(int, const char *, void *), void *info)
119 {
120     start_hook_func = func;
121     start_hook_info = info;
122 }
123
124 void log_event_end (void (*func)(int, const char *, void *), void *info)
125 {
126     end_hook_func = func;
127     end_hook_info = info;
128 }
129
130 void yaz_log(int level, const char *fmt, ...)
131 {
132     va_list ap;
133     char buf[4096], flags[1024];
134     int i;
135     time_t ti;
136     struct tm *tim;
137     char tbuf[50];
138     int o_level = level;
139
140     if (!(level & l_level))
141         return;
142     if (!l_file)
143         l_file = stderr;
144     *flags = '\0';
145     for (i = 0; level && mask_names[i].name; i++)
146         if (mask_names[i].mask & level)
147         {
148             if (*mask_names[i].name)
149                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
150             level -= mask_names[i].mask;
151         }
152     va_start(ap, fmt);
153 #ifdef WIN32
154     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
155 #else
156 /* !WIN32 */
157 #if HAVE_VSNPRINTF
158     vsnprintf(buf, sizeof(buf), fmt, ap);
159 #else
160     vsprintf(buf, fmt, ap);
161 #endif
162 #endif
163 /* WIN32 */
164     if (o_level & LOG_ERRNO)
165     {
166         strcat(buf, " [");
167         yaz_strerror(buf+strlen(buf), 2048);
168         strcat(buf, "]");
169     }
170     va_end (ap);
171     if (start_hook_func)
172         (*start_hook_func)(o_level, buf, start_hook_info);
173     ti = time(0);
174     tim = localtime(&ti);
175     strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
176     fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
177             l_prefix2, buf);
178     fflush(l_file);
179     if (end_hook_func)
180         (*end_hook_func)(o_level, buf, end_hook_info);
181 }
182
183 int yaz_log_mask_str (const char *str)
184 {
185     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
186 }
187
188 int yaz_log_mask_str_x (const char *str, int level)
189 {
190     const char *p;
191     int i;
192
193     while (*str)
194     {
195         for (p = str; *p && *p != ','; p++)
196             ;
197         if (*str == '-' || isdigit(*str))
198             level = atoi (str);
199         else
200             for (i = 0; mask_names[i].name; i++)
201                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
202                     memcmp (mask_names[i].name, str, p-str) == 0)
203                 {
204                     if (mask_names[i].mask)
205                         level |= mask_names[i].mask;
206                     else
207                         level = 0;
208                 }
209         if (*p == ',')
210             p++;
211         str = p;
212     }
213     return level;
214 }