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