From cffddedaf36db7f12ffbf223793e746d35848b7f Mon Sep 17 00:00:00 2001 From: Ales Kozumplik Date: Jan 10 2011 12:20:59 +0000 Subject: logging: log_method_return() for devicetree.getDeviceByName() Together with identing the depth of the getDeviceByName() call, this aims to make storage.log easier to read. --- diff --git a/pyanaconda/anaconda_log.py b/pyanaconda/anaconda_log.py index c570024..04dea8d 100644 --- a/pyanaconda/anaconda_log.py +++ b/pyanaconda/anaconda_log.py @@ -58,12 +58,23 @@ def setHandlersLevel(logger, level): map(lambda hdlr: hdlr.setLevel(level), filter (lambda hdlr: hasattr(hdlr, "autoSetLevel") and hdlr.autoSetLevel, logger.handlers)) -def log_method_call(d, *args, **kwargs): - classname = d.__class__.__name__ +def function_name_and_depth(): + IGNORED_FUNCS = ["function_name_and_depth", + "log_method_call", + "log_method_return"] stack = inspect.stack() - methodname = stack[1][3] - spaces = len(stack) * ' ' + for i, frame in enumerate(stack): + methodname = frame[3] + if methodname not in IGNORED_FUNCS: + return (methodname, len(stack) - i) + + return ("unknown function?", 0) + +def log_method_call(d, *args, **kwargs): + classname = d.__class__.__name__ + (methodname, depth) = function_name_and_depth() + spaces = depth * ' ' fmt = "%s%s.%s:" fmt_args = [spaces, classname, methodname] @@ -77,6 +88,14 @@ def log_method_call(d, *args, **kwargs): logging.getLogger("storage").debug(fmt % tuple(fmt_args)) +def log_method_return(d, retval): + classname = d.__class__.__name__ + (methodname, depth) = function_name_and_depth() + spaces = depth * ' ' + fmt = "%s%s.%s returned %s" + fmt_args = (spaces, classname, methodname, retval) + logging.getLogger("storage").debug(fmt % fmt_args) + class AnacondaSyslogHandler(SysLogHandler): def __init__(self, address=('localhost', SYSLOG_UDP_PORT), diff --git a/pyanaconda/storage/devicetree.py b/pyanaconda/storage/devicetree.py index 4461eb0..829159c 100644 --- a/pyanaconda/storage/devicetree.py +++ b/pyanaconda/storage/devicetree.py @@ -40,7 +40,7 @@ import devicelibs.loop from udev import * from pyanaconda import iutil from pyanaconda import tsort -from pyanaconda.anaconda_log import log_method_call +from pyanaconda.anaconda_log import log_method_call, log_method_return import parted import _ped @@ -1859,8 +1859,9 @@ class DeviceTree(object): return found def getDeviceByName(self, name): - log.debug("looking for device '%s'..." % name) + log_method_call(self, name=name) if not name: + log_method_return(self, None) return None found = None @@ -1873,12 +1874,13 @@ class DeviceTree(object): found = device break - log.debug("found %s" % found) + log_method_return(self, found) return found def getDeviceByPath(self, path): - log.debug("looking for device '%s'..." % path) + log_method_call(self, path=path) if not path: + log_method_return(self, None) return None found = None @@ -1891,7 +1893,7 @@ class DeviceTree(object): found = device break - log.debug("found %s" % found) + log_method_return(self, found) return found def getDevicesByType(self, device_type):