blib/lib/Tail/Tool.pm

filter: Statementstmt 52.6% 69 / 131 Branchbran 15.9% 7 / 44 Conditioncond 14.2% 4 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 77.7% 14 / 18 Podpod 100% 3 / 3 totaltotal 40.5% 97 / 239 CC 42 SCAR 47.3
CC
42
Cov
39%
run
306.0
__ANON__
88.1
_pre_process_set
12.7
CRAP 113.2
↑ Summary blib/lib/Tail/Tool/File.pm »
1 package Tail::Tool;
2  
3 # Created on: 2010-10-06 14:15:40
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Tail::Tool::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = version->new('0.4.6');
17  
18 has files => (
19 is => 'rw',
20 isa => 'ArrayRef[Tail::Tool::File]',
21 1 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 );
23 2 has lines => (
24 is => 'rw',
25 isa => 'Int',
26 default => 10,
27 );
28 has pre_process => (
29 is => 'rw',
30 isa => 'ArrayRef',
31 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 2 trigger => \&_pre_process_set,
33 );
34 has post_process => (
35 is => 'rw',
36 isa => 'ArrayRef',
37 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 2 );
39 2 has printer => (
40 is => 'rw',
41 isa => 'CodeRef',
42 predicate => 'has_printer',
43 #default => sub {
44 # sub { print "Default printer\n", ( ref $_ eq 'ARRAY' ? @$_ : @_ ) };
45 #},
46 );
47 2 has last => (
48 is => 'rw',
49 isa => 'Tail::Tool::File',
50 );
51  
52 around BUILDARGS => sub {
53 1 ▶ my ($orig, $class, @params) = @_;
Subroutine __ANON__: called CC=15 SCAR=44.8
54 1 my %param;
55  
56 1 ▶ if ( ref $params[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 0 1 1
57 0 %param = %{ shift @params };
58 }
59 else {
60 1 %param = @params;
61 }
62  
63 1 ▶ $param{pre_process} ||= [];
Condition50%1/2
$param{"pre_process"} ||= []
l!l
01
MC/DC0%0/1
$param{"pre_process"} ||= []
$param{"pre_process"}
Truth table50%1/2
A
A= $param{"pre_process"}
Aresult
11
00
64 1 ▶ $param{post_process} ||= [];
Condition50%1/2
$param{"post_process"} ||= []
l!l
01
MC/DC0%0/1
$param{"post_process"} ||= []
$param{"post_process"}
Truth table50%1/2
A
A= $param{"post_process"}
Aresult
11
00
65  
66 1 for my $key ( keys %param ) {
67 2 ▶ next if $key eq 'post_process' || $key eq 'pre_process';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "post_process" or $key eq "pre_process" 2 0 2
Condition66%2/3
$key eq "post_process" or $key eq "pre_process"
l!l&&r!l&&!r
110
MC/DC0%0/2
$key eq "post_process" or $key eq "pre_process"
$key eq "post_process" $key eq "pre_process"
Truth table66%2/3
A or B
A= $key eq "post_process"B= $key eq "pre_process"
ABresult
1X1
011
000
68  
69 0 ▶ if ( $key eq 'files' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'files') { } 0 0 0
elsif ($key eq 'lines' or $key eq 'printer' or $key eq 'no_inotify' or $key eq 'restart') { } 0 0 0
70 my @extra = (
71 no_inotify => $param{no_inotify},
72 restart => $param{restart},
73 0 );
74 0 for my $file ( @{ $param{$key} } ) {
75 0 ▶ $file = Tail::Tool::File->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
76 ref $file ? $file : ( name => $file, @extra )
77 );
78 }
79 }
80 ▶ elsif ( $key eq 'lines' || $key eq 'printer' || $key eq 'no_inotify' || $key eq 'restart' ) {
Condition0%0/9
$key eq "lines" or $key eq "printer" or $key eq "no_inotify" or $key eq "restart"
l!l&&r!l&&!r
000
$key eq "lines" or $key eq "printer" or $key eq "no_inotify"
l!l&&r!l&&!r
000
$key eq "lines" or $key eq "printer"
l!l&&r!l&&!r
000
MC/DC0%0/4
$key eq "lines" or $key eq "printer" or $key eq "no_inotify" or $key eq "restart"
$key eq "lines" $key eq "printer" $key eq "no_inotify" $key eq "restart"
Truth table0%0/5
A or B or C or D
A= $key eq "lines"B= $key eq "printer"C= $key eq "no_inotify"D= $key eq "restart"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
81 }
82 else {
83 0 my $plg = _new_plugin( $key, $param{$key} );
84 0 delete $param{$key};
85  
86 0 ▶ push @{ $param{ ( $plg->post ? 'post' : 'pre' ) . '_process' } }, $plg;
Branch: 1 branch
BranchTrueFalseTotal
$plg->post ? : 0 0 0
87 }
88 }
89  
90 1 return $class->$orig(%param);
91 2 };
92  
93 sub _new_plugin {
94 2 ▶ my ( $name, $value ) = @_;
Subroutine _new_plugin: called CC=1 SCAR=0.0
95 2 my $plugin = _load_plugin($name);
96  
97 2 my $plg = $plugin->new($value);
98  
99 2 return $plg;
100 }
101  
102 sub _load_plugin {
103 2 ▶ my ( $name ) = @_;
Subroutine _load_plugin: called CC=4 SCAR=14.0
104 2 ▶ my $plugin
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^\+/ ? : 1 1 2
105 = $name =~ /^\+/
106 ? substr $name, 1, 999
107 : "Tail::Tool::Plugin::$name";
108 2 my $plugin_file = $plugin;
109 2 $plugin_file =~ s{::}{/}gxms;
110 2 $plugin_file .= '.pm';
111 {
112 # don't load twice
113 2 ▶ no strict qw/refs/; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶ if ( !${"Tail::Tool::Plugin::${name}::"}{VERSION} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do { "Tail::Tool::Plugin::${name}::" }->{"VERSION"}) 2 0 2
115 2 eval { require $plugin_file };
116 2 ▶ if ( $EVAL_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Tail::Tool::EVAL_ERROR) 0 2 2
117 0 confess "Could not load the plugin $name (via $plugin_file)\n";
118 }
119 }
120 }
121  
122 2 return $plugin;
123 }
124  
125 sub tail {
126 1 ▶ my ( $self, $no_start ) = @_;
Subroutine tail: called CC=4 SCAR=24.2
Pod documented
127  
128 1 for my $file (@{ $self->files }) {
129 0 ▶ next if $file->runner;
Branch: 1 branch
BranchTrueFalseTotal
if $file->runner 0 0 0
130 0 ▶ $file->runner( sub { $self->run(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 $file->tailer($self);
132 0 $file->watch();
133 0 ▶ $file->run() if !$no_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_start 0 0 0
134 }
135 }
136  
137 sub run {
138 0 ▶ my ( $self, $file ) = @_;
Subroutine run: not called CC=17 SCAR=57.2
Pod documented
139  
140 0 my $first = !$file->started;
141 0 my @lines = $file->get_line;
142  
143 0 ▶ if ( $first && @lines > $self->lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first and @lines > $self->lines) 0 0 0
Condition0%0/3
$first and @lines > $self->lines
!ll&&!rl&&r
000
MC/DC0%0/2
$first and @lines > $self->lines
$first @lines > $self->lines
Truth table0%0/3
A and B
A= $firstB= @lines > $self->lines
ABresult
0X0
100
111
144 0 @lines = @lines[ -$self->lines .. -1 ];
145 }
146  
147 0 for my $pre ( @{ $self->pre_process } ) {
148 0 my @new;
149 0 ▶ if (@lines) {
Branch: 2 branches
BranchTrueFalseTotal
if (@lines) { } 0 0 0
elsif ($pre->can('allow_empty') and $pre->allow_empty) { } 0 0 0
150 0 for my $line (@lines) {
151 0 push @new, $pre->process($line, $file);
152 }
153 }
154 ▶ elsif ( $pre->can('allow_empty') && $pre->allow_empty ) {
Condition0%0/3
$pre->can("allow_empty") and $pre->allow_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$pre->can("allow_empty") and $pre->allow_empty
$pre->can("allow_empty") $pre->allow_empty
Truth table0%0/3
A and B
A= $pre->can("allow_empty")B= $pre->allow_empty
ABresult
0X0
100
111
155 0 push @new, $pre->process('', $file);
156 }
157 0 @lines = @new;
158 }
159 0 for my $post ( @{ $self->post_process } ) {
160 0 my @new;
161 0 for my $line (@lines) {
162 0 push @new, $post->process($line, $file);
163 }
164 0 @lines = @new;
165 }
166  
167 0 ▶ if ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 0 0 0
168 0 ▶ if ( @{ $self->files } > 1 && ( !$self->last || $file ne $self->last ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->files;} > 1 and !$self->last || $file ne $self->last) 0 0 0
Condition0%0/6
@{$self->files;} > 1 and !$self->last || $file ne $self->last
!ll&&!rl&&r
000
not $self->last or $file ne $self->last
l!l&&r!l&&!r
000
MC/DC0%0/3
@{$self->files;} > 1 and !$self->last || $file ne $self->last
@{$self->files;} > 1 not $self->last $file ne $self->last
Truth table0%0/4
A and not(B) or C
A= @{$self->files;} > 1B= not $self->lastC= $file ne $self->last
ABCresult
0XX0
1000
11X1
1011
169 0 unshift @lines, "\n==> " . $file->name . " <==\n";
170 }
171 0 $self->last($file);
172 }
173  
174 #warn join "", @lines if @lines;
175 0 ▶ if ( $self->has_printer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_printer) { } 0 0 0
176 0 my $printer = $self->printer;
177 0 warn "Lines = " . scalar @lines, "\tPrinter " . $printer . "\n";
178  
179 0 $_ = \@lines;
180 0 eval { &{$printer}() };
181 0 ▶ warn "Error in printer: " . $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
182 }
183 else {
184 0 $self->default_printer(@lines);
185 }
186  
187 0 ▶ $file->started(1) if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 0 0 0
188 0 return;
189 }
190  
191 sub default_printer {
192 1 ▶ my ( $self, @lines ) = @_;
Subroutine default_printer: called CC=1 SCAR=0.0
Pod documented
193 1 print @lines;
194 }
195  
196 sub _pre_process_set {
197 1 ▶ my ($self, $pre_process) = @_;
Subroutine _pre_process_set: called CC=6 SCAR=25.4
198 1 my @pre = @{ $pre_process };
199 1 my @group;
200 my @other;
201  
202 # sort (in order) pre process plugins
203 1 for my $pre (@pre) {
204 0 ▶ if ( ref $pre eq 'Tail::Tool::Plugin::GroupLines' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pre eq 'Tail::Tool::Plugin::GroupLines') { } 0 0 0
205 0 push @group, $pre;
206 }
207 else {
208 0 push @other, $pre;
209 }
210 }
211  
212 # check that the sorted plugins match the current order
213 1 my $differ = 0;
214 1 for my $new_pre ( @group, @other ) {
215 0 ▶ if ( $new_pre != shift @pre ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pre != shift @pre) 0 0 0
216 0 $differ = 1;
217 0 last;
218 }
219 }
220  
221 # if the orders differ, reset the plugins.
222 1 ▶ if ($differ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($differ) 0 1 1
223 0 $self->pre_process([ @group, @other ]);
224 }
225 }
226  
227 2 1;
228  
229 __END__
230  
231 =head1 NAME
232  
233 Tail::Tool - Tool for sophisticated tailing of files
234  
235 =head1 VERSION
236  
237 This documentation refers to Tail::Tool version 0.4.6.
238  
239 =head1 SYNOPSIS
240  
241 use Tail::Tool;
242  
243 # Create a new Tail::Tool object tailing /tmp/test.log
244 # with the spacing plugin initialised.
245 my $tt = Tail::Tool->new(
246 files => [
247 '/tmpl/test.log',
248 ],
249 Spacing => {
250 short_time => 2,
251 short_lines => 2,
252 long_time => 5,
253 long_lines => 10,
254 },
255 ...
256 );
257  
258 # run the tail
259 $tt->tail();
260  
261 =head1 DESCRIPTION
262  
263 =head1 SUBROUTINES/METHODS
264  
265 =head2 C<tail ()>
266  
267 Description: Start tailing?
268  
269 =head2 C<run ($file, $first)>
270  
271 Param: C<$file> - Tail::Tool::File - The file to run
272  
273 Param: C<$first> - bool - Specifies that this is the first time run has been
274 called.
275  
276 =head2 C<run ( $file )>
277  
278 Runs the the tailing of C<$file>.
279  
280 =head2 C<default_printer ( @lines )>
281  
282 Prints C<@lines> to STDOUT
283  
284 =head1 DIAGNOSTICS
285  
286 =head1 CONFIGURATION AND ENVIRONMENT
287  
288 =head1 DEPENDENCIES
289  
290 =head1 INCOMPATIBILITIES
291  
292 =head1 BUGS AND LIMITATIONS
293  
294 There are no known bugs in this module.
295  
296 Please report problems to Ivan Wills (ivan.wills@gmail.com).
297  
298 Patches are welcome.
299  
300 =head1 AUTHOR
301  
302 Ivan Wills - (ivan.wills@gmail.com)
303  
304 =head1 LICENSE AND COPYRIGHT
305  
306 Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia).
307 All rights reserved.
308  
309 This module is free software; you can redistribute it and/or modify it under
310 the same terms as Perl itself. See L<perlartistic>. This program is
311 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
312 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
313 PARTICULAR PURPOSE.
314  
315 =cut
↑ Summary blib/lib/Tail/Tool/File.pm »