a9fb5fc717f4057ebb646f02a4a0b8edbce60d0f
[yaz-moved-to-github.git] / util / log.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: log.c,v $
7  * Revision 1.5  1995-05-16 08:51:11  quinn
8  * License, documentation, and memory fixes
9  *
10  * Revision 1.4  1995/05/15  11:56:55  quinn
11  * Debuggng & adjustments.
12  *
13  * Revision 1.3  1995/04/10  10:23:51  quinn
14  * Fixes.
15  *
16  * Revision 1.2  1995/03/31  10:16:55  quinn
17  * Fixed logging.
18  *
19  * Revision 1.1  1995/03/30  10:26:53  quinn
20  * Logging system
21  *
22  * Revision 1.9  1994/12/12  12:09:02  quinn
23  * Changes
24  *
25  * Revision 1.8  1994/11/22  13:15:38  quinn
26  * Simple
27  *
28  * Revision 1.7  1994/10/05  10:16:11  quinn
29  * Added xrealloc. Fixed bug in log.
30  *
31  * Revision 1.6  1994/10/04  14:02:19  quinn
32  * Fixed log_init
33  *
34  * Revision 1.5  1994/09/28  13:07:41  adam
35  * Implemented log_mask_str.
36  *
37  * Revision 1.4  1994/09/27  20:04:13  quinn
38  * Added fflush.
39  *
40  * Revision 1.3  1994/08/18  08:18:48  quinn
41  * Added prefix to log_init.
42  *
43  * Revision 1.2  1994/08/17  14:27:53  quinn
44  * added LOG_ERRNO
45  *
46  * Revision 1.1  1994/08/17  13:23:15  quinn
47  * First version
48  * Added log.c
49  *
50  */
51
52 #include <log.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <ctype.h>
56 #include <stdarg.h>
57 #include <errno.h>
58
59 static int l_level = LOG_DEFAULT_LEVEL;
60 static FILE *l_file = stderr;
61 static char l_prefix[30] = "log";
62
63 static struct {
64     int mask;
65     char *name;
66 } mask_names[] =
67 {
68     { LOG_FATAL, "fatal"},
69     { LOG_DEBUG, "debug"},
70     { LOG_WARN,  "warn" },
71     { LOG_LOG,   "log"  },
72     { LOG_ERRNO, ""},
73     { LOG_ALL,   "all"  },
74     { 0,         "none" },
75     { 0, NULL }
76 };  
77
78 #ifndef strerror
79
80 char *strerror(int n)
81 {
82         extern char *sys_errlist[];
83         return sys_errlist[n];
84 }
85
86 #endif
87
88 void log_init(int level, const char *prefix, const char *name)
89 {
90     l_level = level;
91     if (prefix && *prefix)
92         strcpy(l_prefix, prefix);
93     if (!name || !*name || l_file != stderr)
94         return;
95     if (!(l_file = fopen(name, "a")))
96         return;
97     setvbuf(l_file, 0, _IONBF, 0);
98 }
99
100 void logf(int level, const char *fmt, ...)
101 {
102     va_list ap;
103     char buf[4096], flags[1024];
104     int i, p_error = 0;
105
106     if (!(level & l_level))
107         return;
108     if (level & LOG_ERRNO)
109         p_error = 1;
110     *flags = '\0';
111     for (i = 0; level && mask_names[i].name; i++)
112         if (mask_names[i].mask & level)
113         {
114             if (*mask_names[i].name)
115                 sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
116             level -= mask_names[i].mask;
117         }
118     va_start(ap, fmt);
119     vsprintf(buf, fmt, ap);
120     if (p_error)
121         sprintf(buf + strlen(buf), " [%s]", strerror(errno));
122     fprintf(l_file, "%s: %s %s\n", l_prefix, flags, buf);
123     fflush(l_file);
124 }
125
126 int log_mask_str (const char *str)
127 {
128     const char *p;
129     int i, level = LOG_DEFAULT_LEVEL;
130
131     while (*str)
132     {
133         for (p = str; *p && *p != ','; p++)
134             ;
135         if (*str == '-' || isdigit(*str))
136             level = atoi (str);
137         else
138             for (i = 0; mask_names[i].name; i++)
139                 if (strlen (mask_names[i].name) == p-str &&
140                     memcmp (mask_names[i].name, str, p-str) == 0)
141                 {
142                     if (mask_names[i].mask)
143                         level |= mask_names[i].mask;
144                     else
145                         level = 0;
146                 }
147         if (*p == ',')
148             p++;
149         str = p;
150     }
151     return level;
152 }