| 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=2.6
| |||
| 36 | use JSON qw(encode_json); | ||
| 37 | use base qw(Class::Accessor::Fast); | ||
| 38 | 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 | sub new { | ||
| 57 | my $this=shift; | ||
| 58 | # uncoverable condition false | ||
| 59 | my $class=ref($this) || $this; | ||
| 60 | my $self={'log'=>[], | ||
| 61 | 'filehandles'=>[], | ||
| 62 | 'num_pass'=>0, | ||
| 63 | 'num_fail'=>0, | ||
| 64 | 'num_warn'=>0, | ||
| 65 | @_}; | ||
| 66 | bless($self, $class); | ||
| 67 | 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 | sub fh { | ||
| 83 | my $self=shift; | ||
| 84 | if (@_) { | ||
| 85 | $self->{filehandles} = []; | ||
| 86 | foreach my $fhspec (@_) { | ||
| 87 | $fhspec = [$fhspec] unless (ref($fhspec) eq 'ARRAY'); | ||
| 88 | $self->add_fh(@$fhspec); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | 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 | sub add_fh { | ||
| 104 | my $self=shift; | ||
| 105 | my ($fh,$type)=@_; | ||
| 106 | return() if (not $fh); | ||
| 107 | $type ||= 'md'; | ||
| 108 | push(@{$self->{filehandles}},{'fh'=>$fh,'type'=>$type}); | ||
| 109 | 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 | sub total { | ||
| 121 | my $self=shift; | ||
| 122 | 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 | sub start { | ||
| 133 | my $self=shift; | ||
| 134 | my ($title)=@_; | ||
| 135 | 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 | sub request { | ||
| 148 | my $self=shift; | ||
| 149 | my ($url,$type,$content)=@_; | ||
| 150 | return $self->_add('REQUEST',$url,$type||'',$content||''); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | =head3 note($note) | ||
| 155 | |||
| 156 | Add note of extra information that doesn't impact validity. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub note { | ||
| 161 | my $self=shift; | ||
| 162 | my ($note)=@_; | ||
| 163 | 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 | sub fail { | ||
| 174 | my $self=shift; | ||
| 175 | my ($msg)=@_; | ||
| 176 | $self->{num_fail}++; | ||
| 177 | 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 | sub warn { | ||
| 188 | my $self=shift; | ||
| 189 | my ($msg)=@_; | ||
| 190 | $self->{num_warn}++; | ||
| 191 | 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 | sub pass { | ||
| 203 | my $self=shift; | ||
| 204 | my ($msg)=@_; | ||
| 205 | $self->{num_pass}++; | ||
| 206 | 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 | sub _add { | ||
| 221 | my $self=shift; | ||
| 222 | my $type=shift; | ||
| 223 | my $msg=join(' ',@_); | ||
| 224 | # do a little tidy on the message | ||
| 225 | $msg=~s/\s+$//; | ||
| 226 | $msg=~s/\n/ /g; | ||
| 227 | push(@{$self->{log}}, [$type,$msg]); | ||
| 228 | if (scalar($self->filehandles)>0) { | ||
| 229 | $self->_write_to_filehandles([$type,$msg], $self->filehandles); | ||
| 230 | } | ||
| 231 | 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 | sub _write_to_filehandles { | ||
| 240 | my $self = shift(@_); | ||
| 241 | my ($entry, $filehandles) = @_; | ||
| 242 | my ($type, $msg) = @$entry; | ||
| 243 | foreach my $fhd (@$filehandles) { | ||
| 244 | if ($fhd->{'type'} eq 'json') { | ||
| 245 | print {$fhd->{'fh'}} $self->_json($type,$msg); | ||
| 246 | } elsif ($fhd->{'type'} eq 'html') { | ||
| 247 | print {$fhd->{'fh'}} $self->_html($type,$msg); | ||
| 248 | } else { | ||
| 249 | print {$fhd->{'fh'}} $self->_md($type,$msg); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | return(1); | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | # _md($type, $msg) - Return markdown for a log entry | ||
| 257 | # | ||
| 258 | sub _md { | ||
| 259 | my $self=shift; | ||
| 260 | my ($type,$msg)=@_; | ||
| 261 | my $md_prefix = ''; | ||
| 262 | my $md_suffix = "\n"; | ||
| 263 | if ($type eq 'TITLE') { | ||
| 264 | $md_prefix = "\n### "; | ||
| 265 | $md_suffix = "\n\n"; | ||
| 266 | } else { | ||
| 267 | $md_prefix = sprintf("%-8s ",$type.':'); | ||
| 268 | } | ||
| 269 | 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 | sub _html { | ||
| 276 | my $self=shift; | ||
| 277 | my ($type,$msg)=@_; | ||
| 278 | if ($type eq 'TITLE') { | ||
| 279 | return('<h3 class="oaipmh-log-title">'.$msg."</h3>\n"); | ||
| 280 | } else { | ||
| 281 | return('<div class="oaipmh-log-line oaipmh-log-'.$type.'">'. | ||
| 282 | '<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 | sub _json { | ||
| 292 | my $self=shift; | ||
| 293 | my ($type,$msg)=@_; | ||
| 294 | return(encode_json({type=>$type, msg=>$msg, | ||
| 295 | 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 | sub failures { | ||
| 314 | my $self=shift; | ||
| 315 | return('') if ($self->num_fail==0); #shirt circuit if no failures | ||
| 316 | |||
| 317 | my $str="\n## Failure summary\n"; | ||
| 318 | my $last_title='Unknown title'; | ||
| 319 | my $last_request=undef; | ||
| 320 | for my $entry (@{$self->log}) { | ||
| 321 | my ($type, $msg) = @$entry; | ||
| 322 | if ($type eq 'TITLE') { | ||
| 323 | $last_title=$entry; | ||
| 324 | $last_request=undef; | ||
| 325 | } elsif ($type eq 'REQUEST') { | ||
| 326 | $last_request=$entry; | ||
| 327 | } elsif ($type eq 'FAIL') { | ||
| 328 | $str .= $self->_md(@$last_title) if (defined $last_title); | ||
| 329 | $str .= $self->_md(@$last_request) if (defined $last_request); | ||
| 330 | $str .= $self->_md(@$entry); | ||
| 331 | $last_title = undef; | ||
| 332 | $last_request = undef; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | 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 | sub last_match { | ||
| 348 | my $self=shift; | ||
| 349 | my ($regex)=@_; | ||
| 350 | foreach my $entry (reverse(@{$self->log})) { | ||
| 351 | if ($entry->[1]=~$regex) { | ||
| 352 | return($entry); | ||
| 353 | } | ||
| 354 | } | ||
| 355 | return; | ||
| 356 | } | ||
| 357 | |||
| 358 | 1; | ||