2012-09-27 08:58:54 -04:00
#
2013-10-27 21:59:46 +01:00
# Copyright (C) 2011, 2013 Red Hat, Inc.
2012-09-27 08:58:54 -04:00
# 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.
#
# This module provides a simple way to trace any activity on a specific
# python class or module. The trace output is logged using the regular
# logging infrastructure. Invoke this with virt-manager --trace-libvirt
import logging
import time
import re
import traceback
from types import FunctionType
from types import ClassType
from types import MethodType
2013-04-13 14:34:52 -04:00
2012-10-21 13:12:21 -04:00
def generate_wrapper ( origfunc , name , do_tb ) :
2012-09-27 08:58:54 -04:00
def newfunc ( * args , * * kwargs ) :
2012-10-21 13:12:21 -04:00
tb = do_tb and ( " \n %s " % " " . join ( traceback . format_stack ( ) ) ) or " "
logging . debug ( " TRACE %s : %s %s %s %s " ,
time . time ( ) , name , args , kwargs , tb )
2012-09-27 08:58:54 -04:00
return origfunc ( * args , * * kwargs )
return newfunc
2013-04-13 14:34:52 -04:00
2012-09-27 08:58:54 -04:00
def wrap_func ( module , funcobj , tb ) :
name = funcobj . __name__
2012-10-21 13:12:21 -04:00
logging . debug ( " wrapfunc %s %s " , funcobj , name )
2012-09-27 08:58:54 -04:00
newfunc = generate_wrapper ( funcobj , name , tb )
setattr ( module , name , newfunc )
2013-04-13 14:34:52 -04:00
2012-09-27 08:58:54 -04:00
def wrap_method ( classobj , methodobj , tb ) :
name = methodobj . __name__
fullname = classobj . __name__ + " . " + name
2012-10-21 13:12:21 -04:00
logging . debug ( " wrapmeth %s " , fullname )
2012-09-27 08:58:54 -04:00
newfunc = generate_wrapper ( methodobj , fullname , tb )
setattr ( classobj , name , newfunc )
2013-04-13 14:34:52 -04:00
2012-09-27 08:58:54 -04:00
def wrap_class ( classobj , tb ) :
2012-10-21 13:12:21 -04:00
logging . debug ( " wrapclas %s %s " , classobj , classobj . __name__ )
2012-09-27 08:58:54 -04:00
for name in dir ( classobj ) :
obj = getattr ( classobj , name )
if type ( obj ) is MethodType :
wrap_method ( classobj , obj , tb )
2013-04-13 14:34:52 -04:00
2012-09-27 08:58:54 -04:00
def wrap_module ( module , regex = None , tb = False ) :
for name in dir ( module ) :
if regex and not re . match ( regex , name ) :
continue
obj = getattr ( module , name )
if type ( obj ) is FunctionType :
wrap_func ( module , obj , tb )
2013-07-06 14:33:02 -04:00
if type ( obj ) is ClassType or type ( obj ) is type :
2012-09-27 08:58:54 -04:00
wrap_class ( obj , tb )