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