import re
import threading
import time
import traceback
from types import FunctionType
from virtinst import log
CHECK_MAINLOOP = False
def generate_wrapper(origfunc, name):
def newfunc(*args, **kwargs):
threadname = threading.current_thread().name
is_main_thread = (threading.current_thread().name == "MainThread")
is_non_network_libvirt_call = (name.endswith(".name") or
name.endswith(".UUIDString") or
name.endswith(".__init__") or
name.endswith(".__del__") or
name.endswith(".connect") or
name.startswith("libvirtError"))
if (not is_non_network_libvirt_call and
(is_main_thread or not CHECK_MAINLOOP)):
tb = ""
if is_main_thread:
tb = "\n%s" % "".join(traceback.format_stack())
log.debug("TRACE %s: thread=%s: %s %s %s%s",
time.time(), threadname, name, args, kwargs, tb)
return origfunc(*args, **kwargs)
return newfunc
def wrap_func(module, funcobj):
name = funcobj.__name__
log.debug("wrapfunc %s %s", funcobj, name)
newfunc = generate_wrapper(funcobj, name)
setattr(module, name, newfunc)
def wrap_method(classobj, methodobj):
name = methodobj.__name__
fullname = classobj.__name__ + "." + name
log.debug("wrapmeth %s", fullname)
newfunc = generate_wrapper(methodobj, fullname)
setattr(classobj, name, newfunc)
def wrap_class(classobj):
log.debug("wrapclas %s %s", classobj, classobj.__name__)
for name in dir(classobj):
obj = getattr(classobj, name)
if isinstance(obj, FunctionType):
wrap_method(classobj, obj)
def wrap_module(module, mainloop, regex):
global CHECK_MAINLOOP
CHECK_MAINLOOP = mainloop
for name in dir(module):
if regex and not re.match(regex, name):
continue
obj = getattr(module, name)
if isinstance(obj, FunctionType):
wrap_func(module, obj)
if isinstance(obj, type):
wrap_class(obj)