Footprintless/Log.pm

filter: Statementstmt 79.2% 80 / 101 Branchbran 47.9% 23 / 48 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.4% 19 / 21 Podpod 100% 5 / 5 totaltotal 71.1% 128 / 180 CC 26 SCAR 22.5
CC
26
Cov
68%
_runner_options
25.3
__ANON__
12.0
_init
9.5
CRAP 9.5
« Footprintless/Command.pm ↑ Summary Footprintless.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Log;
5 3 $Footprintless::Log::VERSION = '1.29';
6 # ABSTRACT: A log manager
7 # PODNAME: Footprintless::Log
8  
9 3 ▶use parent qw(Footprintless::MixableBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Footprintless::Command qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 command
14 tail_command
15 3 );
16 3 ▶use Footprintless::CommandOptionsFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Footprintless::Localhost;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Footprintless::Mixins qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 _entity
20 3 );
21 3 ▶use Footprintless::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 dumper
23 invalid_entity
24 3 );
25 3 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 my $logger = Log::Any->get_logger();
28  
29 28 ▶sub _action_args {
Subroutine _action_args: called CC=5 SCAR=16.6
30 28 my ($args) = @_;
31 28 ▶ return '' unless ($args);
Branch: 1 branch
BranchTrueFalseTotal
unless $args 13 15 28
32  
33 15 my $ref = ref($args);
34 15 ▶ return "$args " unless ($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 15 15
35  
36 15 ▶ croak("unsupported ref type [$ref] for action options")
Branch: 1 branch
BranchTrueFalseTotal
unless $ref eq "ARRAY" 0 15 15
37 unless ( $ref eq 'ARRAY' );
38  
39 15 ▶ return scalar(@$args)
Branch: 1 branch
BranchTrueFalseTotal
scalar @$args ? : 15 0 15
40 ? join( ' ', @$args, '' )
41 : '';
42 }
43  
44 15 ▶sub cat {
Subroutine cat: called CC=1 SCAR=0.0
Pod documented
45 15 my ( $self, %options ) = @_;
46  
47 15 my $action_args = _action_args( $options{args} );
48  
49 return $self->{command_runner}
50 ->run_or_die( command( "cat $action_args$self->{log_file}", $self->{command_options} ),
51 15 $self->_runner_options( $options{runner_options} ) );
52 }
53  
54 1 ▶sub follow {
Subroutine follow: called CC=4 SCAR=15.0
Pod documented
55 1 my ( $self, %options ) = @_;
56  
57 1 eval {
58 $self->{command_runner}->run_or_die(
59 tail_command( $self->{log_file}, follow => 1, $self->{command_options} ),
60 $self->_runner_options( $options{runner_options}, $options{until} )
61 1 );
62 };
63 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
64 1 my $exception = $self->{command_runner}->get_exception();
65 1 ▶ croak($@) unless ( $exception && $exception =~ /^until found .*$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $exception and $exception =~ /^until found .*$/ 0 1 1
Condition33%1/3
$exception and $exception =~ /^until found .*$/
!ll&&!rl&&r
001
MC/DC0%0/2
$exception and $exception =~ /^until found .*$/
$exception $exception =~ /^until found .*$/
Truth table33%1/3
A and B
A= $exceptionB= $exception =~ /^until found .*$/
ABresult
0X0
100
111
66 }
67 }
68  
69 5 ▶sub grep {
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
70 5 my ( $self, %options ) = @_;
71  
72 5 my $action_args = _action_args( $options{args} );
73  
74 return $self->{command_runner}
75 ->run_or_die( command( "grep $action_args$self->{log_file}", $self->{command_options} ),
76 5 $self->_runner_options( $options{runner_options} ) );
77 }
78  
79 4 ▶sub head {
Subroutine head: called CC=1 SCAR=0.0
Pod documented
80 4 my ( $self, %options ) = @_;
81  
82 4 my $action_args = _action_args( $options{args} );
83  
84 return $self->{command_runner}
85 ->run_or_die( command( "head $action_args$self->{log_file}", $self->{command_options} ),
86 4 $self->_runner_options( $options{runner_options} ) );
87 }
88  
89 9 ▶sub _init {
Subroutine _init: called CC=5 SCAR=22.5
90 9 my ( $self, %options ) = @_;
91  
92 9 $self->{spec} = $self->_entity( $self->{coordinate}, 1 );
93  
94 # Allow string, hashref with file key, or object
95 9 my $ref = ref( $self->{spec} );
96 9 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 9 9
97 0 ▶ if ( $ref eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'SCALAR') { } 0 0 0
98 0 ▶ if ( $self->{spec}{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'spec'}{'file'}) { } 0 0 0
99 0 $self->{log_file} = $self->{spec}{file};
100 }
101 else {
102 0 invalid_entity( $self->{coordinate}, "must be file, or hashref with 'file' key" );
103 }
104 }
105 elsif ( $ref eq 'SCALAR' ) {
106 0 $self->{log_file} = $self->{spec};
107 }
108 else {
109 0 invalid_entity( $self->{coordinate}, "must be file, or hashref with 'file' key" );
110 }
111 }
112 else {
113 9 $self->{log_file} = $self->{spec};
114 }
115  
116 9 $self->{command_runner} = $self->{factory}->command_runner();
117 $self->{command_options} = $self->{factory}->command_options(
118 9 %{ $self->{factory}->entities()->fill(
119 $self->{coordinate},
120 9 { hostname => undef,
121 ssh => undef,
122 ssh_username => undef,
123 sudo_command => undef,
124 sudo_username => undef
125 },
126 ancestry => 1
127 )
128 }
129 );
130  
131 9 return $self;
132 }
133  
134 29 ▶sub _runner_options {
Subroutine _runner_options: called CC=11 SCAR=32.3
135 29 my ( $self, $runner_options, $until ) = @_;
136  
137 29 ▶ $runner_options = {} unless ($runner_options);
Branch: 1 branch
BranchTrueFalseTotal
unless $runner_options 24 5 29
138  
139 29 my $options = {};
140 29 ▶ if ($until) {
Branch: 1 branch
BranchTrueFalseTotal
if ($until) { } 1 28 29
141 1 ▶ if ( $runner_options->{out_buffer} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($runner_options->{'out_buffer'}) { } 0 1 1
elsif ($runner_options->{'out_callback'}) { } 1 0 1
142 0 ▶ $options->{out_callback} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 0 my ($line) = @_;
144 0 ${ $runner_options->{out_buffer} } .= "$line\n";
145 0 ▶ die('until found') if ( $line =~ $until );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$until/ 0 0 0
146 0 };
147 }
148 elsif ( $runner_options->{out_callback} ) {
149 4 ▶ $options->{out_callback} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
150 4 my ($line) = @_;
151 4 &{ $runner_options->{out_callback} }($line);
152 4 ▶ die('until found') if ( $line =~ $until );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$until/ 1 3 4
153 1 };
154 }
155 else {
156 0 my $handle = $runner_options->{out_handle};
157 0 ▶ $options->{out_callback} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
158 0 my ($line) = @_;
159 0 ▶ print( $handle "$line\n" ) if ($handle);
Branch: 1 branch
BranchTrueFalseTotal
if $handle 0 0 0
160 0 ▶ die('until found') if ( $line =~ $until );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$until/ 0 0 0
161 0 };
162 }
163 }
164 else {
165 28 ▶ if ( exists( $runner_options->{out_buffer} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $runner_options->{'out_buffer'}) { } 0 28 28
elsif (exists $runner_options->{'out_callback'}) { } 0 28 28
elsif (exists $runner_options->{'out_handle'}) { } 4 24 28
166 0 $options->{out_buffer} = $runner_options->{out_buffer};
167 }
168 elsif ( exists( $runner_options->{out_callback} ) ) {
169 0 $options->{out_callback} = $runner_options->{out_callback};
170 }
171 elsif ( exists( $runner_options->{out_handle} ) ) {
172 4 $options->{out_handle} = $runner_options->{out_handle};
173 }
174 }
175  
176 29 ▶ if ( exists( $runner_options->{err_buffer} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $runner_options->{'err_buffer'}) { } 0 29 29
elsif (exists $runner_options->{'err_callback'}) { } 0 29 29
elsif (exists $runner_options->{'err_handle'}) { } 0 29 29
177 0 $options->{err_buffer} = $runner_options->{err_buffer};
178 }
179 elsif ( exists( $runner_options->{err_callback} ) ) {
180 0 $options->{err_callback} = $runner_options->{err_callback};
181 }
182 elsif ( exists( $runner_options->{err_handle} ) ) {
183 0 $options->{err_handle} = $runner_options->{err_handle};
184 }
185  
186 29 return $options;
187 }
188  
189 4 ▶sub tail {
Subroutine tail: called CC=1 SCAR=0.0
Pod documented
190 4 my ( $self, %options ) = @_;
191  
192 4 my $action_args = _action_args( $options{args} );
193  
194 return $self->{command_runner}
195 ->run_or_die( command( "tail $action_args$self->{log_file}", $self->{command_options} ),
196 4 $self->_runner_options( $options{runner_options} ) );
197 }
198  
199 3 1;
200  
201 __END__
« Footprintless/Command.pm ↑ Summary Footprintless.pm »