__all__ = ["LogReader", "ENTER", "EXIT", "LINE"]
def __init__(self, logfn):
# (fileno, lineno) -> filename, funcname
self._reader = _hotshot.logreader(logfn)
self._nextitem = self._reader.next
self._info = self._reader.info
if 'current-directory' in self._info:
self.cwd = self._info['current-directory']
# This mirrors the call stack of the profiled code as the log
# is read back in. It contains tuples of the form:
# (file name, line number of function def, function name)
self._append = self._stack.append
self._pop = self._stack.pop
"""Return the file descriptor of the log reader's log file."""
return self._reader.fileno()
def addinfo(self, key, value):
"""This method is called for each additional ADD_INFO record.
This can be overridden by applications that want to receive
these events. The default implementation does not need to be
called by alternate implementations.
The initial set of ADD_INFO records do not pass through this
mechanism; this is only needed to receive notification when
new values are added. Subclasses can inspect self._info after
calling LogReader.__init__().
def get_filename(self, fileno):
return self._filemap[fileno]
raise ValueError, "unknown fileno"
return self._filemap.values()
def get_fileno(self, filename):
filename = os.path.normcase(os.path.normpath(filename))
for fileno, name in self._filemap.items():
raise ValueError, "unknown filename"
def get_funcname(self, fileno, lineno):
return self._funcmap[(fileno, lineno)]
raise ValueError, "unknown function location"
# This adds an optional (& ignored) parameter to next() so that the
# same bound method can be used as the __getitem__() method -- this
# avoids using an additional method call which kills the performance.
# This call may raise StopIteration:
what, tdelta, fileno, lineno = self._nextitem()
# handle the most common cases first
filename, funcname = self._decode_location(fileno, lineno)
t = (filename, lineno, funcname)
return what, self._pop(), tdelta
filename, firstlineno, funcname = self._stack[-1]
return what, (filename, lineno, funcname), tdelta
if what == WHAT_DEFINE_FILE:
filename = os.path.normcase(os.path.normpath(tdelta))
self._filemap[fileno] = filename
elif what == WHAT_DEFINE_FUNC:
filename = self._filemap[fileno]
self._funcmap[(fileno, lineno)] = (filename, tdelta)
elif what == WHAT_ADD_INFO:
# value already loaded into self.info; call the
# overridable addinfo() handler so higher-level code
# can pick up the new value
if tdelta == 'current-directory':
self.addinfo(tdelta, lineno)
raise ValueError, "unknown event type"
def _decode_location(self, fileno, lineno):
return self._funcmap[(fileno, lineno)]
# This should only be needed when the log file does not
# contain all the DEFINE_FUNC records needed to allow the
# function name to be retrieved from the log file.
if self._loadfile(fileno):
filename = funcname = None
filename, funcname = self._funcmap[(fileno, lineno)]
filename = self._filemap.get(fileno)
self._funcmap[(fileno, lineno)] = (filename, funcname)
return filename, funcname
def _loadfile(self, fileno):
filename = self._filemap[fileno]
print "Could not identify fileId", fileno
absname = os.path.normcase(os.path.join(self.cwd, filename))
st = parser.suite(fp.read())
# Scan the tree looking for def and lambda nodes, filling in
# self._funcmap with all the available information.
except (IndexError, TypeError):
self._funcmap[(fileno, tree[2][2])] = filename, tree[2][1]
self._funcmap[(fileno, tree[1][2])] = filename, "<lambda>"
stack.extend(list(tree[1:]))