blib/lib/Mojo/IOLoop/Tail.pm

filter: Statementstmt 30.7% 16 / 52 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 3 / 6 Podpod 0.0% 0 / 1 totaltotal 24.6% 19 / 77 CC 10 SCAR 40.1
CC
10
Cov
23%
__ANON__
90.0
run
6.0
__ANON__
1.1
CRAP 55.1
↑ Summary
1 package Mojo::IOLoop::Tail;
2  
3 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.02';
10  
11 =head1 NAME
12  
13 Mojo::IOLoop::Tail - IOLoop interface to tail a file asynchronously
14  
15 =head1 VERSION
16  
17 0.02
18  
19 =head1 DESCRIPTION
20  
21 This is an IOLoop interface to tail a file asynchronously
22  
23 =head1 SYNOPSIS
24  
25 use Mojo::Base -strict;
26  
27 use Mojo::IOLoop::Tail;
28  
29 my $tail = Mojo::IOLoop::Tail->new(file => "tail.me");
30  
31 $tail->on(oneline => sub {
32 my $tail = shift;
33 my $line = shift;
34  
35 print($line);
36 });
37  
38 $tail->run;
39  
40 $tail->ioloop->start unless $tail->ioloop->is_running;
41  
42 =cut
43  
44 0 ▶has 'ioloop' => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 1 has 'file';
46 1 has 'leftovers';
47 1 has 'whence';
48 1 has 'size';
49  
50 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my ($self, @args) = @_;
52  
53 0 my $fh = IO::File->new();
54 0 ▶ unless ($fh->open($self->file, "r")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($self->file, "r")) 0 0 0
55 0 $self->emit(error => sprintf("Can't open %s: $!", $self->file));
56 0 return;
57 }
58  
59 0 $fh->autoflush(1);
60 0 $fh->sysseek(0, 2);
61 0 $self->{whence} = $fh->sysseek(0, SEEK_CUR);
62 0 $self->{size} = ($fh->stat)[7];
63  
64 0 my $reactor = $self->ioloop->reactor;
65 $reactor->io($fh =>
66 0 ▶ sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
67 0 my $reactor = shift;
68  
69 0 my ($ret, $buf);
70  
71 0 my $size = ($fh->stat)[7];
72  
73 # Did we get truncated
74 0 ▶ if ($size < $self->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < $self->size) { } 0 0 0
75 0 $self->{size} = $size;
76 0 $fh->sysseek(0, 2);
77 0 $self->{whence} = $fh->sysseek(0, SEEK_CUR);
78 }
79 else {
80 0 $self->{size} = $size;
81 0 my $unread = $size - $self->whence;
82 0 ▶ return unless $unread;
Branch: 1 branch
BranchTrueFalseTotal
unless $unread 0 0 0
83 }
84  
85 0 ▶ while ($ret = $fh->sysread($buf, 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = $fh->sysread($buf, 1024)) 0 0 0
86 0 $self->{whence} = $fh->sysseek(0, SEEK_CUR); # == systell (IO::Async::FileStream)
87  
88 0 ▶ if ($self->leftovers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->leftovers) 0 0 0
89 0 $buf = $self->leftovers . $buf;
90  
91 0 $self->{leftovers} = undef;
92 }
93  
94 # eol patterns from IO::Async::Protocol::LineStream
95  
96 0 ▶ if ($buf !~ m/\x0d?\x0a/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /\x0d?\x0a/u) 0 0 0
97 0 $self->{leftovers} = $buf;
98 0 next;
99 }
100  
101 0 ▶ while ($buf =~ m/\G(.*?)(\x0d?\x0a)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /\G(.*?)(\x0d?\x0a)/gu) 0 0 0
102 0 $self->emit(oneline => "$1$2");
103 }
104 }
105  
106 0 ▶ $self->{leftovers} = $1 if $buf =~ m/\G(.{1,-})$/g;
Branch: 1 branch
BranchTrueFalseTotal
if $buf =~ /\G(.{1,-})$/gu 0 0 0
107  
108 0 ▶ if (!defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ret) 0 0 0
109 0 die("Error reading data\n");
110 }
111 }
112 0 )->watch($fh, 1, 0);
113  
114 0 return $self;
115 }
116  
117 1 1;
↑ Summary