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