From: Ben Pfaff Date: Thu, 5 Mar 2009 01:30:20 +0000 (-0800) Subject: vlog: Add functions for testing a rate-limit without emitting a message. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=821863053ea9f0fa5d99d227ecaa918766e86550;p=openvswitch vlog: Add functions for testing a rate-limit without emitting a message. This allows clients to avoid doing work to construct a rate-limited message until they know that the message will actually be displayed. --- diff --git a/lib/vlog.c b/lib/vlog.c index e23d0173..08f1e4eb 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -622,14 +622,12 @@ vlog(enum vlog_module module, enum vlog_level level, const char *message, ...) va_end(args); } -void -vlog_rate_limit(enum vlog_module module, enum vlog_level level, - struct vlog_rate_limit *rl, const char *message, ...) +bool +vlog_should_drop(enum vlog_module module, enum vlog_level level, + struct vlog_rate_limit *rl) { - va_list args; - if (!vlog_is_enabled(module, level)) { - return; + return true; } if (rl->tokens < VLOG_MSG_TOKENS) { @@ -649,21 +647,31 @@ vlog_rate_limit(enum vlog_module module, enum vlog_level level, rl->first_dropped = now; } rl->n_dropped++; - return; + return true; } } rl->tokens -= VLOG_MSG_TOKENS; - va_start(args, message); - vlog_valist(module, level, message, args); - va_end(args); - if (rl->n_dropped) { vlog(module, level, "Dropped %u messages in last %u seconds due to excessive rate", rl->n_dropped, (unsigned int) (time_now() - rl->first_dropped)); rl->n_dropped = 0; } + return false; +} + +void +vlog_rate_limit(enum vlog_module module, enum vlog_level level, + struct vlog_rate_limit *rl, const char *message, ...) +{ + if (!vlog_should_drop(module, level, rl)) { + va_list args; + + va_start(args, message); + vlog_valist(module, level, message, args); + va_end(args); + } } void diff --git a/lib/vlog.h b/lib/vlog.h index 8785851c..ca66309b 100644 --- a/lib/vlog.h +++ b/lib/vlog.h @@ -121,6 +121,8 @@ void vlog_set_levels(enum vlog_module, enum vlog_facility, enum vlog_level); char *vlog_set_levels_from_string(const char *); char *vlog_get_levels(void); bool vlog_is_enabled(enum vlog_module, enum vlog_level); +bool vlog_should_drop(enum vlog_module, enum vlog_level, + struct vlog_rate_limit *); void vlog_set_verbosity(const char *arg); /* Configuring log facilities. */ @@ -161,10 +163,7 @@ void vlog_rate_limit(enum vlog_module, enum vlog_level, #define VLOG_IS_INFO_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_INFO) #define VLOG_IS_DBG_ENABLED() vlog_is_enabled(THIS_MODULE, VLL_DBG) -/* Convenience macros. To use these, define THIS_MODULE as a macro that - * expands to the module used by the current source file, e.g. - * #include "vlog.h" - * #define THIS_MODULE VLM_netlink +/* Convenience macros for rate-limiting. * Guaranteed to preserve errno. */ #define VLOG_ERR_RL(RL, ...) VLOG_RL(RL, VLL_ERR, __VA_ARGS__) @@ -172,6 +171,11 @@ void vlog_rate_limit(enum vlog_module, enum vlog_level, #define VLOG_INFO_RL(RL, ...) VLOG_RL(RL, VLL_INFO, __VA_ARGS__) #define VLOG_DBG_RL(RL, ...) VLOG_RL(RL, VLL_DBG, __VA_ARGS__) +#define VLOG_DROP_ERR(RL) vlog_should_drop(THIS_MODULE, VLL_ERR, RL) +#define VLOG_DROP_WARN(RL) vlog_should_drop(THIS_MODULE, VLL_WARN, RL) +#define VLOG_DROP_INFO(RL) vlog_should_drop(THIS_MODULE, VLL_INFO, RL) +#define VLOG_DROP_DBG(RL) vlog_should_drop(THIS_MODULE, VLL_DBG, RL) + /* Command line processing. */ #define VLOG_OPTION_ENUMS OPT_LOG_FILE #define VLOG_LONG_OPTIONS \