[virt-tools-list] [RFC PATCH 02/10] virt-manager-tui: Log to ~/.virt-manager, not /var/log

Cole Robinson crobinso at redhat.com
Mon Apr 18 18:06:44 UTC 2011


Add a new file virtManager/cli.py to share code between virt-manager
and virt-manager-tui. Move virt-manager.py setup_logging there

Have virt-manager-tui log to ~/.virt-manager/virt-manager-tui.log, rather
than the hardcoded path in /var/log that requires root perms

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 src/virt-manager-tui.py.in  |    5 ++-
 src/virt-manager.py.in      |   68 ++--------------------------------
 src/virtManager/cli.py      |   85 +++++++++++++++++++++++++++++++++++++++++++
 src/virtManagerTui/utils.py |    7 ----
 4 files changed, 93 insertions(+), 72 deletions(-)
 create mode 100644 src/virtManager/cli.py

diff --git a/src/virt-manager-tui.py.in b/src/virt-manager-tui.py.in
index 9e1a283..c76eb9f 100644
--- a/src/virt-manager-tui.py.in
+++ b/src/virt-manager-tui.py.in
@@ -28,7 +28,7 @@ import sys
 import traceback
 
 # These are substituted into code based on --prefix given to configure
-appname = "::PACKAGE::"
+appname = "::PACKAGE::-tui"
 appversion = "::VERSION::"
 gettext_app = "virt-manager"
 gettext_dir = "::GETTEXTDIR::"
@@ -112,7 +112,10 @@ def main():
     setup_i18n()
     setup_pypath()
 
+    from virtManager import cli
+
     (options, ignore) = parse_commandline()
+    cli.setup_logging(appname, options.debug)
 
     # Make sure we have a sufficiently new virtinst version, since we are
     # very closely tied to the lib
diff --git a/src/virt-manager.py.in b/src/virt-manager.py.in
index 67b151f..75a2cb5 100755
--- a/src/virt-manager.py.in
+++ b/src/virt-manager.py.in
@@ -1,4 +1,3 @@
-# -*- python -*-
 #
 # Copyright (C) 2006 Red Hat, Inc.
 # Copyright (C) 2006 Daniel P. Berrange <berrange at redhat.com>
@@ -23,12 +22,9 @@ import os
 import os.path
 import sys
 
-import libvirt
-
+import logging
 import locale
 import gettext
-import logging
-import logging.handlers
 import traceback
 import signal
 from optparse import OptionParser, OptionValueError
@@ -133,64 +129,6 @@ def drop_stdio():
     os.dup2(0, 1)
     os.dup2(0, 2)
 
-def setup_logging(appname, debug_stdout):
-    # Configure python logging to capture all logs we generate
-    # to $HOME/.virt-manager/${app}.log This file has
-    # proved invaluable for debugging
-    MAX_LOGSIZE   = 1024 * 1024  # 1MB
-    ROTATE_NUM    = 5
-    DIR_NAME      = ".virt-manager"
-    FILE_NAME     = "%s.log" % appname
-    FILE_MODE     = 'ae'
-    FILE_FORMAT   = ("[%(asctime)s virt-manager %(process)d] "
-                     "%(levelname)s (%(module)s:%(lineno)d) %(message)s")
-    DATEFMT       = "%a, %d %b %Y %H:%M:%S"
-
-    # set up logging
-    vm_dir = os.path.expanduser("~/%s" % DIR_NAME)
-    if not os.access(vm_dir, os.W_OK):
-        if os.path.exists(vm_dir):
-            raise RuntimeError("No write access to %s" % vm_dir)
-
-        try:
-            os.mkdir(vm_dir, 0751)
-        except IOError, e:
-            raise RuntimeError("Could not create directory %s: %s" %
-                               (vm_dir, e))
-
-    filename = "%s/%s" % (vm_dir, FILE_NAME)
-    rootLogger = logging.getLogger()
-    rootLogger.setLevel(logging.DEBUG)
-    fileHandler = logging.handlers.RotatingFileHandler(filename,
-                                    FILE_MODE, MAX_LOGSIZE, ROTATE_NUM)
-    fileHandler.setFormatter(logging.Formatter(FILE_FORMAT, DATEFMT))
-    rootLogger.addHandler(fileHandler)
-
-    if debug_stdout:
-        streamHandler = logging.StreamHandler(sys.stderr)
-        streamHandler.setLevel(logging.DEBUG)
-        streamHandler.setFormatter(logging.Formatter(
-                        "%(asctime)s (%(module)s:%(lineno)d): %(message)s"))
-        rootLogger.addHandler(streamHandler)
-
-    logging.info("%s startup" % appname)
-
-    # Register libvirt handler
-    def libvirt_callback(ctx_ignore, err):
-        if err[3] != libvirt.VIR_ERR_ERROR:
-            # Don't log libvirt errors: global error handler will do that
-            logging.warn("Non-error from libvirt: '%s'" % err[2])
-    libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
-
-    # Log uncaught exceptions
-    def exception_log(typ, val, tb):
-        if not traceback:
-            return
-        s = traceback.format_exception(typ, val, tb)
-        logging.exception("".join(s))
-        sys.__excepthook__(typ, val, tb)
-    sys.excepthook = exception_log
-
 def parse_commandline():
     optParser = OptionParser(version=appversion,
                              usage="virt-manager [options]")
