mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-03-08 04:58:29 +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 traceback
|
||||
|
||||
|
||||
# pylint: disable=E0611
|
||||
from gi.repository import GObject
|
||||
from gi.repository import LibvirtGLib
|
||||
# pylint: enable=E0611
|
||||
|
||||
from virtinst import cli as virtinstcli
|
||||
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
|
||||
|
||||
|
||||
GObject.threads_init()
|
||||
|
||||
|
||||
def _show_startup_error(msg, details):
|
||||
if logging_setup:
|
||||
logging.exception("Error starting virt-manager")
|
||||
@ -193,12 +185,11 @@ def main():
|
||||
cliutils.setup_i18n()
|
||||
(options, leftovers) = parse_commandline()
|
||||
|
||||
cliutils.setup_logging("virt-manager", options.debug)
|
||||
virtinstcli.setupLogging("virt-manager", options.debug, False, False)
|
||||
global logging_setup
|
||||
logging_setup = True
|
||||
|
||||
import virtManager
|
||||
logging.debug("Launched as: %s", sys.argv)
|
||||
logging.debug("virt-manager version: %s", cliconfig.__version__)
|
||||
logging.debug("virtManager import: %s", str(virtManager))
|
||||
|
||||
|
@ -20,75 +20,10 @@
|
||||
|
||||
import gettext
|
||||
import locale
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
import libvirt
|
||||
|
||||
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():
|
||||
try:
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
|
@ -29,6 +29,7 @@ import re
|
||||
import shlex
|
||||
import sys
|
||||
import tempfile
|
||||
import traceback
|
||||
|
||||
import libvirt
|
||||
|
||||
@ -156,11 +157,15 @@ def earlyLogging():
|
||||
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
|
||||
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 os.path.exists(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)
|
||||
fileHandler = logging.handlers.RotatingFileHandler(filename, "ae",
|
||||
1024 * 1024, 5)
|
||||
|
||||
fileHandler.setFormatter(logging.Formatter(fileFormat,
|
||||
dateFormat))
|
||||
rootLogger.addHandler(fileHandler)
|
||||
|
||||
streamHandler = VirtStreamHandler(sys.stderr)
|
||||
if debug:
|
||||
if debug_stdout:
|
||||
streamHandler.setLevel(logging.DEBUG)
|
||||
streamHandler.setFormatter(logging.Formatter(fileFormat,
|
||||
dateFormat))
|
||||
elif not cli_app:
|
||||
streamHandler = None
|
||||
else:
|
||||
if quiet:
|
||||
level = logging.ERROR
|
||||
@ -204,7 +210,9 @@ def setupLogging(appname, debug=False, do_quiet=False):
|
||||
level = logging.WARN
|
||||
streamHandler.setLevel(level)
|
||||
streamHandler.setFormatter(logging.Formatter(streamErrorFormat))
|
||||
rootLogger.addHandler(streamHandler)
|
||||
|
||||
if streamHandler:
|
||||
rootLogger.addHandler(streamHandler)
|
||||
|
||||
# Register libvirt handler
|
||||
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])
|
||||
libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
|
||||
|
||||
# Register python error handler to log exceptions
|
||||
# Log uncaught exceptions
|
||||
def exception_log(typ, val, tb):
|
||||
import traceback
|
||||
s = traceback.format_exception(typ, val, tb)
|
||||
logging.exception("".join(s))
|
||||
logging.debug("Uncaught exception:\n%s",
|
||||
"".join(traceback.format_exception(typ, val, tb)))
|
||||
sys.__excepthook__(typ, val, tb)
|
||||
sys.excepthook = exception_log
|
||||
|
||||
# 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
|
||||
"""
|
||||
logging.error(msg)
|
||||
import traceback
|
||||
if traceback.format_exc().strip() != "None":
|
||||
logging.debug("", exc_info=True)
|
||||
if do_exit:
|
||||
|
Loading…
x
Reference in New Issue
Block a user