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