views:

299

answers:

5

Hi all,

I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve “my” code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in “my” code and locating it instantly in a stack trace is a bit hard for eyes. Under “my” code I mean the code that is under current working directory.

So I realized that I want something that will colorize stack traces and highlight lines that are mine. Compare original to highlighted.

I could write a python script that I could use this way:

nosetests | colorize_stack_trace.py

But I believe there is a quicker and more elegant way to do this using Linux toolset. Any ideas?

UPD:

Using supercat suggested by Dennis Williamson, the intermediate result is following bash function:

pyst() {
    rc=/tmp/spcrc;
    echo '#################### ### # # # ########################################' > $rc;
    echo '                     blk   0 r ^(.*)$' >> $rc;
    echo '                     mag b 0 r ^\s*File "'`pwd`'/(.*)"' >> $rc;
    spc -c $rc;
}

Now I can do:

nosetests 2>&1 | pyst

Not too elegant, but works at some degree. There are two problems left:

  1. I can't see any output before nosetests completes. I.e. I don't see the progress.
  2. I have to write 2>&1 over and over again.

UPD 2:

Asking this question I had mainly nosetests in mind. And I just found great solution: rednose nose plugin. It highlights paths that are local plus do many more handy readability things.

Returning to original question: problems that I noted with supercat don't relate to it completely but its a problem of Unix shell streaming, flushing, piping, redirecting. So as as an answer to the question asked I accept an answer that suggests supercat.

+1  A: 

Maybe you could use the cgitb module (short official doc here) as a starting point (it creates nice HTML tracebacks). It should be relatively simple to make the modifications you need (e.g. add a color tag according to the file path). But of course this is only viable if you are willing to use the browser to view the traces.

nikow
Thanks for hint, but its a bit strange to use browser for viewing test results. Smells like action script and QUnit. Not as flexible as it can be :)
nailxx
@nailxx You might want to look at this further. From the official doc: `It was originally designed to display extensive traceback information in HTML for CGI scripts. It was later generalized to also display this information in plain text.`
Alison R.
A: 

As a starting point for colorizing (and otherwise formatting) text, you'll probably want to look at the curses library. Also see this how-to, which looks useful.

As for overriding Python's built-in error handling for all programs...I've never tried it, but I would think that would involve some fairly low-level changes. You could always wrap your code in a huge try/except block, but I assume you don't want to do that. I favor the more Unixy approach of writing a little script that does one thing, and does it well: have it take an input and, if it's a stack trace, colorize it. Otherwise, pass the text through unchanged. Using a pipe, like you suggested, may be the best way. (In this case, to pipe stderr you'd want to do something like this, which merges stderr with stdout before piping: cmd1 2>&1 | cmd2 )

peppergrower
+3  A: 

Take a look at Supercat (spc). It does both ANSI and HTML highlighting and can be configured for your particular output. It comes with some configuration files for source code files in C and Python, for example and log files, Changelogs, diffs and others.

Based on Dave Kirby's suggestion for vim, this does something similar:

less -p regex file_name

Or

some_command | less -p regex
Dennis Williamson
A: 

Load the text into vim:

nosetests | vim -

Set vim to highlight all lines that match a search

:set hlsearch

Search for lines with "your" path

/.*/path/to/my/code.*

Voila - all the lines with your path will be highlighted.

If you want to highlight the next line as well then you can do that too:

/.*/path/to/my/code.*\n.*
Dave Kirby
+3  A: 

Actually, there is a great Python syntax highlighting library called Pygments, which is also able to highlight tracebacks. Here is an example (the pastebin here uses Pygments).

So, all you have to do is:

$ easy_install pygments # downloads and installs pygments
$ cat traceback.txt | pygmentize -l pytb

"pytb" is the shortcut for the PythonTracebackLexer. There is also a special lexer for Python 3 Tracebacks included, which is called "py3tb".

You can format the output in various formats (including html, latex, svg, several image formats and so on). But there is also a terminal formatter available which might look like this (and if you are wondering... of course there are different color themes available!):

Pygments Console Traceback Highlighting

You can use -f html to select another formatter (in that case, the HTML formatter).

Regards,
Christoph

tux21b
Cool option, can be a good alternative to spc
nailxx
+1 for Pygments. It is the Bee's Knees.
jathanism