| 1 | package Devel::SummarizedWarnings; | ||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use vars qw(@LOGGED_WARNINGS $INSTALLED_HANDLER $VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | ▶ | sub install_handler { | ||||||||||||
|
Subroutine
install_handler: called CC=2 SCAR=7.9
Pod
undocumented
| |||||||||||||||
| 6 | 2 | my $old_handler = $SIG{'__WARN__'}; | |||||||||||||
| 7 | my $new_handler = | ||||||||||||||
| 8 | ( $old_handler | ||||||||||||||
| 9 | 0 | ▶ | ? sub { &$old_handler; &append_to_warning_log; } | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 10 | 2 | ▶ | : \&append_to_warning_log ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 2 | $INSTALLED_HANDLER = $SIG{'__WARN__'} = $new_handler; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | return; | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | # Modify this globally | ||||||||||||||
| 18 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 1 | $VERSION = 0.01; | |||||||||||||
| 20 | 1 | install_handler(); | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 1 | ▶ | END { | ||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 1 | dump_warnings(); | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | sub append_to_warning_log { | ||||||||||||
|
Subroutine
append_to_warning_log: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 28 | 1 | push @LOGGED_WARNINGS, @_; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 1 | ▶ | sub dump_warnings { | ||||||||||||
|
Subroutine
dump_warnings: called CC=12 SCAR=43.8
Pod
undocumented
| |||||||||||||||
| 32 | 1 | local $^W; | |||||||||||||
| 33 | 1 | ▶ | if ( $SIG{'__WARN__'} != $INSTALLED_HANDLER ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 0 | push | |||||||||||||
| 35 | @LOGGED_WARNINGS, | ||||||||||||||
| 36 | __PACKAGE__ . " was disabled prior to summarization\n"; | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | # Summarize the saved warnings | ||||||||||||||
| 40 | 1 | my %order; | |||||||||||||
| 41 | my %sum; | ||||||||||||||
| 42 | 1 | ▶ | while ( my $warning = shift @LOGGED_WARNINGS ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 0 | my $msg; | |||||||||||||
| 44 | my $line; | ||||||||||||||
| 45 | 0 | ▶ | if ( not $warning =~ /^(.*) at .+? line (\d+)\.$/s ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | $warning =~ s/\n$//; | |||||||||||||
| 47 | 0 | $msg = $warning; | |||||||||||||
| 48 | 0 | $line = 'NoSuchLine'; | |||||||||||||
| 49 | } else { | ||||||||||||||
| 50 | 0 | $msg = $1; | |||||||||||||
| 51 | 0 | $line = $2; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | 0 | $sum{$msg}{$line}++; | |||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | ▶ | if ( not exists $order{$msg} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | $order{$msg} = 1 + %order; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | # Reformat the summarization | ||||||||||||||
| 61 | 1 | my @out; | |||||||||||||
| 62 | 0 | for ( sort { $order{$a} <=> $order{$b} } | |||||||||||||
| 63 | keys %order ) { | ||||||||||||||
| 64 | 0 | my $wrn = $sum{$_}; | |||||||||||||
| 65 | 0 | ▶ | if ( exists $wrn->{'NoSuchLine'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | push | ||||||||||||||
| 67 | @out, | ||||||||||||||
| 68 | $_ | ||||||||||||||
| 69 | 0 | ▶ | . ( $wrn->{'NoSuchLine'} > 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | ? " (x$wrn->{'NoSuchLine'})" | ||||||||||||||
| 71 | : '' ); | ||||||||||||||
| 72 | } else { | ||||||||||||||
| 73 | push | ||||||||||||||
| 74 | @out, | ||||||||||||||
| 75 | 0 | ▶ | "$_ on line@{[1 < keys %$wrn ? 's' : '']} " | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 76 | . join( 2 == keys %$wrn ? ' and ' : ', ', | ||||||||||||||
| 77 | 0 | ▶ | map "$_@{[ $wrn->{$_} == 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | ? '' | ||||||||||||||
| 79 | : qq[ (x$wrn->{$_})]]}", | ||||||||||||||
| 80 | 0 | sort { $a <=> $b } | |||||||||||||
| 81 | keys %$wrn ); | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 1 | local $\ = "\n"; | |||||||||||||
| 86 | 1 | print STDERR for @out; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 1 | 1; | |||||||||||||
| 90 | |||||||||||||||
| 91 | __END__ | ||||||||||||||