Debian Codegraph @

lib/Test2/Harness/Util/File/Stream.pm

raw
1 package Test2::Harness::Util::File::Stream;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Carp qw/croak/;
8 use Test2::Harness::Util qw/lock_file unlock_file/;
9 use Fcntl qw/SEEK_SET/;
10
11 use parent 'Test2::Harness::Util::File';
12 use Test2::Harness::Util::HashBase qw/use_write_lock -tail/;
13
14 sub init {
15 my $self = shift;
16
17 $self->SUPER::init();
18
19 my $tail = $self->{+TAIL} or return;
20
21 return unless $self->exists;
22
23 my @lines = $self->poll_with_index;
24 if (@lines < $self->{+TAIL}) {
25 $self->seek(0);
26 }
27 else {
28 $self->seek($lines[0 - $tail]->[0]);
29 }
30 }
31
32 sub poll_with_index {
33 my $self = shift;
34 my %params = @_;
35
36 my $max = delete $params{max} || 0;
37
38 my $pos = $params{from};
39 $pos = $self->{+LINE_POS} ||= 0 unless defined $pos;
40
41 my @out;
42 while (!$max || @out < $max) {
43 my ($spos, $epos, $line, $err) = $self->read_line(%params, from => $pos);
44 last unless defined($line) || defined($spos) || defined($epos) || $err;
45
46 $self->{+LINE_POS} = $epos unless $params{peek} || defined $params{from};
47 push @out => [$spos, $epos, $line] unless $err;
48 $pos = $epos;
49 }
50
51 return @out;
52 }
53
54 sub read {
55 my $self = shift;
56
57 return $self->poll(from => 0);
58 }
59
60 sub poll {
61 my $self = shift;
62 my @lines = $self->poll_with_index(@_);
63 return map { $_->[-1] } @lines;
64 }
65
66 sub write {
67 my $self = shift;
68
69 my $name = $self->{+NAME};
70
71 my $fh;
72 if ($self->{+USE_WRITE_LOCK}) {
73 $fh = lock_file($self->name, '>>');
74 }
75 else {
76 $fh = Test2::Harness::Util::open_file($self->name, '>>');
77 }
78
79 $fh->autoflush(1);
80 seek($fh,2,0);
81 print {$fh} $self->encode($_) for @_;
82
83 unlock_file($fh) if $self->{+USE_WRITE_LOCK};
84
85 close($fh) or die "Could not close file '$name': $!";
86
87 return @_;
88 }
89
90 sub seek {
91 my $self = shift;
92 my ($pos) = @_;
93
94 my $fh = $self->fh;
95 my $name = $self->{+NAME};
96
97 seek($fh, $pos, SEEK_SET) or die "Could not seek to position $pos in file '$name': $!";
98 $self->{+LINE_POS} = $pos;
99 }
100
101 1;
102
103 __END__
104
105 =pod
106
107 =encoding UTF-8
108
109 =head1 NAME
110
111 Test2::Harness::Util::File::Stream - Utility class for manipulating a file that
112 serves as an output stream.
113
114 =head1 DESCRIPTION
115
116 Subclass of L<Test2::Harness::File> that streams the contents of a file, even
117 if the file is still being written.
118
119 =head1 SYNOPSIS
120
121 use Test2::Harness::Util::File::Stream;
122
123 my $stream = Test2::Harness::Util::File::Stream->new(name => 'path/to/file');
124
125 # Read some lines
126 my @lines = $stream->poll;
127
128 ...
129
130 # Read more lines, if any.
131 push @lines => $stream->poll;
132
133 =head1 ATTRIBUTES
134
135 See L<Test2::Harness::File> for additional attributes.
136
137 These can be passed in as construction arguments if desired.
138
139 =over 4
140
141 =item $bool = $stream->use_write_lock
142
143 =item $stream->use_write_lock($bool)
144
145 Lock the file for every C<write()> operation.
146
147 =item $bool = $stream->tail
148
149 Start near the end of the file and only poll for updates appended to it.
150
151 =back
152
153 =head1 METHODS
154
155 See L<Test2::Harness::File> for additional methods.
156
157 =over 4
158
159 =item @lines = $stream->read()
160
161 Read all lines from the beginning. Every time it is called it returns ALL lines.
162
163 =item @lines = $stream->poll()
164
165 =item @lines = $stream->poll(max => $int)
166
167 Poll for lines. This is an iterator, it should not return the same line more
168 than once, you can call it multiple times to get any additional lines that have
169 been added since the last poll.
170
171 =item $stream->write(@content)
172
173 Append @content to the file.
174
175 =back
176
177 =head1 SOURCE
178
179 The source code repository for Test2-Harness can be found at
180 F<http://github.com/Test-More/Test2-Harness/>.
181
182 =head1 MAINTAINERS
183
184 =over 4
185
186 =item Chad Granum E<lt>[email protected]<gt>
187
188 =back
189
190 =head1 AUTHORS
191
192 =over 4
193
194 =item Chad Granum E<lt>[email protected]<gt>
195
196 =back
197
198 =head1 COPYRIGHT
199
200 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
201
202 This program is free software; you can redistribute it and/or
203 modify it under the same terms as Perl itself.
204
205 See F<http://dev.perl.org/licenses/>
206
207 =cut