misc/experiments/tracer.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 ################################################################################
       
     2 # Copyright (c) 2010 Nokia Corporation
       
     3 #
       
     4 # Licensed under the Apache License, Version 2.0 (the "License");
       
     5 # you may not use this file except in compliance with the License.
       
     6 # You may obtain a copy of the License at
       
     7 #
       
     8 #     http://www.apache.org/licenses/LICENSE-2.0
       
     9 #
       
    10 # Unless required by applicable law or agreed to in writing, software
       
    11 # distributed under the License is distributed on an "AS IS" BASIS,
       
    12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13 # See the License for the specific language governing permissions and
       
    14 # limitations under the License.
       
    15 ################################################################################
       
    16 
       
    17 # From http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html
       
    18 import sys
       
    19 import linecache
       
    20 import random
       
    21 
       
    22 def traceit(frame, event, arg):
       
    23     if event == "line":
       
    24         lineno = frame.f_lineno
       
    25         filename = frame.f_globals.get("__file__", "<nofilename>")
       
    26         if filename == "<stdin>":
       
    27             filename = "traceit.py"
       
    28         if (filename.endswith(".pyc") or
       
    29             filename.endswith(".pyo")):
       
    30             filename = filename[:-1]
       
    31         name = frame.f_globals.get("__name__", "<noname>")
       
    32         line = linecache.getline(filename, lineno)
       
    33         # Modify the name below to trace only a particular .py file
       
    34         if name in ['httplib']:
       
    35             print "%s:%s: %s" % (name, lineno, line.rstrip())
       
    36     return traceit