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.
45 #include "dynamic-string.h"
50 #define THIS_MODULE VLM_vlog
52 /* Name for each logging level. */
53 static const char *level_names[VLL_N_LEVELS] = {
60 /* Name for each logging facility. */
61 static const char *facility_names[VLF_N_FACILITIES] = {
62 [VLF_CONSOLE] = "console",
63 [VLF_SYSLOG] = "syslog",
66 /* Name for each logging module */
67 static const char *module_names[VLM_N_MODULES] = {
68 #define VLOG_MODULE(NAME) #NAME,
73 static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
75 /* Searches the 'n_names' in 'names'. Returns the index of a match for
76 * 'target', or 'n_names' if no name matches. */
78 search_name_array(const char *target, const char **names, size_t n_names)
82 for (i = 0; i < n_names; i++) {
84 if (!strcasecmp(names[i], target)) {
91 /* Returns the name for logging level 'level'. */
93 vlog_get_level_name(enum vlog_level level)
95 assert(level < VLL_N_LEVELS);
96 return level_names[level];
99 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
100 * is not the name of a logging level. */
102 vlog_get_level_val(const char *name)
104 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
107 /* Returns the name for logging facility 'facility'. */
109 vlog_get_facility_name(enum vlog_facility facility)
111 assert(facility < VLF_N_FACILITIES);
112 return facility_names[facility];
115 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
116 * not the name of a logging facility. */
118 vlog_get_facility_val(const char *name)
120 return search_name_array(name, facility_names, ARRAY_SIZE(facility_names));
123 /* Returns the name for logging module 'module'. */
124 const char *vlog_get_module_name(enum vlog_module module)
126 assert(module < VLM_N_MODULES);
127 return module_names[module];
130 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
131 * the name of a logging module. */
133 vlog_get_module_val(const char *name)
135 return search_name_array(name, module_names, ARRAY_SIZE(module_names));
138 /* Returns the current logging level for the given 'module' and 'facility'. */
140 vlog_get_level(enum vlog_module module, enum vlog_facility facility)
142 assert(module < VLM_N_MODULES);
143 assert(facility < VLF_N_FACILITIES);
144 return levels[module][facility];
148 set_facility_level(enum vlog_facility facility, enum vlog_module module,
149 enum vlog_level level)
151 assert(facility >= 0 && facility < VLF_N_FACILITIES);
152 assert(level < VLL_N_LEVELS);
154 if (module == VLM_ANY_MODULE) {
155 for (module = 0; module < VLM_N_MODULES; module++) {
156 levels[module][facility] = level;
159 levels[module][facility] = level;
163 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
165 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
166 enum vlog_level level)
168 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
169 if (facility == VLF_ANY_FACILITY) {
170 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
171 set_facility_level(facility, module, level);
174 set_facility_level(facility, module, level);
178 /* Set debugging levels:
180 * mod[:facility[:level]] mod2[:facility[:level]] ...
182 * Return null if successful, otherwise an error message that the caller must
186 vlog_set_levels_from_string(const char *s_)
189 char *s = xstrdup(s_);
190 char *module, *level, *facility;
192 for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
193 module = strtok_r(NULL, ": \t", &save_ptr)) {
194 enum vlog_module e_module;
195 enum vlog_level e_level;
196 enum vlog_facility e_facility;
198 facility = strtok_r(NULL, ":", &save_ptr);
199 level = strtok_r(NULL, ":", &save_ptr);
201 if (!strcmp(module, "ANY")) {
202 e_module = VLM_ANY_MODULE;
204 e_module = vlog_get_module_val(module);
205 if (e_module >= VLM_N_MODULES) {
206 char *msg = xasprintf("unknown module \"%s\"", module);
212 if (!facility || !strcmp(facility, "ANY")) {
213 e_facility = VLF_ANY_FACILITY;
215 e_facility = vlog_get_facility_val(facility);
216 if (e_facility >= VLF_N_FACILITIES) {
217 char *msg = xasprintf("unknown facility \"%s\"", facility);
223 e_level = level ? vlog_get_level_val(level) : VLL_DBG;
224 if (e_level >= VLL_N_LEVELS) {
225 char *msg = xasprintf("unknown level \"%s\"", level);
230 vlog_set_levels(e_module, e_facility, e_level);
236 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
237 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
239 vlog_set_verbosity(const char *arg)
242 char *msg = vlog_set_levels_from_string(arg);
244 fatal(0, "processing \"%s\": %s", arg, msg);
247 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_DBG);
251 /* Initializes the logging subsystem. */
257 openlog(program_name, LOG_NDELAY, LOG_DAEMON);
258 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_WARN);
265 localtime_r(&now, &tm);
266 strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm);
267 VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
271 /* Closes the logging subsystem. */
278 /* Print the current logging level for each module. */
280 vlog_get_levels(void)
282 struct ds s = DS_EMPTY_INITIALIZER;
283 enum vlog_module module;
285 ds_put_format(&s, " console syslog\n");
286 ds_put_format(&s, " ------- ------\n");
288 for (module = 0; module < VLM_N_MODULES; module++) {
289 ds_put_format(&s, "%-16s %4s %4s\n",
290 vlog_get_module_name(module),
291 vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
292 vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
298 /* Returns true if a log message emitted for the given 'module' and 'level'
299 * would cause some log output, false if that module and level are completely
302 vlog_is_enabled(enum vlog_module module, enum vlog_level level)
304 return (levels[module][VLF_CONSOLE] >= level
305 || levels[module][VLF_SYSLOG] >= level);
308 /* Writes 'message' to the log at the given 'level' and as coming from the
311 * Guaranteed to preserve errno. */
313 vlog_valist(enum vlog_module module, enum vlog_level level,
314 const char *message, va_list args)
316 bool log_console = levels[module][VLF_CONSOLE] >= level;
317 bool log_syslog = levels[module][VLF_SYSLOG] >= level;
318 if (log_console || log_syslog) {
319 int save_errno = errno;
321 const char *module_name = vlog_get_module_name(module);
322 const char *level_name = vlog_get_level_name(level);
329 localtime_r(&now, &tm);
331 /* Compose log message. */
333 ds_reserve(&s, 1024);
334 ds_put_strftime(&s, "%b %d %H:%M:%S|", &tm);
336 ds_put_format(&s, "%05d|%s|%s:", ++msg_num, module_name, level_name);
337 ds_put_format_valist(&s, message, args);
341 fprintf(stderr, "%s\n", s.string);
345 static const int syslog_levels[VLL_N_LEVELS] = {
346 [VLL_EMER] = LOG_ALERT,
348 [VLL_WARN] = LOG_WARNING,
349 [VLL_DBG] = LOG_DEBUG,
351 char *save_ptr = NULL;
354 for (line = strtok_r(&s.string[time_len], "\n", &save_ptr); line;
355 line = strtok_r(NULL, "\n", &save_ptr)) {
356 syslog(syslog_levels[level], "%s", line);
366 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
370 va_start(args, message);
371 vlog_valist(module, level, message, args);
376 vlog_rate_limit(enum vlog_module module, enum vlog_level level,
377 struct vlog_rate_limit *rl, const char *message, ...)
381 if (!vlog_is_enabled(module, level)) {
385 if (rl->tokens < VLOG_MSG_TOKENS) {
386 time_t now = time_now();
387 if (rl->last_fill > now) {
388 /* Last filled in the future? Time must have gone backward, or
389 * 'rl' has not been used before. */
390 rl->tokens = rl->burst;
391 } else if (rl->last_fill < now) {
392 unsigned int add = sat_mul(rl->rate, now - rl->last_fill);
393 unsigned int tokens = sat_add(rl->tokens, add);
394 rl->tokens = MIN(tokens, rl->burst);
397 if (rl->tokens < VLOG_MSG_TOKENS) {
398 if (!rl->n_dropped) {
399 rl->first_dropped = now;
405 rl->tokens -= VLOG_MSG_TOKENS;
407 va_start(args, message);
408 vlog_valist(module, level, message, args);
413 "Dropped %u messages in last %u seconds due to excessive rate",
414 rl->n_dropped, (unsigned int) (time_now() - rl->first_dropped));