Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Devel-StackTrace CPAN distribution.

Report information
The Basics
Id: 54611
Status: resolved
Priority: 0/
Queue: Devel-StackTrace

People
Owner: Nobody in particular
Requestors: milu71 [...] googlemail.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Enhancement proposal: Allow user to format stacktrace
(As outlined by mail privately the other day:) Currently, the user has limited control over the way the stacktrace is formatted for display. It would be nice to be able to have stacktraces in ANSI colors, for example. They stand out better in the logfile. The modifications required to accomodate this new feature are relatively minor. See the attached patch (not the same as the one
Subject: dst.diff
--- /usr/lib/perl5/site_perl/5.10/devel/stacktrace.pm 2010-02-10 22:28:43.000000000 +0100 +++ lib/Devel/StackTrace.pm 2010-02-15 03:07:29.453125000 +0100 @@ -32,6 +32,51 @@ %p, }, $class; + # provide missing main formatter + + $self->{formatter} ||= sub { + my( $sub, $args, $filename, $line, $first) = @_; + return sprintf '%s at %s line %s', + $first ? 'Trace begun' : "$sub$args called", $filename, $line; + }; + + # provide missing argument formatter + # if there are any arguments in the sub-routine call, format them + # according to the format variables defined elsewhere in this file + + $self->{arg_formatter} ||= sub { + my($arg) = @_; + # set args to the string "undef" if undefined + $arg = "undef", next unless defined $arg; + + $arg = $self->_ref_to_string($arg) + if ref $arg; + + eval + { + if ( $self->{max_arg_length} + && length $arg > $self->{max_arg_length} ) + { + substr( $arg, $self->{max_arg_length} ) = '...'; + } + + s/'/\\'/g; + + # 'quote' arg unless it looks like a number + $arg = "'$arg'" unless /^-?[\d.]+$/; + + # print control/high ASCII chars as 'M-<char>' or '^<char>' + s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg; + s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg; + }; + + if ( my $e = $@ ) + { + $arg = $e =~ /malformed utf-8/i ? '(bad utf-8)' : '?'; + } + return $arg; + }; + $self->_record_caller_data(); return $self; @@ -147,7 +192,7 @@ push @{ $self->{frames} }, Devel::StackTraceFrame->new( $c, $args, - $self->{respect_overload}, $self->{max_arg_length} ); + $self->{formatter}, $self->{arg_formatter} ); } sub next_frame @@ -277,9 +322,9 @@ $self->{args} = $_[1]; - $self->{respect_overload} = $_[2]; + $self->{formatter} = $_[2]; - $self->{max_arg_length} = $_[3]; + $self->{arg_formatter} = $_[3]; return $self; } @@ -298,13 +343,10 @@ my $first = shift; my $sub = $self->subroutine; + my $sub_args = ''; # This code stolen straight from Carp.pm and then tweaked. All # errors are probably my fault -dave - if ($first) - { - $sub = 'Trace begun'; - } - else + unless ($first) { # Build a string, $sub, which names the sub-routine called. # This may also be "require ...", "eval '...' or "eval {...}" @@ -325,54 +367,14 @@ $sub = 'eval {...}'; } - # if there are any arguments in the sub-routine call, format - # them according to the format variables defined earlier in - # this file and join them onto the $sub sub-routine string - # - # We copy them because they're going to be modified. - # - if ( my @a = $self->args ) - { - for (@a) - { - # set args to the string "undef" if undefined - $_ = "undef", next unless defined $_; - - # hack! - $_ = $self->Devel::StackTrace::_ref_to_string($_) - if ref $_; - - eval - { - if ( $self->{max_arg_length} - && length $_ > $self->{max_arg_length} ) - { - substr( $_, $self->{max_arg_length} ) = '...'; - } - - s/'/\\'/g; - - # 'quote' arg unless it looks like a number - $_ = "'$_'" unless /^-?[\d.]+$/; - - # print control/high ASCII chars as 'M-<char>' or '^<char>' - s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg; - s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg; - }; - - if ( my $e = $@ ) - { - $_ = $e =~ /malformed utf-8/i ? '(bad utf-8)' : '?'; - } - } + # format subroutine arguments for inclusion in output + my @a = map $self->{arg_formatter}($_), $self->args; - # append ('all', 'the', 'arguments') to the $sub string - $sub .= '(' . join(', ', @a) . ')'; - $sub .= ' called'; - } + # append ('all', 'the', 'arguments') to the $sub string + $sub_args = '(' . join(', ', @a) . ')'; } - return "$sub at " . $self->filename . ' line ' . $self->line; + return $self->{formatter}( $sub, $sub_args, $self->filename, $self->line, $first); } 1; @@ -506,6 +508,16 @@ argument's string representation if it is longer than this number of characters. +=item * formatter => $subroutine_reference + +You can take control of how the stacktrace is formatted. Details to +be specified. + +=item * arg_formatter => $subroutine_reference + +You can take control of how arguments to subroutine calls appearing in +the stacktrace are formatted. Details to be specified. + =back =item * $trace->next_frame
Subject: ColorStackTrace.pm
package ColorStackTrace; use strict; use warnings; use base 'Devel::StackTrace'; use Term::ANSIColor; # use colors in output use Cwd (); # clear up paths use constant DFLT_LOGFILE => sprintf '/tmp/%s.log', join '-', split '::', __PACKAGE__; my %counters; # count subroutine invocations # print colored stacktrace # aggressively try to get relative path sub new { my( $self, $cwd, %args ) = @_; $cwd ||= Cwd::cwd; my $fmt = sub { # callback for stacktrace formatting my( $pkg_and_sub, $subargs, $file, $line, $first ) = @_; $counters{ $pkg_and_sub }++; # match package (1), subroutine (2) and arguments (3) $pkg_and_sub =~ m/((?:\w+::)*)(\w+)$/; my $func = color('bold blue'); # package color $func .= ( $1 && $2 ) # subroutine and arguments colors ? $1 . color('bold green') . $2 . color('magenta') . $subargs : $pkg_and_sub; (my $rel = Cwd::abs_path( $file )) =~ s#^$cwd/##; # get relative path return sprintf "%s%s %s %s %s", $first ? "\n" : '', # insert newline to mark ST beginning color('bold red') . $rel, color('bold yellow') . $line, $func, color('bold yellow') . "($counters{ $pkg_and_sub })" . color('reset'); }; # add myself to the list of ignored packages return $self->SUPER::new( formatter => $fmt, # arg_formatter => sub { 'xxx' }, ignore_class => __PACKAGE__, %args ); } # convenience constructor sub cwd { my $frame_filter = sub { #use Data::Dumper; warn Dumper \@_; my %args = %{ $_[0] }; my $pkg = $args{caller}[0]; return if $pkg =~ m/^Class::MOP\b/; return 1; }; return $_[0]->new( '/virtual/www/michael.ludwig/sandbox/eins/perl', frame_filter => $frame_filter, ); } sub logfile { my( $self, $logfile ) = @_; $logfile ||= DFLT_LOGFILE; open my $fh, '>>', $logfile or die "open $logfile: $!"; return print $fh $self->as_string; close $fh; } 1;
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Sun, 14 Feb 2010 21:04:00 -0600 (CST)
To: Michael Ludwig via RT <bug-Devel-StackTrace [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sun, 14 Feb 2010, Michael Ludwig via RT wrote: Show quoted text
> (As outlined by mail privately the other day:) > > Currently, the user has limited control over the way the stacktrace is > formatted for display. It would be nice to be able to have stacktraces > in ANSI colors, for example. They stand out better in the logfile. > > The modifications required to accomodate this new feature are relatively > minor. See the attached patch (not the same as the one
I think this would be better done as hooks to allow the use of subclasses as needed, rather than adding functionality to D::ST itself. If you're using Exception::Class, you might also want to tweak that module to allow for a custom stacktrace subclass. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
From: milu71 [...] googlemail.com
Am So 14. Feb 2010, 22:04:56, autarch@urth.org schrieb: Show quoted text
> > The modifications required to accomodate this new feature are relatively > > minor. See the attached patch (not the same as the one
> > I think this would be better done as hooks to allow the use of subclasses > as needed, rather than adding functionality to D::ST itself.
My patch does not add any functionality at all to D::ST. It just allows the user to pass in subroutine references as parameters, defaulting to the existing formatting implementation. Plus, it resolves what you've marked as "hack" in the code, where StackFrame explicitly calls StackTrace's method, and it also gets rid of one redundant if statement preceding a for loop. Maybe you could take another look at the patch? The documentation will be updated once you agree this enhancement makes sense. Show quoted text
> If you're using Exception::Class, you might also want to tweak that module > to allow for a custom stacktrace subclass.
I'm not using Exception::Class. Devel::StackTrace is just really fine in that it has almost zero dependencies. Best, Michael
From: milu71 [...] googlemail.com
Am So 14. Feb 2010, 22:04:56, autarch@urth.org schrieb: Show quoted text
> I think this would be better done as hooks to allow the use of subclasses > as needed, rather than adding functionality to D::ST itself.
I considered subclassing, but the arrangement of D::ST as a pair of classes - Devel::StackTrace and Devel::StackTraceFrame - made parametrizing seem to me less intrusive and easier. Michael
From: milu71 [...] googlemail.com
Hi Dave, Am So 14. Feb 2010, 22:04:56, autarch@urth.org schrieb: Show quoted text
> > I think this would be better done as hooks to allow the use of subclasses > as needed, rather than adding functionality to D::ST itself.
Hi Dave, are you still considering the issue or have you made up your mind about it? It would be good to let me know. If you tend to decline the patch, are you considering releasing a new version of D::ST that provides hooks for formatting? Please note that my patch does not add functionality to D::ST itself, it just allows for parameterizing the formatting code, leaving your formatting implementation as the default handler. I think that's preferable to having to extend D::ST in order to modify how messages are formatted, and it exploits the great Perl feature of subroutines as data objects, but ultimately it might be a matter of taste, and some people just prefer inheritance, despite Perl's poor OO support built up around @ISA. Best, Michael
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Tue, 9 Mar 2010 21:15:18 -0600 (CST)
To: Michael Ludwig via RT <bug-Devel-StackTrace [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sun, 7 Mar 2010, Michael Ludwig via RT wrote: Show quoted text
> If you tend to decline the patch, are you considering releasing a new > version of D::ST that provides hooks for formatting?
Like I said, I'd rather see a hooks to allow using a different Frame class in D::ST itself. Then you can easily write a subclass which formats however you like. That way, in the future, if someone else wants an unrelated feature which would otherwise need a hook, they can just write a subclass. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Wed, 10 Mar 2010 08:31:01 +0100
To: "autarch [...] urth.org via RT" <bug-Devel-StackTrace [...] rt.cpan.org>
From: Michael Ludwig <milu71 [...] gmx.de>
autarch@urth.org via RT schrieb am 09.03.2010 um 22:16:26 (-0500): Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=54611 > > > On Sun, 7 Mar 2010, Michael Ludwig via RT wrote: >
> > If you tend to decline the patch, are you considering releasing a > > new version of D::ST that provides hooks for formatting?
> > Like I said, I'd rather see a hooks to allow using a different Frame > class in D::ST itself. Then you can easily write a subclass which > formats however you like. That way, in the future, if someone else > wants an unrelated feature which would otherwise need a hook, they can > just write a subclass.
Well, fair enough, it's your module. I think that way, it'll take a little more refactoring than going with callback parameters. I'm waiting for the release, then. -- Michael Ludwig
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Wed, 10 Mar 2010 09:43:23 -0600 (CST)
To: Michael Ludwig via RT <bug-Devel-StackTrace [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Wed, 10 Mar 2010, Michael Ludwig via RT wrote: Show quoted text
> Well, fair enough, it's your module. I think that way, it'll take a > little more refactoring than going with callback parameters. I'm waiting > for the release, then.
You might be waiting a while. I think this is _your_ itch, not mine. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Wed, 10 Mar 2010 21:28:48 +0100
To: "autarch [...] urth.org via RT" <bug-Devel-StackTrace [...] rt.cpan.org>
From: Michael Ludwig <milu71 [...] gmx.de>
autarch@urth.org via RT schrieb am 10.03.2010 um 12:33:43 (-0500): Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=54611 > > > On Wed, 10 Mar 2010, Michael Ludwig via RT wrote: >
> > Well, fair enough, it's your module. I think that way, it'll take a > > little more refactoring than going with callback parameters. I'm > > waiting for the release, then.
> > You might be waiting a while. I think this is _your_ itch, not mine.
Cool! Maybe a fork will do then :-) -- Michael Ludwig
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Wed, 10 Mar 2010 15:54:49 -0600 (CST)
To: Michael Ludwig via RT <bug-Devel-StackTrace [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Wed, 10 Mar 2010, Michael Ludwig via RT wrote: Show quoted text
> Queue: Devel-StackTrace > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=54611 > > > autarch@urth.org via RT schrieb am 10.03.2010 um 12:33:43 (-0500):
>> <URL: https://rt.cpan.org/Ticket/Display.html?id=54611 > >> >> On Wed, 10 Mar 2010, Michael Ludwig via RT wrote: >>
>>> Well, fair enough, it's your module. I think that way, it'll take a >>> little more refactoring than going with callback parameters. I'm >>> waiting for the release, then.
>> >> You might be waiting a while. I think this is _your_ itch, not mine.
> > Cool! Maybe a fork will do then :-)
A fork isn't necessary. Just give me a patch to add subclassibility and I'll apply it and release. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
Subject: Re: [rt.cpan.org #54611] Enhancement proposal: Allow user to format stacktrace
Date: Wed, 10 Mar 2010 23:56:21 +0100
To: "autarch [...] urth.org via RT" <bug-Devel-StackTrace [...] rt.cpan.org>
From: Michael Ludwig <milu71 [...] gmx.de>
autarch@urth.org via RT schrieb am 10.03.2010 um 16:55:51 (-0500): Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=54611 > > > On Wed, 10 Mar 2010, Michael Ludwig via RT wrote:
Show quoted text
> > Cool! Maybe a fork will do then :-)
> > A fork isn't necessary. Just give me a patch to add subclassibility > and I'll apply it and release.
Okay, that's a different song. I'll see what I can come up - later. Won't have time to look at this during the next couple of days. -- Michael Ludwig