@@ -323,6 +261,8 @@ def main():
     setup_i18n()
     setup_pypath()
 
+    from virtManager import cli
+
     # Need to do this before GTK strips args like --sync
     gtk_error = None
     origargs = " ".join(sys.argv[:])
@@ -361,7 +301,7 @@ def main():
             raise RuntimeError(_("Unable to initialize GTK: %s") % gtk_error)
         raise gtk_error
 
-    setup_logging(appname, options.debug)
+    cli.setup_logging(appname, options.debug)
     global logging_setup
     logging_setup = True
 
diff --git a/src/virtManager/cli.py b/src/virtManager/cli.py
new file mode 100644
index 0000000..4b59286
--- /dev/null
+++ b/src/virtManager/cli.py
@@ -0,0 +1,85 @@
+#
+# Copyright (C) 2011 Red Hat, Inc.
+# Copyright (C) 2011 Cole Robinson <crobinso at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301 USA.
+#
+
+import os
+import sys
+import logging
+import logging.handlers
+import traceback
+
+import libvirt
+
+def setup_logging(appname, debug_stdout):
+    # Configure python logging to capture all logs we generate
+    # to $HOME/.virt-manager/${app}.log This file has
+    # proved invaluable for debugging
+    MAX_LOGSIZE   = 1024 * 1024  # 1MB
+    ROTATE_NUM    = 5
+    DIR_NAME      = ".virt-manager"
+    FILE_NAME     = "%s.log" % appname
+    FILE_MODE     = 'ae'
+    FILE_FORMAT   = ("[%(asctime)s virt-manager %(process)d] "
+                     "%(levelname)s (%(module)s:%(lineno)d) %(message)s")
+    DATEFMT       = "%a, %d %b %Y %H:%M:%S"
+
+    # set up logging
+    vm_dir = os.path.expanduser("~/%s" % DIR_NAME)
+    if not os.access(vm_dir, os.W_OK):
+        if os.path.exists(vm_dir):
+            raise RuntimeError("No write access to %s" % vm_dir)
+
+        try:
+            os.mkdir(vm_dir, 0751)
+        except IOError, e:
+            raise RuntimeError("Could not create directory %s: %s" %
+                               (vm_dir, e))
+
+    filename = "%s/%s" % (vm_dir, FILE_NAME)
+    rootLogger = logging.getLogger()
+    rootLogger.setLevel(logging.DEBUG)
+    fileHandler = logging.handlers.RotatingFileHandler(filename,
+                                    FILE_MODE, MAX_LOGSIZE, ROTATE_NUM)
+    fileHandler.setFormatter(logging.Formatter(FILE_FORMAT, DATEFMT))
+    rootLogger.addHandler(fileHandler)
+
+    if debug_stdout:
+        streamHandler = logging.StreamHandler(sys.stderr)
+        streamHandler.setLevel(logging.DEBUG)
+        streamHandler.setFormatter(logging.Formatter(
+                        "%(asctime)s (%(module)s:%(lineno)d): %(message)s"))
+        rootLogger.addHandler(streamHandler)
+
+    logging.info("%s startup" % appname)
+
+    # Register libvirt handler
+    def libvirt_callback(ctx_ignore, err):
+        if err[3] != libvirt.VIR_ERR_ERROR:
+            # Don't log libvirt errors: global error handler will do that
+            logging.warn("Non-error from libvirt: '%s'" % err[2])
+    libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
+
+    # Log uncaught exceptions
+    def exception_log(typ, val, tb):
+        if not traceback:
+            return
+        s = traceback.format_exception(typ, val, tb)
+        logging.exception("".join(s))
+        sys.__excepthook__(typ, val, tb)
+    sys.excepthook = exception_log
diff --git a/src/virtManagerTui/utils.py b/src/virtManagerTui/utils.py
index d4e3f52..1401c1f 100644
--- a/src/virtManagerTui/utils.py
+++ b/src/virtManagerTui/utils.py
@@ -16,15 +16,8 @@
 # MA  02110-1301, USA.  A copy of the GNU General Public License is
 # also available at http://www.gnu.org/copyleft/gpl.html.
 
-import logging
 import re
 
-logging.basicConfig(level=logging.DEBUG,
-                    format='%(asctime)s %(levelname)-8s %(message)s',
-                    datefmt='%a, %d %b %Y %H:%M:%S',
-                    filename='/var/log/ovirt-nodeadmin.log',
-                    filemode='w')
-
 def string_is_not_blank(value):
     if len(value) > 0: return True
     return False
-- 
1.7.4




More information about the virt-tools-list mailing list