I needed to get a stack trace when a CGI program was throwing errors, so I turned to Devel::StackTrace and printed the result using CGI::Carp's set_message subroutine. That was a good start, but the result wasn't very easy to read.

So I wrote the below to colorize the output of the stack trace. The colors can be configured with the %TRACE_COLORS hash. Setting $VERBOSE to a true value will cause the arguments to the subroutines to be printed with Data::Dumper--useful if you're going to passing references. Otherwise, it'll print out the raw args.

Remember, you need to import both fatalsToBrowser and set_message from CGI::Carp for this to work.

use CGI::Carp qw( fatalsToBrowser set_message ); BEGIN { my %TRACE_COLORS = ( subroutine => 'blue', args => 'red', file => 'blue', line => 'green', ); my $VERBOSE = 1; sub print_stack_trace { my $msg = shift; use Devel::StackTrace; print "<h1>Error</h1>\n"; print "<pre>$msg</pre>\n"; print "<hr>\n"; print "<h3>Stacktrace</h3>\n"; print qq(<pre>\n); my $trace = Devel::StackTrace->new(); while( my $frame = $trace->next_frame() ) { print qq(<font color="$TRACE_COLORS{subroutine +}">); print $frame->subroutine; print qq#</font>(#; if($VERBOSE) { print qq(<font color="$TRACE_COLORS{ar +gs}">); use Data::Dumper 'Dumper'; print Dumper(+($frame->args)); print qq(</font>); } else { print join ', ', map { qq(<font color="$TRACE_COLORS{ +args}">'). $_ . qq('</font>) } $frame->args; } print qq#)\n\t called at #; print qq(<font color="$TRACE_COLORS{file}">); print $frame->filename; print qq(</font> ); print qq(line ); print qq(<font color="$TRACE_COLORS{line}">); print $frame->line; print qq(</font>); print qq(\n); } print "</pre>\n"; } set_message(\&print_stack_trace); }

In reply to Colorized HTML stack trace by hardburn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.