Replace numerous 'print' statements with calls to python logging. Added syntax checking of all modules

This commit is contained in:
Daniel P. Berrange 2006-09-25 18:22:27 -04:00
parent 3e0da9be93
commit 4ccdffa212
9 changed files with 52 additions and 39 deletions

View File

@ -1,5 +1,5 @@
SUBDIRS = virtManager vncViewer graphWidgets
SUBDIRS = vncViewer graphWidgets virtManager
bin_SOURCES = virt-manager.in
bin_SCRIPTS = virt-manager

View File

@ -24,6 +24,7 @@ import sys
import locale
import gettext
import logging
gettext_app = "virt-manager"
gettext_dir = "::GETTEXTDIR::"
@ -31,6 +32,20 @@ gettext_dir = "::GETTEXTDIR::"
locale.setlocale(locale.LC_ALL, '')
gettext.install(gettext_app, gettext_dir)
# set up logging
vm_dir = os.path.expanduser("~/.virt-manager")
if not os.access(vm_dir,os.W_OK):
try:
os.mkdir(vm_dir)
except IOError, e:
raise RuntimeError, "Could not create %d directory: " % vm_dir, e
# XXX should we get logging level from gconf, or command line args ?
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%a, %d %b %Y %H:%M:%S",
filename="%s/virt-manager.log" % vm_dir,
filemode='w')
import gtk
gtk.gdk.threads_init()
@ -90,6 +105,8 @@ def main():
else:
bus = None
try:
if os.getenv("DBUS_STARTER_ADDRESS") == None and os.getenv("DBUS_SESSION_BUS_ADDRESS") == None:
raise RuntimeError, "DBus session/starter bus address not defined"
bus = dbus.SessionBus()
dbusProxy = bus.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus")
@ -110,8 +127,8 @@ def main():
# yes, we exit completely now - remote service is in charge
return
except:
print str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1])
print _("Could not connection to session bus, disabling DBus service")
logging.warning("Could not connection to session bus, disabling DBus service " + \
str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1]))
if options.uri != None:
engine.show_manager(options.uri)
else:

View File

