vlog: Make vlog initialize itself when necessary.
authorBen Pfaff <blp@nicira.com>
Fri, 16 Jul 2010 18:23:31 +0000 (11:23 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 21 Jul 2010 22:47:09 +0000 (15:47 -0700)
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
tests/test-lockfile.c
tests/test-reconnect.c

index e086e798438de9c2564c2f817a673b87b6f4d53b..a77a6e5546d83730c077f0a3189e47d732120580 100644 (file)
@@ -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++;
index 81a9e9f16cfe3120a2fddb4120ba0deef6a8a4a3..f18e24e8b61cbb0b22b1e664f060856d661fdaf6 100644 (file)
@@ -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",
index 2d0d44148d0afeb5e82327cd5f0e5858d2fad79c..2bb59d5fb2474d947aca6938635eef8534099cd0 100644 (file)
@@ -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");