blib/lib/gerr.pm

filter: Statementstmt 62.5% 70 / 112 Branchbran 43.7% 21 / 48 Conditioncond 46.1% 6 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 80.0% 8 / 10 Podpod 100% 4 / 4 totaltotal 56.9% 111 / 195 CC 32 SCAR 32.2
CC
32
Cov
56%
trace
50.1
error
15.8
Die
12.0
CRAP 24.9
↑ Summary
1 #!/usr/bin/perl
2 ################################################################################
3  
4 ############################################################################
5 # #
6 # Eureka Error System v1.1.7 #
7 # (C) 2020 OnEhIppY, Domero #
8 # ALL RIGHTS RESERVED #
9 # #
10 ############################################################################
11  
12 ################################################################################
13  
14 package gerr;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $VERSION = '1.1.7';
21 1 our @ISA = qw(Exporter);
22 1 our @EXPORT = qw(error Warn Die);
23 1 our @EXPORT_OK = qw(trace);
24  
25 ################################################################################
26  
27 1 ▶use utf8; # Enable UTF-8 support
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶sub error {
Subroutine error: called CC=13 SCAR=27.6
Pod documented
30 1 my @msg = @_;
31 1 my $return = 0;
32 1 my $type = "FATAL ERROR";
33 1 my $size = 80 - 2;
34 1 my $trace = 2;
35 1 my @lines;
36  
37 1 ▶ while (scalar(@msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @msg) 4 1 5
38 4 ▶ if (!defined $msg[0]) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $msg[0]) { } 0 4 4
elsif ($msg[0] =~ /^return=(.+)$/s) { } 1 3 4
elsif ($msg[0] =~ /^type=(.+)$/s) { } 1 2 3
elsif ($msg[0] =~ /^size=(.+)$/s) { } 0 2 2
elsif ($msg[0] =~ /^trace=(.+)$/s) { } 1 1 2
39 0 shift(@msg);
40 }
41 elsif ($msg[0] =~ /^return=(.+)$/s) {
42 1 $return = $1;
43 1 shift(@msg);
44 }
45 elsif ($msg[0] =~ /^type=(.+)$/s) {
46 1 $type = $1;
47 1 shift(@msg);
48 }
49 elsif ($msg[0] =~ /^size=(.+)$/s) {
50 0 $size = $1;
51 0 shift(@msg);
52 }
53 elsif ($msg[0] =~ /^trace=(.+)$/s) {
54 1 $trace = $1;
55 1 shift(@msg);
56 }
57 else {
58 1 push @lines, split(/\n/, shift(@msg));
59 }
60 }
61  
62 1 $type = " $type ";
63 1 my $tsize = length("$type");
64 1 push @lines, "";
65  
66 1 my $ls = ($size >> 1) - ($tsize >> 1);
67 1 my $rs = $size - ($size >> 1) - ($tsize >> 1) - 1;
68 1 my $tit = " " . ("#" x $ls) . $type . ("#" x $rs) . "\n";
69 1 my $str = "\n$tit\n";
70  
71 1 foreach my $line (@lines) {
72 2 ▶ while (length($line) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > 0) 1 2 3
73 1 $str .= " # ";
74 1 ▶ if (length($line) > $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > $size) { } 0 1 1
75 0 $str .= substr($line, 0, $size - 6) . "..." . " #\n";
76 0 $line = "..." . substr($line, $size - 6);
77 } else {
78 1 ▶ $str .= $line . (length(" " x ($size - length($line) - 3)) > 0 ? (" " x ($size - length($line) - 3)) : '') . " #\n";
Branch: 1 branch
BranchTrueFalseTotal
length ' ' x ($size - length($line) - 3) > 0 ? : 1 0 1
79 1 $line = "";
80 }
81 }
82 }
83  
84 1 $str .= trace($trace,$size); # Include stack trace if enabled
85  
86 # Only exit if not in an eval block
87 1 ▶ if (!$return && !$^S) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($return or $^S) 0 1 1
Condition33%1/3
$return or $^S
l!l&&r!l&&!r
100
MC/DC0%0/2
$return or $^S
$return $^S
Truth table33%1/3
A or B
A= $returnB= $^S
ABresult
1X1
011
000
88 0 $| = 1; # Autoflush STDERR
89 0 binmode STDERR, ":encoding(UTF-8)"; # Set UTF-8 encoding for STDERR
90 0 print STDERR $str;
91 0 exit 1;
92 }
93  
94 1 return $str;
95 }
96  
97 ################################################################################
98  
99 2 ▶sub trace {
Subroutine trace: called CC=15 SCAR=39.1
Pod documented
100 2 ▶ my $depth = $_[0] || 1;
Condition50%1/2
$_[0] || 1
l!l
20
MC/DC0%0/1
$_[0] || 1
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
101 2 ▶ my $size = $_[1] || 80-2;
Condition100%2/2
$_[1] || 78
l!l
11
MC/DC100%1/1
$_[1] || 78
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
102 2 my @out = ();
103  
104 2 ▶ while ($depth > 0 && $depth < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0 and $depth < 20) 2 2 4
Condition66%2/3
$depth > 0 and $depth < 20
!ll&&!rl&&r
202
MC/DC50%1/2
$depth > 0 and $depth < 20
$depth > 0 $depth < 20
Truth table66%2/3
A and B
A= $depth > 0B= $depth < 20
ABresult
0X0
100
111
105 2 my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller($depth);
106
107 2 ▶ if (!$package) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $package) { } 2 0 2
108 2 $depth = 0;
109 } else {
110 0 ▶ push @out, [$line, "$package($filename)", "Calling $subroutine" . ($hasargs ? "@DB::args" : ""), ($subroutine eq '(eval)' && $evaltext ? "[$evaltext]" : "")];
Branch: 2 branches
BranchTrueFalseTotal
$hasargs ? : 0 0 0
$subroutine eq '(eval)' && $evaltext ? : 0 0 0
Condition0%0/3
$subroutine eq "(eval)" and $evaltext
!ll&&!rl&&r
000
MC/DC0%0/2
$subroutine eq "(eval)" and $evaltext
$subroutine eq "(eval)" $evaltext
Truth table0%0/3
A and B
A= $subroutine eq "(eval)"B= $evaltext
ABresult
0X0
100
111
111 0 $depth++;
112 }
113 }
114  
115 2 @out = reverse @out;
116  
117 2 ▶ if (@out) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out) 0 2 2
118 0 for my $i (0 .. $#out) {
119 0 ▶ my $dept = "# " . (" " x $i) . ($i > 0 ? "`[" : "-[");
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 0 0 0
120 0 my ($ln, $pk, $cl, $ev) = @{$out[$i]};
121 0 my $ll = (60 - length($dept . $cl));
122 0 my $rr = (6 - length($ln));
123 0 ▶ $out[$i] = "$dept $cl" . (" " x ($ll > 0 ? $ll : 0)) . " at line: " . (" " x ($rr > 0 ? $rr : 0)) . "$ln : $pk" . ($ev ? "\n$ev" : "");
Branch: 3 branches
BranchTrueFalseTotal
$ll > 0 ? : 0 0 0
$rr > 0 ? : 0 0 0
$ev ? : 0 0 0
124 }
125 }
126  
127 2 my $type = " Trace Stack ";
128 2 my $tsize = length("$type");
129 2 my $ls = ($size >> 1) - ($tsize >> 1);
130 2 my $rs = $size - ($size >> 1) - ($tsize >> 1) - 1;
131 2 my $tit = " " . ("#" x $ls) . $type . ("#" x $rs) . "\n";
132 2 return "$tit\n".join("\n", @out)."\n" . ("#" x $size) . "\n";
133 }
134  
135 ################################################################################
136  
137 0 ▶sub Warn {
Subroutine Warn: not called CC=2 SCAR=17.9
Pod documented
138 0 my ($message) = @_;
139 0 my $file = (caller)[1];
140 0 my $line = (caller)[2];
141 0 my $formatted_message = error("$message at $file line $line.", "return=1", "type=Warning", "trace=3");
142 0 ▶ if (ref($SIG{__WARN__}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $SIG{'__WARN__'} eq 'CODE') { } 0 0 0
143 0 $SIG{__WARN__}->($formatted_message);
144 } else {
145 0 binmode STDERR, ":encoding(UTF-8)"; # Set UTF-8 encoding for STDERR
146 0 print STDERR $formatted_message;
147 }
148 0 return $formatted_message;
149 }
150  
151 ################################################################################
152  
153 0 ▶sub Die {
Subroutine Die: not called CC=3 SCAR=24.8
Pod documented
154 0 my ($message) = @_;
155 0 my $file = (caller)[1];
156 0 my $line = (caller)[2];
157 0 my $formatted_message = error("$message at $file line $line.", "return=1", "type=Fatal", "trace=3");
158 0 ▶ if (ref($SIG{__DIE__}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $SIG{'__DIE__'} eq 'CODE') { } 0 0 0
159 0 $SIG{__DIE__}->($formatted_message);
160 } else {
161 0 binmode STDERR, ":encoding(UTF-8)"; # Set UTF-8 encoding for STDERR
162 0 print STDERR $formatted_message;
163 }
164 0 ▶ exit 1 unless $^S; # Only exit if not in an eval block
Branch: 1 branch
BranchTrueFalseTotal
unless $^S 0 0 0
165 0 return $formatted_message;
166 }
167  
168 ################################################################################
169  
170 1 ▶sub import {
Subroutine import: called CC=3 SCAR=13.2
171 1 my ($class, @args) = @_;
172  
173 # Handle import arguments
174 0 ▶ if (grep { $_ eq ':control' } @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":control";} @args) 0 1 1
175 # Override global warn and die
176 1 ▶ no strict 'refs'; # Allow modifying symbolic references
Subroutine BEGIN: called CC=1 SCAR=0.0
177 0 *CORE::GLOBAL::warn = \&Warn;
178 0 *CORE::GLOBAL::die = \&Die;
179 }
180  
181 # Export default functions
182 1 $class->export_to_level(1, $class, @EXPORT);
183  
184 # Conditionally export functions based on import arguments
185 0 ▶ if (grep { $_ eq ':control' } @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":control";} @args) 0 1 1
186 0 $class->export_to_level(1, $class, @EXPORT_OK);
187 }
188 }
189  
190 1 1;
191  
192 ################################################################################
193 # EOF gerr.pm (C) 2020 Domero
↑ Summary