blib/lib/Perlilog/PLerrsys.pm untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 272 CC 41 SCAR 62.4
CC
41
Cov
0%
stackdump
756.0
constdump
132.0
oneplace
30.0
CRAP 512.0
1 #
2 # This file is part of the Perlilog project.
3 #
4 # Copyright (C) 2003, Eli Billauer
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #
20 # A copy of the license can be found in a file named "licence.txt", at the
21 # root directory of this project.
22 #
23  
24 package Perlilog::PLerror;
25  
26 $CarpLevel = 0; # How many extra package levels to skip on carp.
27 $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
28 $MaxArgLen = 64; # How much of each argument to print. 0 = all.
29 $MaxArgNums = 8; # How many arguments to print. 0 = all.
30 $Verbose = 0; # If true then make shortmess call longmess instead
31 $DumpLevel = 0;
32  
33 sub oneplace {
34 my $i = 1;
35 my $errcr;
36 my ($package, $filename, $line);
37 while (1) {
38 ($package, $filename, $line) = caller($i++);
39 last unless ($package);
40 $errcr = ${"$package"."::errorcrawl"} || "";
41 next if ($errcr eq 'skip');
42 next if ($errcr eq 'system');
43 last;
44 }
45 return "at $filename line $line" if ($package);
46 return "";
47 }
48  
49 sub stackdump {
50 return @_ if ref $_[0];
51 my $error = join '', @_;
52 my $mess;
53 my @messlist = ();
54 my $i = 1 + $DumpLevel;
55 my ($pack,$file,$line,$sub,$hargs,$junk,$eval,$require);
56 my (@a);
57 my (@b);
58 my ($xsub, $xpack, $name, $errcr);
59 #
60 # crawl up the stack....
61 #
62 CRAWL: while (do { { package DB; @a = caller($i++) } } ) {
63 $mess= "";
64 # get copies of the variables returned from caller()
65 ($pack,$file,$line,$sub,$hargs,$junk,$eval,$require) = @a;
66 $errcr = ${"$pack"."::errorcrawl"} || "";
67 next CRAWL if ($errcr eq 'skip');
68 last CRAWL if ($errcr eq 'halt');
69 @a=();
70 @a = @DB::args if $hargs; # must get local copy of args
71  
72 #
73 # if the $error error string is newline terminated then it
74 # is copied into $mess. Otherwise, $mess gets set (at the end of
75 # the 'else' section below) to one of two things. The first time
76 # through, it is set to the "$error at $file line $line" message.
77 # $error is then set to 'called' which triggers subsequent loop
78 # iterations to append $sub to $mess before appending the "$error
79 # at $file line $line" which now actually reads "called at $file line
80 # $line". Thus, the stack trace message is constructed:
81 #
82 # first time: $mess = $error at $file line $line
83 # subsequent times: $mess .= $sub $error at $file line $line
84 # ^^^^^^
85 # "called"
86 if ($error =~ m/\n$/) {
87 $mess .= $error;
88 } else {
89 # Build a string, $sub, which names the sub-routine called.
90 # This may also be "require ...", "eval '...' or "eval {...}"
91 if (defined $eval) {
92 if ($require) {
93 $sub = "require $eval";
94 } else {
95 $eval =~ s/([\\\'])/\\$1/g;
96 if ($MaxEvalLen && length($eval) > $MaxEvalLen) {
97 substr($eval,$MaxEvalLen) = '...';
98 }
99 $sub = "eval '$eval'";
100 }
101 } elsif ($sub eq '(eval)') {
102 $sub = 'eval {...}';
103 } else {
104  
105 # Now we attempt to handle autoloads gracefully. The idea is to
106 # steal the subroutine name from the function that the autoloader
107 # calls, hence one step shallower in the call stack ($i-2 because
108 # $i was incremented before).
109  
110 if ($sub eq 'UNIVERSAL::AUTOLOAD') {
111 do { { package DB; @b = caller($i-2) } };
112 ($sub) = $b[3] =~ /.*::(.*)/;
113 }
114  
115 # Now we try to substitute the classic Foo::Bar with something
116 # more useful. Hopefully, we'll resolve the object's name.
117  
118 ($xpack, $xsub) = ($sub =~ /(.*)::(.*)/);
119 if ((defined $xpack) && (exists $packhash{$xpack})) { # Is the package autoloaded?
120 if (($Perlilog::classes{ref($a[0])}) # Paranoid check before...
121 && ($name = $a[0]->who)) { # calling a method
122 $sub = $name."->".$xsub;
123 shift @a; # Don't show ugly object references
124 } else {
125 $sub = "(".$packhash{$xpack}.")->".$xsub;
126 }
127 }
128 if ((defined $xsub) && ($xsub eq '__ANON__')) {
129 $sub = "CALLBACK ";
130 }
131 }
132 # if there are any arguments in the sub-routine call, format
133 # them according to the format variables defined earlier in
134 # this file and join them onto the $sub sub-routine string
135 if ($hargs) {
136 # we may trash some of the args so we take a copy
137 # don't print any more than $MaxArgNums
138 if ($MaxArgNums and @a > $MaxArgNums) {
139 # cap the length of $#a and set the last element to '...'
140 $#a = $MaxArgNums;
141 $a[$#a] = "...";
142 }
143 for (@a) {
144 # set args to the string "undef" if undefined
145 $_ = "undef", next unless defined $_;
146 if (ref $_) {
147 if ($Perlilog::classes{ref($_)}) { # Is this a known object?
148 $_=$_->who; # Get the object's pretty ID
149 next;
150 }
151 # force reference to string representation
152 $_ .= '';
153 s/'/\\'/g;
154 }
155 else {
156 s/'/\\'/g;
157 # terminate the string early with '...' if too long
158 substr($_,$MaxArgLen) = '...'
159 if $MaxArgLen and $MaxArgLen < length;
160 }
161 # 'quote' arg unless it looks like a number
162 $_ = "'$_'" unless /^-?[\d.]+$/;
163 # print high-end chars as 'M-<char>'
164 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
165 # print remaining control chars as ^<char>
166 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
167 }
168 # append ('all', 'the', 'arguments') to the $sub string
169 $sub .= '(' . join(', ', @a) . ')';
170 }
171 # here's where the error message, $mess, gets constructed
172  
173 $mess .= "$sub " if $error eq "called";
174 if ($errcr eq 'system') {
175 $mess .= "by System";
176 } else {
177 $mess .= "$error at $file line $line";
178 }
179 if (defined &Thread::tid) {
180 my $tid = Thread->self->tid;
181 $mess .= " thread $tid" if $tid;
182 }
183 $mess .= "\n";
184 }
185 # we don't need to print the actual error message again so we can
186 # change this to "called" so that the string "$error at $file line
187 # $line" makes sense as "called at $file line $line".
188 $error = "called";
189 push @messlist, $mess;
190 }
191 return ("", $error) unless @messlist;
192 $mess="";
193  
194 $error = shift @messlist;
195 $mess = "Calling chain:\n".join('',reverse @messlist)
196 if (@messlist);
197 return ($mess, $error);
198 }
199  
200 # constdump is a special error reporting subroutine to be used by const only.
201 # The aim: To clarify exactly what happened if a chain of magic callsbacks
202 # eventually cause an inconsistency in const values.
203  
204 sub constdump {
205 my $mess;
206 my @messlist = ();
207 my $i = 1 + $DumpLevel;
208 my ($pack,$file,$line,$sub,$hargs,$junk,$eval,$require);
209 my (@a);
210 my (@b);
211 my ($xsub, $xpack, $name, $xat);
212 #
213 # crawl up the stack....
214 #
215 CRAWL: while (do { { package DB; @a = caller($i++) } } ) {
216 # get copies of the variables returned from caller()
217 ($pack,$file,$line,$sub,$hargs,$junk,$eval,$require) = @a;
218 @a=();
219 @a = @DB::args if $hargs; # must get local copy of args
220  
221 # This call is only interesting if it was a call to const. If it was,
222 # we want to know what object got it.
223  
224 ($xpack, $xsub) = ($sub =~ /(.*)::(.*)/);
225 next CRAWL unless ($xsub eq 'const');
226 next CRAWL unless ((exists $packhash{$xpack}) && # Is this a "good" object?
227 ($Perlilog::classes{ref($a[0])}));
228 next CRAWL unless ($name = $a[0]->who); # It better have a name!
229 $sub = $name."->".$xsub;
230 $xat = "at $file line $line";
231 shift @a; # Don't show ugly object references
232  
233 # Now we fetch the argument. There is only one, so we set $_ and go on.
234  
235 foreach (@a[0..1]) {
236 $_ = "undef", next unless defined $_;
237 if (ref $_) {
238 if ($Perlilog::classes{ref($_)}) { # Is this a known object?
239 $_=$_->who; # Get the object's pretty ID
240 next;
241 }
242 # force reference to string representation
243 $_ .= '';
244 s/'/\\'/g;
245 }
246 else {
247 s/'/\\'/g;
248 # terminate the string early with '...' if too long
249 substr($_,$MaxArgLen) = '...'
250 if $MaxArgLen and $MaxArgLen < length;
251 }
252 # 'quote' arg unless it looks like a number
253 $_ = "'$_'" unless /^-?[\d.]+$/;
254 # print high-end chars as 'M-<char>'
255 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
256 # print remaining control chars as ^<char>
257 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
258 }
259  
260 my ($prop, $val) = @a;
261 $mess = "Property $prop = $val on $sub\n";
262 push @messlist, $mess;
263 }
264 # If we only caught one const, there was no callback chain. No hints.
265 return ($xat, "") if ($#messlist < 1);
266  
267 $mess = "\nHint: This is probably due to a chain of \"magic\" property settings\n";
268 $mess .= "in the following sequence:\n";
269 $mess .= join('',reverse @messlist)."\n";
270 return ($xat, $mess);
271 }
272  
273  
274 1;