vlog: Avoid calling any function if nothing will be logged.
authorBen Pfaff <blp@nicira.com>
Wed, 22 Oct 2008 20:50:03 +0000 (13:50 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 23 Oct 2008 18:09:02 +0000 (11:09 -0700)
include/vlog.h
lib/vlog.c

index f96ce1a..6530de6 100644 (file)
@@ -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 */
index 057d6d5..756433c 100644 (file)
@@ -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 *