xenserver: Add --no-syslog feature to interface-reconfigure.
authorBen Pfaff <blp@nicira.com>
Tue, 23 Feb 2010 00:26:50 +0000 (16:26 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 23 Feb 2010 17:55:44 +0000 (09:55 -0800)
This makes it easier to do unit tests (some of which will be added in an
upcoming commit) by allowing messages to be read from stderr instead of
having to somehow intercept syslog calls.

Signed-off-by: Ben Pfaff <blp@nicira.com>
xenserver/opt_xensource_libexec_InterfaceReconfigure.py
xenserver/opt_xensource_libexec_interface-reconfigure

index 576c36985ea90486803297c0ad810e28d60a32bf..c7a231a8b5c19559d81e59795658a533176caa49 100644 (file)
@@ -10,6 +10,7 @@
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Lesser General Public License for more details.
 #
+import sys
 import syslog
 import os
 
@@ -25,12 +26,25 @@ def set_root_prefix(prefix):
     global the_root_prefix
     the_root_prefix = prefix
 
+log_destination = "syslog"
+def get_log_destination():
+    """Returns the current log destination.
+    'syslog' means "log to syslog".
+    'stderr' means "log to stderr"."""
+    return log_destination
+def set_log_destination(dest):
+    global log_destination
+    log_destination = dest
+
 #
 # Logging.
 #
 
 def log(s):
-    syslog.syslog(s)
+    if get_log_destination() == 'syslog':
+        syslog.syslog(s)
+    else:
+        print >>sys.stderr, s
 
 #
 # Exceptions.
index 153f509ed4abd04f588a43b8e160445b807fa69f..f16f681af97f0dee182494ca1edacf8a39a2be56 100755 (executable)
@@ -34,6 +34,7 @@
     --pif-uuid          The UUID of a PIF.
     --force             An interface name.
     --root-prefix=DIR   Use DIR as alternate root directory (for testing).
+    --no-syslog         Write log messages to stderr instead of system log.
 """
 
 # Notes:
@@ -582,6 +583,7 @@ def main(argv=None):
                         "management",
                         "mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
                         "root-prefix=",
+                        "no-syslog",
                         "help" ]
             arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
         except getopt.GetoptError, msg:
@@ -604,12 +606,15 @@ def main(argv=None):
                 force_rewrite_config[o[2:]] = a
             elif o == "--root-prefix":
                 set_root_prefix(a)
+            elif o == "--no-syslog":
+                set_log_destination("stderr")
             elif o == "-h" or o == "--help":
                 print __doc__ % {'command-name': os.path.basename(argv[0])}
                 return 0
 
-        syslog.openlog(os.path.basename(argv[0]))
-        log("Called as " + str.join(" ", argv))
+        if get_log_destination() == "syslog":
+            syslog.openlog(os.path.basename(argv[0]))
+            log("Called as " + str.join(" ", argv))
 
         if len(args) < 1:
             raise Usage("Required option <action> not present")