diff -r cc09cfea3dd4 -r 47d0843647d1 mercurial/util.py --- a/mercurial/util.py Tue Jan 07 22:29:57 2014 +0100 +++ b/mercurial/util.py Sun Jan 12 23:28:21 2014 +0100 @@ -1983,3 +1983,20 @@ self._hooks.sort(key=lambda x: x[0]) for source, hook in self._hooks: hook(*args) + +def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr): + '''Writes a message to f (stderr) with a nicely formatted stacktrace. + Skips the 'skip' last entries. + It can be used everywhere and do intentionally not require an ui object. + Not be used in production code but very convenient while developing. + ''' + f.write('%s at:\n' % msg) + entries = [('%s:%s' % (fn, ln), func) + for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]] + if entries: + fnmax = max(len(entry[0]) for entry in entries) + for fnln, func in entries: + f.write(' %-*s in %s\n' % (fnmax, fnln, func)) + +# convenient shortcut +dst = debugstacktrace