App-CLI-Plugin-StackTrace

 view release on metacpan or  search on metacpan

lib/App/CLI/Plugin/StackTrace.pm  view on Meta::CPAN

package App::CLI::Plugin::StackTrace;

use strict;
use warnings;
use Devel::StackTrace;
use Fcntl qw(:DEFAULT :flock);

our $CONTEXT_LINE   = 5;
our @IGNORE_PACKAGE = ( __PACKAGE__, "Carp", "Error::subs" );
our $VERSION        = '1.1';

sub setup {

	my($self, @argv) = @_;

    my $stacktrace = (exists $self->config->{stacktrace}) ? $self->config->{stacktrace} : 0;

	if ( (defined $stacktrace && $stacktrace != 0) ||
		 (exists $ENV{APPCLI_STACKTRACE_ENABLE} && $ENV{APPCLI_STACKTRACE_ENABLE} != 0) ||
		 (exists $self->{stacktrace} && defined $self->{stacktrace} && $self->{stacktrace} != 0)
	) {
		$self->_build_override_die_subroutine;
	}

	$self->maybe::next::method(@argv);
}

sub _build_override_die_subroutine {

	my $self = shift;

	$SIG{__DIE__} = sub {

		my $message = shift;
		my @frames;
		my $pkg   = ref $self;
		my $trace = Devel::StackTrace->new( ignore_package => \@IGNORE_PACKAGE );
		my $stacktrace_message = <<STACKTRACE_MESSAGE;
$pkg

  $message

----------
STACKTRACE_MESSAGE

		chomp $message;

		LOOP_OF_FRAMES:
		while ( my $frame = $trace->next_frame ) {

			my $start_line = $frame->line - $CONTEXT_LINE;
			my $end_line   = $frame->line + $CONTEXT_LINE;
			if ($start_line < 1) {
				$start_line = 1;
			}

			my @lines;
			open my $fh, "<", $frame->filename or die sprintf("can not open %s. %s", $frame->filename, $!);
			flock $fh, LOCK_EX                 or die sprintf("can not flock %s. %s", $frame->filename, $!);
			while ( my $line = <$fh> ) {

				chomp $line;
				my $current_line = $.;
				if ($current_line < $start_line || $current_line > $end_line) {
					next;
				}
				my $mark = ($current_line == $frame->line) ? "*" : " ";
				push @lines, sprintf("   %s %05d: %s", $mark, $current_line, $line);
			}
			close $fh or die sprintf("can not close %s. %s", $frame->filename, $!);

			my $package  = $frame->package;
			my $filename = $frame->filename;
			my $line     = $frame->line;
			my $lines    = join "\n", @lines;
			$stacktrace_message .= <<STACKTRACE_MESSAGE;
  $package at $filename line $line.

$lines

  ==========
STACKTRACE_MESSAGE

		} # end of LOOP_OF_FRAMES

		$stacktrace_message .= <<STACKTRACE_MESSAGE;
----------

STACKTRACE_MESSAGE

		# rethrow
		die $stacktrace_message;
	};

}

1;

__END__

=head1 NAME

App::CLI::Plugin::StackTrace - for App::CLI::Extension error stacktrace module

=head1 VERSION

1.1

=head1 SYNOPSIS

  # MyApp.pm
  package MyApp;
  
  use strict;
  use base qw(App::CLI::Extension);
  
  # extension method
  __PACKAGE__->load_plugins(qw(StackTrace));

  __PACKAGE__->config(stacktrace => 1);
  
  1;
  
  # MyApp/Hello.pm
  package MyApp::Hello;
  use strict;
  use feature ":5.10.0";
  use base qw(App::CLI::Command);
  
  sub run {
  
      my($self, @args) = @_;
	  my $x = 1;
	  my $y = 0;



( run in 1.680 second using v1.01-cache-2.11-cpan-e1769b4cff6 )