DebugScreen.pm

filter: Statementstmt 71.1% 101 / 142 Branchbran 58.0% 29 / 50 Conditioncond 38.1% 8 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 60.0% 15 / 25 Podpod 100% 9 / 9 totaltotal 62.5% 162 / 259 CC 37 SCAR 22.4
CC
37
Cov
65%
_output
42.0
_get_stacktraces
14.0
_get_context
7.4
CRAP 9.4
« DebugScreen/Dumper.pm ↑ Summary DebugScreen/DefaultView.pm »
1 package CGI::Carp::DebugScreen;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use CGI::Carp qw/fatalsToBrowser/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 2 our $VERSION = '0.16';
9
10 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.1
11 2 my $MyDebug = 0;
12 CGI::Carp::set_message(
13 0 ▶ sub { __PACKAGE__->_output(@_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 2 ▶ ) unless $MyDebug;
Branch: 1 branch
BranchTrueFalseTotal
unless $MyDebug 2 0 2
15 }
16
17 2 $Carp::Verbose = 1; # for stacktraces
18
19 20 ▶sub _default_stylesheet {
Subroutine _default_stylesheet: called CC=1 SCAR=0.0
20 20 return <<'EOS';
21 <style type="text/css">
22 <!--
23 body {
24 font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
25 Tahoma, Arial, helvetica, sans-serif;
26 color: #000;
27 background-color: #f60;
28 margin: 0px;
29 padding: 0px;
30 }
31 :link, :link:hover, :visited, :visited:hover {
32 color: #333;
33 }
34 div#page {
35 position: relative;
36 background-color: #fff;
37 border: 1px solid #600;
38 padding: 10px;
39 margin: 10px;
40 -moz-border-radius: 10px;
41 }
42 div.navi {
43 color: #333;
44 padding: 0 4px;
45 }
46 div.box {
47 background-color: #fff;
48 border: 3px solid #fc9;
49 padding: 8px;
50 margin: 4px;
51 margin-bottom: 10px;
52 -moz-border-radius: 10px;
53 }
54 h1 {
55 margin: 0;
56 color: #666;
57 }
58 h2 {
59 margin-top: 0;
60 margin-bottom: 10px;
61 font-size: medium;
62 font-weight: bold;
63 text-decoration: underline;
64 }
65 table.code {
66 font-size: .8em;
67 line-height: 120%;
68 font-family: 'Courier New', Courier, monospace;
69 background-color: #fc9;
70 color: #333;
71 border: 1px dotted #600;
72 margin: 8px;
73 width: 90%;
74 border-collapse: collapse;
75 }
76 table.code tr.hit {
77 font-weight: bold;
78 color: #000;
79 background-color: #f90;
80 }
81 table.code td {
82 padding-left: 1em;
83 line-height: 130%;
84 }
85 table.code td.num {
86 width: 4em;
87 text-align:right
88 }
89 table.watch {
90 line-height: 120%;
91 }
92 table.watch th {
93 font-weight: normal;
94 color: #000;
95 background-color: #fc9;
96 padding: 0 1em;
97 }
98 table.watch td {
99 line-height: 130%;
100 padding: 2px;
101 }
102 div.scrollable {
103 font-size: .8em;
104 overflow: auto;
105 margin-left: 1em;
106 }
107 pre.raw_error {
108 background-color: #fff;
109 border: 3px solid #fc9;
110 padding: 8px;
111 margin: 4px;
112 margin-bottom: 10px;
113 -moz-border-radius: 10px;
114 font-size: .8em;
115 line-height: 120%;
116 font-family: 'Courier New', Courier, monospace;
117 overflow: auto;
118 }
119 ul#stacktraces, ul#traces, ul#modules ul#watch {
120 margin: 1em 1em;
121 padding: 0 1em;
122 }
123 table#environment {
124 margin: 0 1em;
125 }
126 p.footer {
127 margin: 0 1em;
128 font-size: .8em;
129 text-align:right;
130 }
131 -->
132 </style>
133 EOS
134 }
135
136 2 my %Options;
137 2 my %Mapping = (
138 debug => qr/^d(?:ebug)?$/,
139 engine => qr/^e(?:ngine)?$/,
140 show_lines => qr/^l(?:ines)?$/,
141 show_mod => qr/^m(?:od(?:ules)?)?$/,
142 show_env => qr/^env(?:ironment)?$/,
143 show_raw_error => qr/^raw(?:_error)?$/,
144 ignore_overload => qr/^(?:ignore_)?overload$/,
145 debug_template => qr/^d(?:ebug_)?t(?:emplate)?$/,
146 error_template => qr/^e(?:rror_)?t(?:emplate)?$/,
147 style => qr/^s(?:tyle)?$/,
148 );
149
150 20 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
151 20 my ($class, %options) = @_;
152
153 20 %Options = (
154 debug => 1,
155 engine => 'DefaultView',
156 show_lines => 3,
157 show_mod => 0,
158 show_env => 0,
159 show_raw_error => 0,
160 ignore_overload => 0,
161 debug_template => '',
162 error_template => '',
163 style => _default_stylesheet(),
164 watchlist => {},
165 );
166
167 20 ▶ while(my ($key, $value) = each %options) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 39 20 59
168 39 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 39 39
169 39 foreach my $canonkey ( keys %Mapping ) {
170 249 ▶ if ( $key =~ $Mapping{$canonkey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /$Mapping{$canonkey}/) 39 210 249
171 39 $Options{$canonkey} = $value;
172 39 last;
173 }
174 }
175 }
176 }
177
178 0 ▶sub debug { shift; $Options{debug} = shift; }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
179 0 ▶sub set_debug_template { shift; $Options{debug_template} = shift; }
Subroutine set_debug_template: not called CC=1 SCAR=6.9
Pod documented
180 0 ▶sub set_error_template { shift; $Options{error_template} = shift; }
Subroutine set_error_template: not called CC=1 SCAR=6.9
Pod documented
181 0 ▶sub set_style { shift; $Options{style} = shift; }
Subroutine set_style: not called CC=1 SCAR=6.9
Pod documented
182 0 ▶sub show_modules { shift; $Options{show_mod} = shift; }
Subroutine show_modules: not called CC=1 SCAR=6.9
Pod documented
183 0 ▶sub show_environment { shift; $Options{show_env} = shift; }
Subroutine show_environment: not called CC=1 SCAR=6.9
Pod documented
184 0 ▶sub show_raw_error { shift; $Options{show_raw_error} = shift; }
Subroutine show_raw_error: not called CC=1 SCAR=6.9
Pod documented
185 0 ▶sub ignore_overload { shift; $Options{ignore_overload} = shift; }
Subroutine ignore_overload: not called CC=1 SCAR=6.9
Pod documented
186
187 6 ▶sub add_watchlist {
Subroutine add_watchlist: called CC=2 SCAR=6.9
Pod documented
188 6 my ($class, %hash) = @_;
189 6 foreach my $key (keys %hash) {
190 6 $Options{watchlist}->{$key} = $hash{$key};
191 }
192 }
193
194 18 ▶sub _get_stacktraces {
Subroutine _get_stacktraces: called CC=11 SCAR=26.4
195 18 my ($class, $raw_error) = @_;
196
197 18 my $first_message = '';
198 18 my $no_more_first;
199
200 my @stacktraces = grep {
201 54 ▶ my $caller = $_->{caller} || '';
Condition50%1/2
$_->{'caller'} || ''
l!l
540
MC/DC0%0/1
$_->{'caller'} || ''
$_->{'caller'}
Truth table50%1/2
A
A= $_->{'caller'}
Aresult
11
00
202 (
203 $caller eq '' or # ignore undefined caller;
204 $caller eq $INC{'Carp.pm'} or # ignore Carp;
205 54 ▶ $caller eq $INC{'CGI/Carp.pm'} # ignore CGI::Carp;
Branch: 1 branch
BranchTrueFalseTotal
$caller eq '' || $caller eq $INC{'Carp.pm'} || $caller eq $INC{'CGI/Carp.pm'} ? : 0 54 54
Condition33%2/6
$caller eq "" or $caller eq $INC{"Carp.pm"} or $caller eq $INC{"CGI/Carp.pm"}
l!l&&r!l&&!r
0054
$caller eq "" or $caller eq $INC{"Carp.pm"}
l!l&&r!l&&!r
0054
MC/DC0%0/3
$caller eq "" or $caller eq $INC{"Carp.pm"} or $caller eq $INC{"CGI/Carp.pm"}
$caller eq "" $caller eq $INC{"Carp.pm"} $caller eq $INC{"CGI/Carp.pm"}
Truth table25%1/4
A or B or C
A= $caller eq ""B= $caller eq $INC{"Carp.pm"}C= $caller eq $INC{"CGI/Carp.pm"}
ABCresult
1XX1
01X1
0011
0000
206 ) ? 0 : 1;
207 }
208 map {
209 18 my $line = $_;
210 54 my ($message, $caller, $line_no) = $line =~ /^(?:\s*)(.*?)(?: called)? at (\S+) line (.+)$/;
211 54 ▶ $first_message .= "$line<br>" if !defined $message && !$no_more_first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message or $no_more_first 0 54 54
Condition33%1/3
defined $message or $no_more_first
l!l&&r!l&&!r
5400
MC/DC0%0/2
defined $message or $no_more_first
defined $message $no_more_first
Truth table33%1/3
A or B
A= defined $messageB= $no_more_first
ABresult
1X1
011
000
212 54 ▶ $no_more_first = 1 if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 54 0 54
213 54 ▶ $first_message = $message unless $first_message;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_message 18 36 54
214 54 ▶ $caller ||= '';
Condition50%1/2
$caller ||= ""
l!l
540
MC/DC0%0/1
$caller ||= ""
$caller
Truth table50%1/2
A
A= $caller
Aresult
11
00
215 54 ▶ $line_no ||= 0;
Condition50%1/2
$line_no ||= 0
l!l
540
MC/DC0%0/1
$line_no ||= 0
$line_no
Truth table50%1/2
A
A= $line_no
Aresult
11
00
216 54 my $context = $class->_get_context($caller, $line_no);
217 +{
218 54 message => $message,
219 caller => $caller,
220 line => $line_no,
221 context => $context,
222
223 # XXX: will be deprecated next time
224 contents => $context,
225 };
226 } split(/\n/, $raw_error);
227
228 18 my $error_at = $stacktraces[$#stacktraces]->{caller};
229 18 my $error_message = $first_message.' at '.$stacktraces[0]->{caller}.' line '.$stacktraces[0]->{line};
230
231 18 return ( $error_at, $error_message, @stacktraces );
232 }
233
234 54 ▶sub _get_context {
Subroutine _get_context: called CC=7 SCAR=20.1
235 54 my ($class, $file, $line_no) = @_;
236
237 54 ▶ return unless $file && -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -f $file 0 54 54
Condition33%1/3
$file and -f $file
!ll&&!rl&&r
0054
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table33%1/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
238
239 54 my @context;
240 54 ▶ if (open my $fh, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $file) 54 0 54
241 54 my $ct = 0;
242 54 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 4680 0 4680
243 4680 $ct++;
244 4680 ▶ next if $ct < $line_no - $Options{show_lines};
Branch: 1 branch
BranchTrueFalseTotal
if $ct < $line_no - $Options{"show_lines"} 4248 432 4680
245 432 ▶ last if $ct > $line_no + $Options{show_lines};
Branch: 1 branch
BranchTrueFalseTotal
if $ct > $line_no + $Options{"show_lines"} 54 378 432
246 378 push @context, {
247 no => $ct,
248 line => $line,
249 hit => ($ct == $line_no),
250 };
251 }
252 }
253 54 \@context;
254 }
255
256 18 ▶sub _get_modules {
Subroutine _get_modules: called CC=2 SCAR=6.9
257 18 my ($class, $flag) = @_;
258
259 18 ▶ return unless $flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag 12 6 18
260
261 return map {
262 941 my $key = $_;
263 941 (my $package = $key) =~ s|/|::|g;
264 +{
265 package => $package,
266 941 file => $INC{$key},
267 }
268 6 } sort {$a cmp $b} keys %INC;
269 }
270
271 18 ▶sub _get_env {
Subroutine _get_env: called CC=2 SCAR=6.9
272 18 my ($class, $flag) = @_;
273
274 18 ▶ return unless $flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag 12 6 18
275
276 return map {
277 +{
278 key => $_,
279 204 value => $ENV{$_},
280 }
281 6 } sort {$a cmp $b} keys %ENV;
282 }
283
284 18 ▶sub _get_watchlist {
Subroutine _get_watchlist: called CC=3 SCAR=11.0
285 18 my ($class, $href, $overload) = @_;
286
287 18 my @list;
288 18 ▶ if (%{ $href }) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$href;}) 6 12 18
289 6 require CGI::Carp::DebugScreen::Dumper;
290 6 CGI::Carp::DebugScreen::Dumper->ignore_overload($overload);
291 0 foreach my $key (sort {$a cmp $b} keys %{ $href }) {
292 6 my $dump = CGI::Carp::DebugScreen::Dumper->dump($href->{$key});
293 6 push @list, {
294 key => $key,
295 value => $dump,
296
297 # XXX: will be deprecated next time
298 table => $dump,
299 };
300 }
301 }
302 18 return @list;
303 }
304
305 18 ▶sub _load_view {
Subroutine _load_view: called CC=6 SCAR=19.3
306 18 my ($class, $engine) = @_;
307
308 18 my ($view_class, $view);
309 18 ▶ if ( ref $engine && $engine->can('as_html') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $engine and $engine->can('as_html')) { } 0 18 18
Condition33%1/3
ref $engine and $engine->can("as_html")
!ll&&!rl&&r
1800
MC/DC0%0/2
ref $engine and $engine->can("as_html")
ref $engine $engine->can("as_html")
Truth table33%1/3
A and B
A= ref $engineB= $engine->can("as_html")
ABresult
0X0
100
111
310 0 $view_class = ref $engine;
311 0 $view = $engine;
312 }
313 else {
314 # engine alias
315 18 ▶ $engine = 'TT' if lc $engine eq 'template';
Branch: 1 branch
BranchTrueFalseTotal
if lc $engine eq "template" 0 18 18
316
317 18 ▶ $view_class = ( $engine =~ s/^\+// ) ? $engine : __PACKAGE__.'::'.$engine;
Branch: 1 branch
BranchTrueFalseTotal
$engine =~ s/^\+// ? : 0 18 18
318
319 18 eval "require $view_class";
320 18 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 12 18
321 6 require CGI::Carp::DebugScreen::DefaultView;
322 6 $view_class = 'CGI::Carp::DebugScreen::DefaultView';
323 }
324 18 $view = $view_class;
325 }
326 18 return ( $view_class, $view );
327 }
328
329 18 ▶sub _render {
Subroutine _render: called CC=1 SCAR=0.0
330 18 my ($class, $raw_error) = @_;
331
332 18 my ($error_at, $error_message, @stacktraces) = $class->_get_stacktraces($raw_error);
333
334 18 my @modules = $class->_get_modules($Options{show_mod});
335 18 my @environment = $class->_get_env($Options{show_env});
336 my @watchlist = $class->_get_watchlist(
337 $Options{watchlist},
338 $Options{ignore_overload},
339 18 );
340
341 18 my ($view_class, $view) = $class->_load_view($Options{engine});
342
343 return $view->as_html(
344 version => $VERSION,
345 debug => $Options{debug},
346 debug_template => $Options{debug_template},
347 error_template => $Options{error_template},
348 view => $view_class,
349 style => $Options{style},
350 error_at => $error_at,
351 error_message => $error_message,
352 raw_error => $raw_error,
353 show_raw_error => $Options{show_raw_error},
354 stacktraces => \@stacktraces,
355 modules => \@modules,
356 environment => \@environment,
357 watchlist => \@watchlist,
358
359 # XXX: will be deprecated next time
360 debug_tmpl => $Options{debug_template},
361 error_tmpl => $Options{error_template},
362 18 traces => \@stacktraces,
363 );
364 }
365
366 0 ▶sub _output {
Subroutine _output: not called CC=6 SCAR=37.4
367 0 my ($class, $raw_error) = @_;
368
369 0 my $html = $class->_render($raw_error);
370
371 # shamelessly stolen from CGI::Carp
372
373 0 ▶ if (exists $ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'MOD_PERL'}) { } 0 0 0
374 0 my $r;
375 my $mod_perl;
376 0 ▶ if ($ENV{MOD_PERL_API_VERSION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'MOD_PERL_API_VERSION'}) { } 0 0 0
377 0 $mod_perl = 2;
378 0 require Apache2::RequestRec;
379 0 require Apache2::RequestIO;
380 0 require Apache2::RequestUtil;
381 0 require APR::Pool;
382 0 require ModPerl::Util;
383 0 require Apache2::Response;
384 0 $r = Apache2::RequestUtil->request;
385 }
386 else {
387 0 $r = Apache->request;
388 }
389 0 ▶ if ($r->bytes_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bytes_sent) { } 0 0 0
390 0 $r->print($html);
391 0 ▶ $mod_perl == 2 ? ModPerl::Util::exit(0) : $r->exit;
Branch: 1 branch
BranchTrueFalseTotal
$mod_perl == 2 ? : 0 0 0
392 }
393 else {
394 0 ▶ if ($ENV{HTTP_USER_AGENT} =~ /MSIE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_USER_AGENT"} =~ /MSIE/) 0 0 0
395 0 $html = "<!-- " . (' ' x 513) . " -->\n$html";
396 }
397 0 $r->custom_response(500, $html);
398 }
399 }
400 else {
401 0 print $html;
402 }
403 }
404
405 2 1;
406
407 __END__
« DebugScreen/Dumper.pm ↑ Summary DebugScreen/DefaultView.pm »