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