class DtraceHandler(logging.Handler): """ Handler to fire USDT probes with log messages """ def __init__(self): logging.Handler.__init__(self) self.provider = Provider("python", "dtrace-logger") self.probes = {} for key in _DESC.keys(): self.probes[key] = Probe("logging", _DESC[key], ["int", "char *"]) self.provider.add_probe(self.probes[key]) self.provider.enable() def emit(self, record): """ Fire the appropriate USDT probe for this record's log level """ probe = None for level in _LEVELS: if record.levelno >= level: probe = self.probes[level] break if not probe: probe = self.probes[logging.NOTSET] probe.fire([record.levelno, self.format(record)])
class fbt(object): """ simple function boundary tracing decorator """ def __init__(self, func): self.func = func probename = func.__name__ self.entry_probe = Probe(probename, "entry", ["char *"]) self.return_probe = Probe(probename, "return", ["char *"]) self.provider = Provider("python-fbt", "fbt") self.provider.add_probe(self.entry_probe) self.provider.add_probe(self.return_probe) self.provider.enable() def __call__(self, *args): self.entry_probe.fire([", ".join([str(x) for x in args])]) ret = self.func(*args) self.return_probe.fire([str(ret)]) return ret def __get__(self, obj, objtype): """Support instance methods. (http://stackoverflow.com/a/3296318)""" return functools.partial(self.__call__, obj)