4793f616ae726514c8f833fb49a09fc28a2aed90
[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.1  1995-03-30 10:26:53  quinn
8  * Logging system
9  *
10  * Revision 1.9  1994/12/12  12:09:02  quinn
11  * Changes
12  *
13  * Revision 1.8  1994/11/22  13:15:38  quinn
14  * Simple
15  *
16  * Revision 1.7  1994/10/05  10:16:11  quinn
17  * Added xrealloc. Fixed bug in log.
18  *
19  * Revision 1.6  1994/10/04  14:02:19  quinn
20  * Fixed log_init
21  *
22  * Revision 1.5  1994/09/28  13:07:41  adam
23  * Implemented log_mask_str.
24  *
25  * Revision 1.4  1994/09/27  20:04:13  quinn
26  * Added fflush.
27  *
28  * Revision 1.3  1994/08/18  08:18:48  quinn
29  * Added prefix to log_init.
30  *
31  * Revision 1.2  1994/08/17  14:27:53  quinn
32  * added LOG_ERRNO
33  *
34  * Revision 1.1  1994/08/17  13:23:15  quinn
35  * First version
36  * Added log.c
37  *
38  */
39
40 #include <log.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <stdarg.h>
45 #include <errno.h>
46
47 static int l_level = LOG_DEFAULT_LEVEL;
48 static FILE *l_file = stderr;
49 static char l_prefix[30] = "log";
50
51 static struct {
52     int mask;
53     char *name;
54 } mask_names[] =
55 {
56     { LOG_FATAL, "fatal"},
57     { LOG_DEBUG, "debug"},
58     { LOG_WARN,  "warn" },
59     { LOG_LOG,   "log"  },
60     { LOG_ERRNO, "errno"},
61     { LOG_ALL,   "all"  },
62     { 0,         "none" },
63     { 0, NULL }
64 };  
65
66 void log_init(int level, const char *prefix, const char *name)
67 {
68     l_level = level;
69     if (prefix && *prefix)
70         strcpy(l_prefix, prefix);
71     if (!name || !*name || l_file != stderr)
72         return;
73     if (!(l_file = fopen(name, "a")))
74         return;
75     setbuffer(l_file, 0, 0);
76 }
77
78 void logf(int level, const char *fmt, ...)
79 {
80     va_list ap;
81     char buf[4096], flags[1024];
82     int i;
83
84     if (!(level & l_level))
85         return;
86     *flags = '\0';
87     for (i = 0; level && mask_names[i].name; i++)
88         if (mask_names[i].mask & level)
89         {
90             sprintf(flags + strlen(flags), "[%s]", mask_names[i].name);
91             level -= mask_names[i].mask;
92         }
93     va_start(ap, fmt);
94     vsprintf(buf, fmt, ap);
95     if (level & LOG_ERRNO)
96         sprintf(buf + strlen(buf), " [%s]", strerror(errno));
97     fprintf(l_file, "%s: %s %s\n", l_prefix, flags, buf);
98     fflush(l_file);
99 }
100
101 int log_mask_str (const char *str)
102 {
103     const char *p;
104     int i, level = LOG_DEFAULT_LEVEL;
105
106     while (*str)
107     {
108         for (p = str; *p && *p != ','; p++)
109             ;
110         if (*str == '-' || isdigit(*str))
111             level = atoi (str);
112         else
113             for (i = 0; mask_names[i].name; i++)
114                 if (strlen (mask_names[i].name) == p-str &&
115                     memcmp (mask_names[i].name, str, p-str) == 0)
116                 {
117                     if (mask_names[i].mask)
118                         level |= mask_names[i].mask;
119                     else
120                         level = 0;
121                 }
122         if (*p == ',')
123             p++;
124         str = p;
125     }
126     return level;
127 }