blib/lib/CGI/Application/Plugin/DebugScreen.pm

filter: Statementstmt 33.6% 37 / 110 Branchbran 8.8% 3 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 3 totaltotal 25.5% 49 / 192 CC 25 SCAR 39.9
CC
25
Cov
25%
print_context
132.0
debug_report
72.0
__ANON__
12.0
CRAP 53.8
↑ Summary
1 package CGI::Application::Plugin::DebugScreen;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.00';
12  
13 1 our $A_CODE = '<a class="package" href="?rm=view_code&amp;module=';
14 1 our $A_POD = '<a class="package" href="?rm=view_pod&amp;module=';
15 1 our $A_TAIL = '">';
16 1 our $A_END = '</a>';
17  
18 1 our $TEMPLATE = qq{
19 <html><!-- HTML::Template -->
20 <head>
21 <title>Error in <!-- TMPL_VAR NAME="title" --></title>
22 <style type="text/css">
23 body {
24 font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
25 Tahoma, Arial, helvetica, sans-serif;
26 color: #000;
27 background-color: #E68200;
28 margin: 0px;
29 padding: 0px;
30 }
31 :link, :link:hover, :visited, :visited:hover {
32 color: #000;
33 }
34 div.box {
35 position: relative;
36 background-color: #fff;
37 border: 1px solid #aaa;
38 padding: 4px;
39 margin: 10px;
40 -moz-border-radius: 10px;
41 }
42 div.infos {
43 background-color: #fff;
44 border: 3px solid #FFCC99;
45 padding: 8px;
46 margin: 4px;
47 margin-bottom: 10px;
48 -moz-border-radius: 10px;
49 overflow: auto;
50 }
51 div.infos td.head{
52 text-align: center;
53 }
54  
55 h1 {
56 margin: 0;
57 color: #999999;
58 }
59 h2 {
60 margin-top: 0;
61 margin-bottom: 10px;
62 font-size: medium;
63 font-weight: bold;
64 text-decoration: underline;
65 }
66 div.url {
67 font-size: x-small;
68 overflow: auto;
69 }
70 pre {
71 font-size: .8em;
72 line-height: 120%;
73 font-family: 'Courier New', Courier, monospace;
74 background-color: #FFCC99;
75 color: #333;
76 border: 1px dotted #000;
77 padding: 5px;
78 margin: 8px;
79 overflow: auto;
80 }
81 pre b {
82 font-weight: bold;
83 color: #000;
84 background-color: #E68200;
85 }
86 h1 a.package {
87 color: #999999;
88 }
89 </style>
90 </head>
91 <body>
92 <div class="box">
93 <h1><!-- TMPL_VAR NAME="title_a" --></h1>
94  
95 <div class="url"><!-- TMPL_VAR NAME="url" --></div>
96 <div class="infos">
97 <!-- TMPL_VAR NAME="desc" --><br />
98 </div>
99 <div class="infos">
100 <h2>StackTrace</h2>
101 <table>
102  
103 <tr>
104 <th>Package</th>
105 <th>Line </th>
106 <th>File </th>
107 <!-- TMPL_IF NAME="view" -->
108 <th>View </th>
109 <!-- /TMPL_IF -->
110 </tr>
111 <!-- TMPL_LOOP NAME="stacktrace" -->
112 <tr>
113 <td class="head"><!-- TMPL_VAR NAME="package" --></td>
114  
115 <td class="head"><!-- TMPL_VAR NAME="line" --></td>
116 <td class="head"><!-- TMPL_VAR NAME="filename" --></td>
117 <!-- TMPL_IF NAME="view" -->
118 <td class="head"><!-- TMPL_VAR NAME="code" --> <!-- TMPL_VAR NAME="pod" --></td>
119 <!-- /TMPL_IF -->
120 </tr>
121 <tr>
122 <td colspan="4"><pre><!-- TMPL_VAR NAME="code_preview" --></pre></td>
123 </tr>
124 <!-- /TMPL_LOOP -->
125 </table>
126 </div>
127 </div>
128 </body>
129 </html>
130 };
131  
132 1 ▶sub import {
Subroutine import: called CC=4 SCAR=22.6
133 1 my $self = shift;
134 1 my $caller = scalar caller;
135  
136 0 ▶ $caller->add_callback( 'init', sub{
Subroutine __ANON__: not called CC=1 SCAR=5.0
137 0 my $self = shift;
138  
139 0 ▶ $SIG{__DIE__} = sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 push @{$self->{__stacktrace}},[Devel::StackTrace->new(ignore_package=>[qw/CGI::Application::Plugin::DebugScreen Carp CGI::Carp/])->frames];
141 0 die @_; # rethrow
142 0 };
143 {
144 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
145 0 *{"$caller\::report"} = \&debug_report;
146  
147 0 ▶ *{"$caller\::__debugscreen_error"} = sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
148 0 my $self = shift;
149 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INC{"CGI/Application/Plugin/ViewCode.pm"} and not exists $INC{"CGI/Application/Dispatch.pm"}) 0 0 0
Condition0%0/3
exists $INC{"CGI/Application/Plugin/ViewCode.pm"} and not exists $INC{"CGI/Application/Dispatch.pm"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $INC{"CGI/Application/Plugin/ViewCode.pm"} and not exists $INC{"CGI/Application/Dispatch.pm"}
exists $INC{"CGI/Application/Plugin/ViewCode.pm"} not exists $INC{"CGI/Application/Dispatch.pm"}
Truth table0%0/3
A and not(B)
A= exists $INC{"CGI/Application/Plugin/ViewCode.pm"}B= not exists $INC{"CGI/Application/Dispatch.pm"}
ABresult
0X0
100
111
150 exists $INC{'CGI/Application/Plugin/ViewCode.pm'}
151 &&
152 ! exists $INC{'CGI/Application/Dispatch.pm'}
153 )
154 {
155 0 $self->{__viewcode}++;
156 }
157 0 $self->report(@_);
158 0 };
159 }
160 1 });
161  
162 0 ▶ $caller->add_callback( 'error', sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
163 0 my $self = shift;
164 0 ▶ if ( $ENV{CGI_APP_DEBUG} && exists $INC{'CGI/Application/Plugin/ViewCode.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CGI_APP_DEBUG"} and exists $INC{"CGI/Application/Plugin/ViewCode.pm"}) 0 0 0
Condition0%0/3
$ENV{"CGI_APP_DEBUG"} and exists $INC{"CGI/Application/Plugin/ViewCode.pm"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"CGI_APP_DEBUG"} and exists $INC{"CGI/Application/Plugin/ViewCode.pm"}
$ENV{"CGI_APP_DEBUG"} exists $INC{"CGI/Application/Plugin/ViewCode.pm"}
Truth table0%0/3
A and B
A= $ENV{"CGI_APP_DEBUG"}B= exists $INC{"CGI/Application/Plugin/ViewCode.pm"}
ABresult
0X0
100
111
165 0 $self->error_mode('__debugscreen_error');
166 }
167 1 });
168  
169 1 ▶ if ( ! exists $INC{'CGI/Application/Plugin/ViewCode.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $INC{"CGI/Application/Plugin/ViewCode.pm"}) 1 0 1
170 "CGI::Application::Plugin::ViewCode"->require
171 1 ▶ or delete $INC{'CGI/Application/Plugin/ViewCode.pm'};
Branch: 1 branch
BranchTrueFalseTotal
"CGI::Application::Plugin::ViewCode"->require or delete $INC{"CGI/Application/Plugin/ViewCode.pm"} 0 1 1
172 0 ▶ unless ($@) {goto &CGI::Application::Plugin::ViewCode::import}
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 1 1
173 }
174 }
175  
176 0 ▶sub debug_report{
Subroutine debug_report: not called CC=8 SCAR=42.8
Pod undocumented
177 0 my $self = shift;
178 0 my $desc = '' . shift; #stringify
179 #useful in case of exception from CGI::Application::Plugin::TT
180 0 my $url = $self->query->url(-path_info=>1,-query=>1);
181  
182 0 ▶ my $title = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
183  
184 0 $title = html_escape($title);
185 0 my $title_a = $title;
186  
187 0 ▶ if ( $self->{__viewcode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__viewcode"}) 0 0 0
188 0 $title_a = $A_CODE . $title . $A_TAIL . $title . $A_END;
189 }
190  
191 0 my $stacks = $self->{__stacktrace}[0];
192  
193 0 my @stacktraces;
194 0 for my $stack ( @{$stacks} ) {
195 0 my %s;
196 0 ▶ $s{package} = exists $stack->{pkg} ? $stack->{pkg} : $stack->{package};
Branch: 1 branch
BranchTrueFalseTotal
exists $stack->{'pkg'} ? : 0 0 0
197 0 ▶ $s{filename} = exists $stack->{file} ? $stack->{file} : $stack->{filename};
Branch: 1 branch
BranchTrueFalseTotal
exists $stack->{'file'} ? : 0 0 0
198  
199 0 $s{package} = html_escape($s{package});
200 0 $s{filename} = html_escape($s{filename});
201 0 $s{line} = html_escape($stack->{line});
202 0 $s{code_preview} = print_context($s{filename},$s{line},$s{package},$self->{__viewcode});
203  
204 0 ▶ if ( $self->{__viewcode} && $s{package} ne 'main' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__viewcode"} and $s{"package"} ne "main") 0 0 0
Condition0%0/3
$self->{"__viewcode"} and $s{"package"} ne "main"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"__viewcode"} and $s{"package"} ne "main"
$self->{"__viewcode"} $s{"package"} ne "main"
Truth table0%0/3
A and B
A= $self->{"__viewcode"}B= $s{"package"} ne "main"
ABresult
0X0
100
111
205 0 $s{line} = $A_CODE . $s{package} . '#'.$s{line} . $A_TAIL . $s{line} . $A_END;
206 0 $s{pod} = $A_POD . $s{package} . $A_TAIL . 'pod' . $A_END;
207 0 $s{code} = $A_CODE . $s{package} . $A_TAIL . 'code' . $A_END;
208 0 $s{filename} = $A_CODE . $s{package} . $A_TAIL . $s{filename} . $A_END;
209 0 $s{package} = $A_CODE . $s{package} . $A_TAIL . $s{package} . $A_END;
210  
211 0 $s{view} = $self->{__viewcode};
212 }
213 0 push @stacktraces, \%s;
214 }
215  
216 0 my $t = HTML::Template->new(
217 scalarref => \$TEMPLATE,
218 die_on_bad_params => 0,
219 );
220 $t->param(
221 title => $title,
222 title_a => $title_a,
223 view => $self->{__viewcode},
224 0 url => html_escape($url),
225 desc => html_escape($desc),
226 stacktrace => \@stacktraces,
227 );
228  
229 0 $self->header_props( -type => 'text/html' );
230 0 return $t->output;
231 }
232  
233 0 ▶sub print_context {
Subroutine print_context: not called CC=11 SCAR=48.8
Pod undocumented
234 0 my($file, $linenum, $package, $view) = @_;
235 0 my $code;
236 0 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 0 0
237 0 my $start = $linenum - 3;
238 0 my $end = $linenum + 3;
239 0 ▶ $start = $start < 1 ? 1 : $start;
Branch: 1 branch
BranchTrueFalseTotal
$start < 1 ? : 0 0 0
240 0 ▶ if (my $fh = IO::File->new($file, 'r')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = "IO::File"->new($file, "r")) 0 0 0
241 0 my $cur_line = 0;
242 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
243 0 ++$cur_line;
244 0 ▶ last if $cur_line > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_line > $end 0 0 0
245 0 ▶ next if $cur_line < $start;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_line < $start 0 0 0
246 0 ▶ my @tag = $cur_line == $linenum ? qw(<b> </b>) : ("","");
Branch: 1 branch
BranchTrueFalseTotal
$cur_line == $linenum ? : 0 0 0
247 0 ▶ if ( $view && $package ne 'main' && $cur_line == $linenum ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($view and $package ne 'main' and $cur_line == $linenum) { } 0 0 0
Condition0%0/6
$view and $package ne "main" and $cur_line == $linenum
!ll&&!rl&&r
000
$view and $package ne "main"
!ll&&!rl&&r
000
MC/DC0%0/3
$view and $package ne "main" and $cur_line == $linenum
$view $package ne "main" $cur_line == $linenum
Truth table0%0/4
A and B and C
A= $viewB= $package ne "main"C= $cur_line == $linenum
ABCresult
0XX0
10X0
1100
1111
248 0 my $t_line = $A_CODE.$package.'#'.$linenum.$A_TAIL;
249 0 $code .= sprintf(
250 '%s%s%5d: %s%s%s',
251 $tag[0], $t_line, $cur_line, html_escape($line), $A_END, $tag[1],
252 );
253 }
254 else {
255 0 $code .= sprintf(
256 '%s%5d: %s%s',
257 $tag[0], $cur_line, html_escape($line), $tag[1],
258 );
259 }
260 }
261 }
262 }
263 0 return $code;
264 }
265  
266 0 ▶sub html_escape {
Subroutine html_escape: not called CC=1 SCAR=6.9
Pod undocumented
267 0 my $str = shift;
268 0 $str =~ s/&/&amp;/g;
269 0 $str =~ s/</&lt;/g;
270 0 $str =~ s/>/&gt;/g;
271 0 $str =~ s/"/&quot;/g;
272 0 return $str;
273 }
274  
275 1 1;
276  
277 =head1 NAME
278  
279 CGI::Application::Plugin::DebugScreen - add Debug support to CGI::Application.
280  
281 =head1 VERSION
282  
283 This documentation refers to CGI::Application::Plugin::DebugScreen version 0.06
284  
285 =head1 SYNOPSIS
286  
287 use CGI::Application::Plugin::DebugScreen;
288  
289 Only it.
290 If "Internal Server Error" was generated by "run_mode"....
291  
292 =head1 DESCRIPTION
293  
294 This plug-in add Debug support to CGI::Application.
295 This plug-in like Catalyst debug mode.
296  
297 DebugScreen is done when B<$ENV{CGI_APP_DEBUG}> is set,
298 and DebugScreen is not done when not setting it.
299 When your code is released, this plug-in need not be removed.
300  
301 When 'die' is generated by 'run_mode',
302 this plug-in outputs the stack trace by error_mode().
303 As for this plug-in, error_mode() is overwrited in error callback.
304 The error cannot be caught excluding run_mode.
305  
306 This uses CGI::Application::Plugin::ViewCode
307 if a state that CGI::Application::Plugin::ViewCode can be used or used.
308 But CGI::Application::Dispatch is used,
309 this not uses CGI::Application::Plugin::ViewCode.
310  
311 When CGI::Application::Plugin::ViewCode can be used,
312 Title, Package, File, code and line are links to CGI::Application::Plugin::ViewCode's view_code mode.
313 line jumps to the specified line.
314 And pod are links to CGI::Application::Plugin::ViewCode's view_pod mode.
315 The code of the displayed is links to CGI::Application::Plugin::ViewCode's view_code mode.
316  
317 =head1 DEPENDENCIES
318  
319 L<strict>
320  
321 L<warnings>
322  
323 L<CGI::Application>
324  
325 L<HTML::Template>
326  
327 L<Devel::StackTrace>
328  
329 L<IO::File>
330  
331 L<CGI::Application::Plugin::ViewCode>
332  
333 L<UNIVERSAL::require>
334  
335 =head1 BUGS AND LIMITATIONS
336  
337 There are no known bugs in this module.
338 Please report problems to Atsushi Kobayashi (E<lt>nekokak@cpan.orgE<gt>)
339 Patches are welcome.
340  
341 =head1 SEE ALSO
342  
343 L<CGI::Application::Plugin::ViewCode>
344  
345 L<Sledge::Plugin::DebugScreen>
346  
347 L<CGI::Carp::DebugScreen>
348  
349 L<Catalyst::Plugin::StackTrace>
350  
351 =head1 Thanks To
352  
353 MATSUNO Tokuhiro (MATSUNO)
354  
355 Koichi Taniguchi (TANIGUCHI)
356  
357 Masahiro Nagano (KAZEBURO)
358  
359 Tomoyuki Misonou
360  
361 =head1 AUTHOR
362  
363 Atsushi Kobayashi, E<lt>nekokak@cpan.orgE<gt>
364  
365 =head1 COPYRIGHT AND LICENSE
366  
367 Copyright (C) 2006 by Atsushi Kobayashi (E<lt>nekokak@cpan.orgE<gt>). All rights reserved.
368  
369 This library is free software; you can redistribute it and/or modify it
370 under the same terms as Perl itself. See L<perlartistic>.
371  
372 =cut
↑ Summary