Log.pm

filter: Statementstmt 100% 123 / 123 Branchbran 93.3% 28 / 30 Conditioncond 100% 8 / 9 (1 uncoverable) MC/DCmcdc 100% 3 / 5 (2 uncoverable) Subroutinesub 100% 21 / 21 Podpod 91.6% 11 / 12 totaltotal 98.5% 194 / 200 (3 uncoverable) CC 24 SCAR 12.3
CC
24
Cov
99%
failures
8.0
fh
4.0
_write_to_filehandles
4.0
CRAP 3.4
↑ Summary Validator.pm »
1 package HTTP::OAIPMH::Log;
2  
3 =head1 NAME
4  
5 HTTP::OAIPMH::Log - Log of validation results
6  
7 =head1 SYNOPSIS
8  
9 Validation logging for L<HTTP::OAIPMH::Validator>. Stores log of information
10 as an array of entries in $obj->log, where each entry is itself an array
11 where the first element is the type (indicated by a string) and then additional
12 information.
13  
14 Also supports output of a text summary (markdown) and/or JSON data
15 during operation if the $obj->filehandles array is set to include one
16 or more filehandle and types for output.
17  
18 Example use:
19  
20 my $log = HTTP::OAIPMH::Log->new;
21 $log->fh(\*STDOUT);
22 $log->start("First test");
23 ...
24 $log->note("Got some data");
25 ...
26 if ($good) {
27 $log->pass("It was good, excellent");
28 } else {
29 $log->fail("Should have been good but wasn't");
30 }
31  
32 =cut
33  
34 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use CGI qw(escapeHTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 HTTP::OAIPMH::Log->mk_accessors( qw(log filehandles num_pass num_fail num_warn) );
39  
40 =head2 METHODS
41  
42 =head3 new(%args)
43  
44 Create new HTTP::OAIPMH::Log and optionally set values for any of the
45 attributes. All attributes also have accessors provided via
46 L<Class::Accessor::Fast>:
47  
48 log - internal data structure for log messages (array of arrays)
49 fh - set to a filehandle to write log messages as logging is done
50 num_pass - number of pass messages
51 num_fail - number of fail messages
52 num_warn - number of warn messages
53  
54 =cut
55  
56 9 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
57 9 my $this=shift;
58 # uncoverable condition false
59 9 ▶ my $class=ref($this) || $this;
Condition100%2/3
ref $this || $this
l!l&&r!l&&!r
180
MC/DC100%0/2
ref $this || $this
ref $this $this
Truth table100%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
60 9 my $self={'log'=>[],
61 'filehandles'=>[],
62 'num_pass'=>0,
63 'num_fail'=>0,
64 'num_warn'=>0,
65 @_};
66 9 bless($self, $class);
67 9 return($self);
68 }
69  
70  
71 =head3 fh(@fhspecs)
72  
73 Set the list of filehandle specs that will be written to, clearing
74 any that already exist. Each entry in the @fhspec array should be a
75 either a filehandle or an arrayref [$fh,$type] used to call
76 $self->add_fh($fh,$type) to set the type as well.
77  
78 Returns number of filehandles in the list to write to.
79  
80 =cut
81  
82 13 ▶sub fh {
Subroutine fh: called CC=4 SCAR=13.9
Pod documented
83 13 my $self=shift;
84 13 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 2 13
85 11 $self->{filehandles} = [];
86 11 foreach my $fhspec (@_) {
87 12 ▶ $fhspec = [$fhspec] unless (ref($fhspec) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fhspec eq "ARRAY" 1 11 12
88 12 $self->add_fh(@$fhspec);
89 }
90 }
91 13 return(scalar(@{$self->{filehandles}}));
92 }
93  
94  
95 =head3 add_fh($fh,$type)
96  
97 Add a filehandle to the logger. If $type is set equal to 'json' then
98 JSON will be written, els if 'html then HTML will be written, otherwise
99 text is output in markdown format. The call is ignored unless $fh is True.
100  
101 =cut
102  
103 12 ▶sub add_fh {
Subroutine add_fh: called CC=3 SCAR=11.0
Pod documented
104 12 my $self=shift;
105 12 my ($fh,$type)=@_;
106 12 ▶ return() if (not $fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 12 12
107 12 ▶ $type ||= 'md';
Condition100%2/2
$type ||= "md"
l!l
57
MC/DC100%1/1
$type ||= "md"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
108 12 push(@{$self->{filehandles}},{'fh'=>$fh,'type'=>$type});
109 12 return($fh);
110 }
111  
112  
113 =head3 num_total()
114  
115 Return the total number of pass and fail events recorded. Note
116 that this doesn't include warnings.
117  
118 =cut
119  
120 6 ▶sub total {
Subroutine total: called CC=1 SCAR=0.0
Pod undocumented
121 6 my $self=shift;
122 6 return( $self->{num_pass}+$self->{num_fail} );
123 }
124  
125  
126 =head3 start($title)
127  
128 Start a test or section and record a title.
129  
130 =cut
131  
132 2 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
133 2 my $self=shift;
134 2 my ($title)=@_;
135 2 return $self->_add('TITLE',$title);
136 }
137  
138  
139 =head3 request($url,$type,$content)
140  
141 Add a note of the HTTP request used in this test. Must specify
142 the $url, may include the $type (GET|POST) and for POST
143 the $content.
144  
145 =cut
146  
147 6 ▶sub request {
Subroutine request: called CC=3 SCAR=11.0
Pod documented
148 6 my $self=shift;
149 6 my ($url,$type,$content)=@_;
150 6 ▶ return $self->_add('REQUEST',$url,$type||'',$content||'');
Condition100%4/4
$type || ''
l!l
33
$content || ''
l!l
15
MC/DC100%1/1
$type || ''
$type
MC/DC100%1/1
$content || ''
$content
Truth table100%2/2
A
A= $type
Aresult
11
00
Truth table100%2/2
A
A= $content
Aresult
11
00
151 }
152  
153  
154 =head3 note($note)
155  
156 Add note of extra information that doesn't impact validity.
157  
158 =cut
159  
160 3 ▶sub note {
Subroutine note: called CC=1 SCAR=0.0
Pod documented
161 3 my $self=shift;
162 3 my ($note)=@_;
163 3 return $self->_add('NOTE',$note);
164 }
165  
166  
167 =head3 fail($msg)
168  
169 Record a failure and increment the $obj->num_fail count.
170  
171 =cut
172  
173 4 ▶sub fail {
Subroutine fail: called CC=1 SCAR=0.0
Pod documented
174 4 my $self=shift;
175 4 my ($msg)=@_;
176 4 $self->{num_fail}++;
177 4 return $self->_add('FAIL',$msg);
178 }
179  
180  
181 =head3 warn($msg)
182  
183 Record a warning and increment the $obj->num_warn count.
184  
185 =cut
186  
187 2 ▶sub warn {
Subroutine warn: called CC=1 SCAR=0.0
Pod documented
188 2 my $self=shift;
189 2 my ($msg)=@_;
190 2 $self->{num_warn}++;
191 2 return $self->_add('WARN',$msg);
192 }
193  
194  
195 =head3 pass($msg)
196  
197 Record a success and increment the $obj->num_pass count. Must have
198 a message $msg explaining what has passed.
199  
200 =cut
201  
202 6 ▶sub pass {
Subroutine pass: called CC=1 SCAR=0.0
Pod documented
203 6 my $self=shift;
204 6 my ($msg)=@_;
205 6 $self->{num_pass}++;
206 6 return $self->_add('PASS',$msg);
207 }
208  
209  
210 # _add($type,@content)
211 #
212 # Add an entry to @{$obj->log} which has type $type and then
213 # a set of content elements @content (assumed to be scalars).
214 # Used by all the pass, fail, warn, start methods.
215 #
216 # In addition to recording the data in $self->{log} array, will
217 # write output in markdown, HTML or JSON to each of the filehandles
218 # in $self->filehandles.
219 #
220 31 ▶sub _add {
Subroutine _add: called CC=2 SCAR=6.9
221 31 my $self=shift;
222 31 my $type=shift;
223 31 my $msg=join(' ',@_);
224 # do a little tidy on the message
225 31 $msg=~s/\s+$//;
226 31 $msg=~s/\n/ /g;
227 31 push(@{$self->{log}}, [$type,$msg]);
228 31 ▶ if (scalar($self->filehandles)>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->filehandles > 0) 31 0 31
229 31 $self->_write_to_filehandles([$type,$msg], $self->filehandles);
230 }
231 31 return(1);
232 }
233  
234  
235 # _write_to_filehandles($entry, $filehandles) - write one entry
236 # to zero of more filehandles with formats as specified in
237 # $filehandles data.
238 #
239 31 ▶sub _write_to_filehandles {
Subroutine _write_to_filehandles: called CC=4 SCAR=13.9
240 31 my $self = shift(@_);
241 31 my ($entry, $filehandles) = @_;
242 31 my ($type, $msg) = @$entry;
243 31 foreach my $fhd (@$filehandles) {
244 8 ▶ if ($fhd->{'type'} eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($fhd->{'type'} eq 'json') { } 1 7 8
elsif ($fhd->{'type'} eq 'html') { } 2 5 7
245 1 print {$fhd->{'fh'}} $self->_json($type,$msg);
246 } elsif ($fhd->{'type'} eq 'html') {
247 2 print {$fhd->{'fh'}} $self->_html($type,$msg);
248 } else {
249 5 print {$fhd->{'fh'}} $self->_md($type,$msg);
250 }
251 }
252 31 return(1);
253 }
254  
255  
256 # _md($type, $msg) - Return markdown for a log entry
257 #
258 9 ▶sub _md {
Subroutine _md: called CC=2 SCAR=6.9
259 9 my $self=shift;
260 9 my ($type,$msg)=@_;
261 9 my $md_prefix = '';
262 9 my $md_suffix = "\n";
263 9 ▶ if ($type eq 'TITLE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'TITLE') { } 2 7 9
264 2 $md_prefix = "\n### ";
265 2 $md_suffix = "\n\n";
266 } else {
267 7 $md_prefix = sprintf("%-8s ",$type.':');
268 }
269 9 return($md_prefix.$msg.$md_suffix);
270 }
271  
272 # _html($type,$msg) - Return HTML for a log entry, using
273 # classes to allow CSS styling
274 #
275 2 ▶sub _html {
Subroutine _html: called CC=2 SCAR=6.9
276 2 my $self=shift;
277 2 my ($type,$msg)=@_;
278 2 ▶ if ($type eq 'TITLE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'TITLE') { } 1 1 2
279 1 return('<h3 class="oaipmh-log-title">'.$msg."</h3>\n");
280 } else {
281 return('<div class="oaipmh-log-line oaipmh-log-'.$type.'">'.
282 1 '<span class="oaipmh-log-num">'.scalar(@{$self->{log}}).'</span> '.
283 '<span class="oaipmh-log-type">'.$type.'</span> '.
284 '<span class="oaipmh-log-msg">'.$msg."</span></div>\n");
285 }
286 }
287  
288 # _json($fh,$type,$msg) - Return one-line JSON for a
289 # log entry, terminate with \n.
290 #
291 1 ▶sub _json {
Subroutine _json: called CC=1 SCAR=0.0
292 1 my $self=shift;
293 1 my ($type,$msg)=@_;
294 return(encode_json({type=>$type, msg=>$msg,
295 1 num=>scalar(@{$self->{log}}),
296 pass=>$self->num_pass,
297 fail=>$self->num_fail,
298 warn=>$self->num_warn,
299 timestamp=>''.localtime() })."\n");
300 }
301  
302  
303 =head2 INTERROGATING THE LOG
304  
305 =head3 failures()
306  
307 Return Markdown summary of failure log entries, along with the appropriate
308 titles and request details. Will return empty string if there are no
309 failures in the log.
310  
311 =cut
312  
313 2 ▶sub failures {
Subroutine failures: called CC=8 SCAR=20.8
Pod documented
314 2 my $self=shift;
315 2 ▶ return('') if ($self->num_fail==0); #shirt circuit if no failures
Branch: 1 branch
BranchTrueFalseTotal
if $self->num_fail == 0 1 1 2
316  
317 1 my $str="\n## Failure summary\n";
318 1 my $last_title='Unknown title';
319 1 my $last_request=undef;
320 1 for my $entry (@{$self->log}) {
321 12 my ($type, $msg) = @$entry;
322 12 ▶ if ($type eq 'TITLE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'TITLE') { } 1 11 12
elsif ($type eq 'REQUEST') { } 2 9 11
elsif ($type eq 'FAIL') { } 2 7 9
323 1 $last_title=$entry;
324 1 $last_request=undef;
325 } elsif ($type eq 'REQUEST') {
326 2 $last_request=$entry;
327 } elsif ($type eq 'FAIL') {
328 2 ▶ $str .= $self->_md(@$last_title) if (defined $last_title);
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_title 1 1 2
329 2 ▶ $str .= $self->_md(@$last_request) if (defined $last_request);
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_request 1 1 2
330 2 $str .= $self->_md(@$entry);
331 2 $last_title = undef;
332 2 $last_request = undef;
333 }
334 }
335 1 return($str);
336 }
337  
338  
339  
340 =head3 last_match($regex)
341  
342 Return last log entry where the message matches $regex, else
343 empty return.
344  
345 =cut
346  
347 3 ▶sub last_match {
Subroutine last_match: called CC=3 SCAR=11.0
Pod documented
348 3 my $self=shift;
349 3 my ($regex)=@_;
350 3 foreach my $entry (reverse(@{$self->log})) {
351 23 ▶ if ($entry->[1]=~$regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->[1] =~ /$regex/) 2 21 23
352 2 return($entry);
353 }
354 }
355 1 return;
356 }
357  
358 1 1;
↑ Summary Validator.pm »