[go: up one dir, main page]

File: trace.py

package info (click to toggle)
linkchecker 7.9-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,216 kB
  • sloc: python: 27,155; lex: 1,141; yacc: 821; makefile: 407; sh: 108; ansic: 95; sql: 19; awk: 4
file content (96 lines) | stat: -rw-r--r-- 2,900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2000-2011 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import re
import linecache
import time
import sys
import thread
import threading

# tracing
_trace_ignore = set()
_trace_filter = set()


def trace_ignore (names):
    """Add given names to trace ignore set, or clear set if names is None."""
    if names is None:
        _trace_ignore.clear()
    else:
        _trace_ignore.update(names)


def trace_filter (patterns):
    """Add given patterns to trace filter set or clear set if patterns is
    None."""
    if patterns is None:
        _trace_filter.clear()
    else:
        _trace_filter.update(re.compile(pat) for pat in patterns)


def _trace (frame, event, arg):
    """Trace function calls."""
    if event in ('call', 'c_call'):
        _trace_line(frame, event, arg)
    elif event in ('return', 'c_return'):
        _trace_line(frame, event, arg)
        print "  return:", arg
    #elif event in ('exception', 'c_exception'):
    #    _trace_line(frame, event, arg)
    return _trace


def _trace_full (frame, event, arg):
    """Trace every executed line."""
    if event == "line":
        _trace_line(frame, event, arg)
    else:
        _trace(frame, event, arg)
    return _trace_full


def _trace_line (frame, event, arg):
    """Print current executed line."""
    name = frame.f_globals["__name__"]
    if name in _trace_ignore:
        return _trace_line
    for pat in _trace_filter:
        if not pat.match(name):
            return _trace_line
    lineno = frame.f_lineno
    filename = frame.f_globals["__file__"]
    if filename.endswith((".pyc", ".pyo")):
        filename = filename[:-1]
    line = linecache.getline(filename, lineno)
    tid = thread.get_ident()
    tname = threading.currentThread().getName()
    args = (tid, tname, time.time(), line.rstrip(), name, lineno)
    print "THREAD(%d) %r %.2f %s # %s:%d" % args


def trace_on (full=False):
    """Start tracing of the current thread (and the current thread only)."""
    if full:
        sys.settrace(_trace_full)
    else:
        sys.settrace(_trace)


def trace_off ():
    """Stop tracing of the current thread (and the current thread only)."""
    sys.settrace(None)