PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/eventlet/debug.py

https://bitbucket.org/creiht/eventlet
Python | 127 lines | 101 code | 10 blank | 16 comment | 15 complexity | 4a5014aca5f77d8a924fcb484d842d4a MD5 | raw file
Possible License(s): MIT
  1. """The debug module contains utilities and functions for better
  2. debugging Eventlet-powered applications."""
  3. import os
  4. import sys
  5. import linecache
  6. import string
  7. import inspect
  8. __all__ = ['spew', 'unspew', 'format_hub_listeners', 'hub_listener_stacks',
  9. 'hub_exceptions', 'tpool_exceptions']
  10. class Spew(object):
  11. """
  12. """
  13. def __init__(self, trace_names=None, show_values=True):
  14. self.trace_names = trace_names
  15. self.show_values = show_values
  16. def __call__(self, frame, event, arg):
  17. if event == 'line':
  18. lineno = frame.f_lineno
  19. if '__file__' in frame.f_globals:
  20. filename = frame.f_globals['__file__']
  21. if (filename.endswith('.pyc') or
  22. filename.endswith('.pyo')):
  23. filename = filename[:-1]
  24. name = frame.f_globals['__name__']
  25. line = linecache.getline(filename, lineno)
  26. else:
  27. name = '[unknown]'
  28. try:
  29. src = inspect.getsourcelines(frame)
  30. line = src[lineno]
  31. except IOError:
  32. line = 'Unknown code named [%s]. VM instruction #%d' % (
  33. frame.f_code.co_name, frame.f_lasti)
  34. if self.trace_names is None or name in self.trace_names:
  35. print '%s:%s: %s' % (name, lineno, line.rstrip())
  36. if not self.show_values:
  37. return self
  38. details = '\t'
  39. tokens = line.translate(
  40. string.maketrans(' ,.()', '\0' * 5)).split('\0')
  41. for tok in tokens:
  42. if tok in frame.f_globals:
  43. details += '%s=%r ' % (tok, frame.f_globals[tok])
  44. if tok in frame.f_locals:
  45. details += '%s=%r ' % (tok, frame.f_locals[tok])
  46. if details.strip():
  47. print details
  48. return self
  49. def spew(trace_names=None, show_values=False):
  50. """Install a trace hook which writes incredibly detailed logs
  51. about what code is being executed to stdout.
  52. """
  53. sys.settrace(Spew(trace_names, show_values))
  54. def unspew():
  55. """Remove the trace hook installed by spew.
  56. """
  57. sys.settrace(None)
  58. def format_hub_listeners():
  59. """ Returns a formatted string of the current listeners on the current
  60. hub. This can be useful in determining what's going on in the event system,
  61. especially when used in conjunction with :func:`hub_listener_stacks`.
  62. """
  63. from eventlet import hubs
  64. hub = hubs.get_hub()
  65. result = ['READERS:']
  66. for l in hub.get_readers():
  67. result.append(repr(l))
  68. result.append('WRITERS:')
  69. for l in hub.get_writers():
  70. result.append(repr(l))
  71. return os.linesep.join(result)
  72. def format_hub_timers():
  73. """ Returns a formatted string of the current timers on the current
  74. hub. This can be useful in determining what's going on in the event system,
  75. especially when used in conjunction with :func:`hub_timer_stacks`.
  76. """
  77. from eventlet import hubs
  78. hub = hubs.get_hub()
  79. result = ['TIMERS:']
  80. for l in hub.timers:
  81. result.append(repr(l))
  82. return os.linesep.join(result)
  83. def hub_listener_stacks(state):
  84. """Toggles whether or not the hub records the stack when clients register
  85. listeners on file descriptors. This can be useful when trying to figure
  86. out what the hub is up to at any given moment. To inspect the stacks
  87. of the current listeners, call :func:`format_hub_listeners` at critical
  88. junctures in the application logic.
  89. """
  90. from eventlet import hubs
  91. hubs.get_hub().set_debug_listeners(state)
  92. def hub_timer_stacks(state):
  93. """Toggles whether or not the hub records the stack when timers are set.
  94. To inspect the stacks of the current timers, call :func:`format_hub_timers`
  95. at critical junctures in the application logic.
  96. """
  97. from eventlet.hubs import timer
  98. timer._g_debug = state
  99. def hub_exceptions(state):
  100. """Toggles whether the hub prints exceptions that are raised from its
  101. timers. This can be useful to see how greenthreads are terminating.
  102. """
  103. from eventlet import hubs
  104. hubs.get_hub().set_timer_exceptions(state)
  105. from eventlet import greenpool
  106. greenpool.DEBUG = state
  107. def tpool_exceptions(state):
  108. """Toggles whether tpool itself prints exceptions that are raised from
  109. functions that are executed in it, in addition to raising them like
  110. it normally does."""
  111. from eventlet import tpool
  112. tpool.QUIET = not state