| 1 | ############################################################################## | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Exception::Sink | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2006-2024 Vladi Belperchinov-Shabanski "Cade" | ||||||||||||||||||||||||||||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||||||||||||||||||||
| 6 | # http://cade.noxrun.com/ | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # GPLv2 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | ############################################################################## | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # compact general purpose exception handling | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | ############################################################################## | ||||||||||||||||||||||||||||
| 15 | package Exception::Sink; | ||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 18 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 19 | sink | ||||||||||||||||||||||||||||
| 20 | dive | ||||||||||||||||||||||||||||
| 21 | surface | ||||||||||||||||||||||||||||
| 22 | surface2 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | boom_skip | ||||||||||||||||||||||||||||
| 25 | boom | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 29 | $DEBUG_SINK | ||||||||||||||||||||||||||||
| 30 | get_stack_trace | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | our %EXPORT_TAGS = ( 'none' => [ ] ); | |||||||||||||||||||||||||||
| 34 | 1 | our $VERSION = '3.09'; | |||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Exception::Sink::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | our $DEBUG_SINK = 0; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub sink($); | ||||||||||||||||||||||||||||
| 41 | sub dive(); | ||||||||||||||||||||||||||||
| 42 | sub surface(@); | ||||||||||||||||||||||||||||
| 43 | sub surface2(@); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub boom_skip($$); | ||||||||||||||||||||||||||||
| 46 | sub boom($); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | ############################################################################## | ||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||
| 50 | # sink( "CLASS: ID: message..." ) | ||||||||||||||||||||||||||||
| 51 | # sinks the ship of class and id with reason message | ||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub sink($) | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $msg = shift; | ||||||||||||||||||||||||||
|
Subroutine
sink: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $org = $msg; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $class = 'UNKNOWN'; | |||||||||||||||||||||||||||
| 61 | 0 | my $id = 'UNKNOWN'; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $class = $1 || $2 if $msg =~ s/^([a-z0-9_]+)\s*:\s*|^([a-z0-9_]+)$//i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$1 || $2
MC/DC0%0/2
$1 || $2
$1
$2
Truth table0%0/3
A || B
A= $1B= $2
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $id = $1 if $msg =~ s/^([a-z0-9_]+)\s*:\s*|^([a-z0-9_]+)$//i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | $msg =~ s/\s+at\s+\/\S+.+$//; | |||||||||||||||||||||||||||
| 67 | 0 | chomp( $msg ); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my ( $p, $f, $l ) = caller(); | |||||||||||||||||||||||||||
| 70 | 0 | $f =~ s/^(.*\/)([^\/]+)$/$2/; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $class = uc $class; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | print STDERR "sink: $class ($f:$l)\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | die Exception::Sink::Class->new( | |||||||||||||||||||||||||||
| 77 | 'CLASS' => $class, | ||||||||||||||||||||||||||||
| 78 | 'ID' => $id, | ||||||||||||||||||||||||||||
| 79 | 'MSG' => $msg, | ||||||||||||||||||||||||||||
| 80 | 'PACKAGE' => $p, | ||||||||||||||||||||||||||||
| 81 | 'FILE' => $f, | ||||||||||||||||||||||||||||
| 82 | 'LINE' => $l, | ||||||||||||||||||||||||||||
| 83 | 'ORG' => $org, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ############################################################################## | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | # dive() | ||||||||||||||||||||||||||||
| 90 | # continue sinking... | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub dive() | ||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | print STDERR "dive: pre: $@\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Subroutine
dive: not called CC=6 SCAR=37.4
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return 0 unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if( !ref($@) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | print STDERR "dive: non-ship, resink: $@\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | # re-sink, non-ship | ||||||||||||||||||||||||||||
| 101 | 0 | my $AT=$@; | |||||||||||||||||||||||||||
| 102 | 0 | eval { sink "SINK: $AT"; } | |||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | print STDERR "dive: $@->{CLASS}\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | die; # propagate | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | ############################################################################## | ||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||
| 112 | # surface( class list ) | ||||||||||||||||||||||||||||
| 113 | # stops sinking of specific classes... | ||||||||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub surface(@) | ||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | print STDERR "surface: enter: $@ -> @_\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Subroutine
surface: not called CC=12 SCAR=50.5
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return 0 unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return 1 unless @_; # catch all | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if( !ref($@) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | print STDERR "surface: non-ship, resink: $@\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | # re-sink, non-ship | ||||||||||||||||||||||||||||
| 125 | 0 | my $AT=$@; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | if( $AT =~ /^[A-Z0-9_]+\:/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 0 | eval { sink $AT; } | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | else | ||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | 0 | eval { sink "SINK: $AT"; } | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | }; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | print STDERR "surface: $@->{CLASS} -> @_?\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | for my $class ( @_ ) | |||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return 1 if $class eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | return 1 if uc $class eq $@->{ 'CLASS' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | print STDERR "surface: $@->{CLASS} -> continuing...\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | return 0; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub surface2(@) | ||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | return 1 if surface(@_); | ||||||||||||||||||||||||||
|
Subroutine
surface2: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | dive(); | |||||||||||||||||||||||||||
| 151 | 0 | return 0; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | ############################################################################## | ||||||||||||||||||||||||||||
| 155 | # | ||||||||||||||||||||||||||||
| 156 | # boom() | ||||||||||||||||||||||||||||
| 157 | # sink with stack trace | ||||||||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub boom_skip($$) | ||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $msg = shift; | ||||||||||||||||||||||||||
|
Subroutine
boom_skip: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $skip = shift; | |||||||||||||||||||||||||||
| 164 | 0 | chomp( $msg ); | |||||||||||||||||||||||||||
| 165 | 0 | $msg = "BOOM: [$$] $msg\n"; | |||||||||||||||||||||||||||
| 166 | 0 | sink( join '', ( $msg, get_stack_trace( $skip ) ) ); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub boom($) | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | boom_skip($_[0],0); | ||||||||||||||||||||||||||
|
Subroutine
boom: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub get_stack_trace | ||||||||||||||||||||||||||||
| 175 | { | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | my $skip = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_stack_trace: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | my @st; | |||||||||||||||||||||||||||
| 179 | my $i; | ||||||||||||||||||||||||||||
| 180 | 0 | my $ml; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | while ( my ( $pack, $file, $line, $subname ) = caller($i++) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 0 | my $l = length( "$pack::$subname" ); | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | $ml = $l if $l > $ml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames | |||||||||||||||||||||||||||
| 190 | 0 | my $ii; | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | while ( my ( $pack, $file, $line, $subname ) = caller($i++) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 0 | $ii++; | |||||||||||||||||||||||||||
| 194 | 0 | my $l = length( "$pack::$subname" ); | |||||||||||||||||||||||||||
| 195 | 0 | my $pad = ' ' x ( $ml - $l ); | |||||||||||||||||||||||||||
| 196 | 0 | push @st, " [$$] $ii: $pack::$subname $pad $file line $line\n"; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | return wantarray ? ( @st ) : join( '', @st ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | ############################################################################## | ||||||||||||||||||||||||||||
| 203 | 1 | 1; | |||||||||||||||||||||||||||
| 204 | ############################################################################## | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||