_util.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- test-case-name: twisted.logger.test.test_util -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Logging utilities.
  6. """
  7. def formatTrace(trace):
  8. """
  9. Format a trace (that is, the contents of the C{log_trace} key of a log
  10. event) as a visual indication of the message's propagation through various
  11. observers.
  12. @param trace: the contents of the C{log_trace} key from an event.
  13. @type trace: object
  14. @return: A multi-line string with indentation and arrows indicating the
  15. flow of the message through various observers.
  16. @rtype: L{unicode}
  17. """
  18. def formatWithName(obj):
  19. if hasattr(obj, "name"):
  20. return u"{0} ({1})".format(obj, obj.name)
  21. else:
  22. return u"{0}".format(obj)
  23. result = []
  24. lineage = []
  25. for parent, child in trace:
  26. if not lineage or lineage[-1] is not parent:
  27. if parent in lineage:
  28. while lineage[-1] is not parent:
  29. lineage.pop()
  30. else:
  31. if not lineage:
  32. result.append(u"{0}\n".format(formatWithName(parent)))
  33. lineage.append(parent)
  34. result.append(u" " * len(lineage))
  35. result.append(u"-> {0}\n".format(formatWithName(child)))
  36. return u"".join(result)