Parse.pm

filter: Statementstmt 100% 176 / 176 Branchbran 84.0% 42 / 50 Conditioncond 100% 16 / 16 MC/DCmcdc 100% 8 / 8 Subroutinesub 96.8% 31 / 32 Podpod 100% 10 / 10 totaltotal 96.9% 283 / 292 CC 33 SCAR 15.5
CC
33
Cov
97%
next
11.0
set_logfile
10.3
go_forward
4.0
CRAP 4.7
↑ Summary Parse/Postfix.pm »
1 #!/usr/bin/perl
2  
3 package Mail::Log::Parse;
4 {
5 =head1 NAME
6  
7 Mail::Log::Parse - Parse and return info in maillogs
8  
9 =head1 SYNOPSIS
10  
11 use Mail::Log::Parse;
12  
13 $object = Mail::Log::Parse->new({ log_file => '/path/to/logfile' });
14 %line_info = %{object->next()};
15  
16 $line_num = $object->get_line_number();
17  
18 if ( $object->go_forward($amount) ) {
19 ...
20 }
21  
22 if ( $object->go_backward($amount) ) {
23 ...
24 }
25  
26 %line_info = %{object->previous()};
27  
28 =head1 DESCRIPTION
29  
30 This is the root-level module for a generic mail log file parser. It is capable
31 of opening either a compressed or uncompressed logfile, and either stepping
32 through it line by line, or seeking around in it based on the logical lines.
33 (Lines not pertaining to the type of log currently being searched are skipped,
34 as if they don't exist.)
35  
36 On it's own it doesn't actually do much: You'll need a subclass that can
37 parse a particular program's log entries. But such subclasses are designed to
38 be easy to write and use.
39  
40 =head1 USAGE
41  
42 This is an object-oriented module. Available object methods are below.
43  
44 In a string context, it will return a string specifying the path to the file
45 and the current line number. In a boolean context, it will return whether it
46 has been correctly initialized. (Whether it has a file.) Numeric context throws
47 an error.
48  
49 Oh, and iterator context ('<>') returns the same as 'next'...
50  
51 =cut
52  
53 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use Scalar::Util qw(refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 4 ▶use Mail::Log::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
62 4 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 4 $VERSION = '1.0402';
65 }
66  
67 #
68 # Define class variables. Note that they are hashes...
69 #
70  
71 4 my %log_info;
72 my %parse_buffer;
73 4 my %parse_buffer_start_line;
74 4 my %parse_buffer_size;
75 4 my %debug;
76 4 my %current_line;
77  
78 #
79 # DESTROY class variables.
80 #
81 ### IF NOT DONE THERE IS A MEMORY LEAK. ###
82  
83 12 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
84 12 my ($self) = @_;
85  
86 12 ▶ $log_info{refaddr $self}{filehandle}->close() if defined($log_info{refaddr $self}{filehandle});
Branch: 1 branch
BranchTrueFalseTotal
if defined $log_info{builtin::refaddr($self)}{"filehandle"} 4 8 12
87  
88 12 delete $log_info{refaddr $self};
89 12 delete $parse_buffer{refaddr $self};
90 12 delete $parse_buffer_start_line{refaddr $self};
91 12 delete $parse_buffer_size{refaddr $self};
92 12 delete $debug{refaddr $self};
93 12 delete $current_line{refaddr $self};
94  
95 12 return;
96 }
97  
98 #
99 # Set the coercions to something useful.
100 #
101  
102 4 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
103 # Strings overload to the path and line number.
104 2 ▶ qw{""} => sub { my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
105 return blessed($self)
106 .' File: '
107 .$log_info{$$self}{'filename'}
108 .' Line: '
109 2 .$current_line{$$self};
110 },
111  
112 # Boolean overloads to if we are usable. (Have a filehandle.)
113 4 ▶ qw{bool} => sub { my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
114 4 return defined($log_info{$$self}{'filehandle'});
115 },
116  
117 # Numeric context just doesn't mean anything. Throw an error.
118 2 ▶ q{0+} => sub { Mail::Log::Exceptions->throw(q{Can't get a numeric value of a Mail::Log::Parse.} );
Subroutine __ANON__: called CC=1 SCAR=0.0
119 },
120  
121 # Heh. Iterator context is the same as 'next'...
122 2 ▶ q{<>} => sub { return $_[0]->next(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
123  
124 # Perl standard for everything else.
125 4 fallback => 1,
126 4 );
127  
128 =head2 new (constructor)
129  
130 The base constructor for the Mail::Log::Parse classes. It takes an (optional)
131 hash containing path to the logfile as an argument, and returns the new object.
132  
133 Example:
134  
135 $object = Mail::Log::Parse->new({ log_file => '/path/to/logfile' });
136  
137 Note that it is an error to call any method other than C<set_logfile> if you
138 have not passed it in the constructor.
139  
140 Optional keys in the hash are 'buffer_length' and 'debug'. The buffer length
141 is the number of lines to read at a time (and store in the internal buffer).
142 Default is 128. Setting debug to a true value will result in some debugging
143 information being printed to STDERR. (I reserve the right to remove or change
144 the debug info at any time.)
145  
146 =cut
147  
148 sub new
149 12 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
150 12 my ($class, $parameters_ref) = @_;
151  
152 12 my $self = bless \do{my $anon}, $class;
153 12 $$self = refaddr $self;
154  
155 # Log info.
156 12 ▶ if ( defined($parameters_ref->{'log_file'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parameters_ref->{"log_file"}) 6 6 12
157 6 $self->set_logfile($parameters_ref->{'log_file'}); # Better to keep validation together.
158 }
159  
160 8 $debug{$$self} = defined($parameters_ref->{debug});
161  
162 #$debug{refaddr $self} = 1;
163  
164 # Init the buffer.
165 8 $current_line{$$self} = 0;
166 8 $parse_buffer_start_line{$$self} = 0;
167 8 $parse_buffer{$$self} = undef;
168 8 ▶ $parse_buffer_size{$$self} = defined($parameters_ref->{buffer_length}) ? $parameters_ref->{buffer_length} : 128;
Branch: 1 branch
BranchTrueFalseTotal
defined $parameters_ref->{'buffer_length'} ? : 1 7 8
169  
170  
171 8 return $self;
172 }
173  
174 =head2 set_logfile
175  
176 Sets the logfile that this object will attempt to parse. It will throw
177 exceptions if it can't open the file for any reason, and will return true on
178 success.
179  
180 Files can be compressed or uncompressed: If they are compressed, then
181 C<IO::Uncompress::AnyUncompress> must be installed with the relevant
182 decompression libraries. (As well as version 0.17 or better of File::Temp.)
183 Currently only 'tgz', 'zip', 'gz', and 'bz2' archives are supported, but
184 there is no technical reason not to support more. (It just keeps a couple
185 of lines of code shorter.)
186  
187 Note that to support seeking in the file the log will be uncompressed to disk
188 before it is read: If there is insufficient space to do so, we may have trouble.
189 It also means this method may take a while to return for large compressed logs.
190  
191 Example:
192  
193 $object->set_logfile('path/to/file');
194  
195 =cut
196  
197 8 ▶sub set_logfile {
Subroutine set_logfile: called CC=10 SCAR=23.3
Pod documented
198 8 my ($self, $new_name) = @_;
199  
200 # Check to make sure the file exists,
201 # and then that we can read it, before accpeting the filename.
202 8 ▶ if ( -e $new_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $new_name) { } 6 2 8
203 6 ▶ if ( -r $new_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $new_name) { } 4 2 6
204 4 $log_info{$$self}{'filename'} = $new_name;
205  
206 # We'll check the extension to see if it is compressed.
207 4 my (undef, undef, $suffix) = fileparse($new_name, qw(tgz zip gz bz2));
208 4 ▶ if ( $suffix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix) { } 1 3 4
209  
210 # Since we only need uncompress symantics right here, we'll
211 # only load them if we need them. Neat, huh?
212 1 ▶ eval { require IO::Uncompress::AnyUncompress } or Mail::Log::Exceptions->throw("Need IO::Uncompress::AnyUncompress for compressed files.\n");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IO::Uncompress::AnyUncompress } } or "Mail::Log::Exceptions"->throw("Need IO::Uncompress::AnyUncompress for compressed files.\n") 1 0 1
213 1 IO::Uncompress::AnyUncompress->import( qw(anyuncompress) );
214  
215 # Same with File::Temp;
216 1 ▶ eval { require File::Temp } or Mail::Log::Exceptions->throw("Need File::Temp version 0.17 or better for compressed files.\n");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require File::Temp } } or "Mail::Log::Exceptions"->throw("Need File::Temp version 0.17 or better for compressed files.\n") 1 0 1
217 1 ▶ File::Temp->VERSION( 0.17 ) # Minimum version check.
Branch: 1 branch
BranchTrueFalseTotal
"File::Temp"->VERSION("0.17") or "Mail::Log::Exceptions"->throw("Need File::Temp version 0.17 or better for compressed files.\n") 1 0 1
218 or Mail::Log::Exceptions->throw("Need File::Temp version 0.17 or better for compressed files.\n");
219  
220 # If it is compressed, uncompress to a temp file and use that.
221 1 my $temp = new File::Temp();
222 1 ▶ anyuncompress($new_name, $temp)
Branch: 1 branch
BranchTrueFalseTotal
anyuncompress($new_name, $temp) or "Mail::Log::Exceptions::LogFile"->throw("Unable to uncompress logfile ${new_name}: " . $IO::Uncompress::AnyUncompress::AnyUncompressError . "\n") 1 0 1
223 or Mail::Log::Exceptions::LogFile->throw("Unable to uncompress logfile $new_name: ". $IO::Uncompress::AnyUncompress::AnyUncompressError ."\n");
224 1 ▶ $temp->seek(0,0)
Branch: 1 branch
BranchTrueFalseTotal
$temp->seek(0, 0) or "Mail::Log::Exceptions::LogFile"->throw("Unable to seek to beginning of temp file.\n") 1 0 1
225 or Mail::Log::Exceptions::LogFile->throw("Unable to seek to beginning of temp file.\n");
226 1 $log_info{$$self}{'filehandle'} = $temp;
227 }
228 else {
229 # If it wasn't compressed, open it direct.
230 3 ▶ $log_info{$$self}{'filehandle'} = IO::File->new($new_name, '<')
Branch: 1 branch
BranchTrueFalseTotal
$log_info{$$self}{"filehandle"} = "IO::File"->new($new_name, "<") or "Mail::Log::Exceptions::LogFile"->throw("Unable to open file ${new_name}: $!\n") 3 0 3
231 or Mail::Log::Exceptions::LogFile->throw("Unable to open file $new_name: $!\n");
232 }
233  
234 # Init some location information on the file.
235 4 $current_line{$$self} = 0;
236 4 delete $log_info{$$self}->{'line_positions'};
237 4 ${$log_info{$$self}{'line_positions'}}[$current_line{$$self}] = $log_info{$$self}{'filehandle'}->getpos();
238 }
239 else {
240 2 Mail::Log::Exceptions::LogFile->throw("Log file $new_name is not readable.\n");
241 }
242 }
243 else {
244 2 Mail::Log::Exceptions::LogFile->throw("Log file $new_name does not exist.\n");
245 }
246  
247 4 return 1;
248 }
249  
250 =head2 next
251  
252 Returns a reference to a hash of the next parsable line of the log, or 'undef' on
253 end of file/failure.
254  
255 There are a couple of required keys that any parser must implement:
256  
257 timestamp, program, id, text.
258  
259 Where C<timestamp> must the the unix timestamp, C<program> must be the name of
260 the program that reported the logline (Sub-programs are recommended to be listed,
261 if possible), C<id> is the tracking ID for that message, as reported by the
262 program, and C<text> is the text following any 'standard' headers. (Usually,
263 minus those already required keys.)
264  
265 This version is just a placeholder: It will return a
266 'Mail::Log::Exceptions::Unimplemented' exception if called. Subclasses are
267 expected to override the C<_parse_next_line> method to get an operable parser.
268 (And that is the only method needed to be overridden for a working subclass.)
269  
270 Other 'standard' fields that are expected in a certain format (but are not
271 required to always be present) are 'from', 'to', 'size', 'subject', delay. 'to'
272 should point to an array of addresses. (As listed in the log. That includes
273 angle brackets, usually.)
274  
275 Example:
276  
277 while $hash_ref ( $object->next() ) {
278 ...
279 }
280  
281 or...
282  
283 while $hash_ref ( <$object> ) {
284 ...
285 }
286  
287 =cut
288  
289 53 ▶sub next {
Subroutine next: called CC=11 SCAR=24.0
Pod documented
290 53 my ($self) = @_;
291  
292 # This is the same as $self->get_current_line();
293 # (Or at least it should be. Done for speed.)
294 53 my $current_line = $current_line{$$self};
295  
296 53 ▶ if ( defined($parse_buffer{$$self})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parse_buffer{$$self} and $current_line + 1 <= $parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self};} and $current_line + 1 >= $parse_buffer_start_line{$$self}) { } 18 35 53
Condition100%6/6
defined $parse_buffer{$$self} and $current_line + 1 <= $parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self};} and $current_line + 1 >= $parse_buffer_start_line{$$self}
!ll&&!rl&&r
31418
defined $parse_buffer{$$self} and $current_line + 1 <= $parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self};}
!ll&&!rl&&r
42722
MC/DC100%3/3
defined $parse_buffer{$$self} and $current_line + 1 <= $parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self};} and $current_line + 1 >= $parse_buffer_start_line{$$self}
defined $parse_buffer{$$self} $current_line + 1 <= $parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self};} $current_line + 1 >= $parse_buffer_start_line{$$self}
Truth table100%4/4
A and B and C
A= defined $parse_buffer{$$self}B= $current_line + 1 <= $parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self};}C= $current_line + 1 >= $parse_buffer_start_line{$$self}
ABCresult
0XX0
10X0
1100
1111
297 49 and ( ($current_line+1) <= ($parse_buffer_start_line{$$self} + $#{$parse_buffer{$$self}}) )
298 and ( ($current_line+1) >= $parse_buffer_start_line{$$self})
299 ) {
300  
301 # Increment where we are.
302 18 $current_line{$$self} = $current_line{$$self} + 1;
303  
304 # print STDERR 'Returning line number '. $self->get_line_number() ." from buffer.\n" if $debug{$selfref};
305  
306 # Return the data we were asked for.
307 18 return $parse_buffer{$$self}->[($current_line - $parse_buffer_start_line{$$self}+1)];
308 }
309 else {
310 # Move the actual read postition to where we are.
311 # (But only if we've acutally ever read anything.)
312 35 ▶ if ( defined($log_info{$$self}->{line_positions}->[$current_line]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_info{$$self}{"line_positions"}[$current_line]) 32 3 35
313 32 ▶ $log_info{$$self}{filehandle}->setpos($log_info{$$self}->{line_positions}->[$current_line])
Branch: 1 branch
BranchTrueFalseTotal
$log_info{$$self}{"filehandle"}->setpos($log_info{$$self}{"line_positions"}[$current_line]) or "Mail::Log::Exceptions::LogFile"->throw("Error seeking to position: $!\n") 32 0 32
314 or Mail::Log::Exceptions::LogFile->throw("Error seeking to position: $!\n");
315 }
316  
317 # print STDERR 'Reading buffer for line '. $current_line .".\n" if $debug{refaddr $self};
318  
319 # Check if we've reached the end of the file.
320 # (And that we haven't gone back...)
321 35 ▶ if ( defined($parse_buffer{$$self}->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parse_buffer{$$self}[0] and $#{$parse_buffer{$$self};} < $parse_buffer_size{$$self} and $current_line >= $parse_buffer_start_line{$$self}) 4 31 35
Condition100%6/6
defined $parse_buffer{$$self}[0] and $#{$parse_buffer{$$self};} < $parse_buffer_size{$$self} and $current_line >= $parse_buffer_start_line{$$self}
!ll&&!rl&&r
2924
defined $parse_buffer{$$self}[0] and $#{$parse_buffer{$$self};} < $parse_buffer_size{$$self}
!ll&&!rl&&r
9206
MC/DC100%3/3
defined $parse_buffer{$$self}[0] and $#{$parse_buffer{$$self};} < $parse_buffer_size{$$self} and $current_line >= $parse_buffer_start_line{$$self}
defined $parse_buffer{$$self}[0] $#{$parse_buffer{$$self};} < $parse_buffer_size{$$self} $current_line >= $parse_buffer_start_line{$$self}
Truth table100%4/4
A and B and C
A= defined $parse_buffer{$$self}[0]B= $#{$parse_buffer{$$self};} < $parse_buffer_size{$$self}C= $current_line >= $parse_buffer_start_line{$$self}
ABCresult
0XX0
10X0
1100
1111
322 26 and $#{$parse_buffer{$$self}} < $parse_buffer_size{$$self}
323 and $current_line >= $parse_buffer_start_line{$$self}
324 ) {
325 4 return $parse_buffer{$$self}->[-1];
326 }
327  
328 # Clear the buffer.
329 31 @{$parse_buffer{$$self}} = ();
330  
331 # Read in the buffer.
332 31 READ_LOOP: for my $i (0...$parse_buffer_size{$$self}) {
333 2409 $parse_buffer{$$self}->[$i] = $self->_parse_next_line();
334 2406 ▶ last READ_LOOP unless defined $parse_buffer{$$self}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parse_buffer{$$self}[$i] 2 2404 2406
335 2404 $self->_set_position_as_next_line;
336 }
337  
338 #use Data::Dumper;
339 #print STDERR Data::Dumper->Dump($parse_buffer{refaddr $self});
340  
341 # Move the indexes back to the line we are reading.
342 # (Note the 'current line' direct access again...)
343 28 $parse_buffer_start_line{$$self} = $current_line{$$self} - $#{$parse_buffer{$$self}};
344 28 $self->go_to_line_number($parse_buffer_start_line{$$self});
345  
346 # Return the data.
347 28 return $parse_buffer{$$self}->[0];
348 }
349 }
350  
351 =head2 previous
352  
353 Returns a reference to a hash of the previous line of the log, or undef on
354 failure/beginning of file.
355  
356 See C<next> for details: It works nearly exactly the same. (In fact, it calls
357 next as a parser.)
358  
359 =cut
360  
361 4 ▶sub previous {
Subroutine previous: called CC=2 SCAR=6.9
Pod documented
362 4 my ($self) = @_;
363  
364 # Check if we can.
365 4 ▶ if ( $current_line{$$self} <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line{$$self} <= 1) 2 2 4
366 2 return undef;
367 }
368  
369 # Go back two lines
370 2 $self->go_backward(2);
371  
372 # And read forward one, returning that.
373 2 return $self->next();
374 }
375  
376 =head2 go_forward
377  
378 Goes forward a specified number of (logical) lines, or 1 if unspecified. It will
379 throw an error if it fails to seek as requested.
380  
381 Returns true on success.
382  
383 Example:
384  
385 $object->go_forward(4);
386  
387 =cut
388  
389 21 ▶sub go_forward {
Subroutine go_forward: called CC=4 SCAR=13.9
Pod documented
390 21 my $self = shift;
391 21 my $lines = shift;
392  
393 # Just because I'm paranoid.
394 21 ▶ $lines ||= 1;
Condition100%2/2
$lines ||= 1
l!l
192
MC/DC100%1/1
$lines ||= 1
$lines
Truth table100%2/2
A
A= $lines
Aresult
11
00
395  
396 # If we've read the line before, go straight to it.
397 21 ▶ if ( ${$log_info{$$self}{line_positions}}[($current_line{$$self}+$lines)] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$log_info{$$self}{'line_positions'};}[$current_line{$$self} + $lines]) { } 12 9 21
398 12 $current_line{$$self} = $current_line{$$self} + $lines;
399 12 return 1;
400 }
401 else {
402 # Work out where we are.
403 9 my $start_pos = $self->get_line_number();
404 9 my $end_known_pos = $#{$log_info{$$self}{line_positions}}; # zero-indexed.
405 9 my $lines_remaining = $lines - ($end_known_pos - $start_pos);
406  
407 # Go to the last line we have.
408 9 $current_line{$$self} = $#{$log_info{$$self}{line_positions}};
409  
410 # Then read until we get to the line we want.
411 9 ▶ if ( $self->next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next) { } 7 2 9
412 7 unshift @_, ($self, $lines_remaining - 1 );
413 7 goto &go_forward;
414 }
415 else {
416 2 return 0;
417 }
418 }
419 }
420  
421 =head2 go_backward
422  
423 Goes backward a specified number of (logical) lines, or 1 if unspecified. It will
424 throw an error if it fails to seek as requested.
425  
426 If the seek would go beyond the beginning of the file, it will go to the
427 beginning of the file.
428  
429 Returns true on success.
430  
431 Example:
432  
433 $object->go_backward(4);
434  
435 =cut
436  
437 40 ▶sub go_backward {
Subroutine go_backward: called CC=3 SCAR=11.0
Pod documented
438 40 my ($self, $lines) = @_;
439  
440 # Just because I'm paranoid.
441 40 ▶ $lines ||= 1;
Condition100%2/2
$lines ||= 1
l!l
382
MC/DC100%1/1
$lines ||= 1
$lines
Truth table100%2/2
A
A= $lines
Aresult
11
00
442  
443 # If the line exits, go straight to it.
444 40 ▶ if ( ($current_line{$$self} - $lines ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line{$$self} - $lines > 0) { } 36 4 40
445 36 $current_line{$$self} -= $lines;
446 }
447 else {
448 #If they've asked us to go beyond the beginning of the file, just go to the beginning.
449 4 $current_line{$$self} = 0;
450 4 return 0;
451 }
452 36 return 1;
453 }
454  
455 =head2 go_to_beginning
456  
457 Goes to the beginning of the file, no matter how far away that is.
458  
459 Returns true on success.
460  
461 =cut
462  
463 2 ▶sub go_to_beginning {
Subroutine go_to_beginning: called CC=1 SCAR=0.0
Pod documented
464 2 my ($self) = @_;
465  
466 2 $current_line{$$self} = 0;
467  
468 2 return 1;
469 }
470  
471 =head2 go_to_end
472  
473 Goes to the end of the file, no matter where it is.
474  
475 This attempts to be efficient about it, skipping where it can.
476  
477 Returns true on success.
478  
479 =cut
480  
481 13 ▶sub go_to_end {
Subroutine go_to_end: called CC=2 SCAR=6.9
Pod documented
482 13 my ($self) = @_;
483  
484 # Go to the end of what we have.
485 13 $current_line{$$self} = $#{$log_info{$$self}{line_positions}};
486  
487 13 ▶ if ( !$self->next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->next) { } 2 11 13
488 2 return 1;
489 }
490 else {
491 11 goto &go_to_end;
492 }
493 }
494  
495 =head2 get_line_number
496  
497 Returns the current logical line number.
498  
499 Note that line numbers start at zero, where 0 is the absolute beginning of the
500 file.
501  
502 Example:
503  
504 $line_num = $object->get_line_number();
505  
506 =cut
507  
508 55 ▶sub get_line_number () {
Subroutine get_line_number: called CC=1 SCAR=0.0
Pod documented
509 # This method gets called a lot: speed is an issue.
510 # This is as fast as I could make it.
511 55 return $current_line{${$_[0]}};
512 }
513  
514 =head2 go_to_line_number
515  
516 Goes to a specific logical line number. (Preferably one that exits...)
517  
518 =cut
519  
520 34 ▶sub go_to_line_number {
Subroutine go_to_line_number: called CC=2 SCAR=6.9
Pod documented
521 34 my ($self, $line_number) = @_;
522  
523 # my $current_line_number = $self->get_line_number();
524  
525 4 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
526 34 ▶ if ( $current_line{$$self} >= $line_number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line{$$self} >= $line_number) { } 32 2 34
527 32 return $self->go_backward($current_line{$$self} - $line_number);
528 }
529 else {
530 2 return $self->go_forward($line_number - $current_line{$$self});
531 }
532 }
533  
534 #
535 # To be overrridden by subclasses.
536 #
537  
538 1 ▶sub _parse_next_line {
Subroutine _parse_next_line: called CC=1 SCAR=0.0
539 1 my ($self) = @_;
540  
541 1 Mail::Log::Exceptions::Unimplemented->throw("Method '_parse_next_line' needs to be implemented by the subclass.\n");
542 }
543  
544 #
545 # These are semi-private methods: They are for the use of subclasses only.
546 #
547 =for readme stop
548  
549 =head1 SUBCLASSING
550  
551 This class is useless without subclasses to handle specific file formats. As
552 such, attempts have been made to make subclassing as painless as possible. In
553 general, you should only ever have to implement one method: C<_parse_next_line>.
554  
555 C<_parse_next_line> will be called whenever another line of the log needs to be
556 read. Its responsibility is to identify the next line, report where that is
557 in the actual file, and to parse that line.
558  
559 Specifically, it should I<not> assume that every line in the input file is a
560 valid log line. It is expected to check first.
561  
562 Mail::Log::Parse is (as of v1.3) a cached inside-out object. If you don't know
563 what that means, ignore it: just writing C<_parse_next_line> correctly is enough.
564 However, if you find you need to store sub-class object info for some reason,
565 and want to use an inside-out object syntax yourself, C<$$self == refaddr $self>.
566 Which is useful and fast.
567  
568 Speed I<is> important. It is not unlikely for someone to try to parse through
569 a week's worth of logs from a dozen boxes, where each day's log is hundreds of
570 megabytes worth of data. Be as good as you can.
571  
572 One other thing: Realize that you may also be subclassed. Even if you parse
573 every possible option of some log format, someone somewhere will probably have
574 a customized version with a slightly different format. If you've done your job
575 well, they'll be able to use your parser and just extend it slightly. Key to
576 this is to leave the I<unaltered> line in the return hash under the 'text' key.
577  
578 =head2 Suggested usage:
579  
580 Suggestion on how to use the above two methods to implement a '_parse_next_line' routine in
581 a subclass:
582  
583 sub _parse_next_line {
584 my ($self) = @_;
585  
586 # The hash we will return.
587 my %line_info = ( program => '' );
588  
589 # Some temp variables.
590 my $line;
591  
592 # In a mixed-log enviornment, we can't count on any
593 # particular line being something we can parse. Keep
594 # going until we can.
595 while ( $line_info{program} !~ m/$program_name/ ) {
596 # Read the line, using the Mail::Log::Parse utilty method.
597 $line = $self->_get_data_line() or return undef;
598  
599 # Program name. (We trust the logs. ;) )
600 $line_info{program} = $line ~= m/$regrex/;
601 }
602  
603 # Continue parsing
604 ...
605  
606 return \%line_info;
607 }
608  
609 =head1 UTILITY METHODS
610  
611 The following methods are not for general consumption: They are specifically
612 provided for use in implementing subclasses. Using them incorrectly, or
613 outside a subclass, can get the object into an invalid state.
614  
615 B<ONLY USE IF YOU ARE IMPLEMENTING A SUBCLASS.>
616  
617 =head2 _set_current_position_as_next_line
618  
619 Depreciated: No longer needed. An empty stub exists for backwards-compatibility.
620  
621 =cut
622  
623 0 ▶sub _set_current_position_as_next_line () { }
Subroutine _set_current_position_as_next_line: not called CC=1 SCAR=0.0
624  
625  
626 # Sets the current positition as the next 'line' of logical data.
627 # Purely internal at this point.
628 # Optimized for speed over clarity, since we potentially use this a lot.
629 # (At least once per log line read.)
630 2404 ▶sub _set_position_as_next_line {
Subroutine _set_position_as_next_line: called CC=2 SCAR=7.0
631 2404 $current_line{${$_[0]}} += 1;
632 2404 ▶ ${$log_info{${$_[0]}}}{line_positions}[$current_line{${$_[0]}}] = $log_info{${$_[0]}}{'filehandle'}->getpos()
Branch: 1 branch
BranchTrueFalseTotal
do { $log_info{${$_[0];}} }->{"line_positions"}[$current_line{${$_[0];}}] = $log_info{${$_[0];}}{"filehandle"}->getpos or "Mail::Log::Exceptions::LogFile"->throw("Unable to get current file position: $!\n") 2404 0 2404
633 or Mail::Log::Exceptions::LogFile->throw("Unable to get current file position: $!\n");
634 }
635  
636 =head2 _get_data_line
637  
638 Returns the next line of data, as a string, from the logfile. This is raw data
639 from the logfile, separated by the current input separator.
640  
641 =cut
642  
643 # Optimized for speed over clarity, since we potentially use this a lot.
644 # (At least once per log line read.)
645  
646 2408 ▶sub _get_data_line {
Subroutine _get_data_line: called CC=2 SCAR=6.9
647 2408 ▶ if ( defined($log_info{${$_[0]}}{'filehandle'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_info{${$_[0];}}{'filehandle'}) { } 2406 2 2408
648 2406 return $log_info{${$_[0]}}{'filehandle'}->getline()
649 }
650 else {
651 2 Mail::Log::Exceptions::LogFile->throw("Trying to read without a valid logfile: $!\n");
652 }
653 }
654  
655 =head2 _clear_buffer
656  
657 Clears the internal buffer of any data that may have been read into it so far.
658 Normally you should never need to use this: It is provided only for those rare
659 cases where something that has already been read may be changed because of
660 outside input. (For instance: You can change the year dates are assumed to be
661 in during mid-read on Postfix.)
662  
663 Avoid using unless actually needed.
664  
665 =cut
666  
667 5 ▶sub _clear_buffer {
Subroutine _clear_buffer: called CC=1 SCAR=0.0
668 5 my ($self) = @_;
669 5 @{$parse_buffer{$$self}} = undef;
670 5 $parse_buffer_start_line{$$self} = -1;
671 5 return;
672 }
673  
674 #
675 # Fully private methods.
676 #
677 =for readme continue
678  
679 =head1 BUGS
680  
681 C<go_forward> and C<go_backward> at the moment don't test for negative
682 numbers. They may or may not work with a negative number of lines: It depends
683 where you are in the file and what you've read so far.
684  
685 Those two methods should do slightly better on 'success' testing, to return
686 better values. (They basically always return true at the moment.)
687  
688 C<get_line_number> will return one less than the true line number if you are
689 at the end of the file, and the buffer was completely filled. (So that the
690 end of the file is the last space of the buffer.) Changing the buffer size or
691 just going back and re-reading so that the buffer is restarted at a different
692 location will allow you to retrieve the correct file length.
693  
694 =head1 REQUIRES
695  
696 L<Scalar::Util>, L<File::Basename>, L<IO::File>, L<Mail::Log::Exceptions>
697  
698 =head1 RECOMMENDS
699  
700 L<IO::Uncompress::AnyUncompress>, L<File::Temp>
701  
702 =head1 AUTHOR
703  
704 Daniel T. Staal
705  
706 DStaal@usa.net
707  
708 =head1 SEE ALSO
709  
710 L<Parse::Syslog::Mail>, which does some of what this module does. (This module
711 is a result of running into what that module I<doesn't> support. Namely
712 seeking through a file, both forwards and back.)
713  
714 =for readme stop
715  
716 =head1 HISTORY
717  
718 November 14, 2015 (1.4.2) - Switched from depreciated 'Module::Build::ModuleInfo
719 to it's replacement 'Module::Metadata' and included it in configure_requires.
720 (As it is no longer part of the Module::Build package.)
721  
722 February 8, 2014 (1.4.1) - Switched to using Perl-standard environment variables
723 for checking to run author tests. (Should now test cleanly on Windows.)
724  
725 April 17, 2009 (1.4.0) - Simplified subclassing: No longer need to call
726 C<_set_current_position_as_next_line> in subclass. (A stub exists for backwards
727 compatibility.)
728  
729 April 9, 2009 (1.3.1) - Documentation fixes, better handling of trying to work
730 without a valid logfile.
731  
732 Dec 23, 2008 (1.3.0) - Further code speedups. Now stores a cache of the refaddr
733 for easy and quick access.
734  
735 Dec 09, 2008 (1.2.10) - Profiled and sped up code. (Cut processing time in half
736 for some cases.)
737  
738 Nov 28, 2008 - Documentation fixes.
739  
740 Nov 18, 2008 - Now buffers reading, and prefers data from the buffer.
741  
742 Oct 24, 2008 - File::Temp now optional; only required for uncompressed files.
743 Added go_to_line_number for slightly better functionality.
744  
745 Oct 14, 2008 - Found that I need File::Temp of at least version 0.17.
746  
747 Oct 13, 2008 - Fixed tests so they do a better job of checking if they
748 need to skip.
749  
750 Oct 6, 2008 - Initial version.
751  
752 =for readme continue
753  
754 =head1 COPYRIGHT and LICENSE
755  
756 Copyright (c) 2008 Daniel T. Staal. All rights reserved.
757 This program is free software; you can redistribute it and/or
758 modify it under the same terms as Perl itself.
759  
760 This copyright will expire in 30 years, or 5 years after the author's
761 death, whichever is longer.
762  
763 =cut
764 } # End Package.
765 4 1;
↑ Summary Parse/Postfix.pm »