protecting log rotation with a mutex
[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.8 2004-11-02 15:47:31 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 #if YAZ_POSIX_THREADS
22 #include <pthread.h>
23 #endif
24
25 #if YAZ_GNU_THREADS
26 #include <pth.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <yaz/nmem.h>
37 #include <yaz/log.h>
38 #include <yaz/nmem.h>
39
40 static NMEM_MUTEX log_mutex=0;
41 static mutex_init_flag=0; /* not yet initialized */
42
43 #define HAS_STRERROR 1
44
45 #if HAS_STRERROR
46
47 #else
48 char *strerror(int n)
49 {
50         extern char *sys_errlist[];
51         return sys_errlist[n];
52 }
53
54 #endif
55
56
57
58 static int l_level = LOG_DEFAULT_LEVEL;
59 static FILE *l_file = NULL;
60 static char l_prefix[512] = "";
61 static char l_prefix2[512] = "";
62 static char l_fname[512] = "";
63
64 static char l_old_default_format[]="%H:%M:%S-%d/%m";
65 static char l_new_default_format[]="%Y%m%d-%H%M%S";
66 #define TIMEFORMAT_LEN 50
67 static char l_custom_format[TIMEFORMAT_LEN]="";
68 static char *l_actual_format=l_old_default_format;
69
70 /** l_max_size tells when to rotate the log. Default to 1 GB */
71 /*static int l_max_size=1024*1024*1024;*/
72 static int l_max_size=1024;   /* while testing */
73
74 static struct {
75     int mask;
76     char *name;
77 } mask_names[] =
78 {
79     { LOG_FATAL, "fatal"},
80     { LOG_DEBUG, "debug"},
81     { LOG_WARN,  "warn" },
82     { LOG_LOG,   "log"  },
83     { LOG_ERRNO, ""},
84     { LOG_MALLOC, "malloc"},
85     { LOG_APP,   "app"  },
86     { LOG_NOTIME, "" },
87     { LOG_APP2  , "app2" },
88     { LOG_APP3  , "app3" },
89     { LOG_ALL,   "all"  },
90     { LOG_FLUSH, "flush" },
91     { 0,         "none" },
92     { 0, NULL }
93 };  
94
95 static void init_mutex()
96 {
97     if (mutex_init_flag)
98         return;
99     nmem_mutex_create (&log_mutex);
100     mutex_init_flag=1;
101 }
102
103
104 FILE *yaz_log_file(void)
105 {
106     if (!l_file)
107         l_file = stderr;
108     return l_file;
109 }
110
111 void yaz_log_init_file (const char *fname)
112 {
113     if (!mutex_init_flag)
114         init_mutex();
115     if (fname)
116     {
117         strncpy(l_fname, fname, sizeof(l_fname)-1);
118         l_fname[sizeof(l_fname)-1] = '\0';
119     }
120     else
121         l_fname[0] = '\0';
122     yaz_log_reopen();
123 }
124
125 void yaz_log_reopen(void)
126 {
127     FILE *new_file;
128     if (!mutex_init_flag)
129         init_mutex();
130     if (!l_file)
131         l_file = stderr;
132     if (!*l_fname)
133         new_file=stderr;
134     else if (!(new_file = fopen(l_fname, "a")))
135         return;
136     if (l_file != stderr)
137     {
138         fclose (l_file);
139     }
140     if (l_level & LOG_FLUSH)
141         setvbuf(new_file, 0, _IONBF, 0);
142     l_file = new_file;
143 }
144
145 static void rotate_log()
146 {
147     char newname[512];
148     if (l_file==stderr)
149         return; /* can't rotate that */
150     if (!*l_fname)
151         return; /* hmm, no name, can't rotate */
152     strncpy(newname, l_fname, 510);
153     newname[510]='\0'; /* make sure it is terminated */
154     strcat(newname,".1");
155 #ifdef WIN32
156     fclose(l_file);
157     l_file=stderr;
158 #endif
159     rename(l_fname, newname);
160     yaz_log_reopen();
161 }
162
163
164 void yaz_log_init_level (int level)
165 {
166     if ( (l_level & LOG_FLUSH) != (level & LOG_FLUSH) )
167     {
168         l_level = level;
169         yaz_log_reopen(); /* make sure we set buffering right */
170     } else
171         l_level = level;
172 }
173
174 void yaz_log_init_prefix (const char *prefix)
175 {
176     if (prefix && *prefix)
177         sprintf(l_prefix, "%.511s ", prefix);
178     else
179         *l_prefix = 0;
180 }
181
182 void yaz_log_init_prefix2 (const char *prefix)
183 {
184     if (prefix && *prefix)
185         sprintf(l_prefix2, "%.511s ", prefix);
186     else
187         *l_prefix2 = 0;
188 }
189
190 void yaz_log_init(int level, const char *prefix, const char *fname)
191 {
192     if (!mutex_init_flag)
193         init_mutex();
194     yaz_log_init_level (level);
195     yaz_log_init_prefix (prefix);
196     if (fname && *fname)
197         yaz_log_init_file (fname);
198 }
199
200 static void (*start_hook_func)(int, const char *, void *) = NULL;
201 static void *start_hook_info;
202 static void (*end_hook_func)(int, const char *, void *) = NULL;
203 static void *end_hook_info;
204
205 void log_event_start (void (*func)(int, const char *, void *), void *info)
206 {
207     start_hook_func = func;
208     start_hook_info = info;
209 }
210
211 void log_event_end (void (*func)(int, const char *, void *), void *info)
212 {
213     end_hook_func = func;
214     end_hook_info = info;
215 }
216
217 void yaz_log(int level, const char *fmt, ...)
218 {
219     va_list ap;
220     char buf[4096], flags[1024];
221     int i;
222     time_t ti;
223     struct tm *tim;
224     char tbuf[TIMEFORMAT_LEN]="";
225     int o_level = level;
226     int flen; 
227
228     if (!(level & l_level))
229         return;
230     if (!mutex_init_flag)
231         init_mutex();
232     if (!l_file)
233         l_file = stderr;
234     
235     if (l_file != stderr)
236     {
237         nmem_mutex_enter (log_mutex);
238         flen=ftell(l_file);
239         if (flen>l_max_size) 
240             rotate_log();
241         nmem_mutex_leave (log_mutex);
242     }
243
244     *flags = '\0';
245     for (i = 0; level && mask_names[i].name; i++)
246         if (mask_names[i].mask & level)
247         {
248             if (*mask_names[i].name)
249                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
250             level -= mask_names[i].mask;
251         }
252     va_start(ap, fmt);
253 #ifdef WIN32
254     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
255 #else
256 /* !WIN32 */
257 #if HAVE_VSNPRINTF
258     vsnprintf(buf, sizeof(buf), fmt, ap);
259 #else
260     vsprintf(buf, fmt, ap);
261 #endif
262 #endif
263 /* WIN32 */
264     if (o_level & LOG_ERRNO)
265     {
266         strcat(buf, " [");
267         yaz_strerror(buf+strlen(buf), 2048);
268         strcat(buf, "]");
269     }
270     va_end (ap);
271     if (start_hook_func)
272         (*start_hook_func)(o_level, buf, start_hook_info);
273     ti = time(0);
274     tim = localtime(&ti);
275     if (l_level & LOG_NOTIME)
276       tbuf[0]='\0';
277     else
278       strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
279     tbuf[TIMEFORMAT_LEN-1]='\0';
280     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
281             l_prefix2, buf);
282     if (l_level & (LOG_FLUSH|LOG_DEBUG) )
283         fflush(l_file);
284     if (end_hook_func)
285         (*end_hook_func)(o_level, buf, end_hook_info);
286 }
287
288 void yaz_log_time_format(const char *fmt)
289 {
290     if ( !fmt || !*fmt) 
291     { /* no format, default to new */
292         l_actual_format = l_new_default_format;
293         return; 
294     }
295     if (0==strcmp(fmt,"old"))
296     { /* force the old format */
297         l_actual_format = l_old_default_format;
298         return; 
299     }
300     /* else use custom format */
301     strncpy(l_custom_format, fmt, TIMEFORMAT_LEN-1);
302     l_custom_format[TIMEFORMAT_LEN-1]='\0';
303     l_actual_format = l_custom_format;
304 }
305
306 int yaz_log_mask_str (const char *str)
307 {
308     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
309 }
310
311 int yaz_log_mask_str_x (const char *str, int level)
312 {
313     const char *p;
314     int i;
315
316     while (*str)
317     {
318         for (p = str; *p && *p != ','; p++)
319             ;
320         if (*str == '-' || isdigit(*str))
321             level = atoi (str);
322         else
323             for (i = 0; mask_names[i].name; i++)
324                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
325                     memcmp (mask_names[i].name, str, p-str) == 0)
326                 {
327                     if (mask_names[i].mask)
328                         level |= mask_names[i].mask;
329                     else
330                         level = 0;
331                 }
332         if (*p == ',')
333             p++;
334         str = p;
335     }
336     return level;
337 }