Not buffering the log file, unless LOG_FLUSH is set. Reopening if necessary
[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.6 2004-11-02 13:52: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
51 static char l_old_default_format[]="%H:%M:%S-%d/%m";
52 static char l_new_default_format[]="%Y%m%d-%H%M%S";
53 #define TIMEFORMAT_LEN 50
54 static char l_custom_format[TIMEFORMAT_LEN]="";
55 static char *l_actual_format=l_old_default_format;
56
57 /** l_max_size tells when to rotate the log. Default to 1 GB */
58 /* static int l_max_size=1024*1024*1024;  */
59
60 static struct {
61     int mask;
62     char *name;
63 } mask_names[] =
64 {
65     { LOG_FATAL, "fatal"},
66     { LOG_DEBUG, "debug"},
67     { LOG_WARN,  "warn" },
68     { LOG_LOG,   "log"  },
69     { LOG_ERRNO, ""},
70     { LOG_MALLOC, "malloc"},
71     { LOG_APP,   "app"  },
72     { LOG_NOTIME, "" },
73     { LOG_APP2  , "app2" },
74     { LOG_APP3  , "app3" },
75     { LOG_ALL,   "all"  },
76     { LOG_FLUSH, "flush" },
77     { 0,         "none" },
78     { 0, NULL }
79 };  
80
81 FILE *yaz_log_file(void)
82 {
83     if (!l_file)
84         l_file = stderr;
85     return l_file;
86 }
87
88 void yaz_log_init_file (const char *fname)
89 {
90     if (fname)
91     {
92         strncpy(l_fname, fname, sizeof(l_fname)-1);
93         l_fname[sizeof(l_fname)-1] = '\0';
94     }
95     else
96         l_fname[0] = '\0';
97     yaz_log_reopen();
98 }
99
100 void yaz_log_reopen(void)
101 {
102     FILE *new_file;
103     if (!l_file)
104         l_file = stderr;
105
106     if (!*l_fname)
107         new_file=stderr;
108     else if (!(new_file = fopen(l_fname, "a")))
109         return;
110     if (l_file != stderr)
111     {
112         fclose (l_file);
113     }
114     if (l_level & LOG_FLUSH)
115         setvbuf(new_file, 0, _IONBF, 0);
116     l_file = new_file;
117 }
118
119 void yaz_log_init_level (int level)
120 {
121     if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
122     {
123         l_level = level;
124         yaz_log_reopen(); /* make sure we set buffering right */
125     } else
126         l_level = level;
127 }
128
129 void yaz_log_init_prefix (const char *prefix)
130 {
131     if (prefix && *prefix)
132         sprintf(l_prefix, "%.511s ", prefix);
133     else
134         *l_prefix = 0;
135 }
136
137 void yaz_log_init_prefix2 (const char *prefix)
138 {
139     if (prefix && *prefix)
140         sprintf(l_prefix2, "%.511s ", prefix);
141     else
142         *l_prefix2 = 0;
143 }
144
145 void yaz_log_init(int level, const char *prefix, const char *fname)
146 {
147     yaz_log_init_level (level);
148     yaz_log_init_prefix (prefix);
149     if (fname && *fname)
150         yaz_log_init_file (fname);
151 }
152
153 static void (*start_hook_func)(int, const char *, void *) = NULL;
154 static void *start_hook_info;
155 static void (*end_hook_func)(int, const char *, void *) = NULL;
156 static void *end_hook_info;
157
158 void log_event_start (void (*func)(int, const char *, void *), void *info)
159 {
160     start_hook_func = func;
161     start_hook_info = info;
162 }
163
164 void log_event_end (void (*func)(int, const char *, void *), void *info)
165 {
166     end_hook_func = func;
167     end_hook_info = info;
168 }
169
170 void yaz_log(int level, const char *fmt, ...)
171 {
172     va_list ap;
173     char buf[4096], flags[1024];
174     int i;
175     time_t ti;
176     struct tm *tim;
177     char tbuf[TIMEFORMAT_LEN]="";
178     int o_level = level;
179     /* int flen; */
180
181     if (!(level & l_level))
182         return;
183     if (!l_file)
184         l_file = stderr;
185     
186 /*    
187     flen=ftell(l_file);
188     if (flen>l_max_size) 
189         rotate_log();
190 */
191     *flags = '\0';
192     for (i = 0; level && mask_names[i].name; i++)
193         if (mask_names[i].mask & level)
194         {
195             if (*mask_names[i].name)
196                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
197             level -= mask_names[i].mask;
198         }
199     va_start(ap, fmt);
200 #ifdef WIN32
201     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
202 #else
203 /* !WIN32 */
204 #if HAVE_VSNPRINTF
205     vsnprintf(buf, sizeof(buf), fmt, ap);
206 #else
207     vsprintf(buf, fmt, ap);
208 #endif
209 #endif
210 /* WIN32 */
211     if (o_level & LOG_ERRNO)
212     {
213         strcat(buf, " [");
214         yaz_strerror(buf+strlen(buf), 2048);
215         strcat(buf, "]");
216     }
217     va_end (ap);
218     if (start_hook_func)
219         (*start_hook_func)(o_level, buf, start_hook_info);
220     ti = time(0);
221     tim = localtime(&ti);
222     if (l_level & LOG_NOTIME)
223       tbuf[0]='\0';
224     else
225       strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
226     tbuf[TIMEFORMAT_LEN-1]='\0';
227     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
228             l_prefix2, buf);
229     if (l_level & (LOG_FLUSH|LOG_DEBUG) )
230         fflush(l_file);
231     if (end_hook_func)
232         (*end_hook_func)(o_level, buf, end_hook_info);
233 }
234
235 void yaz_log_time_format(const char *fmt)
236 {
237     if ( !fmt || !*fmt) 
238     { /* no format, default to new */
239         l_actual_format = l_new_default_format;
240         return; 
241     }
242     if (0==strcmp(fmt,"old"))
243     { /* force the old format */
244         l_actual_format = l_old_default_format;
245         return; 
246     }
247     /* else use custom format */
248     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
249     l_custom_format[TIMEFORMAT_LEN-1]='\0';
250     l_actual_format = l_custom_format;
251 }
252
253 int yaz_log_mask_str (const char *str)
254 {
255     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
256 }
257
258 int yaz_log_mask_str_x (const char *str, int level)
259 {
260     const char *p;
261     int i;
262
263     while (*str)
264     {
265         for (p = str; *p && *p != ','; p++)
266             ;
267         if (*str == '-' || isdigit(*str))
268             level = atoi (str);
269         else
270             for (i = 0; mask_names[i].name; i++)
271                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
272                     memcmp (mask_names[i].name, str, p-str) == 0)
273                 {
274                     if (mask_names[i].mask)
275                         level |= mask_names[i].mask;
276                     else
277                         level = 0;
278                 }
279         if (*p == ',')
280             p++;
281         str = p;
282     }
283     return level;
284 }