From 7014135d8b0d96abdd4f4c9ba9b48a9b847e7398 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 22 Oct 2008 13:50:03 -0700 Subject: [PATCH] vlog: Avoid calling any function if nothing will be logged. --- include/vlog.h | 17 +++++++++++++---- lib/vlog.c | 21 +++++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/vlog.h b/include/vlog.h index f96ce1a23..6530de6a0 100644 --- a/include/vlog.h +++ b/include/vlog.h @@ -138,10 +138,10 @@ void vlog_rate_limit(enum vlog_module, enum vlog_level, * #define THIS_MODULE VLM_netlink * Guaranteed to preserve errno. */ -#define VLOG_EMER(...) vlog(THIS_MODULE, VLL_EMER, __VA_ARGS__) -#define VLOG_ERR(...) vlog(THIS_MODULE, VLL_ERR, __VA_ARGS__) -#define VLOG_WARN(...) vlog(THIS_MODULE, VLL_WARN, __VA_ARGS__) -#define VLOG_DBG(...) vlog(THIS_MODULE, VLL_DBG, __VA_ARGS__) +#define VLOG_EMER(...) VLOG(VLL_EMER, __VA_ARGS__) +#define VLOG_ERR(...) VLOG(VLL_ERR, __VA_ARGS__) +#define VLOG_WARN(...) VLOG(VLL_WARN, __VA_ARGS__) +#define VLOG_DBG(...) VLOG(VLL_DBG, __VA_ARGS__) /* More convenience macros, for testing whether a given level is enabled in * THIS_MODULE. When constructing a log message is expensive, this enables it @@ -164,4 +164,13 @@ void vlog_rate_limit(enum vlog_module, enum vlog_level, #define VLOG_DBG_RL(RL, ...) \ vlog_rate_limit(THIS_MODULE, VLL_DBG, RL, __VA_ARGS__) +/* Implementation details. */ +#define VLOG(LEVEL, ...) \ + do { \ + if (min_vlog_levels[THIS_MODULE] >= LEVEL) { \ + vlog(THIS_MODULE, LEVEL, __VA_ARGS__); \ + } \ + } while (0) +extern enum vlog_level min_vlog_levels[VLM_N_MODULES]; + #endif /* vlog.h */ diff --git a/lib/vlog.c b/lib/vlog.c index 057d6d58c..756433ca3 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -86,6 +86,10 @@ static struct facility facilities[VLF_N_FACILITIES] = { /* Current log levels. */ static int levels[VLM_N_MODULES][VLF_N_FACILITIES]; +/* For fast checking whether we're logging anything for a given module and + * level.*/ +enum vlog_level min_vlog_levels[VLM_N_MODULES]; + /* Time at which vlog was initialized, in milliseconds. */ static long long int boot_time; @@ -168,6 +172,18 @@ vlog_get_level(enum vlog_module module, enum vlog_facility facility) return levels[module][facility]; } +static void +update_min_level(enum vlog_module module) +{ + enum vlog_level min_level = VLL_EMER; + enum vlog_facility facility; + + for (facility = 0; facility < VLF_N_FACILITIES; facility++) { + min_level = MAX(min_level, levels[module][facility]); + } + min_vlog_levels[module] = min_level; +} + static void set_facility_level(enum vlog_facility facility, enum vlog_module module, enum vlog_level level) @@ -178,9 +194,11 @@ set_facility_level(enum vlog_facility facility, enum vlog_module module, if (module == VLM_ANY_MODULE) { for (module = 0; module < VLM_N_MODULES; module++) { levels[module][facility] = level; + update_min_level(module); } } else { levels[module][facility] = level; + update_min_level(module); } } @@ -359,8 +377,7 @@ vlog_get_levels(void) bool vlog_is_enabled(enum vlog_module module, enum vlog_level level) { - return (levels[module][VLF_CONSOLE] >= level - || levels[module][VLF_SYSLOG] >= level); + return min_vlog_levels[module] >= level; } static const char * -- 2.45.2