blib/lib/AnyEvent/Multilog.pm

filter: Statementstmt 32.1% 28 / 87 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 26.3% 5 / 19 Podpod 62.5% 5 / 8 totaltotal 26.0% 38 / 146 CC 15 SCAR 27.8
CC
15
Cov
24%
handle_completion
56.0
ensure_validity
12.0
_build_job
12.0
CRAP 16.1
↑ Summary
1 package AnyEvent::Multilog;
2 # ABSTRACT: event-driven interface to a multilog process
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use MooseX::Types::Path::Class qw(File);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use AnyEvent::Subprocess;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use AnyEvent::Subprocess::Job::Delegate::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.01';
10  
11 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 has 'multilog' => (
14 is => 'ro',
15 isa => File,
16 predicate => 'has_multilog_path',
17 coerce => 1,
18 documentation => q{path to multilog, if you don't want to use $PATH},
19 );
20  
21 3 has 'script' => (
22 is => 'ro',
23 isa => 'ArrayRef[Str]',
24 required => 1,
25 documentation => 'multilog "script", not escaped for the shell',
26 );
27  
28 3 has '_job' => (
29 init_arg => undef,
30 reader => '_job',
31 lazy => 1,
32 builder => '_build_job',
33 );
34  
35 has 'job_args' => (
36 is => 'ro',
37 isa => 'HashRef',
38 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 3 );
40  
41 3 has 'run' => (
42 init_arg => undef,
43 reader => 'run',
44 lazy_build => 1,
45 );
46  
47 3 has 'on_exit' => (
48 is => 'ro',
49 isa => 'CodeRef',
50 predicate => 'has_exit_handler',
51 documentation => 'optional callback called when multilog exists successfully',
52 );
53  
54 3 has 'on_error' => (
55 is => 'ro',
56 isa => 'CodeRef',
57 predicate => 'has_error_handler',
58 documentation => 'optional callback called when multilog emits an error',
59 );
60  
61 has 'errors' => (
62 is => 'bare', # uh, no it's not, but mooose bug
63 init_arg => undef,
64 traits => ['Array'],
65 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 3 lazy => 1,
67 handles => {
68 push_error => 'push',
69 has_errors => 'count',
70 errors => 'elements',
71 },
72 );
73  
74 3 has 'is_shutdown' => (
75 init_arg => undef,
76 accessor => 'is_shutdown',
77 isa => 'Bool',
78 );
79  
80 3 has 'leftover_data' => (
81 init_arg => undef,
82 reader => 'leftover_data',
83 writer => 'set_leftover_data',
84 predicate => 'has_leftover_data',
85 );
86  
87  
88 0 ▶sub ensure_validity {
Subroutine ensure_validity: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my $self = shift;
90 0 ▶ confess 'already shutdown, cannot perform further operations' if $self->is_shutdown;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_shutdown 0 0 0
91 0 ▶ confess(join ', ', $self->errors) if $self->has_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_errors 0 0 0
92 }
93  
94 0 ▶sub _build_job {
Subroutine _build_job: not called CC=3 SCAR=24.8
95 0 my $self = shift;
96  
97 0 my $input = AnyEvent::Subprocess::Job::Delegate::Handle->new(
98 name => 'input_handle',
99 direction => 'w',
100 replace => 0,
101 want_leftovers => 1,
102 );
103  
104 0 my $errors = AnyEvent::Subprocess::Job::Delegate::Handle->new(
105 name => 'error_handle',
106 direction => 'r',
107 replace => 2,
108 );
109  
110 0 ▶ my $extra_delegates = delete $self->job_args->{delegates} || [];
Condition0%0/2
delete $self->job_args->{'delegates'} || []
l!l
00
MC/DC0%0/1
delete $self->job_args->{'delegates'} || []
delete $self->job_args->{'delegates'}
Truth table0%0/2
A
A= delete $self->job_args->{'delegates'}
Aresult
11
00
111  
112 0 ▶ my $multilog = $self->has_multilog_path ? $self->multilog->stringify : 'multilog';
Branch: 1 branch
BranchTrueFalseTotal
$self->has_multilog_path ? : 0 0 0
113  
114 return AnyEvent::Subprocess->new(
115 0 %{ $self->job_args },
116 0 delegates => [ @{$extra_delegates}, $input, $errors ],
117 0 ▶ on_completion => sub { $self->handle_completion($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 code => [ $multilog, @{$self->script} ],
119 );
120 }
121  
122 0 ▶sub _build_run {
Subroutine _build_run: not called CC=1 SCAR=6.9
123 0 my $self = shift;
124 0 my $run = $self->_job->run;
125  
126 0 my $errors = $run->delegate('error_handle');
127  
128 0 ▶ my $error_cb; $error_cb = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 my ($h, $line, $eol) = @_;
130 0 $self->handle_error($line);
131 0 $h->push_read( line => $error_cb );
132 0 };
133 0 $errors->handle->push_read( line => $error_cb );
134  
135 0 $run->delegate('input_handle')->handle->{linger} = 0;
136  
137 0 return $run;
138 }
139  
140 0 ▶sub handle_error {
Subroutine handle_error: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my ($self, $msg) = @_;
142 0 ▶ $self->on_error->($msg) if $self->has_error_handler;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error_handler 0 0 0
143 0 $self->push_error($msg);
144 0 return;
145 }
146  
147 0 ▶sub handle_completion {
Subroutine handle_completion: not called CC=7 SCAR=40.3
Pod undocumented
148 0 my ($self, $done) = @_;
149 0 my ($success, $msg);
150  
151 0 ▶ $self->set_leftover_data( $done->delegate('input_handle')->wbuf )
Branch: 1 branch
BranchTrueFalseTotal
if $done->delegate("input_handle")->has_wbuf 0 0 0
152 if $done->delegate('input_handle')->has_wbuf;
153  
154 0 ▶ if($done->exit_value == 111){
Branch: 3 branches
BranchTrueFalseTotal
if ($done->exit_value == 111) { } 0 0 0
elsif ($done->exit_value == 0 and $self->has_leftover_data) { } 0 0 0
elsif ($done->is_success) { } 0 0 0
155 0 $success = 0;
156 0 $msg = 'out of memory, or another multilog '.
157 'process is touching your files';
158 }
159  
160 ▶ elsif($done->exit_value == 0 && $self->has_leftover_data ){
Condition0%0/3
$done->exit_value == 0 and $self->has_leftover_data
!ll&&!rl&&r
000
MC/DC0%0/2
$done->exit_value == 0 and $self->has_leftover_data
$done->exit_value == 0 $self->has_leftover_data
Truth table0%0/3
A and B
A= $done->exit_value == 0B= $self->has_leftover_data
ABresult
0X0
100
111
161 0 $success = 1;
162 0 $msg = 'normal exit, with leftover data';
163 }
164  
165 elsif($done->is_success){
166 0 $success = 1;
167 0 $msg = 'normal exit';
168 }
169  
170 else {
171 0 $success = 0;
172 0 $msg = 'abnormal exit with signal '. $done->exit_signal;
173 }
174  
175 0 ▶ $self->on_exit->($success, $msg, $done) if $self->has_exit_handler;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_exit_handler 0 0 0
176 0 return;
177 }
178  
179 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
180 0 my $self = shift;
181 0 ▶ confess 'already started' if $self->has_run;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_run 0 0 0
182 0 return $self->run;
183 }
184  
185 0 ▶sub push_write {
Subroutine push_write: not called CC=2 SCAR=17.9
Pod documented
186 0 my ($self, $line) = @_;
187 0 $self->ensure_validity;
188 0 ▶ $line .= "\n" if $line !~ /(?:\r\n|\r|\n)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /(?:\r\n|\r|\n)$/ 0 0 0
189 0 $self->run->delegate('input_handle')->handle->push_write($line);
190 }
191  
192 0 ▶sub push_shutdown {
Subroutine push_shutdown: not called CC=1 SCAR=6.9
Pod documented
193 0 my $self = shift;
194 0 $self->run->kill('TERM');
195 }
196  
197 0 ▶sub rotate {
Subroutine rotate: not called CC=1 SCAR=6.9
Pod documented
198 0 my $self = shift;
199 0 $self->run->kill('ALRM');
200 }
201  
202 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
203 0 my $self = shift;
204 0 my $input = $self->run->delegate('input_handle');
205 0 ▶ confess 'already shutdown, cannot perform further operations' if $self->is_shutdown;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_shutdown 0 0 0
206 0 $input->handle->do_not_want;
207 }
208  
209 3 __PACKAGE__->meta->make_immutable;
210  
211 3 1;
212  
213  
214  
215 =pod
216  
217 =head1 NAME
218  
219 AnyEvent::Multilog - event-driven interface to a multilog process
220  
221 =head1 VERSION
222  
223 version 1.102861
224  
225 =head1 SYNOPSIS
226  
227 my $log = AnyEvent::Multilog->new(
228 script => [qw{t +* ./log}],
229 );
230  
231 $log->start;
232 $log->push_write('log message');
233 $log->push_write('another log message');
234 $log->rotate;
235 $log->push_write('another log message in a new log file');
236 $log->shutdown;
237  
238 =head1 DESCRIPTION
239  
240 This module makes it easy to log via a multilog process. It handles
241 spawning the multilog process and handling its errors.
242  
243 =head1 ATTRIBUTES
244  
245 =head2 script
246  
247 Required.
248  
249 This is an ArrayRef representing the multilog script that describes
250 how to log. See the L<multilog|multilog man page> for more
251 information on what this script is and how to write one.
252  
253 Note that the shell is never invoked, so you don't need to escape
254 anything from the shell.
255  
256 To select all lines, add a tai64n timestamp, and log to a directory
257 called "log", your script should be C<['t', '+*', './log']>.
258  
259 =head2 multilog
260  
261 Optional.
262  
263 The path to the multilog binary. By default, checks C<$PATH> and uses
264 the one in there.
265  
266 =head2 on_exit
267  
268 Optional.
269  
270 Coderef that is called when the multilog process exists, successfully
271 or otherwise.
272  
273 Your coderef is passed three arguments, a boolean indicating
274 successful exit, a message indicating why multilog exited, and the
275 L<AnyEvent::Subprocess::Done> object representing the exited
276 subprocess.
277  
278 =head2 on_error
279  
280 Optional.
281  
282 Coderef to be called when multilog writes something to stderr. It's
283 assumed that logging can't proceed after something is read from
284 stderr, so all methods will die regardless of whether or not you
285 handle this callback. Handling this event lets you proactively spawn
286 a new logger and kill this one without losing any messages.
287  
288 Patch welcome to make this automatic. I can't get multilog to die on
289 my machine.
290  
291 =head1 METHODS
292  
293 =head2 start
294  
295 Call this when you are ready to fork off the multilog and start
296 logging. If you call another method before calling this, the results
297 are undefined.
298  
299 =head2 push_write
300  
301 Send a line to multilog. If you don't provide a newline, one will be
302 provided for you.
303  
304 =head2 rotate
305  
306 Ask multilog to rotate the log right now.
307  
308 =head2 push_shutdown
309  
310 Ask multilog to shut down after writing the current line to disk. Any
311 pending data is made available via C<< $self->leftover_data >>, which
312 can be checked for with C<< $self->has_leftover_data >>.
313  
314 When multilog is done writing, your C<on_exit> callback will be
315 called.
316  
317 =head2 shutdown
318  
319 Shutdown immediately by closing the file descriptor that multilog is
320 reading from.
321  
322 When multilog is done writing, your C<on_exit> callback will be
323 called.
324  
325 =head2 is_shutdown
326  
327 Returns true if multilog is done.
328  
329 =head2 has_leftover_data
330  
331 Returns true if multilog exited without reading some data we asked it
332 to log.
333  
334 =head2 leftover_data
335  
336 Returns any data that multilog did not write before exiting.
337  
338 =head2 has_errors
339  
340 Returns true if errors were encourntered.
341  
342 =head2 errors
343  
344 Returns a list of errors that were encountered.
345  
346 =head1 AUTHOR
347  
348 Jonathan Rockway <jrockway@cpan.org>
349  
350 =head1 COPYRIGHT AND LICENSE
351  
352 This software is copyright (c) 2011 by Jonathan Rockway.
353  
354 This is free software; you can redistribute it and/or modify it under
355 the same terms as the Perl 5 programming language system itself.
356  
357 =cut
358  
359  
360 __END__
↑ Summary