32e26d97cb21f1fbc6aa6b496ae79cae16109362
[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.2 2004-10-15 00:19:00 adam 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 static int l_level = LOG_DEFAULT_LEVEL;
45 static FILE *l_file = NULL;
46 static char l_prefix[512] = "";
47 static char l_prefix2[512] = "";
48 static char l_fname[512] = "";
49
50 static struct {
51     int mask;
52     char *name;
53 } mask_names[] =
54 {
55     { LOG_FATAL, "fatal"},
56     { LOG_DEBUG, "debug"},
57     { LOG_WARN,  "warn" },
58     { LOG_LOG,   "log"  },
59     { LOG_ERRNO, ""},
60     { LOG_MALLOC, "malloc"},
61     { LOG_APP,   "app"  },
62     { LOG_NOTIME, "" },
63     { LOG_APP2  , "app2" },
64     { LOG_APP3  , "app3" },
65     { LOG_ALL,   "all"  },
66     { 0,         "none" },
67     { 0, NULL }
68 };  
69
70 FILE *yaz_log_file(void)
71 {
72     if (!l_file)
73         l_file = stderr;
74     return l_file;
75 }
76
77 void yaz_log_init_file (const char *fname)
78 {
79     if (fname)
80     {
81         strncpy(l_fname, fname, sizeof(l_fname)-1);
82         l_fname[sizeof(l_fname)-1] = '\0';
83     }
84     else
85         l_fname[0] = '\0';
86     yaz_log_reopen();
87 }
88
89 void yaz_log_reopen(void)
90 {
91     FILE *new_file;
92     if (!l_file)
93         l_file = stderr;
94
95     if (!*l_fname)
96         new_file=stderr;
97     else if (!(new_file = fopen(l_fname, "a")))
98         return;
99     if (l_file != stderr)
100     {
101         fclose (l_file);
102     }
103     setvbuf(new_file, 0, _IONBF, 0);
104     l_file = new_file;
105 }
106
107 void yaz_log_init_level (int level)
108 {
109     l_level = level;
110 }
111
112 void yaz_log_init_prefix (const char *prefix)
113 {
114     if (prefix && *prefix)
115         sprintf(l_prefix, "%.511s ", prefix);
116     else
117         *l_prefix = 0;
118 }
119
120 void yaz_log_init_prefix2 (const char *prefix)
121 {
122     if (prefix && *prefix)
123         sprintf(l_prefix2, "%.511s ", prefix);
124     else
125         *l_prefix2 = 0;
126 }
127
128 void yaz_log_init(int level, const char *prefix, const char *fname)
129 {
130     yaz_log_init_level (level);
131     yaz_log_init_prefix (prefix);
132     if (fname && *fname)
133         yaz_log_init_file (fname);
134 }
135
136 static void (*start_hook_func)(int, const char *, void *) = NULL;
137 static void *start_hook_info;
138 static void (*end_hook_func)(int, const char *, void *) = NULL;
139 static void *end_hook_info;
140
141 void log_event_start (void (*func)(int, const char *, void *), void *info)
142 {
143     start_hook_func = func;
144     start_hook_info = info;
145 }
146
147 void log_event_end (void (*func)(int, const char *, void *), void *info)
148 {
149     end_hook_func = func;
150     end_hook_info = info;
151 }
152
153 void yaz_log(int level, const char *fmt, ...)
154 {
155     va_list ap;
156     char buf[4096], flags[1024];
157     int i;
158     time_t ti;
159     struct tm *tim;
160     char tbuf[50]="";
161     int o_level = level;
162
163     if (!(level & l_level))
164         return;
165     if (!l_file)
166         l_file = stderr;
167     *flags = '\0';
168     for (i = 0; level && mask_names[i].name; i++)
169         if (mask_names[i].mask & level)
170         {
171             if (*mask_names[i].name)
172                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
173             level -= mask_names[i].mask;
174         }
175     va_start(ap, fmt);
176 #ifdef WIN32
177     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
178 #else
179 /* !WIN32 */
180 #if HAVE_VSNPRINTF
181     vsnprintf(buf, sizeof(buf), fmt, ap);
182 #else
183     vsprintf(buf, fmt, ap);
184 #endif
185 #endif
186 /* WIN32 */
187     if (o_level & LOG_ERRNO)
188     {
189         strcat(buf, " [");
190         yaz_strerror(buf+strlen(buf), 2048);
191         strcat(buf, "]");
192     }
193     va_end (ap);
194     if (start_hook_func)
195         (*start_hook_func)(o_level, buf, start_hook_info);
196     ti = time(0);
197     tim = localtime(&ti);
198     if (l_level & LOG_NOTIME)
199       tbuf[0]='\0';
200     else
201       strftime(tbuf, 50, "%H:%M:%S-%d/%m: ", tim);
202     fprintf(l_file, "%s%s%s %s%s\n", tbuf, l_prefix, flags,
203             l_prefix2, buf);
204     fflush(l_file);
205     if (end_hook_func)
206         (*end_hook_func)(o_level, buf, end_hook_info);
207 }
208
209 int yaz_log_mask_str (const char *str)
210 {
211     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
212 }
213
214 int yaz_log_mask_str_x (const char *str, int level)
215 {
216     const char *p;
217     int i;
218
219     while (*str)
220     {
221         for (p = str; *p && *p != ','; p++)
222             ;
223         if (*str == '-' || isdigit(*str))
224             level = atoi (str);
225         else
226             for (i = 0; mask_names[i].name; i++)
227                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
228                     memcmp (mask_names[i].name, str, p-str) == 0)
229                 {
230                     if (mask_names[i].mask)
231                         level |= mask_names[i].mask;
232                     else
233                         level = 0;
234                 }
235         if (*p == ',')
236             p++;
237         str = p;
238     }
239     return level;
240 }