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