| 1 | package NcFTPd::Log::Parse::Base; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | my @TRANSFER_STATUSES = qw{OK ABOR INCOMPLETE PERM NOENT ERROR}; | |||||||||||||||||||||||||||
| 10 | 4 | my %COMMON_REGEX = ( | |||||||||||||||||||||||||||
| 11 | time => '\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?', # Date and time, optional millisecond precision | ||||||||||||||||||||||||||||
| 12 | process => '\#u\d+|\([a-z]+\)', | ||||||||||||||||||||||||||||
| 13 | decimal => '\d+(?:\.\d+)?', | ||||||||||||||||||||||||||||
| 14 | session => '[+/0-9A-Za-z]+', | ||||||||||||||||||||||||||||
| 15 | status => join '|', @TRANSFER_STATUSES | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub new | ||||||||||||||||||||||||||||
| 19 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=20.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 15 | my ($class, $file, %options) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 15 | ▶ | croak "usage: $class->new(\$file [, \%options ])" unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 15 | ▶ | croak "$file is a directory" if -d $file; # On some platforms IO::File will gladly open a directory | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 12 | ▶ | croak 'filter must be a CODE ref' if defined $options{filter} && ref $options{filter} ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options{"filter"} and ref $options{"filter"} ne "CODE"
MC/DC0%0/2
defined $options{"filter"} and ref $options{"filter"} ne "CODE"
defined $options{"filter"}
ref $options{"filter"} ne "CODE"
Truth table66%2/3
A and B
A= defined $options{"filter"}B= ref $options{"filter"} ne "CODE"
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | my $log = IO::File->new($file, '<') || croak "Error opening file $file: $!"; | ||||||||||||||||||||||||||
|
Condition50%1/2
'IO::File'->new($file, '<') || croak("Error opening file ${file}: $!")
MC/DC0%0/1
'IO::File'->new($file, '<') || croak("Error opening file ${file}: $!")
'IO::File'->new($file, '<')
Truth table50%1/2
A
A= 'IO::File'->new($file, '<')
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | bless { | ||||||||||||||||||||||||||||
| 29 | log => $log, | ||||||||||||||||||||||||||||
| 30 | error => '', | ||||||||||||||||||||||||||||
| 31 | 16 | ▶ | filter => $options{filter} || sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | expand => $options{expand} | ||||||||||||||||||||||||||||
| 33 | 12 | ▶ | }, $class; | ||||||||||||||||||||||||||
|
Condition66%2/3
$options{'filter'} || sub {
1;
}
MC/DC0%0/2
$options{'filter'} || sub {
1;
}
$options{'filter'}
sub {
1;
}
Truth table66%2/3
A || B
A= $options{'filter'}B= sub {
1;
}
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub next | ||||||||||||||||||||||||||||
| 37 | 25 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 25 | my $entry; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 25 | ▶ | while($entry = $self->_next_entry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 30 | local $_ = $entry; | |||||||||||||||||||||||||||
| 43 | 30 | ▶ | last if $self->{filter}->(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 25 | $entry; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub error | ||||||||||||||||||||||||||||
| 50 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | (shift)->{error} | ||||||||||||||||||||||||||||
| 52 | 5 | } | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub _next_entry | ||||||||||||||||||||||||||||
| 55 | 35 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_next_entry: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 56 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 35 | $self->{error} = ''; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 35 | ▶ | my $line = $self->_next_line or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 30 | my $entry = $self->_parse_line($line); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Don't squash an error message set by a subclass | ||||||||||||||||||||||||||||
| 63 | $self->{error} = 'Cannot parse line: unrecognized format' | ||||||||||||||||||||||||||||
| 64 | 30 | ▶ | unless $entry or $self->{error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$entry or $self->{"error"}
MC/DC0%0/2
$entry or $self->{"error"}
$entry
$self->{"error"}
Truth table33%1/3
A or B
A= $entryB= $self->{"error"}
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 30 | $entry; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub _next_line | ||||||||||||||||||||||||||||
| 70 | 35 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_next_line: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 71 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 35 | my $log = $self->{log}; | |||||||||||||||||||||||||||
| 73 | 35 | my $line = $log->getline; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 35 | ▶ | $self->{error} = "Error reading log file: $!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $line or $log->eof
MC/DC0%0/2
defined $line or $log->eof
defined $line
$log->eof
Truth table66%2/3
A or B
A= defined $lineB= $log->eof
| |||||||||||||||||||||||||||||
| 76 | unless defined $line or $log->eof; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 35 | $line; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub _parse_line | ||||||||||||||||||||||||||||
| 82 | 30 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_parse_line: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||
| 83 | 30 | my ($self, $line) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 30 | ▶ | return unless $line and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$line and $line =~ /^($COMMON_REGEX{"time"})\s($COMMON_REGEX{"process"})\s+\|\s(.+)/
MC/DC0%0/2
$line and $line =~ /^($COMMON_REGEX{"time"})\s($COMMON_REGEX{"process"})\s+\|\s(.+)/
$line
$line =~ /^($COMMON_REGEX{"time"})\s($COMMON_REGEX{"process"})\s+\|\s(.+)/
Truth table33%1/3
A and B
A= $lineB= $line =~ /^($COMMON_REGEX{"time"})\s($COMMON_REGEX{"process"})\s+\|\s(.+)/
| |||||||||||||||||||||||||||||
| 86 | $line =~ m{^($COMMON_REGEX{time})\s($COMMON_REGEX{process})\s+\|\s(.+)}; | ||||||||||||||||||||||||||||
| 87 | 30 | my $time = $1; | |||||||||||||||||||||||||||
| 88 | 30 | my $pid = $2; | |||||||||||||||||||||||||||
| 89 | 30 | my $entry = $self->_parse_entry($3); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 30 | ▶ | if($entry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 30 | $entry->{time} = $time; | |||||||||||||||||||||||||||
| 93 | 30 | $entry->{process} = $pid; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 30 | ▶ | if($self->{expand}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | my @fields = ref($self->{expand}) eq 'ARRAY' | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | ? @{$self->{expand}} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | : keys %$entry; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 7 | for my $field (@fields) { | |||||||||||||||||||||||||||
| 101 | 91 | $entry->{$field} = $self->_expand_field($field, $entry->{$field}); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 30 | $entry; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub _expand_field | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_expand_field: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 0 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Default behavior, subclasses might not expand anything | ||||||||||||||||||||||||||||
| 114 | 0 | $value; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub _transfer_statuses | ||||||||||||||||||||||||||||
| 118 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_transfer_statuses: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 2 | @TRANSFER_STATUSES; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub _common_regex | ||||||||||||||||||||||||||||
| 123 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_common_regex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | 4 | %COMMON_REGEX; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _parse_entry | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_parse_entry: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | 0 | croak __PACKAGE__, '->_parse_entry is abstract'; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | 1; | |||||||||||||||||||||||||||