oops, forgot to give a name to LOG_FLUSH. Now added
[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.5 2004-11-02 12:57:54 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     { LOG_FLUSH, "flush" },
73     { 0,         "none" },
74     { 0, NULL }
75 };  
76
77 FILE *yaz_log_file(void)
78 {
79     if (!l_file)
80         l_file = stderr;
81     return l_file;
82 }
83
84 void yaz_log_init_file (const char *fname)
85 {
86     if (fname)
87     {
88         strncpy(l_fname, fname, sizeof(l_fname)-1);
89         l_fname[sizeof(l_fname)-1] = '\0';
90     }
91     else
92         l_fname[0] = '\0';
93     yaz_log_reopen();
94 }
95
96 void yaz_log_reopen(void)
97 {
98     FILE *new_file;
99     if (!l_file)
100         l_file = stderr;
101
102     if (!*l_fname)
103         new_file=stderr;
104     else if (!(new_file = fopen(l_fname, "a")))
105         return;
106     if (l_file != stderr)
107     {
108         fclose (l_file);
109     }
110     setvbuf(new_file, 0, _IONBF, 0);
111     l_file = new_file;
112 }
113
114 void yaz_log_init_level (int level)
115 {
116     l_level = level;
117 }
118
119 void yaz_log_init_prefix (const char *prefix)
120 {
121     if (prefix && *prefix)
122         sprintf(l_prefix, "%.511s ", prefix);
123     else
124         *l_prefix = 0;
125 }
126
127 void yaz_log_init_prefix2 (const char *prefix)
128 {
129     if (prefix && *prefix)
130         sprintf(l_prefix2, "%.511s ", prefix);
131     else
132         *l_prefix2 = 0;
133 }
134
135 void yaz_log_init(int level, const char *prefix, const char *fname)
136 {
137     yaz_log_init_level (level);
138     yaz_log_init_prefix (prefix);
139     if (fname && *fname)
140         yaz_log_init_file (fname);
141 }
142
143 static void (*start_hook_func)(int, const char *, void *) = NULL;
144 static void *start_hook_info;
145 static void (*end_hook_func)(int, const char *, void *) = NULL;
146 static void *end_hook_info;
147
148 void log_event_start (void (*func)(int, const char *, void *), void *info)
149 {
150     start_hook_func = func;
151     start_hook_info = info;
152 }
153
154 void log_event_end (void (*func)(int, const char *, void *), void *info)
155 {
156     end_hook_func = func;
157     end_hook_info = info;
158 }
159
160 void yaz_log(int level, const char *fmt, ...)
161 {
162     va_list ap;
163     char buf[4096], flags[1024];
164     int i;
165     time_t ti;
166     struct tm *tim;
167     char tbuf[TIMEFORMAT_LEN]="";
168     int o_level = level;
169
170     if (!(level & l_level))
171         return;
172     if (!l_file)
173         l_file = stderr;
174     *flags = '\0';
175     for (i = 0; level && mask_names[i].name; i++)
176         if (mask_names[i].mask & level)
177         {
178             if (*mask_names[i].name)
179                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
180             level -= mask_names[i].mask;
181         }
182     va_start(ap, fmt);
183 #ifdef WIN32
184     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
185 #else
186 /* !WIN32 */
187 #if HAVE_VSNPRINTF
188     vsnprintf(buf, sizeof(buf), fmt, ap);
189 #else
190     vsprintf(buf, fmt, ap);
191 #endif
192 #endif
193 /* WIN32 */
194     if (o_level & LOG_ERRNO)
195     {
196         strcat(buf, " [");
197         yaz_strerror(buf+strlen(buf), 2048);
198         strcat(buf, "]");
199     }
200     va_end (ap);
201     if (start_hook_func)
202         (*start_hook_func)(o_level, buf, start_hook_info);
203     ti = time(0);
204     tim = localtime(&ti);
205     if (l_level & LOG_NOTIME)
206       tbuf[0]='\0';
207     else
208       strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
209     tbuf[TIMEFORMAT_LEN-1]='\0';
210     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
211             l_prefix2, buf);
212     if (l_level & (LOG_FLUSH|LOG_DEBUG) )
213         fflush(l_file);
214     if (end_hook_func)
215         (*end_hook_func)(o_level, buf, end_hook_info);
216 }
217
218 void yaz_log_time_format(const char *fmt)
219 {
220     if ( !fmt || !*fmt) 
221     { /* no format, default to new */
222         l_actual_format = l_new_default_format;
223         return; 
224     }
225     if (0==strcmp(fmt,"old"))
226     { /* force the old format */
227         l_actual_format = l_old_default_format;
228         return; 
229     }
230     /* else use custom format */
231     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
232     l_custom_format[TIMEFORMAT_LEN-1]='\0';
233     l_actual_format = l_custom_format;
234 }
235
236 int yaz_log_mask_str (const char *str)
237 {
238     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
239 }
240
241 int yaz_log_mask_str_x (const char *str, int level)
242 {
243     const char *p;
244     int i;
245
246     while (*str)
247     {
248         for (p = str; *p && *p != ','; p++)
249             ;
250         if (*str == '-' || isdigit(*str))
251             level = atoi (str);
252         else
253             for (i = 0; mask_names[i].name; i++)
254                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
255                     memcmp (mask_names[i].name, str, p-str) == 0)
256                 {
257                     if (mask_names[i].mask)
258                         level |= mask_names[i].mask;
259                     else
260                         level = 0;
261                 }
262         if (*p == ',')
263             p++;
264         str = p;
265     }
266     return level;
267 }