@ -3,3 +3,14 @@ pythondir = $(pkgdatadir)/virtManager
python_DATA = $(wildcard $(srcdir)/*.py)
EXTRA_DIST = $(python_DATA)
SYNTAX_CHECK_TSTAMPS = $(python_DATA:$(srcdir)/%.py=.tstamp.%.py)
check-local: $(SYNTAX_CHECK_TSTAMPS)
clean-local:
rm -f $(SYNTAX_CHECK_TSTAMPS)
.tstamp.%.py: %.py
PYTHONPATH=$(srcdir)/..:../graphWidgets/.libs python $< && touch $@

View File

@ -18,7 +18,6 @@
#
import gconf
import os
import logging
import gtk.gdk
import libvirt
@ -33,21 +32,7 @@ class vmmConfig:
self.conf = gconf.client_get_default()
self.conf.add_dir (gconf_dir,
gconf.CLIENT_PRELOAD_NONE)
# set up logging
vm_dir = "%s/.virt-manager" % os.environ['HOME']
if not os.access(vm_dir,os.W_OK):
try:
os.mkdir(vm_dir)
except IOError, e:
raise RuntimeError, "Could not create %d directory: " % vm_dir, e
# XXX should get logging level from gconf
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%a, %d %b %Y %H:%M:%S",
filename="%s/virt-manager.log" % vm_dir,
filemode='w')
logging.debug("Initialized Python logger")
self.glade_dir = glade_dir
self.icon_dir = icon_dir
# We don't create it straight away, since we don't want

View File

@ -127,7 +127,7 @@ class vmmConsole(gobject.GObject):
try:
self.vncViewer.disconnect_from_host()
except:
print "Failure when disconnecting"
logging.error("Failure when disconnecting from VNC server")
return 1
def control_vm_run(self, src):
@ -143,7 +143,7 @@ class vmmConsole(gobject.GObject):
if self.vm.get_id() == 0:
return
#print protocol + "://" + host + ":" + str(port)
logging.debug("Graphics " + protocol + "://" + host + ":" + str(port))
if protocol != "vnc":
self.activate_unavailable_page()
return
@ -152,7 +152,7 @@ class vmmConsole(gobject.GObject):
try:
self.vncViewer.connect_to_host(host, port)
except:
print _("Unable to activate console") + " " + str((sys.exc_info())[0]) + " " + str((sys.exc_info())[1])
logging.error("Unable to activate console " + str((sys.exc_info())[0]) + " " + str((sys.exc_info())[1]))
self.activate_unavailable_page()
return
@ -251,7 +251,7 @@ class vmmConsole(gobject.GObject):
if not(status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]):
self.vm.shutdown()
else:
print _("Shutdown requested, but machine is already shutting down / shutoff")
logging.warning("Shutdown requested, but machine is already shutting down / shutoff")
def control_vm_pause(self, src):
if self.ignorePause:
@ -259,18 +259,18 @@ class vmmConsole(gobject.GObject):
status = self.vm.status()
if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]:
print _("Pause/resume requested, but machine is shutdown / shutoff")
logging.warning("Pause/resume requested, but machine is shutdown / shutoff")
else:
if status in [ libvirt.VIR_DOMAIN_PAUSED ]:
if not src.get_active():
self.vm.resume()
else:
print _("Pause requested, but machine is already paused")
logging.warning("Pause requested, but machine is already paused")
else:
if src.get_active():
self.vm.suspend()
else:
print _("Resume requested, but machine is already running")
logging.warning("Resume requested, but machine is already running")
self.window.get_widget("control-pause").set_active(src.get_active())
self.window.get_widget("menu-vm-pause").set_active(src.get_active())
@ -352,7 +352,7 @@ class vmmConsole(gobject.GObject):
try:
self.try_login()
except:
print _("Couldn't open console: ") + str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1])
logging.error("Couldn't open console " + str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1]))
self.ignorePause = False
else:
self.activate_unavailable_page()

View File

@ -176,7 +176,7 @@ class vmmDetails(gobject.GObject):
if not(status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]):
self.vm.shutdown()
else:
print _("Shutdown requested, but machine is already shutting down / shutoff")
logging.warning("Shutdown requested, but machine is already shutting down / shutoff")
def control_vm_pause(self, src):
if self.ignorePause:
@ -184,18 +184,18 @@ class vmmDetails(gobject.GObject):
status = self.vm.status()
if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]:
print _("Pause/resume requested, but machine is shutdown / shutoff")
logging.warning("Pause/resume requested, but machine is shutdown / shutoff")
else:
if status in [ libvirt.VIR_DOMAIN_PAUSED ]:
if not src.get_active():
self.vm.resume()
else:
print _("Pause requested, but machine is already paused")
logging.warning("Pause requested, but machine is already paused")
else:
if src.get_active():
self.vm.suspend()
else:
print _("Resume requested, but machine is already running")
logging.warning("Resume requested, but machine is already running")
self.window.get_widget("control-pause").set_active(src.get_active())
self.window.get_widget("details-menu-pause").set_active(src.get_active())

View File

@ -390,8 +390,8 @@ class vmmDomain(gobject.GObject):
def set_memory(self, memory):
memory = int(memory)
if(memory > self.maximum_memory()):
print "XXX add proper error handling here. You may not set vm memory larger than the maximum set for the vm."
if (memory > self.maximum_memory()):
logging.warning("Requested memory " + str(memory) + " over maximum " + self.maximum_memory())
self.vm.setMemory(memory)
return self.vm.info()[2]

View File

@ -63,8 +63,8 @@ class vmmEngine:
conn = self.get_connection(uri, readOnly)
self.show_manager(uri)
except:
print _("Unable to open connection to hypervisor URI '%s'") % str(uri)
print str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1])
logging.error((("Unable to open connection to hypervisor URI '%s'") % str(uri)) + \
str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1]))
if len(self.connections.keys()) == 0:
gtk.main_quit()
@ -105,8 +105,7 @@ class vmmEngine:
except KeyboardInterrupt:
raise KeyboardInterrupt
except:
print str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1])
print _("Error refreshing connection '%s'") % uri
logging.error(("Could not refresh connection %s" % (uri)) + str(sys.exc_info()[0]) + " " + str(sys.exc_info()[1]))
return 1
def change_timer_interval(self,ignore1,ignore2,ignore3,ignore4):
@ -241,7 +240,7 @@ class vmmEngine:
libvirt.VIR_DOMAIN_SHUTOFF,
libvirt.VIR_DOMAIN_CRASHED,
libvirt.VIR_DOMAIN_PAUSED ]:
print _("Save requested, but machine is shutdown / shutoff / paused")
logging.warning("Save requested, but machine is shutdown / shutoff / paused")
else:
self.fcdialog = gtk.FileChooserDialog(_("Save Virtual Machine"),
src.window.get_widget("vmm-details"),

View File

@ -19,6 +19,7 @@
from virtManager.secret import *
import sys
import logging
haveKeyring = False
@ -26,7 +27,7 @@ try:
import gnomekeyring
haveKeyring = True
except:
print _("No support for gnome-keyring")
logging.warning("No support for gnome-keyring")
pass
class vmmKeyring:
@ -38,7 +39,7 @@ class vmmKeyring:
gnomekeyring.create_sync("default", None)
self.keyring = gnomekeyring.get_default_keyring_sync()
except:
print _("Keyring unavailable: '%s'") % (str((sys.exc_info())[0]) + " " + str((sys.exc_info())[1]))
logging.warning(("Keyring unavailable: '%s'") % (str((sys.exc_info())[0]) + " " + str((sys.exc_info())[1])))
self.keyring = None
else:
self.keyring = None