# Utility functions and classes.


def log_method(method):
    def _method(self, *args, **kwargs):
        # Decorate a class method to log when it is called.
        #
        # :param method: The method to log the call of.
        if self.log is not None:
            arg_strings = [repr(x) for x in args]
            for key,value in kwargs.items():
                arg_strings.append('%s=%s' % (key,value))
            self.log.debug('Called %s.%s(%s)' % (self.__class__.__name__,
                                                 method.__name__,
                                                 ', '.join(arg_strings)))
        return method(self, *args, **kwargs)
    return _method

