6b3d871f5197f402425863b3156780e298383a1a
[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 <time.h>
44 #include "dynamic-string.h"
45 #include "util.h"
46
47 #define THIS_MODULE VLM_vlog
48
49 /* Name for each logging level. */
50 static const char *level_names[VLL_N_LEVELS] = {
51     [VLL_EMER] = "EMER",
52     [VLL_ERR] = "ERR",
53     [VLL_WARN] = "WARN",
54     [VLL_DBG] = "DBG",
55 };
56
57 /* Name for each logging facility. */
58 static const char *facility_names[VLF_N_FACILITIES] = { 
59     [VLF_CONSOLE] = "console",
60     [VLF_SYSLOG] = "syslog",
61 };
62
63 /* Name for each logging module */
64 static const char *module_names[VLM_N_MODULES] = { 
65 #define VLOG_MODULE(NAME) #NAME,
66     VLOG_MODULES
67 #undef VLOG_MODULES
68 };
69
70 static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
71
72 /* Searches the 'n_names' in 'names'.  Returns the index of a match for
73  * 'target', or 'n_names' if no name matches. */
74 static size_t
75 search_name_array(const char *target, const char **names, size_t n_names) 
76 {
77     size_t i;
78
79     for (i = 0; i < n_names; i++) {
80         assert(names[i]);
81         if (!strcasecmp(names[i], target)) {
82             break;
83         }
84     }
85     return i;
86 }
87
88 /* Returns the name for logging level 'level'. */
89 const char *
90 vlog_get_level_name(enum vlog_level level)
91 {
92     assert(level < VLL_N_LEVELS);
93     return level_names[level];
94 }
95
96 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
97  * is not the name of a logging level. */
98 enum vlog_level
99 vlog_get_level_val(const char *name) 
100 {
101     return search_name_array(name, level_names, ARRAY_SIZE(level_names));
102 }
103
104 /* Returns the name for logging facility 'facility'. */
105 const char *
106 vlog_get_facility_name(enum vlog_facility facility) 
107 {
108     assert(facility < VLF_N_FACILITIES);
109     return facility_names[facility];
110 }
111
112 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
113  * not the name of a logging facility. */
114 enum vlog_facility
115 vlog_get_facility_val(const char *name) 
116 {
117     return search_name_array(name, facility_names, ARRAY_SIZE(facility_names));
118 }
119
120 /* Returns the name for logging module 'module'. */
121 const char *vlog_get_module_name(enum vlog_module module) 
122 {
123     assert(module < VLM_N_MODULES);
124     return module_names[module];
125 }
126
127 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
128  * the name of a logging module. */
129 enum vlog_module
130 vlog_get_module_val(const char *name) 
131 {
132     return search_name_array(name, module_names, ARRAY_SIZE(module_names));
133 }
134
135 /* Returns the current logging level for the given 'module' and 'facility'. */
136 enum vlog_level
137 vlog_get_level(enum vlog_module module, enum vlog_facility facility) 
138 {
139     assert(module < VLM_N_MODULES);
140     assert(facility < VLF_N_FACILITIES);
141     return levels[module][facility];
142 }
143
144 static void
145 set_facility_level(enum vlog_facility facility, enum vlog_module module,
146                    enum vlog_level level)
147 {
148     assert(facility >= 0 && facility < VLF_N_FACILITIES);
149     assert(level < VLL_N_LEVELS);
150
151     if (module == VLM_ANY_MODULE) {
152         for (module = 0; module < VLM_N_MODULES; module++) {
153             levels[module][facility] = level;
154         }
155     } else {
156         levels[module][facility] = level;
157     }
158 }
159
160 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
161 void
162 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
163                 enum vlog_level level) 
164 {
165     assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
166     if (facility == VLF_ANY_FACILITY) {
167         for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
168             set_facility_level(facility, module, level);
169         }
170     } else {
171         set_facility_level(facility, module, level);
172     }
173 }
174
175 /* Set debugging levels:
176  *
177  *  mod[:facility[:level]] mod2[:facility[:level]] ...
178  *
179  * Return null if successful, otherwise an error message that the caller must
180  * free().
181  */
182 char *
183 vlog_set_levels_from_string(const char *s_)
184 {
185     char *save_ptr;
186     char *s = xstrdup(s_);
187     char *module, *level, *facility;
188
189     for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
190          module = strtok_r(NULL, ": \t", &save_ptr)) {
191         enum vlog_module e_module;
192         enum vlog_level e_level;
193         enum vlog_facility e_facility;
194
195         facility = strtok_r(NULL, ":", &save_ptr);
196         level = strtok_r(NULL, ":", &save_ptr);
197
198         if (!strcmp(module, "ANY")) {
199             e_module = VLM_ANY_MODULE;
200         } else {
201             e_module = vlog_get_module_val(module);
202             if (e_module >= VLM_N_MODULES) {
203                 char *msg = xasprintf("unknown module \"%s\"", module);
204                 free(s);
205                 return msg;
206             }
207         }
208
209         if (!facility || !strcmp(facility, "ANY")) {
210             e_facility = VLF_ANY_FACILITY;
211         } else {
212             e_facility = vlog_get_facility_val(facility);
213             if (e_facility >= VLF_N_FACILITIES) {
214                 char *msg = xasprintf("unknown facility \"%s\"", facility);
215                 free(s);
216                 return msg;
217             }
218         }
219
220         e_level = level ? vlog_get_level_val(level) : VLL_DBG;
221         if (e_level >= VLL_N_LEVELS) {
222             char *msg = xasprintf("unknown level \"%s\"", level);
223             free(s);
224             return msg;
225         }
226
227         vlog_set_levels(e_module, e_facility, e_level);
228     }
229     free(s);
230     return NULL;
231 }
232
233 /* If 'arg' is null, configure maximum verbosity.  Otherwise, sets
234  * configuration according to 'arg' (see vlog_set_levels_from_string()). */
235 void
236 vlog_set_verbosity(const char *arg)
237 {
238     if (arg) {
239         char *msg = vlog_set_levels_from_string(arg);
240         if (msg) {
241             fatal(0, "processing \"%s\": %s", arg, msg);
242         }
243     } else {
244         vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_DBG);
245     }
246 }
247
248 /* Initializes the logging subsystem. */
249 void
250 vlog_init(void) 
251 {
252     time_t now;
253
254     openlog(program_name, LOG_NDELAY, LOG_DAEMON);
255     vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_WARN);
256
257     now = time(0);
258     if (now < 0) {
259         struct tm tm;
260         char s[128];
261
262         localtime_r(&now, &tm);
263         strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm);
264         VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
265     }
266 }
267
268 /* Closes the logging subsystem. */
269 void
270 vlog_exit(void) 
271 {
272     closelog(); 
273 }
274
275 /* Print the current logging level for each module. */
276 char *
277 vlog_get_levels(void)
278 {
279     struct ds s = DS_EMPTY_INITIALIZER;
280     enum vlog_module module;
281
282     ds_put_format(&s, "                 console    syslog\n");
283     ds_put_format(&s, "                 -------    ------\n");
284
285     for (module = 0; module < VLM_N_MODULES; module++) {
286         ds_put_format(&s, "%-16s  %4s       %4s\n",
287            vlog_get_module_name(module),
288            vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
289            vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
290     }
291
292     return ds_cstr(&s);
293 }
294
295 /* Returns true if a log message emitted for the given 'module' and 'level'
296  * would cause some log output, false if that module and level are completely
297  * disabled. */
298 bool
299 vlog_is_enabled(enum vlog_module module, enum vlog_level level)
300 {
301     return (levels[module][VLF_CONSOLE] >= level
302             || levels[module][VLF_SYSLOG] >= level);
303 }
304
305 /* Writes 'message' to the log at the given 'level' and as coming from the
306  * given 'module'.
307  *
308  * Guaranteed to preserve errno. */
309 void
310 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
311 {
312     bool log_console = levels[module][VLF_CONSOLE] >= level;
313     bool log_syslog = levels[module][VLF_SYSLOG] >= level;
314     if (log_console || log_syslog) {
315         int save_errno = errno;
316         static int msg_num;
317         const char *module_name = vlog_get_module_name(module);
318         const char *level_name = vlog_get_level_name(level);
319         time_t now;
320         struct tm tm;
321         va_list args;
322         char s[1024];
323         size_t len, time_len;
324
325         now = time(0);
326         localtime_r(&now, &tm);
327
328         len = time_len = strftime(s, sizeof s, "%b %d %H:%M:%S|", &tm);
329         len += sprintf(s + len, "%05d|%s|%s:",
330                        ++msg_num, module_name, level_name);
331         va_start(args, message);
332         len += vsnprintf(s + len, sizeof s - len, message, args);
333         va_end(args);
334         if (len >= sizeof s) {
335             len = sizeof s;
336         }
337         if (s[len - 1] == '\n') {
338             s[len - 1] = '\0';
339         }
340
341         if (log_console) {
342             fprintf(stderr, "%s\n", s);
343         }
344
345         if (log_syslog) {
346             static const int syslog_levels[VLL_N_LEVELS] = {
347                 [VLL_EMER] = LOG_EMERG,
348                 [VLL_ERR] = LOG_ERR,
349                 [VLL_WARN] = LOG_WARNING,
350                 [VLL_DBG] = LOG_DEBUG,
351             };
352
353             syslog(syslog_levels[level], "%s", s + time_len);
354         }
355         errno = save_errno;
356     }
357 }