mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-03-09 08:58:27 +03:00
Merge virt-* logging setup
This commit is contained in:
parent
255ba11079
commit
35758d0fb7
19
virt-manager
19
virt-manager
@ -27,29 +27,21 @@ import signal
|
|||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=E0611
|
# pylint: disable=E0611
|
||||||
from gi.repository import GObject
|
from gi.repository import GObject
|
||||||
from gi.repository import LibvirtGLib
|
from gi.repository import LibvirtGLib
|
||||||
# pylint: enable=E0611
|
# pylint: enable=E0611
|
||||||
|
|
||||||
|
from virtinst import cli as virtinstcli
|
||||||
from virtcli import cliutils, cliconfig
|
from virtcli import cliutils, cliconfig
|
||||||
|
|
||||||
|
|
||||||
GObject.threads_init()
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Make sure we have a default '_' implementation, in case something
|
|
||||||
# fails before gettext is set up
|
|
||||||
__builtins__._ = lambda msg: msg
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
logging_setup = False
|
logging_setup = False
|
||||||
|
|
||||||
|
|
||||||
|
GObject.threads_init()
|
||||||
|
|
||||||
|
|
||||||
def _show_startup_error(msg, details):
|
def _show_startup_error(msg, details):
|
||||||
if logging_setup:
|
if logging_setup:
|
||||||
logging.exception("Error starting virt-manager")
|
logging.exception("Error starting virt-manager")
|
||||||
@ -193,12 +185,11 @@ def main():
|
|||||||
cliutils.setup_i18n()
|
cliutils.setup_i18n()
|
||||||
(options, leftovers) = parse_commandline()
|
(options, leftovers) = parse_commandline()
|
||||||
|
|
||||||
cliutils.setup_logging("virt-manager", options.debug)
|
virtinstcli.setupLogging("virt-manager", options.debug, False, False)
|
||||||
global logging_setup
|
global logging_setup
|
||||||
logging_setup = True
|
logging_setup = True
|
||||||
|
|
||||||
import virtManager
|
import virtManager
|
||||||
logging.debug("Launched as: %s", sys.argv)
|
|
||||||
logging.debug("virt-manager version: %s", cliconfig.__version__)
|
logging.debug("virt-manager version: %s", cliconfig.__version__)
|
||||||
logging.debug("virtManager import: %s", str(virtManager))
|
logging.debug("virtManager import: %s", str(virtManager))
|
||||||
|
|
||||||
|
@ -20,75 +20,10 @@
|
|||||||
|
|
||||||
import gettext
|
import gettext
|
||||||
import locale
|
import locale
|
||||||
import logging
|
|
||||||
import logging.handlers
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
import libvirt
|
|
||||||
|
|
||||||
from virtcli import cliconfig
|
from virtcli import cliconfig
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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):
|
|
||||||
s = traceback.format_exception(typ, val, tb)
|
|
||||||
logging.debug("Uncaught exception:\n" + "".join(s))
|
|
||||||
sys.__excepthook__(typ, val, tb)
|
|
||||||
sys.excepthook = exception_log
|
|
||||||
|
|
||||||
|
|
||||||
def setup_i18n():
|
def setup_i18n():
|
||||||
try:
|
try:
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
|
@ -29,6 +29,7 @@ import re
|
|||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import traceback
|
||||||
|
|
||||||
import libvirt
|
import libvirt
|
||||||
|
|
||||||
@ -156,11 +157,15 @@ def earlyLogging():
|
|||||||
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
|
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
|
||||||
|
|
||||||
|
|
||||||
def setupLogging(appname, debug=False, do_quiet=False):
|
def setupLogging(appname, debug_stdout, do_quiet, cli_app=True):
|
||||||
global quiet
|
global quiet
|
||||||
quiet = do_quiet
|
quiet = do_quiet
|
||||||
|
|
||||||
vi_dir = os.path.expanduser("~/.virtinst")
|
dirname = "~/.virtinst"
|
||||||
|
if appname == "virt-manager":
|
||||||
|
dirname = "~/.virt-manager"
|
||||||
|
|
||||||
|
vi_dir = os.path.expanduser(dirname)
|
||||||
if not os.access(vi_dir, os.W_OK):
|
if not os.access(vi_dir, os.W_OK):
|
||||||
if os.path.exists(vi_dir):
|
if os.path.exists(vi_dir):
|
||||||
raise RuntimeError("No write access to directory %s" % vi_dir)
|
raise RuntimeError("No write access to directory %s" % vi_dir)
|
||||||
@ -187,16 +192,17 @@ def setupLogging(appname, debug=False, do_quiet=False):
|
|||||||
rootLogger.setLevel(logging.DEBUG)
|
rootLogger.setLevel(logging.DEBUG)
|
||||||
fileHandler = logging.handlers.RotatingFileHandler(filename, "ae",
|
fileHandler = logging.handlers.RotatingFileHandler(filename, "ae",
|
||||||
1024 * 1024, 5)
|
1024 * 1024, 5)
|
||||||
|
|
||||||
fileHandler.setFormatter(logging.Formatter(fileFormat,
|
fileHandler.setFormatter(logging.Formatter(fileFormat,
|
||||||
dateFormat))
|
dateFormat))
|
||||||
rootLogger.addHandler(fileHandler)
|
rootLogger.addHandler(fileHandler)
|
||||||
|
|
||||||
streamHandler = VirtStreamHandler(sys.stderr)
|
streamHandler = VirtStreamHandler(sys.stderr)
|
||||||
if debug:
|
if debug_stdout:
|
||||||
streamHandler.setLevel(logging.DEBUG)
|
streamHandler.setLevel(logging.DEBUG)
|
||||||
streamHandler.setFormatter(logging.Formatter(fileFormat,
|
streamHandler.setFormatter(logging.Formatter(fileFormat,
|
||||||
dateFormat))
|
dateFormat))
|
||||||
|
elif not cli_app:
|
||||||
|
streamHandler = None
|
||||||
else:
|
else:
|
||||||
if quiet:
|
if quiet:
|
||||||
level = logging.ERROR
|
level = logging.ERROR
|
||||||
@ -204,7 +210,9 @@ def setupLogging(appname, debug=False, do_quiet=False):
|
|||||||
level = logging.WARN
|
level = logging.WARN
|
||||||
streamHandler.setLevel(level)
|
streamHandler.setLevel(level)
|
||||||
streamHandler.setFormatter(logging.Formatter(streamErrorFormat))
|
streamHandler.setFormatter(logging.Formatter(streamErrorFormat))
|
||||||
rootLogger.addHandler(streamHandler)
|
|
||||||
|
if streamHandler:
|
||||||
|
rootLogger.addHandler(streamHandler)
|
||||||
|
|
||||||
# Register libvirt handler
|
# Register libvirt handler
|
||||||
def libvirt_callback(ignore, err):
|
def libvirt_callback(ignore, err):
|
||||||
@ -213,16 +221,15 @@ def setupLogging(appname, debug=False, do_quiet=False):
|
|||||||
logging.warn("Non-error from libvirt: '%s'", err[2])
|
logging.warn("Non-error from libvirt: '%s'", err[2])
|
||||||
libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
|
libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
|
||||||
|
|
||||||
# Register python error handler to log exceptions
|
# Log uncaught exceptions
|
||||||
def exception_log(typ, val, tb):
|
def exception_log(typ, val, tb):
|
||||||
import traceback
|
logging.debug("Uncaught exception:\n%s",
|
||||||
s = traceback.format_exception(typ, val, tb)
|
"".join(traceback.format_exception(typ, val, tb)))
|
||||||
logging.exception("".join(s))
|
|
||||||
sys.__excepthook__(typ, val, tb)
|
sys.__excepthook__(typ, val, tb)
|
||||||
sys.excepthook = exception_log
|
sys.excepthook = exception_log
|
||||||
|
|
||||||
# Log the app command string
|
# Log the app command string
|
||||||
logging.debug("Launched with command line:\n%s", " ".join(sys.argv))
|
logging.debug("Launched with command line: %s", " ".join(sys.argv))
|
||||||
|
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
@ -397,7 +404,6 @@ def fail(msg, do_exit=True):
|
|||||||
Convenience function when failing in cli app
|
Convenience function when failing in cli app
|
||||||
"""
|
"""
|
||||||
logging.error(msg)
|
logging.error(msg)
|
||||||
import traceback
|
|
||||||
if traceback.format_exc().strip() != "None":
|
if traceback.format_exc().strip() != "None":
|
||||||
logging.debug("", exc_info=True)
|
logging.debug("", exc_info=True)
|
||||||
if do_exit:
|
if do_exit:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user