Package flumotion :: Package common :: Module debug
[hide private]

Source Code for Module flumotion.common.debug

  1  # Flumotion - a streaming media server 
  2  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  3  # All rights reserved. 
  4   
  5  # This file may be distributed and/or modified under the terms of 
  6  # the GNU General Public License version 2 as published by 
  7  # the Free Software Foundation. 
  8  # This file is distributed without any warranty; without even the implied 
  9  # warranty of merchantability or fitness for a particular purpose. 
 10  # See "LICENSE.GPL" in the source distribution for more information. 
 11   
 12  # Licensees having purchased or holding a valid Flumotion Advanced 
 13  # Streaming Server license may use this file in accordance with the 
 14  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 15  # See "LICENSE.Flumotion" in the source distribution for more information. 
 16   
 17  # Headers in this file shall remain intact. 
 18   
 19   
 20  """ 
 21  Debugging helper code 
 22  """ 
 23   
 24   
 25  import sys 
 26  import re 
 27  import linecache 
 28   
 29  from twisted.python.reflect import filenameToModuleName 
 30   
 31   
 32  _tracing = 0 
 33  _indent = '' 
 34   
35 -def trace_start(func_filter=None, ignore_files_re=None, print_returns=False, 36 write=None):
37 global _tracing, _indent 38 39 if func_filter: 40 func_filter = re.compile(func_filter) 41 42 if ignore_files_re: 43 ignore_files_re = re.compile(ignore_files_re) 44 45 if not write: 46 def write(indent, str, *args): 47 print (indent + str) % args
48 49 def do_trace(frame, event, arg): 50 global _tracing, _indent 51 52 if not _tracing: 53 print '[tracing stopped]' 54 return None 55 56 co = frame.f_code 57 58 if event == 'line': 59 return do_trace 60 if func_filter and not func_filter.search(co.co_name): 61 return None 62 if ignore_files_re and ignore_files_re.search(co.co_filename): 63 return None 64 elif event == 'call' or event == 'c_call': 65 if co.co_name == '?': 66 return None 67 module = filenameToModuleName(co.co_filename) 68 write(_indent, '%s:%d:%s():', module, frame.f_lineno, co.co_name) 69 _indent += ' ' 70 return do_trace 71 elif event == 'return' or event == 'c_return': 72 if print_returns: 73 write(_indent, 'return %r', arg) 74 _indent = _indent[:-2] 75 return None 76 elif event == 'exception' or event == 'c_exception': 77 if arg: 78 write(_indent, 'Exception: %s:%d: %s (%s)', co.co_filename, 79 frame.f_lineno, arg[0].__name__, arg[1]) 80 else: 81 write(_indent, 'Exception: (from C)') 82 return do_trace 83 else: 84 write(_indent, 'unknown event: %s', event) 85 return None 86 87 _tracing += 1 88 if _tracing == 1: 89 assert _indent == '' 90 sys.settrace(do_trace) 91
92 -def trace_stop():
93 global _tracing, _indent 94 assert _tracing > 0 95 _tracing -= 1 96 if not _tracing: 97 sys.settrace(None) 98 _indent = ''
99 122