line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
139378
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
77
|
|
2
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
163
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Footprintless::Log; |
5
|
|
|
|
|
|
|
$Footprintless::Log::VERSION = '1.29'; |
6
|
|
|
|
|
|
|
# ABSTRACT: A log manager |
7
|
|
|
|
|
|
|
# PODNAME: Footprintless::Log |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
18
|
use parent qw(Footprintless::MixableBase); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
16
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
108
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
151
|
|
12
|
3
|
|
|
|
|
170
|
use Footprintless::Command qw( |
13
|
|
|
|
|
|
|
command |
14
|
|
|
|
|
|
|
tail_command |
15
|
3
|
|
|
3
|
|
1303
|
); |
|
3
|
|
|
|
|
6
|
|
16
|
3
|
|
|
3
|
|
1037
|
use Footprintless::CommandOptionsFactory; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
74
|
|
17
|
3
|
|
|
3
|
|
16
|
use Footprintless::Localhost; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
54
|
|
18
|
3
|
|
|
|
|
152
|
use Footprintless::Mixins qw( |
19
|
|
|
|
|
|
|
_entity |
20
|
3
|
|
|
3
|
|
1086
|
); |
|
3
|
|
|
|
|
9
|
|
21
|
3
|
|
|
|
|
124
|
use Footprintless::Util qw( |
22
|
|
|
|
|
|
|
dumper |
23
|
|
|
|
|
|
|
invalid_entity |
24
|
3
|
|
|
3
|
|
21
|
); |
|
3
|
|
|
|
|
3
|
|
25
|
3
|
|
|
3
|
|
16
|
use Log::Any; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
11
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $logger = Log::Any->get_logger(); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _action_args { |
30
|
28
|
|
|
28
|
|
53
|
my ($args) = @_; |
31
|
28
|
100
|
|
|
|
66
|
return '' unless ($args); |
32
|
|
|
|
|
|
|
|
33
|
15
|
|
|
|
|
26
|
my $ref = ref($args); |
34
|
15
|
50
|
|
|
|
31
|
return "$args " unless ($ref); |
35
|
|
|
|
|
|
|
|
36
|
15
|
50
|
|
|
|
33
|
croak("unsupported ref type [$ref] for action options") |
37
|
|
|
|
|
|
|
unless ( $ref eq 'ARRAY' ); |
38
|
|
|
|
|
|
|
|
39
|
15
|
50
|
|
|
|
56
|
return scalar(@$args) |
40
|
|
|
|
|
|
|
? join( ' ', @$args, '' ) |
41
|
|
|
|
|
|
|
: ''; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub cat { |
45
|
15
|
|
|
15
|
1
|
54
|
my ( $self, %options ) = @_; |
46
|
|
|
|
|
|
|
|
47
|
15
|
|
|
|
|
39
|
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
|
|
|
|
|
62
|
$self->_runner_options( $options{runner_options} ) ); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub follow { |
55
|
1
|
|
|
1
|
1
|
11
|
my ( $self, %options ) = @_; |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
2
|
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
|
|
|
|
|
14
|
); |
62
|
|
|
|
|
|
|
}; |
63
|
1
|
50
|
|
|
|
48
|
if ($@) { |
64
|
1
|
|
|
|
|
18
|
my $exception = $self->{command_runner}->get_exception(); |
65
|
1
|
50
|
33
|
|
|
46
|
croak($@) unless ( $exception && $exception =~ /^until found .*$/ ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub grep { |
70
|
5
|
|
|
5
|
1
|
20
|
my ( $self, %options ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
5
|
|
|
|
|
16
|
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
|
|
|
|
|
28
|
$self->_runner_options( $options{runner_options} ) ); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub head { |
80
|
4
|
|
|
4
|
1
|
12
|
my ( $self, %options ) = @_; |
81
|
|
|
|
|
|
|
|
82
|
4
|
|
|
|
|
14
|
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
|
|
|
|
|
20
|
$self->_runner_options( $options{runner_options} ) ); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _init { |
90
|
9
|
|
|
9
|
|
19
|
my ( $self, %options ) = @_; |
91
|
|
|
|
|
|
|
|
92
|
9
|
|
|
|
|
51
|
$self->{spec} = $self->_entity( $self->{coordinate}, 1 ); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Allow string, hashref with file key, or object |
95
|
9
|
|
|
|
|
21
|
my $ref = ref( $self->{spec} ); |
96
|
9
|
50
|
|
|
|
20
|
if ($ref) { |
97
|
0
|
0
|
|
|
|
0
|
if ( $ref eq 'HASH' ) { |
|
|
0
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
0
|
if ( $self->{spec}{file} ) { |
99
|
0
|
|
|
|
|
0
|
$self->{log_file} = $self->{spec}{file}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else { |
102
|
0
|
|
|
|
|
0
|
invalid_entity( $self->{coordinate}, "must be file, or hashref with 'file' key" ); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
elsif ( $ref eq 'SCALAR' ) { |
106
|
0
|
|
|
|
|
0
|
$self->{log_file} = $self->{spec}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
0
|
|
|
|
|
0
|
invalid_entity( $self->{coordinate}, "must be file, or hashref with 'file' key" ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
9
|
|
|
|
|
30
|
$self->{log_file} = $self->{spec}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
9
|
|
|
|
|
28
|
$self->{command_runner} = $self->{factory}->command_runner(); |
117
|
|
|
|
|
|
|
$self->{command_options} = $self->{factory}->command_options( |
118
|
9
|
|
|
|
|
30
|
%{ $self->{factory}->entities()->fill( |
119
|
|
|
|
|
|
|
$self->{coordinate}, |
120
|
9
|
|
|
|
|
24
|
{ 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
|
|
|
|
|
73
|
return $self; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _runner_options { |
135
|
29
|
|
|
29
|
|
78
|
my ( $self, $runner_options, $until ) = @_; |
136
|
|
|
|
|
|
|
|
137
|
29
|
100
|
|
|
|
53
|
$runner_options = {} unless ($runner_options); |
138
|
|
|
|
|
|
|
|
139
|
29
|
|
|
|
|
44
|
my $options = {}; |
140
|
29
|
100
|
|
|
|
58
|
if ($until) { |
141
|
1
|
50
|
|
|
|
6
|
if ( $runner_options->{out_buffer} ) { |
|
|
50
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$options->{out_callback} = sub { |
143
|
0
|
|
|
0
|
|
0
|
my ($line) = @_; |
144
|
0
|
|
|
|
|
0
|
${ $runner_options->{out_buffer} } .= "$line\n"; |
|
0
|
|
|
|
|
0
|
|
145
|
0
|
0
|
|
|
|
0
|
die('until found') if ( $line =~ $until ); |
146
|
0
|
|
|
|
|
0
|
}; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
elsif ( $runner_options->{out_callback} ) { |
149
|
|
|
|
|
|
|
$options->{out_callback} = sub { |
150
|
4
|
|
|
4
|
|
10
|
my ($line) = @_; |
151
|
4
|
|
|
|
|
9
|
&{ $runner_options->{out_callback} }($line); |
|
4
|
|
|
|
|
22
|
|
152
|
4
|
100
|
|
|
|
48
|
die('until found') if ( $line =~ $until ); |
153
|
1
|
|
|
|
|
20
|
}; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
else { |
156
|
0
|
|
|
|
|
0
|
my $handle = $runner_options->{out_handle}; |
157
|
|
|
|
|
|
|
$options->{out_callback} = sub { |
158
|
0
|
|
|
0
|
|
0
|
my ($line) = @_; |
159
|
0
|
0
|
|
|
|
0
|
print( $handle "$line\n" ) if ($handle); |
160
|
0
|
0
|
|
|
|
0
|
die('until found') if ( $line =~ $until ); |
161
|
0
|
|
|
|
|
0
|
}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
else { |
165
|
28
|
50
|
|
|
|
80
|
if ( exists( $runner_options->{out_buffer} ) ) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
166
|
0
|
|
|
|
|
0
|
$options->{out_buffer} = $runner_options->{out_buffer}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
elsif ( exists( $runner_options->{out_callback} ) ) { |
169
|
0
|
|
|
|
|
0
|
$options->{out_callback} = $runner_options->{out_callback}; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
elsif ( exists( $runner_options->{out_handle} ) ) { |
172
|
4
|
|
|
|
|
8
|
$options->{out_handle} = $runner_options->{out_handle}; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
29
|
50
|
|
|
|
72
|
if ( exists( $runner_options->{err_buffer} ) ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
177
|
0
|
|
|
|
|
0
|
$options->{err_buffer} = $runner_options->{err_buffer}; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
elsif ( exists( $runner_options->{err_callback} ) ) { |
180
|
0
|
|
|
|
|
0
|
$options->{err_callback} = $runner_options->{err_callback}; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
elsif ( exists( $runner_options->{err_handle} ) ) { |
183
|
0
|
|
|
|
|
0
|
$options->{err_handle} = $runner_options->{err_handle}; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
29
|
|
|
|
|
148
|
return $options; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub tail { |
190
|
4
|
|
|
4
|
1
|
10
|
my ( $self, %options ) = @_; |
191
|
|
|
|
|
|
|
|
192
|
4
|
|
|
|
|
16
|
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
|
|
|
|
|
24
|
$self->_runner_options( $options{runner_options} ) ); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
1; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
__END__ |