Initial import
[sliver-openvswitch.git] / lib / vlog.c
1 /* Copyright (C) 2007, 2008 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include "vlog.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/ipc.h>
29 #include <sys/shm.h>
30 #include <syslog.h>
31 #include "dynamic-string.h"
32 #include "util.h"
33
34 /* Name for each logging level. */
35 static const char *level_names[VLL_N_LEVELS] = {
36     [VLL_EMER] = "EMER",
37     [VLL_ERR] = "ERR",
38     [VLL_WARN] = "WARN",
39     [VLL_DBG] = "DBG",
40 };
41
42 /* Name for each logging facility. */
43 static const char *facility_names[VLF_N_FACILITIES] = { 
44     [VLF_CONSOLE] = "console",
45     [VLF_SYSLOG] = "syslog",
46 };
47
48 /* Name for each logging module */
49 static const char *module_names[VLM_N_MODULES] = { 
50 #define VLOG_MODULE(NAME) #NAME,
51     VLOG_MODULES
52 #undef VLOG_MODULES
53 };
54
55 static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
56
57 /* Searches the 'n_names' in 'names'.  Returns the index of a match for
58  * 'target', or 'n_names' if no name matches. */
59 static size_t
60 search_name_array(const char *target, const char **names, size_t n_names) 
61 {
62     size_t i;
63
64     for (i = 0; i < n_names; i++) {
65         assert(names[i]);
66         if (!strcasecmp(names[i], target)) {
67             break;
68         }
69     }
70     return i;
71 }
72
73 /* Returns the name for logging level 'level'. */
74 const char *
75 vlog_get_level_name(enum vlog_level level)
76 {
77     assert(level < VLL_N_LEVELS);
78     return level_names[level];
79 }
80
81 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
82  * is not the name of a logging level. */
83 enum vlog_level
84 vlog_get_level_val(const char *name) 
85 {
86     return search_name_array(name, level_names, ARRAY_SIZE(level_names));
87 }
88
89 /* Returns the name for logging facility 'facility'. */
90 const char *
91 vlog_get_facility_name(enum vlog_facility facility) 
92 {
93     assert(facility < VLF_N_FACILITIES);
94     return facility_names[facility];
95 }
96
97 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
98  * not the name of a logging facility. */
99 enum vlog_facility
100 vlog_get_facility_val(const char *name) 
101 {
102     return search_name_array(name, facility_names, ARRAY_SIZE(facility_names));
103 }
104
105 /* Returns the name for logging module 'module'. */
106 const char *vlog_get_module_name(enum vlog_module module) 
107 {
108     assert(module < VLM_N_MODULES);
109     return module_names[module];
110 }
111
112 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
113  * the name of a logging module. */
114 enum vlog_module
115 vlog_get_module_val(const char *name) 
116 {
117     return search_name_array(name, module_names, ARRAY_SIZE(module_names));
118 }
119
120 /* Returns the current logging level for the given 'module' and 'facility'. */
121 enum vlog_level
122 vlog_get_level(enum vlog_module module, enum vlog_facility facility) 
123 {
124     assert(module < VLM_N_MODULES);
125     assert(facility < VLF_N_FACILITIES);
126     return levels[module][facility];
127 }
128
129 static void
130 set_facility_level(enum vlog_facility facility, enum vlog_module module,
131                    enum vlog_level level)
132 {
133     assert(facility >= 0 && facility < VLF_N_FACILITIES);
134     assert(level < VLL_N_LEVELS);
135
136     if (module == VLM_ANY_MODULE) {
137         for (module = 0; module < VLM_N_MODULES; module++) {
138             levels[module][facility] = level;
139         }
140     } else {
141         levels[module][facility] = level;
142     }
143 }
144
145 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
146 void
147 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
148                 enum vlog_level level) 
149 {
150     assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
151     if (facility == VLF_ANY_FACILITY) {
152         for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
153             set_facility_level(facility, module, level);
154         }
155     } else {
156         set_facility_level(facility, module, level);
157     }
158 }
159
160 /* Set debugging levels:
161  *
162  *  mod:facility:level mod2:facility:level ...
163  *
164  * Return null if successful, otherwise an error message that the caller must
165  * free().
166  */
167 char *
168 vlog_set_levels_from_string(const char *s_)
169 {
170     char *save_ptr;
171     char *s = xstrdup(s_);
172     char *module, *level, *facility;
173
174     for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
175          module = strtok_r(NULL, ": \t", &save_ptr)) {
176         enum vlog_module e_module;
177         enum vlog_level e_level;
178         enum vlog_facility e_facility;
179
180         facility = strtok_r(NULL, ":", &save_ptr);
181         level = strtok_r(NULL, ":", &save_ptr);
182         if (level == NULL || facility == NULL) {
183             free(s);
184             return xstrdup("syntax error in level string");
185         }
186
187         if (!strcmp(module, "ANY")) {
188             e_module = VLM_ANY_MODULE;
189         } else {
190             e_module = vlog_get_module_val(module);
191             if (e_module >= VLM_N_MODULES) {
192                 char *msg = xasprintf("unknown module \"%s\"", module);
193                 free(s);
194                 return msg;
195             }
196         }
197
198         if (!strcmp(facility, "ANY")) {
199             e_facility = VLF_ANY_FACILITY;
200         } else {
201             e_facility = vlog_get_facility_val(facility);
202             if (e_facility >= VLF_N_FACILITIES) {
203                 char *msg = xasprintf("unknown facility \"%s\"", facility);
204                 free(s);
205                 return msg;
206             }
207         }
208
209         e_level = vlog_get_level_val(level);
210         if (e_level >= VLL_N_LEVELS) {
211             char *msg = xasprintf("unknown level \"%s\"", level);
212             free(s);
213             return msg;
214         }
215
216         vlog_set_levels(e_module, e_facility, e_level);
217     }
218     free(s);
219     return NULL;
220 }
221
222 /* If 'arg' is null, configure maximum verbosity.  Otherwise, sets
223  * configuration according to 'arg' (see vlog_set_levels_from_string()).  If
224  * parsing fails, default to maximum verbosity. */
225 void
226 vlog_set_verbosity(const char *arg)
227 {
228     if (arg == NULL || !vlog_set_levels_from_string(arg)) {
229         vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_DBG);
230     }
231 }
232
233 /* Initializes the logging subsystem. */
234 void
235 vlog_init(void) 
236 {
237     openlog(program_name, LOG_NDELAY, LOG_DAEMON);
238     vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
239 }
240
241 /* Closes the logging subsystem. */
242 void
243 vlog_exit(void) 
244 {
245     closelog(); 
246 }
247
248 /* Print the current logging level for each module. */
249 char *
250 vlog_get_levels(void)
251 {
252     struct ds s = DS_EMPTY_INITIALIZER;
253     enum vlog_module module;
254
255     ds_put_format(&s, "                 console    syslog\n");
256     ds_put_format(&s, "                 -------    ------\n");
257
258     for (module = 0; module < VLM_N_MODULES; module++) {
259         ds_put_format(&s, "%-16s  %4s       %4s\n",
260            vlog_get_module_name(module),
261            vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
262            vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
263     }
264
265     return ds_cstr(&s);
266 }
267
268 /* Writes 'message' to the log at the given 'level' and as coming from the
269  * given 'module'. */
270 void
271 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
272 {
273     bool log_console = levels[module][VLF_CONSOLE] >= level;
274     bool log_syslog = levels[module][VLF_SYSLOG] >= level;
275     if (log_console || log_syslog) {
276         static int msg_num;
277         const char *module_name = vlog_get_module_name(module);
278         const char *level_name = vlog_get_level_name(level);
279         va_list args;
280         char s[1024];
281         size_t len;
282
283         len = sprintf(s, "%05d|%s|%s:", ++msg_num, module_name, level_name);
284         va_start(args, message);
285         len += vsnprintf(s + len, sizeof s - len, message, args);
286         va_end(args);
287         if (len >= sizeof s) {
288             len = sizeof s;
289         }
290         if (s[len - 1] == '\n') {
291             s[len - 1] = '\0';
292         }
293
294         if (log_console) {
295             fprintf(stderr, "%s\n", s);
296         }
297
298         if (log_syslog) {
299             static const int syslog_levels[VLL_N_LEVELS] = {
300                 [VLL_EMER] = LOG_EMERG,
301                 [VLL_ERR] = LOG_ERR,
302                 [VLL_WARN] = LOG_WARNING,
303                 [VLL_DBG] = LOG_DEBUG,
304             };
305
306             syslog(syslog_levels[level], "%s", s);
307         }
308     }
309 }