virt-manager-tui: Log to ~/.virt-manager, not /var/log

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
This commit is contained in:
Cole Robinson 2011-04-15 18:47:23 -04:00
parent 0445be92c2
commit ebb228b76b
4 changed files with 93 additions and 72 deletions

View File

@ -28,7 +28,7 @@ import sys
import traceback import traceback
# These are substituted into code based on --prefix given to configure # These are substituted into code based on --prefix given to configure
appname = "::PACKAGE::" appname = "::PACKAGE::-tui"
appversion = "::VERSION::" appversion = "::VERSION::"
gettext_app = "virt-manager" gettext_app = "virt-manager"
gettext_dir = "::GETTEXTDIR::" gettext_dir = "::GETTEXTDIR::"
@ -112,7 +112,10 @@ def main():
setup_i18n() setup_i18n()
setup_pypath() setup_pypath()
from virtManager import cli
(options, ignore) = parse_commandline() (options, ignore) = parse_commandline()
cli.setup_logging(appname, options.debug)
# Make sure we have a sufficiently new virtinst version, since we are # Make sure we have a sufficiently new virtinst version, since we are
# very closely tied to the lib # very closely tied to the lib

View File

@ -1,4 +1,3 @@
# -*- python -*-
# #
# Copyright (C) 2006 Red Hat, Inc. # Copyright (C) 2006 Red Hat, Inc.
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com> # Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
@ -23,12 +22,9 @@ import os
import os.path import os.path
import sys import sys
import libvirt import logging
import locale import locale
import gettext import gettext
import logging
import logging.handlers
import traceback import traceback
import signal import signal
from optparse import OptionParser, OptionValueError from optparse import OptionParser, OptionValueError
@ -133,64 +129,6 @@ def drop_stdio():
os.dup2(0, 1) os.dup2(0, 1)
os.dup2(0, 2) 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(): def parse_commandline():
optParser = OptionParser(version=appversion, optParser = OptionParser(version=appversion,
usage="virt-manager [options]") usage="virt-manager [options]")
@ -323,6 +261,8 @@ def main():
setup_i18n() setup_i18n()
setup_pypath() setup_pypath()
from virtManager import cli
# Need to do this before GTK strips args like --sync # Need to do this before GTK strips args like --sync
gtk_error = None gtk_error = None
origargs = " ".join(sys.argv[:]) origargs = " ".join(sys.argv[:])
@ -361,7 +301,7 @@ def main():
raise RuntimeError(_("Unable to initialize GTK: %s") % gtk_error) raise RuntimeError(_("Unable to initialize GTK: %s") % gtk_error)
raise gtk_error raise gtk_error
setup_logging(appname, options.debug) cli.setup_logging(appname, options.debug)
global logging_setup global logging_setup
logging_setup = True logging_setup = True

85
src/virtManager/cli.py Normal file
View File

@ -0,0 +1,85 @@
#
# Copyright (C) 2011 Red Hat, Inc.
# Copyright (C) 2011 Cole Robinson <crobinso@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

View File

@ -16,15 +16,8 @@
# MA 02110-1301, USA. A copy of the GNU General Public License is # MA 02110-1301, USA. A copy of the GNU General Public License is
# also available at http://www.gnu.org/copyleft/gpl.html. # also available at http://www.gnu.org/copyleft/gpl.html.
import logging
import re 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): def string_is_not_blank(value):
if len(value) > 0: return True if len(value) > 0: return True
return False return False