[ovs-dev] [vlog v2 4/7] vlog: Make vlog initialize itself when necessary.

Ben Pfaff blp at nicira.com
Fri Jul 16 19:00:43 UTC 2010


It's more convenient if clients don't have to initialize modules
explicitly.

The most important part of this change is to initialize the default
log levels statically.  Previously, by initializing log levels only
from vlog_init(), all the log levels appeared to be VLL_EMER (0) if
vlog_init() was accidentally not called at all.  This was not intended
behavior, so this commit fixes it.

This commit also fixes up a few test programs whose tests accidentally
depended on this behavior, by making them explicitly turn off log
messages that were implicitly turned off before.
---
 lib/vlog.c             |   28 ++++++++++++++++++++++++----
 tests/test-lockfile.c  |    2 ++
 tests/test-reconnect.c |    3 +++
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/lib/vlog.c b/lib/vlog.c
index e086e79..a77a6e5 100644
--- a/lib/vlog.c
+++ b/lib/vlog.c
@@ -69,11 +69,19 @@ static struct facility facilities[VLF_N_FACILITIES] = {
 };
 
 /* Current log levels. */
-static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
+static int levels[VLM_N_MODULES][VLF_N_FACILITIES] = {
+#define VLOG_MODULE(NAME) { VLL_INFO, VLL_INFO, VLL_INFO },
+#include "vlog-modules.def"
+#undef VLOG_MODULE
+};
 
 /* For fast checking whether we're logging anything for a given module and
  * level.*/
-enum vlog_level min_vlog_levels[VLM_N_MODULES];
+enum vlog_level min_vlog_levels[VLM_N_MODULES] = {
+#define VLOG_MODULE(NAME) VLL_INFO,
+#include "vlog-modules.def"
+#undef VLOG_MODULE
+};
 
 /* Time at which vlog was initialized, in milliseconds. */
 static long long int boot_time;
@@ -82,6 +90,9 @@ static long long int boot_time;
 static char *log_file_name;
 static FILE *log_file;
 
+/* vlog initialized? */
+static bool vlog_inited;
+
 static void format_log_message(enum vlog_module, enum vlog_level,
                                enum vlog_facility, unsigned int msg_num,
                                const char *message, va_list, struct ds *)
@@ -424,8 +435,12 @@ vlog_init(void)
 {
     time_t now;
 
+    if (vlog_inited) {
+        return;
+    }
+    vlog_inited = true;
+
     openlog(program_name, LOG_NDELAY, LOG_DAEMON);
-    vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_INFO);
 
     boot_time = time_msec();
     now = time_wall();
@@ -447,7 +462,10 @@ vlog_init(void)
 void
 vlog_exit(void) 
 {
-    closelog(); 
+    if (vlog_inited) {
+        closelog();
+        vlog_inited = false;
+    }
 }
 
 /* Print the current logging level for each module. */
@@ -602,6 +620,8 @@ vlog_valist(enum vlog_module module, enum vlog_level level,
         static unsigned int msg_num;
         struct ds s;
 
+        vlog_init();
+
         ds_init(&s);
         ds_reserve(&s, 1024);
         msg_num++;
diff --git a/tests/test-lockfile.c b/tests/test-lockfile.c
index 81a9e9f..f18e24e 100644
--- a/tests/test-lockfile.c
+++ b/tests/test-lockfile.c
@@ -26,6 +26,7 @@
 #include "process.h"
 #include "timeval.h"
 #include "util.h"
+#include "vlog.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -240,6 +241,7 @@ main(int argc, char *argv[])
     size_t i;
 
     set_program_name(argv[0]);
+    vlog_set_levels(VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
 
     if (argc != 2) {
         ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
diff --git a/tests/test-reconnect.c b/tests/test-reconnect.c
index 2d0d441..2bb59d5 100644
--- a/tests/test-reconnect.c
+++ b/tests/test-reconnect.c
@@ -27,6 +27,7 @@
 #include "compiler.h"
 #include "svec.h"
 #include "util.h"
+#include "vlog.h"
 
 static struct reconnect *reconnect;
 static int now;
@@ -44,6 +45,8 @@ main(void)
     int old_time;
     char line[128];
 
+    vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_EMER);
+
     now = 1000;
     reconnect = reconnect_create(now);
     reconnect_set_name(reconnect, "remote");
-- 
1.7.1





More information about the dev mailing list