1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
44 #include "dynamic-string.h"
47 /* Name for each logging level. */
48 static const char *level_names[VLL_N_LEVELS] = {
55 /* Name for each logging facility. */
56 static const char *facility_names[VLF_N_FACILITIES] = {
57 [VLF_CONSOLE] = "console",
58 [VLF_SYSLOG] = "syslog",
61 /* Name for each logging module */
62 static const char *module_names[VLM_N_MODULES] = {
63 #define VLOG_MODULE(NAME) #NAME,
68 static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
70 /* Searches the 'n_names' in 'names'. Returns the index of a match for
71 * 'target', or 'n_names' if no name matches. */
73 search_name_array(const char *target, const char **names, size_t n_names)
77 for (i = 0; i < n_names; i++) {
79 if (!strcasecmp(names[i], target)) {
86 /* Returns the name for logging level 'level'. */
88 vlog_get_level_name(enum vlog_level level)
90 assert(level < VLL_N_LEVELS);
91 return level_names[level];
94 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
95 * is not the name of a logging level. */
97 vlog_get_level_val(const char *name)
99 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
102 /* Returns the name for logging facility 'facility'. */
104 vlog_get_facility_name(enum vlog_facility facility)
106 assert(facility < VLF_N_FACILITIES);
107 return facility_names[facility];
110 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
111 * not the name of a logging facility. */
113 vlog_get_facility_val(const char *name)
115 return search_name_array(name, facility_names, ARRAY_SIZE(facility_names));
118 /* Returns the name for logging module 'module'. */
119 const char *vlog_get_module_name(enum vlog_module module)
121 assert(module < VLM_N_MODULES);
122 return module_names[module];
125 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
126 * the name of a logging module. */
128 vlog_get_module_val(const char *name)
130 return search_name_array(name, module_names, ARRAY_SIZE(module_names));
133 /* Returns the current logging level for the given 'module' and 'facility'. */
135 vlog_get_level(enum vlog_module module, enum vlog_facility facility)
137 assert(module < VLM_N_MODULES);
138 assert(facility < VLF_N_FACILITIES);
139 return levels[module][facility];
143 set_facility_level(enum vlog_facility facility, enum vlog_module module,
144 enum vlog_level level)
146 assert(facility >= 0 && facility < VLF_N_FACILITIES);
147 assert(level < VLL_N_LEVELS);
149 if (module == VLM_ANY_MODULE) {
150 for (module = 0; module < VLM_N_MODULES; module++) {
151 levels[module][facility] = level;
154 levels[module][facility] = level;
158 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
160 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
161 enum vlog_level level)
163 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
164 if (facility == VLF_ANY_FACILITY) {
165 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
166 set_facility_level(facility, module, level);
169 set_facility_level(facility, module, level);
173 /* Set debugging levels:
175 * mod:facility:level mod2:facility:level ...
177 * Return null if successful, otherwise an error message that the caller must
181 vlog_set_levels_from_string(const char *s_)
184 char *s = xstrdup(s_);
185 char *module, *level, *facility;
187 for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
188 module = strtok_r(NULL, ": \t", &save_ptr)) {
189 enum vlog_module e_module;
190 enum vlog_level e_level;
191 enum vlog_facility e_facility;
193 facility = strtok_r(NULL, ":", &save_ptr);
194 level = strtok_r(NULL, ":", &save_ptr);
195 if (level == NULL || facility == NULL) {
197 return xstrdup("syntax error in level string");
200 if (!strcmp(module, "ANY")) {
201 e_module = VLM_ANY_MODULE;
203 e_module = vlog_get_module_val(module);
204 if (e_module >= VLM_N_MODULES) {
205 char *msg = xasprintf("unknown module \"%s\"", module);
211 if (!strcmp(facility, "ANY")) {
212 e_facility = VLF_ANY_FACILITY;
214 e_facility = vlog_get_facility_val(facility);
215 if (e_facility >= VLF_N_FACILITIES) {
216 char *msg = xasprintf("unknown facility \"%s\"", facility);
222 e_level = vlog_get_level_val(level);
223 if (e_level >= VLL_N_LEVELS) {
224 char *msg = xasprintf("unknown level \"%s\"", level);
229 vlog_set_levels(e_module, e_facility, e_level);
235 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
236 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
238 vlog_set_verbosity(const char *arg)
241 char *msg = vlog_set_levels_from_string(arg);
243 fatal(0, "processing \"%s\": %s", arg, msg);
246 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_DBG);
250 /* Initializes the logging subsystem. */
254 openlog(program_name, LOG_NDELAY, LOG_DAEMON);
255 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
258 /* Closes the logging subsystem. */
265 /* Print the current logging level for each module. */
267 vlog_get_levels(void)
269 struct ds s = DS_EMPTY_INITIALIZER;
270 enum vlog_module module;
272 ds_put_format(&s, " console syslog\n");
273 ds_put_format(&s, " ------- ------\n");
275 for (module = 0; module < VLM_N_MODULES; module++) {
276 ds_put_format(&s, "%-16s %4s %4s\n",
277 vlog_get_module_name(module),
278 vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
279 vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
285 /* Returns true if a log message emitted for the given 'module' and 'level'
286 * would cause some log output, false if that module and level are completely
289 vlog_is_enabled(enum vlog_module module, enum vlog_level level)
291 return (levels[module][VLF_CONSOLE] >= level
292 || levels[module][VLF_SYSLOG] >= level);
295 /* Writes 'message' to the log at the given 'level' and as coming from the
298 * Guaranteed to preserve errno. */
300 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
302 bool log_console = levels[module][VLF_CONSOLE] >= level;
303 bool log_syslog = levels[module][VLF_SYSLOG] >= level;
304 if (log_console || log_syslog) {
305 int save_errno = errno;
307 const char *module_name = vlog_get_module_name(module);
308 const char *level_name = vlog_get_level_name(level);
313 size_t len, time_len;
316 localtime_r(&now, &tm);
318 len = time_len = strftime(s, sizeof s, "%b %d %H:%M:%S|", &tm);
319 len += sprintf(s + len, "%05d|%s|%s:",
320 ++msg_num, module_name, level_name);
321 va_start(args, message);
322 len += vsnprintf(s + len, sizeof s - len, message, args);
324 if (len >= sizeof s) {
327 if (s[len - 1] == '\n') {
332 fprintf(stderr, "%s\n", s);
336 static const int syslog_levels[VLL_N_LEVELS] = {
337 [VLL_EMER] = LOG_EMERG,
339 [VLL_WARN] = LOG_WARNING,
340 [VLL_DBG] = LOG_DEBUG,
343 syslog(syslog_levels[level], "%s", s + time_len);