include nmem.h to get prototype for yaz_errno
[yaz-moved-to-github.git] / util / log.c
1 /*
2  * Copyright (c) 1995-2002, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: log.c,v 1.31 2002-11-26 16:56:39 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_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         return;
74     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     yaz_log_init_file (fname);
110 }
111
112 static void (*start_hook_func)(int, const char *, void *) = NULL;
113 static void *start_hook_info;
114 static void (*end_hook_func)(int, const char *, void *) = NULL;
115 static void *end_hook_info;
116
117 void log_event_start (void (*func)(int, const char *, void *), void *info)
118 {
119     start_hook_func = func;
120     start_hook_info = info;
121 }
122
123 void log_event_end (void (*func)(int, const char *, void *), void *info)
124 {
125     end_hook_func = func;
126     end_hook_info = info;
127 }
128
129 void yaz_log(int level, const char *fmt, ...)
130 {
131     va_list ap;
132     char buf[4096], flags[1024];
133     int i;
134     time_t ti;
135     struct tm *tim;
136     char tbuf[50];
137     int o_level = level;
138
139     if (!(level & l_level))
140         return;
141     if (!l_file)
142         l_file = stderr;
143     *flags = '\0';
144     for (i = 0; level && mask_names[i].name; i++)
145         if (mask_names[i].mask & level)
146         {
147             if (*mask_names[i].name)
148                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
149             level -= mask_names[i].mask;
150         }
151     va_start(ap, fmt);
152 #ifdef WIN32
153     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
154 #else
155 /* !WIN32 */
156 #if HAVE_VSNPRINTF
157     vsnprintf(buf, sizeof(buf), fmt, ap);
158 #else
159     vsprintf(buf, fmt, ap);
160 #endif
161 #endif
162 /* WIN32 */
163     if (o_level & LOG_ERRNO)
164     {
165 #ifdef WIN32
166         DWORD err = GetLastError();
167         if (err)
168         {
169             strcat(buf, " [");
170             FormatMessage(
171                 FORMAT_MESSAGE_FROM_SYSTEM,
172                 NULL,
173                 err,
174                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
175                 (LPTSTR) buf + strlen(buf),
176                 2048,
177                 NULL);
178             strcat(buf, "]");
179         }
180 #else
181         sprintf(buf + strlen(buf), " [%s]", strerror(yaz_errno()));
182 #endif
183     }
184     va_end (ap);
185     if (start_hook_func)
186         (*start_hook_func)(o_level, buf, start_hook_info);
187     ti = time(0);
188     tim = localtime(&ti);
189     strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
190     fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
191             l_prefix2, buf);
192     fflush(l_file);
193     if (end_hook_func)
194         (*end_hook_func)(o_level, buf, end_hook_info);
195 }
196
197 int yaz_log_mask_str (const char *str)
198 {
199     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
200 }
201
202 int yaz_log_mask_str_x (const char *str, int level)
203 {
204     const char *p;
205     int i;
206
207     while (*str)
208     {
209         for (p = str; *p && *p != ','; p++)
210             ;
211         if (*str == '-' || isdigit(*str))
212             level = atoi (str);
213         else
214             for (i = 0; mask_names[i].name; i++)
215                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
216                     memcmp (mask_names[i].name, str, p-str) == 0)
217                 {
218                     if (mask_names[i].mask)
219                         level |= mask_names[i].mask;
220                     else
221                         level = 0;
222                 }
223         if (*p == ',')
224             p++;
225         str = p;
226     }
227     return level;
228 }