Ficed the log level display for level app
[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.35 2003-02-11 16:35:17 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_ALL,   "all"  }, */
57     { 0,         "none" },
58     { 0, NULL }
59 };  
60
61 FILE *yaz_log_file(void)
62 {
63     if (!l_file)
64         l_file = stderr;
65     return l_file;
66 }
67
68 void yaz_log_init_file (const char *fname)
69 {
70     FILE *new_file;
71     if (!l_file)
72         l_file = stderr;
73     if (!fname || !*fname)
74         new_file=stderr;
75     else if (!(new_file = fopen(fname, "a")))
76         return;
77     if (l_file != stderr)
78     {
79         fclose (l_file);
80     }
81     setvbuf(new_file, 0, _IONBF, 0);
82     l_file = new_file;
83 }
84
85 void yaz_log_init_level (int level)
86 {
87     l_level = level;
88 }
89
90 void yaz_log_init_prefix (const char *prefix)
91 {
92     if (prefix && *prefix)
93         sprintf(l_prefix, "%.511s ", prefix);
94     else
95         *l_prefix = 0;
96 }
97
98 void yaz_log_init_prefix2 (const char *prefix)
99 {
100     if (prefix && *prefix)
101         sprintf(l_prefix2, "%.511s ", prefix);
102     else
103         *l_prefix2 = 0;
104 }
105
106 void yaz_log_init(int level, const char *prefix, const char *fname)
107 {
108     yaz_log_init_level (level);
109     yaz_log_init_prefix (prefix);
110     if (fname && *fname)
111         yaz_log_init_file (fname);
112 }
113
114 static void (*start_hook_func)(int, const char *, void *) = NULL;
115 static void *start_hook_info;
116 static void (*end_hook_func)(int, const char *, void *) = NULL;
117 static void *end_hook_info;
118
119 void log_event_start (void (*func)(int, const char *, void *), void *info)
120 {
121     start_hook_func = func;
122     start_hook_info = info;
123 }
124
125 void log_event_end (void (*func)(int, const char *, void *), void *info)
126 {
127     end_hook_func = func;
128     end_hook_info = info;
129 }
130
131 void yaz_log(int level, const char *fmt, ...)
132 {
133     va_list ap;
134     char buf[4096], flags[1024];
135     int i;
136     time_t ti;
137     struct tm *tim;
138     char tbuf[50];
139     int o_level = level;
140
141     if (!(level & l_level))
142         return;
143     if (!l_file)
144         l_file = stderr;
145     *flags = '\0';
146     for (i = 0; level && mask_names[i].name; i++)
147         if (mask_names[i].mask & level)
148         {
149             if (*mask_names[i].name)
150                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
151             level -= mask_names[i].mask;
152         }
153     va_start(ap, fmt);
154 #ifdef WIN32
155     _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
156 #else
157 /* !WIN32 */
158 #if HAVE_VSNPRINTF
159     vsnprintf(buf, sizeof(buf), fmt, ap);
160 #else
161     vsprintf(buf, fmt, ap);
162 #endif
163 #endif
164 /* WIN32 */
165     if (o_level & LOG_ERRNO)
166     {
167         strcat(buf, " [");
168         yaz_strerror(buf+strlen(buf), 2048);
169         strcat(buf, "]");
170     }
171     va_end (ap);
172     if (start_hook_func)
173         (*start_hook_func)(o_level, buf, start_hook_info);
174     ti = time(0);
175     tim = localtime(&ti);
176     strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
177     fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
178             l_prefix2, buf);
179     fflush(l_file);
180     if (end_hook_func)
181         (*end_hook_func)(o_level, buf, end_hook_info);
182 }
183
184 int yaz_log_mask_str (const char *str)
185 {
186     return yaz_log_mask_str_x (str, LOG_DEFAULT_LEVEL);
187 }
188
189 int yaz_log_mask_str_x (const char *str, int level)
190 {
191     const char *p;
192     int i;
193
194     while (*str)
195     {
196         for (p = str; *p && *p != ','; p++)
197             ;
198         if (*str == '-' || isdigit(*str))
199             level = atoi (str);
200         else
201             for (i = 0; mask_names[i].name; i++)
202                 if (strlen (mask_names[i].name) == (size_t) (p-str) &&
203                     memcmp (mask_names[i].name, str, p-str) == 0)
204                 {
205                     if (mask_names[i].mask)
206                         level |= mask_names[i].mask;
207                     else
208                         level = 0;
209                 }
210         if (*p == ',')
211             p++;
212         str = p;
213     }
214     return level;
215 }