| 1 | |||||||||||||||||||||||||||||||||
| 2 | package Logfile::Tail; | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | Logfile::Tail - read log files | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | use Logfile::Tail (); | ||||||||||||||||||||||||||||||||
| 11 | my $file = new Logfile::Tail('/var/log/messages'); | ||||||||||||||||||||||||||||||||
| 12 | while (<$file>) { | ||||||||||||||||||||||||||||||||
| 13 | # process the line | ||||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | and later in different process | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | my $file = new Logfile::Tail('/var/log/messages'); | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | and continue reading where we've left out the last time. Also possible | ||||||||||||||||||||||||||||||||
| 21 | is to explicitly save the current position: | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | my $file = new Logfile::Tail('/var/log/messages', | ||||||||||||||||||||||||||||||||
| 24 | { autocommit => 0 }); | ||||||||||||||||||||||||||||||||
| 25 | my $line = $file->getline(); | ||||||||||||||||||||||||||||||||
| 26 | $file->commit(); | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | 13 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 13 | our $VERSION = '0.9'; | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 13 | ▶ | use Symbol (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 13 | ▶ | use IO::File (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 13 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | 13 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | 13 | ▶ | use Fcntl qw( O_RDWR O_CREAT ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | 13 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 46 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 46 | my $class = shift; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 46 | my $self = Symbol::gensym(); | |||||||||||||||||||||||||||||||
| 46 | 46 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 47 | 46 | tie *$self, $self; | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 46 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 45 | ▶ | $self->open(@_) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 36 | return $self; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 13 | my $STATUS_SUBDIR = '.logfile-tail-status'; | |||||||||||||||||||||||||||||||
| 57 | 13 | my $CHECK_LENGTH = 512; | |||||||||||||||||||||||||||||||
| 58 | 45 | ▶ | sub open { | ||||||||||||||||||||||||||||||
|
Subroutine
open: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 59 | 45 | my $self = shift; | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 45 | my $filename = shift; | |||||||||||||||||||||||||||||||
| 62 | 45 | ▶ | if (@_ and ref $_[-1] eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and ref $_[-1] eq "HASH"
MC/DC100%2/2
@_ and ref $_[-1] eq "HASH"
@_
ref $_[-1] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[-1] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 63 | 13 | *$self->{opts} = pop @_; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 45 | ▶ | if (not exists *$self->{opts}{autocommit}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 41 | *$self->{opts}{autocommit} = 1; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 45 | my ($archive, $offset, $checksum) = $self->_load_data_from_status($filename); | |||||||||||||||||||||||||||||||
| 70 | 45 | ▶ | return unless defined $offset; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 40 | my $need_commit = *$self->{opts}{autocommit}; | |||||||||||||||||||||||||||||||
| 73 | 40 | ▶ | if (not defined $checksum) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 13 | $need_commit = 1; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 40 | ▶ | my ($fh, $content) = $self->_open(defined $archive ? $filename . $archive : $filename, $offset); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 40 | ▶ | if (not defined $fh) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 79 | 5 | ▶ | if (not defined $archive) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 3 | return; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 2 | my ($older_fh, $older_archive, $older_content) = $self->_get_archive($archive, 'older', $offset, $checksum); | |||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | if (defined $older_fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 1 | $fh = $older_fh; | |||||||||||||||||||||||||||||||
| 85 | 1 | $content = $older_content; | |||||||||||||||||||||||||||||||
| 86 | 1 | $archive = $older_archive; | |||||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||||||
| 88 | 1 | return; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | } elsif (not defined $checksum) { | ||||||||||||||||||||||||||||||||
| 91 | 11 | $content = $self->_seek_to($fh, 0); | |||||||||||||||||||||||||||||||
| 92 | ▶ | } elsif (not defined $content | |||||||||||||||||||||||||||||||
|
Condition100%3/3
not defined $content or $checksum ne &Digest::SHA::sha256_hex($content)
MC/DC100%2/2
not defined $content or $checksum ne &Digest::SHA::sha256_hex($content)
not defined $content
$checksum ne &Digest::SHA::sha256_hex($content)
Truth table100%3/3
not(A) or B
A= not defined $contentB= $checksum ne &Digest::SHA::sha256_hex($content)
| |||||||||||||||||||||||||||||||||
| 93 | or $checksum ne Digest::SHA::sha256_hex($content)) { | ||||||||||||||||||||||||||||||||
| 94 | 9 | my ($older_fh, $older_archive, $older_content) = $self->_get_archive($archive, 'older', $offset, $checksum); | |||||||||||||||||||||||||||||||
| 95 | 9 | ▶ | if (defined $older_fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 5 | $fh->close(); | |||||||||||||||||||||||||||||||
| 97 | 5 | $fh = $older_fh; | |||||||||||||||||||||||||||||||
| 98 | 5 | $content = $older_content; | |||||||||||||||||||||||||||||||
| 99 | 5 | $archive = $older_archive; | |||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||
| 101 | 4 | $content = $self->_seek_to($fh, 0); | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 36 | my $layers = $_[0]; | |||||||||||||||||||||||||||||||
| 106 | 36 | ▶ | if (defined $layers and $layers =~ /<:/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $layers and $layers =~ /<:/
MC/DC100%2/2
defined $layers and $layers =~ /<:/
defined $layers
$layers =~ /<:/
Truth table100%3/3
A and B
A= defined $layersB= $layers =~ /<:/
| |||||||||||||||||||||||||||||||||
| 107 | 2 | $layers =~ s!<:!<:scalar:!; | |||||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||||||
| 109 | 34 | $layers = '<:scalar'; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 36 | my $buffer = ''; | |||||||||||||||||||||||||||||||
| 113 | 36 | *$self->{int_buffer} = \$buffer; | |||||||||||||||||||||||||||||||
| 114 | 36 | my $int_fh; | |||||||||||||||||||||||||||||||
| 115 | 36 | ▶ | eval { open $int_fh, $layers, *$self->{int_buffer} }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 116 | 36 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 1 | warn "$@\n"; | |||||||||||||||||||||||||||||||
| 118 | 1 | return; | |||||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||||||
| 120 | 35 | *$self->{int_fh} = $int_fh; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 35 | *$self->{_fh} = $fh; | |||||||||||||||||||||||||||||||
| 123 | 35 | *$self->{data_array} = [ $content ]; | |||||||||||||||||||||||||||||||
| 124 | 35 | *$self->{data_length} = length $content; | |||||||||||||||||||||||||||||||
| 125 | 35 | *$self->{archive} = $archive; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 35 | ▶ | if ($need_commit) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 32 | $self->commit(); | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | 35 | 1; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 80 | ▶ | sub _open { | ||||||||||||||||||||||||||||||
|
Subroutine
_open: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 134 | 80 | my ($self, $filename, $offset) = @_; | |||||||||||||||||||||||||||||||
| 135 | 80 | ▶ | my $fh = new IO::File or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 78 | ▶ | $fh->open($filename, '<:raw') or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 69 | ▶ | if ($offset > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 35 | my $content = $self->_seek_to($fh, $offset); | |||||||||||||||||||||||||||||||
| 140 | 35 | return ($fh, $content); | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | 34 | return ($fh, ''); | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 2083 | ▶ | sub _fh { | ||||||||||||||||||||||||||||||
|
Subroutine
_fh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 146 | 2083 | *{$_[0]}->{_fh}; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 50 | ▶ | sub _seek_to { | ||||||||||||||||||||||||||||||
|
Subroutine
_seek_to: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 150 | 50 | my ($self, $fh, $offset) = @_; | |||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 50 | my $offset_start = $offset - $CHECK_LENGTH; | |||||||||||||||||||||||||||||||
| 153 | 50 | ▶ | $offset_start = 0 if $offset_start < 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | # no point in checking the return value, seek will | ||||||||||||||||||||||||||||||||
| 156 | # go beyond the end of the file anyway | ||||||||||||||||||||||||||||||||
| 157 | 50 | $fh->seek($offset_start, 0); | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 50 | my $buffer = ''; | |||||||||||||||||||||||||||||||
| 160 | 50 | ▶ | while ($offset - $offset_start > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | 38 | my $read = $fh->read($buffer, $offset - $offset_start, length($buffer)); | |||||||||||||||||||||||||||||||
| 162 | # $read is not defined for example when we try to read directory | ||||||||||||||||||||||||||||||||
| 163 | 38 | ▶ | last if not defined $read or $read <= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $read or $read <= 0
MC/DC100%2/2
not defined $read or $read <= 0
not defined $read
$read <= 0
Truth table100%3/3
not(A) or B
A= not defined $readB= $read <= 0
| |||||||||||||||||||||||||||||||||
| 164 | 30 | $offset_start += $read; | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | 50 | ▶ | if ($offset_start == $offset) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 42 | return $buffer; | |||||||||||||||||||||||||||||||
| 168 | } else { | ||||||||||||||||||||||||||||||||
| 169 | 8 | return; | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 45 | ▶ | sub _load_data_from_status { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_data_from_status: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 174 | 45 | my ($self, $log_filename) = @_; | |||||||||||||||||||||||||||||||
| 175 | 45 | my $abs_filename = Cwd::abs_path($log_filename); | |||||||||||||||||||||||||||||||
| 176 | 45 | ▶ | if (not defined $abs_filename) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | # can we access the file at all? | ||||||||||||||||||||||||||||||||
| 178 | 1 | warn "Cannot access file [$log_filename]\n"; | |||||||||||||||||||||||||||||||
| 179 | 1 | return; | |||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | 44 | my @abs_stat = stat $abs_filename; | |||||||||||||||||||||||||||||||
| 182 | 44 | ▶ | if (defined $abs_stat[1] and (stat $log_filename)[1] == $abs_stat[1]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $abs_stat[1] and (stat $log_filename)[1] == $abs_stat[1]
MC/DC100%2/2
defined $abs_stat[1] and (stat $log_filename)[1] == $abs_stat[1]
defined $abs_stat[1]
(stat $log_filename)[1] == $abs_stat[1]
Truth table100%3/3
A and B
A= defined $abs_stat[1]B= (stat $log_filename)[1] == $abs_stat[1]
| |||||||||||||||||||||||||||||||||
| 183 | 40 | $log_filename = $abs_filename; | |||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 44 | *$self->{filename} = $log_filename; | |||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 44 | my $status_filename = *$self->{opts}{status_file}; | |||||||||||||||||||||||||||||||
| 189 | 44 | ▶ | if (not defined $status_filename) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | 39 | $status_filename = Digest::SHA::sha256_hex($log_filename); | |||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | 44 | my $status_dir = *$self->{opts}{status_dir}; | |||||||||||||||||||||||||||||||
| 193 | 44 | ▶ | if (not defined $status_dir) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 194 | 38 | $status_dir = $STATUS_SUBDIR; | |||||||||||||||||||||||||||||||
| 195 | } elsif ($status_dir eq '') { | ||||||||||||||||||||||||||||||||
| 196 | 1 | $status_dir = '.'; | |||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | 44 | ▶ | if (not -d $status_dir) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 5 | mkdir $status_dir, 0775; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | 44 | my $status_path = File::Spec->catfile($status_dir, $status_filename); | |||||||||||||||||||||||||||||||
| 202 | 44 | my $status_fh = new IO::File $status_path, O_RDWR | O_CREAT; | |||||||||||||||||||||||||||||||
| 203 | 44 | ▶ | if (not defined $status_fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 2 | warn "Error reading/creating status file [$status_path]\n"; | |||||||||||||||||||||||||||||||
| 205 | 2 | return; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | 42 | *$self->{status_fh} = $status_fh; | |||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 42 | my $status_line = <$status_fh>; | |||||||||||||||||||||||||||||||
| 210 | 42 | my ($offset, $checksum, $archive_filename) = (0, undef, undef); | |||||||||||||||||||||||||||||||
| 211 | 42 | ▶ | if (defined $status_line) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | 29 | ▶ | if (not $status_line =~ /^File \[(.+?)\] (?:archive \[(.+)\] )?offset \[(\d+)\] checksum \[([0-9a-z]+)\]\n/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 213 | 1 | warn "Status file [$status_path] has bad format\n"; | |||||||||||||||||||||||||||||||
| 214 | 1 | return; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | 28 | my $check_filename = $1; | |||||||||||||||||||||||||||||||
| 217 | 28 | $archive_filename = $2; | |||||||||||||||||||||||||||||||
| 218 | 28 | $offset = $3; | |||||||||||||||||||||||||||||||
| 219 | 28 | $checksum = $4; | |||||||||||||||||||||||||||||||
| 220 | 28 | ▶ | if ($check_filename ne $log_filename) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | 1 | warn "Status file [$status_path] is for file [$check_filename] while expected [$log_filename]\n"; | |||||||||||||||||||||||||||||||
| 222 | 1 | return; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | 40 | return ($archive_filename, $offset, $checksum); | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 70 | ▶ | sub _save_offset_to_status { | ||||||||||||||||||||||||||||||
|
Subroutine
_save_offset_to_status: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 230 | 70 | my ($self, $offset) = @_; | |||||||||||||||||||||||||||||||
| 231 | 70 | my $log_filename = *$self->{filename}; | |||||||||||||||||||||||||||||||
| 232 | 70 | my $status_fh = *$self->{status_fh}; | |||||||||||||||||||||||||||||||
| 233 | 70 | my $checksum = $self->_get_current_checksum(); | |||||||||||||||||||||||||||||||
| 234 | 70 | ▶ | $status_fh->seek(0, 0) if defined $status_fh; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 235 | 70 | ▶ | my $archive_text = defined *$self->{archive} ? " archive [@{[ *$self->{archive} ]}]" : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | 70 | $status_fh->printflush("File [$log_filename]$archive_text offset [$offset] checksum [$checksum]\n"); | |||||||||||||||||||||||||||||||
| 237 | 70 | $status_fh->truncate($status_fh->tell); | |||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | 2068 | ▶ | sub _push_to_data { | ||||||||||||||||||||||||||||||
|
Subroutine
_push_to_data: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 241 | 2068 | my $self = shift; | |||||||||||||||||||||||||||||||
| 242 | 2068 | my $chunk = shift; | |||||||||||||||||||||||||||||||
| 243 | 2068 | ▶ | if (length($chunk) >= $CHECK_LENGTH) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 244 | 1 | *$self->{data_array} = [ substr $chunk, -$CHECK_LENGTH ]; | |||||||||||||||||||||||||||||||
| 245 | 1 | *$self->{data_length} = $CHECK_LENGTH; | |||||||||||||||||||||||||||||||
| 246 | 1 | return; | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | 2067 | my $data = *$self->{data_array}; | |||||||||||||||||||||||||||||||
| 249 | 2067 | my $data_length = *$self->{data_length}; | |||||||||||||||||||||||||||||||
| 250 | 2067 | push @$data, $chunk; | |||||||||||||||||||||||||||||||
| 251 | 2067 | $data_length += length($chunk); | |||||||||||||||||||||||||||||||
| 252 | 2067 | ▶ | while ($data_length - length($data->[0]) >= $CHECK_LENGTH) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 253 | 1895 | $data_length -= length($data->[0]); | |||||||||||||||||||||||||||||||
| 254 | 1895 | shift @$data; | |||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||
| 256 | 2067 | *$self->{data_length} = $data_length; | |||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | 74 | ▶ | sub _get_current_checksum { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_current_checksum: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 260 | 74 | my $self = shift; | |||||||||||||||||||||||||||||||
| 261 | 74 | my $data_length = *$self->{data_length}; | |||||||||||||||||||||||||||||||
| 262 | 74 | my $data = *$self->{data_array}; | |||||||||||||||||||||||||||||||
| 263 | 74 | my $i = 0; | |||||||||||||||||||||||||||||||
| 264 | 74 | my $digest = new Digest::SHA('sha256'); | |||||||||||||||||||||||||||||||
| 265 | 74 | ▶ | if ($data_length > $CHECK_LENGTH) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 266 | 1 | $digest->add(substr($data->[0], $data_length - $CHECK_LENGTH)); | |||||||||||||||||||||||||||||||
| 267 | 1 | $i++; | |||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||
| 269 | 74 | ▶ | for (; $i <= $#$data; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 270 | 156 | $digest->add($data->[$i]); | |||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||
| 272 | 74 | return $digest->hexdigest(); | |||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||
| 275 | 36 | ▶ | sub _get_archive { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_archive: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||||||
| 276 | 36 | my ($self, $start, $older_newer, $offset, $checksum) = @_; | |||||||||||||||||||||||||||||||
| 277 | 36 | my @types = ( '-', '.' ); | |||||||||||||||||||||||||||||||
| 278 | 36 | my $start_num; | |||||||||||||||||||||||||||||||
| 279 | 36 | ▶ | if (defined $start) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 280 | 27 | @types = substr($start, 0, 1); | |||||||||||||||||||||||||||||||
| 281 | 27 | $start_num = substr($start, 1); | |||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||
| 283 | 36 | my $filename = *$self->{filename}; | |||||||||||||||||||||||||||||||
| 284 | 36 | for my $t (@types) { | |||||||||||||||||||||||||||||||
| 285 | 43 | my $srt; | |||||||||||||||||||||||||||||||
| 286 | 43 | ▶ | if ($t eq '.') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 287 | 30 | ▶ | if ($older_newer eq 'newer') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | 17 | ▶ | $srt = sub { $_[1] <=> $_[0] }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 289 | } else { | ||||||||||||||||||||||||||||||||
| 290 | 13 | ▶ | $srt = sub { $_[0] <=> $_[1] }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | } else { | ||||||||||||||||||||||||||||||||
| 293 | 13 | ▶ | if ($older_newer eq 'newer') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 294 | 4 | ▶ | $srt = sub { $_[0] cmp $_[1] }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||||||
| 296 | 9 | ▶ | $srt = sub { $_[1] cmp $_[0] }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | 50 | my @archives = map { "$t$_" } # make it a suffix | |||||||||||||||||||||||||||||||
| 300 | 36 | sort { $srt->($a, $b) } # sort properly | |||||||||||||||||||||||||||||||
| 301 | 125 | ▶ | grep { not defined $start_num or $srt->($_, $start_num) == 1} # only newer / older | ||||||||||||||||||||||||||||||
|
Condition100%3/3
!defined($start_num) || &$srt($_, $start_num) == 1
MC/DC100%2/2
!defined($start_num) || &$srt($_, $start_num) == 1
!defined($start_num)
&$srt($_, $start_num) == 1
Truth table100%3/3
not(A) || B
A= !defined($start_num)B= &$srt($_, $start_num) == 1
| |||||||||||||||||||||||||||||||||
| 302 | 125 | grep { /^[0-9]+$/ } # only numerical suffixes | |||||||||||||||||||||||||||||||
| 303 | 43 | map { substr($_, length($filename) + 1) } # only get the numerical suffixes | |||||||||||||||||||||||||||||||
| 304 | 13 | ▶ | glob "$filename$t*"; # we look at file.1, file.2 or file-20091231, ... | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 305 | 43 | ▶ | if ($older_newer eq 'newer' and -f $filename) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$older_newer eq "newer" and -f $filename
MC/DC100%2/2
$older_newer eq "newer" and -f $filename
$older_newer eq "newer"
-f $filename
Truth table100%3/3
A and B
A= $older_newer eq "newer"B= -f $filename
| |||||||||||||||||||||||||||||||||
| 306 | 17 | push @archives, ''; | |||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||
| 308 | 43 | for my $a (@archives) { | |||||||||||||||||||||||||||||||
| 309 | 40 | ▶ | my ($fh, $content) = $self->_open($filename . $a, ($offset || 0)); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$offset || 0
MC/DC100%1/1
$offset || 0
$offset
Truth table100%2/2
A
A= $offset
| |||||||||||||||||||||||||||||||||
| 310 | 40 | ▶ | if (not defined $fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 311 | 6 | next; | |||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||
| 313 | 34 | ▶ | if (defined $checksum) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 314 | 14 | ▶ | if (defined $content | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $content and $checksum eq &Digest::SHA::sha256_hex($content)
MC/DC100%2/2
defined $content and $checksum eq &Digest::SHA::sha256_hex($content)
defined $content
$checksum eq &Digest::SHA::sha256_hex($content)
Truth table100%3/3
A and B
A= defined $contentB= $checksum eq &Digest::SHA::sha256_hex($content)
| |||||||||||||||||||||||||||||||||
| 315 | and $checksum eq Digest::SHA::sha256_hex($content)) { | ||||||||||||||||||||||||||||||||
| 316 | 9 | return ($fh, $a, $content); | |||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | } else { | ||||||||||||||||||||||||||||||||
| 319 | 20 | ▶ | return ($fh, ($a eq '' ? undef : $a), $content); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | 5 | $fh->close(); | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | 7 | return; | |||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | 35 | ▶ | sub _close_status { | ||||||||||||||||||||||||||||||
|
Subroutine
_close_status: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 328 | 35 | my ($self, $offset) = @_; | |||||||||||||||||||||||||||||||
| 329 | 35 | my $status_fh = delete *$self->{status_fh}; | |||||||||||||||||||||||||||||||
| 330 | 35 | ▶ | $status_fh->close() if defined $status_fh; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 2083 | ▶ | sub _getline { | ||||||||||||||||||||||||||||||
|
Subroutine
_getline: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||
| 334 | 2083 | my $self = shift; | |||||||||||||||||||||||||||||||
| 335 | 2083 | my $fh = $self->_fh; | |||||||||||||||||||||||||||||||
| 336 | 2083 | ▶ | if (defined $fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 337 | 2082 | my $buffer_ref = *$self->{int_buffer}; | |||||||||||||||||||||||||||||||
| 338 | 2105 | DO_GETLINE: | |||||||||||||||||||||||||||||||
| 339 | my $ret = undef; | ||||||||||||||||||||||||||||||||
| 340 | 2105 | $$buffer_ref = $fh->getline(); | |||||||||||||||||||||||||||||||
| 341 | 2105 | ▶ | if (not defined $$buffer_ref) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 342 | # we are at the end of the current file | ||||||||||||||||||||||||||||||||
| 343 | # we need to check if the file was rotated | ||||||||||||||||||||||||||||||||
| 344 | # in the meantime | ||||||||||||||||||||||||||||||||
| 345 | 37 | my @fh_stat = stat($fh); | |||||||||||||||||||||||||||||||
| 346 | 37 | my $filename = *$self->{filename}; | |||||||||||||||||||||||||||||||
| 347 | 37 | ▶ | my @file_stat = stat($filename . ( defined *$self->{archive} ? *$self->{archive} : '' )); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 348 | 37 | ▶ | if (not @file_stat or "@fh_stat[0, 1]" ne "@file_stat[0, 1]") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not @file_stat or join($", @fh_stat[0, 1]) ne join($", @file_stat[0, 1])
MC/DC100%2/2
not @file_stat or join($", @fh_stat[0, 1]) ne join($", @file_stat[0, 1])
not @file_stat
join($", @fh_stat[0, 1]) ne join($", @file_stat[0, 1])
Truth table100%3/3
not(A) or B
A= not @file_statB= join($", @fh_stat[0, 1]) ne join($", @file_stat[0, 1])
| |||||||||||||||||||||||||||||||||
| 349 | # our file was rotated, or generally | ||||||||||||||||||||||||||||||||
| 350 | # is no longer where it was when | ||||||||||||||||||||||||||||||||
| 351 | # we started to read | ||||||||||||||||||||||||||||||||
| 352 | my ($older_fh, $older_archive, $older_content) | ||||||||||||||||||||||||||||||||
| 353 | 4 | = $self->_get_archive(*$self->{archive}, 'older', $fh->tell, $self->_get_current_checksum); | |||||||||||||||||||||||||||||||
| 354 | 4 | ▶ | if (not defined $older_fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 355 | # we have lost the file / sync | ||||||||||||||||||||||||||||||||
| 356 | 1 | return; | |||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||
| 358 | 3 | *$self->{_fh}->close(); | |||||||||||||||||||||||||||||||
| 359 | 3 | *$self->{_fh} = $fh = $older_fh; | |||||||||||||||||||||||||||||||
| 360 | 3 | *$self->{data_array} = [ $older_content ]; | |||||||||||||||||||||||||||||||
| 361 | 3 | *$self->{data_length} = length $older_content; | |||||||||||||||||||||||||||||||
| 362 | 3 | *$self->{archive} = $older_archive; | |||||||||||||||||||||||||||||||
| 363 | 3 | goto DO_GETLINE; | |||||||||||||||||||||||||||||||
| 364 | } elsif (defined *$self->{archive}) { | ||||||||||||||||||||||||||||||||
| 365 | # our file was not rotated | ||||||||||||||||||||||||||||||||
| 366 | # however, if our file is in fact | ||||||||||||||||||||||||||||||||
| 367 | # a rotate file, we should go to the | ||||||||||||||||||||||||||||||||
| 368 | # next one | ||||||||||||||||||||||||||||||||
| 369 | 21 | my ($newer_fh, $newer_archive) = $self->_get_archive(*$self->{archive}, 'newer'); | |||||||||||||||||||||||||||||||
| 370 | 21 | ▶ | if (not defined $newer_fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 371 | # clear the EOF flag to allow for repeated read | ||||||||||||||||||||||||||||||||
| 372 | 1 | $fh->seek(0, 1); | |||||||||||||||||||||||||||||||
| 373 | 1 | return; | |||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||
| 375 | 20 | *$self->{_fh}->close(); | |||||||||||||||||||||||||||||||
| 376 | 20 | *$self->{_fh} = $fh = $newer_fh; | |||||||||||||||||||||||||||||||
| 377 | 20 | *$self->{data_array} = [ '' ]; | |||||||||||||||||||||||||||||||
| 378 | 20 | *$self->{data_length} = 0; | |||||||||||||||||||||||||||||||
| 379 | 20 | *$self->{archive} = $newer_archive; | |||||||||||||||||||||||||||||||
| 380 | 20 | goto DO_GETLINE; | |||||||||||||||||||||||||||||||
| 381 | } else { | ||||||||||||||||||||||||||||||||
| 382 | # clear the EOF flag to allow for repeated read | ||||||||||||||||||||||||||||||||
| 383 | 12 | $fh->seek(0, 1); | |||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||
| 385 | 12 | return; | |||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||
| 387 | 2068 | $self->_push_to_data($$buffer_ref); | |||||||||||||||||||||||||||||||
| 388 | 2068 | seek(*$self->{int_fh}, 0, 0); | |||||||||||||||||||||||||||||||
| 389 | 2068 | my $line = *$self->{int_fh}->getline(); | |||||||||||||||||||||||||||||||
| 390 | 2068 | return $line; | |||||||||||||||||||||||||||||||
| 391 | } else { | ||||||||||||||||||||||||||||||||
| 392 | 1 | return undef; | |||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||
| 396 | 59 | ▶ | sub getline { | ||||||||||||||||||||||||||||||
|
Subroutine
getline: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 397 | 59 | my $self = shift; | |||||||||||||||||||||||||||||||
| 398 | 59 | my $ret = $self->_getline(); | |||||||||||||||||||||||||||||||
| 399 | 13 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 400 | 59 | ▶ | if (*$self->{opts}{autocommit} == 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 401 | 1 | $self->commit(); | |||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||
| 403 | 59 | return $ret; | |||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | 7 | ▶ | sub getlines { | ||||||||||||||||||||||||||||||
|
Subroutine
getlines: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 407 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||
| 408 | 7 | my @out; | |||||||||||||||||||||||||||||||
| 409 | 7 | while (1) { | |||||||||||||||||||||||||||||||
| 410 | 2024 | my $l = $self->_getline(); | |||||||||||||||||||||||||||||||
| 411 | 2024 | ▶ | if (not defined $l) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 412 | 7 | last; | |||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||
| 414 | 2017 | push @out, $l; | |||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||
| 416 | 13 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 417 | 7 | ▶ | if (*$self->{opts}{autocommit} == 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 418 | 1 | $self->commit(); | |||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||
| 420 | 7 | @out; | |||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | 70 | ▶ | sub commit { | ||||||||||||||||||||||||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 424 | 70 | my $self = shift; | |||||||||||||||||||||||||||||||
| 425 | 70 | my $fh = *$self->{_fh}; | |||||||||||||||||||||||||||||||
| 426 | 70 | my $offset = $fh->tell; | |||||||||||||||||||||||||||||||
| 427 | 70 | $self->_save_offset_to_status($offset); | |||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | 35 | ▶ | sub close { | ||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 431 | 35 | my $self = shift; | |||||||||||||||||||||||||||||||
| 432 | 35 | ▶ | if (*$self->{opts}{autocommit}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 433 | 31 | $self->commit(); | |||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||
| 435 | 35 | $self->_close_status(); | |||||||||||||||||||||||||||||||
| 436 | 35 | my $fh = delete *$self->{_fh}; | |||||||||||||||||||||||||||||||
| 437 | 35 | ▶ | $fh->close() if defined $fh; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||
| 440 | 47 | ▶ | sub TIEHANDLE() { | ||||||||||||||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 441 | 47 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 442 | # if we already have object, probably called from new(), | ||||||||||||||||||||||||||||||||
| 443 | # just return that | ||||||||||||||||||||||||||||||||
| 444 | 46 | return $_[0]; | |||||||||||||||||||||||||||||||
| 445 | } else { | ||||||||||||||||||||||||||||||||
| 446 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||
| 447 | 1 | return $class->new(@_); | |||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | 29 | ▶ | sub READLINE() { | ||||||||||||||||||||||||||||||
|
Subroutine
READLINE: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 452 | 29 | ▶ | goto &getlines if wantarray; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 453 | 24 | goto &getline; | |||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | 1 | ▶ | sub CLOSE() { | ||||||||||||||||||||||||||||||
|
Subroutine
CLOSE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 457 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 458 | 1 | $self->close(); | |||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||
| 461 | 45 | ▶ | sub DESTROY() { | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 462 | 45 | my $self = shift; | |||||||||||||||||||||||||||||||
| 463 | 45 | ▶ | $self->close() if defined *$self->{_fh}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | 13 | 1; | |||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||
| 470 | Log files are files that are generated by various running programs. | ||||||||||||||||||||||||||||||||
| 471 | They are generally only appended to. When parsing information from | ||||||||||||||||||||||||||||||||
| 472 | log files, it is important to only read each record / line once, | ||||||||||||||||||||||||||||||||
| 473 | both for performance and for accounting and statistics reasons. | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | The C<Logfile::Tail> provides an easy way to achieve the | ||||||||||||||||||||||||||||||||
| 476 | read-just-once processing of log files. | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | The module remembers for each file the position where it left | ||||||||||||||||||||||||||||||||
| 479 | out the last time, in external status file, and upon next invocation | ||||||||||||||||||||||||||||||||
| 480 | it seeks to the remembered position. It also stores checksum | ||||||||||||||||||||||||||||||||
| 481 | of 512 bytes before that position, and if the checksum does not | ||||||||||||||||||||||||||||||||
| 482 | match the file content the next time it is read, it will try to | ||||||||||||||||||||||||||||||||
| 483 | find the rotated file and read the end of it before advancing to | ||||||||||||||||||||||||||||||||
| 484 | newer rotated file or to the current log file. | ||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||
| 486 | Both .num and -date suffixed rotated files are supported. | ||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||
| 488 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||
| 490 | =over 4 | ||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||
| 492 | =item new() | ||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||
| 494 | =item new( FILENAME [,MODE [,PERMS]], [ { attributes } ] ) | ||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||
| 496 | =item new( FILENAME, IOLAYERS, [ { attributes } ] ) | ||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||
| 498 | Constructor, creates new C<Logfile::Tail> object. Like C<IO::File>, | ||||||||||||||||||||||||||||||||
| 499 | it passes any parameters to method C<open>; it actually creates | ||||||||||||||||||||||||||||||||
| 500 | an C<IO::File> handle internally. | ||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||
| 502 | Returns new object, or undef upon error. | ||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||
| 504 | =item open( FILENAME [,MODE [,PERMS]], [ { attributes } ] ) | ||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||
| 506 | =item open( FILENAME, IOLAYERS, [ { attributes } ] ) | ||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||
| 508 | Opens the file using C<IO::File>. If the file was read before, the | ||||||||||||||||||||||||||||||||
| 509 | offset where the reading left out the last time is read from an | ||||||||||||||||||||||||||||||||
| 510 | external file in the ./.logfile-tail-status directory and seek is | ||||||||||||||||||||||||||||||||
| 511 | made to that offset, to continue reading at the last remembered | ||||||||||||||||||||||||||||||||
| 512 | position. | ||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||
| 514 | If however checksum, which is also stored with the offset, does not | ||||||||||||||||||||||||||||||||
| 515 | match the current content of the file (512 bytes before the offset | ||||||||||||||||||||||||||||||||
| 516 | are checked), the module assumes that the file was rotated / reused | ||||||||||||||||||||||||||||||||
| 517 | / truncated in the mean time since the last read. It will try to | ||||||||||||||||||||||||||||||||
| 518 | find the checksum among the rotated files. If no match is found, | ||||||||||||||||||||||||||||||||
| 519 | it will reset the offset to zero and start from the beginning of | ||||||||||||||||||||||||||||||||
| 520 | the file. | ||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||
| 522 | Returns true, or undef upon error. | ||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||
| 524 | The attributes are passed as an optional hashref of key => value | ||||||||||||||||||||||||||||||||
| 525 | pairs. The supported attribute is | ||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||
| 527 | =over 4 | ||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||
| 529 | =item autocommit | ||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||
| 531 | Value 0 means that no saving takes place; you need to save explicitly | ||||||||||||||||||||||||||||||||
| 532 | using the commit() method. | ||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||
| 534 | Value 1 (the default) means that position is saved when the object is | ||||||||||||||||||||||||||||||||
| 535 | closed via explicit close() call, or when it is destroyed. The value | ||||||||||||||||||||||||||||||||
| 536 | is also saved upon the first open. | ||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||
| 538 | Value 2 causes the position to be save in all cases as value 1, | ||||||||||||||||||||||||||||||||
| 539 | plus after each successful read. | ||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | =item status_dir | ||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||
| 543 | The attribute specifies the directory (or subdirectory of current | ||||||||||||||||||||||||||||||||
| 544 | directory) which is used to hold status files. By default, | ||||||||||||||||||||||||||||||||
| 545 | ./.logfile-tail-status directory is used. To store the status | ||||||||||||||||||||||||||||||||
| 546 | files in the current directory, pass empty string or dot (.). | ||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | =item status_file | ||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||
| 550 | The attribute specifies the name of the status file which is used to | ||||||||||||||||||||||||||||||||
| 551 | hold the offset and SHA256 checksum of 512 bytes before the offset. | ||||||||||||||||||||||||||||||||
| 552 | By default, SHA256 of the full (absolute) logfile filename is used | ||||||||||||||||||||||||||||||||
| 553 | as the status file name. | ||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||
| 555 | =back | ||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||
| 557 | =item commit() | ||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||
| 559 | Explicitly save the current position and checksum in the status file. | ||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||
| 561 | Returns true, or undef upon error. | ||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||
| 563 | =item close() | ||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | Closes the internal filehandle. It stores the current position | ||||||||||||||||||||||||||||||||
| 566 | and checksum in an external file in the ./.logfile-tail-status | ||||||||||||||||||||||||||||||||
| 567 | directory. | ||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||
| 569 | Returns true, or undef upon error. | ||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||
| 571 | =item getline() | ||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||
| 573 | Line <$fh> in scalar context. | ||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||
| 575 | =item getlines() | ||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | Line <$fh> in list context. | ||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||
| 579 | =back | ||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||
| 581 | =head1 AUTHOR AND LICENSE | ||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||
| 583 | Copyright (c) 2010--2024 Jan Pazdziora. | ||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||
| 585 | Logfile::Tail is free software. You can redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 586 | it under the terms of either: | ||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||
| 588 | a) the GNU General Public License, version 2 or 3; | ||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||
| 590 | b) the Artistic License, either the original or version 2.0. | ||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||