Mnet/Tee.pm

filter: Statementstmt 94.6% 88 / 93 Branchbran 79.4% 27 / 34 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 96.1% 25 / 26 Podpod 10.0% 1 / 10 totaltotal 83.0% 147 / 177 CC 22 SCAR 11.2
CC
22
Cov
88%
tee_file
7.1
file
4.3
__ANON__
4.0
CRAP 3.1
« Mnet/Version.pm ↑ Summary Mnet/Log/Conditional.pm »
1 package Mnet::Tee;
2  
3 =head1 NAME
4  
5 Mnet::Tee - Redirect stdout and stderr to a file
6  
7 =head1 SYNOPSIS
8  
9 # use this module on it's own
10 use Mnet::Tee;
11 Mnet::Tee::file($file);
12  
13 # or use --tee with Mnet command line options
14 use Mnet::Tee;
15 use Mnet::Opts::Cli;
16 my $cli = Mnet::Opts::Cli->new;
17  
18 =head1 DESCRIPTION
19  
20 Mnet::Tee can be used to capture all stdout and stderr output from the
21 calling script, saving the combined output to a file. This module is used
22 by the L<Mnet::Test> module.
23  
24 The variables stdout and stderr can be imported from this module to use for
25 output that should not be captured by the Mnet::Tee module.
26  
27 When used with the L<Mnet::Log> module the --tee file will contain log
28 entries that are not displayed to the terminal due to the use of the --quiet
29 or --silent options.
30  
31 When used with the L<Mnet::Batch> module the output from the batch parent and
32 all child processes will be merged into a single file.
33  
34 Note that output captured by this module is stored in memory during script
35 execution. That could be a problem for scripts that generate gigabytes of
36 stdout and/or stderr output.
37  
38 The perl tie command used in this module requires perl 5.010 or newer.
39  
40 =head1 FUNCTIONS
41  
42 Mnet::Tee implements the functions listed below.
43  
44 =cut
45  
46 # required modules
47 # perl 5.10 for tie to capture stdout/stderr
48 # update Mnet.pm and this perldoc if changed
49 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 87 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 87 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 87 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
54 87 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 87 ▶use Mnet::Log::Conditional qw( DEBUG INFO WARN FATAL NOTICE );
Subroutine BEGIN: called CC=1 SCAR=0.0
56 87 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # export function names
59 87 our @EXPORT_OK = qw( $stderr $stdout );
60  
61  
62  
63 # begin block to initialize capture of stdout and stderr
64 87 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.1
65  
66 # autoflush standard output
67 # so that multi-process syswrite lines are not split
68 87 $| = 1;
69  
70 # original stderr and stdout for bypassing output captured by Mnet::Tee
71 87 ▶ open(our $stderr, ">&STDERR") or die "error duping stderr, $!";
Branch: 1 branch
BranchTrueFalseTotal
open our $stderr, ">&STDERR" or die "error duping stderr, $!" 87 0 87
72 87 ▶ open(our $stdout, ">&STDOUT") or die "error duping stdout, $!";
Branch: 1 branch
BranchTrueFalseTotal
open our $stdout, ">&STDOUT" or die "error duping stdout, $!" 87 0 87
73  
74 # init global scalar variable to accumulate stdout+stderr test outputs
75 87 my ($test_outputs, $test_paused) = ("", undef);
76  
77 # init filehandle that may be used to --tee redirected data
78 # also init buffer to hold first line, which may be notice log entry
79 # this is needed especially for batch child with --tee in batch list
80 # other implementations should call Mnet::Tee::file before any output
81 87 my ($fh, $fh_line1) = (undef, undef);
82  
83 # declare tie contructor used to capture stdout/stderr handles
84 220 ▶ sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
85 220 my ($class, $fh) = (shift, shift);
86 220 return bless({ fh => $fh }, $class);
87 }
88  
89 # declare tie method triggered for print to handles
90 1723 ▶ sub PRINT {
Subroutine PRINT: called CC=1 SCAR=0.0
91 1723 my $self = shift;
92 1723 &{$self->{fh}}(@_);
93 1723 return 1;
94 }
95  
96 # declare tie method triggered for printf to handles
97 0 ▶ sub PRINTF {
Subroutine PRINTF: not called CC=1 SCAR=6.9
98 0 my $self = shift;
99 0 return $self->PRINT(sprintf(@_));
100 }
101  
102 # declare tie method triggered for write to handles
103 1700 ▶ sub WRITE {
Subroutine WRITE: called CC=2 SCAR=7.6
104 1700 my $self = shift;
105 1700 ▶ my ($buffer, $length ,$offset) = (shift, shift, shift // 0);
Condition50%1/2
shift() // 0
l!l
01700
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
106 1700 return $self->PRINT(substr($buffer, $offset, $length));
107 0 return 1;
108 }
109  
110 # create copied of stderr and stdout to be used for output from tie subs
111 87 open(my $stderr_fh, ">&STDERR");
112 87 open(my $stdout_fh, ">&STDOUT");
113  
114 # declare sub used to save --tee output to a file
115 # write new text to file if file handle is already open
116 # otherwise check for --tee option and open file if necessary
117 # save first output line if not ready to write to file yet
118 1936 ▶ sub tee_file {
Subroutine tee_file: called CC=7 SCAR=19.7
Pod undocumented
119 1936 ▶ my $text = shift // return;
Condition50%1/2
shift() // (return)
l!l
19360
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
120 1936 ▶ if ($Mnet::Tee::fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mnet::Tee::fh) { } 32 1904 1936
121 32 syswrite $Mnet::Tee::fh, $text;
122 } else {
123 1904 my $cli = Mnet::Opts::Cli::Cache::get({});
124 1904 ▶ if ($cli->{tee}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cli->{'tee'}) { } 2 1902 1904
elsif (not defined $Mnet::Tee::fh_line1) { } 90 1812 1902
125 open($Mnet::Tee::fh, ">", $cli->{tee})
126 2 ▶ or die "unable to open --tee $cli->{tee}, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $Mnet::Tee::fh, ">", $cli->{"tee"} or die qq[unable to open --tee $cli->{"tee"}, $!\n] 2 0 2
127 2 ▶ syswrite $Mnet::Tee::fh, $Mnet::Tee::fh_line1
Branch: 1 branch
BranchTrueFalseTotal
if defined $Mnet::Tee::fh_line1 0 2 2
128 if defined $Mnet::Tee::fh_line1;
129 2 syswrite $Mnet::Tee::fh, $text;
130 } elsif (not defined $Mnet::Tee::fh_line1) {
131 90 $Mnet::Tee::fh_line1 = $text;
132 }
133 }
134 }
135  
136 # declare sub used to enable capture of stderr and stdout using tie command
137 110 ▶ sub tie_enable {
Subroutine tie_enable: called CC=1 SCAR=0.0
Pod undocumented
138 22 ▶ tie(*STDERR => 'Mnet::Tee' , sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
139 22 my $text = "@_";
140 22 ▶ $Mnet::Tee::test_outputs .= $text if not $Mnet::Tee::test_paused;
Branch: 1 branch
BranchTrueFalseTotal
unless $Mnet::Tee::test_paused 21 1 22
141 22 tee_file($text);
142 return
143 if $INC{"Mnet/Opts/Set/Silent.pm"}
144 22 ▶ and not $INC{"Mnet/Opts/Set/Quiet.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Set/Silent.pm"} and not $INC{"Mnet/Opts/Set/Quiet.pm"} 1 21 22
Condition66%2/3
$INC{"Mnet/Opts/Set/Silent.pm"} and not $INC{"Mnet/Opts/Set/Quiet.pm"}
!ll&&!rl&&r
2101
MC/DC50%1/2
$INC{"Mnet/Opts/Set/Silent.pm"} and not $INC{"Mnet/Opts/Set/Quiet.pm"}
$INC{"Mnet/Opts/Set/Silent.pm"} not $INC{"Mnet/Opts/Set/Quiet.pm"}
Truth table66%2/3
A and not(B)
A= $INC{"Mnet/Opts/Set/Silent.pm"}B= not $INC{"Mnet/Opts/Set/Quiet.pm"}
ABresult
0X0
100
111
145 21 syswrite $stderr_fh, $text;
146 110 });
147 1701 ▶ tie(*STDOUT => 'Mnet::Tee' , sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
148 1701 my $text = "@_";
149 1701 ▶ $Mnet::Tee::test_outputs .= $text if not $Mnet::Tee::test_paused;
Branch: 1 branch
BranchTrueFalseTotal
unless $Mnet::Tee::test_paused 154 1547 1701
150 1701 tee_file($text);
151 1701 ▶ return if $INC{"Mnet/Opts/Set/Quiet.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Set/Quiet.pm"} 2 1699 1701
152 1699 ▶ return if $INC{"Mnet/Opts/Set/Silent.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Set/Silent.pm"} 39 1660 1699
153 1660 syswrite $stdout_fh, $text;
154 110 });
155 }
156  
157 # use tie to capture stderr and stdout to global test outputs variable
158 87 Mnet::Tee::tie_enable();
159  
160 # declare sub used from Mnet::Expect to temporarily untie filehandles
161 23 ▶ sub tie_disable {
Subroutine tie_disable: called CC=1 SCAR=0.0
Pod undocumented
162 23 untie *STDERR;
163 23 untie *STDOUT;
164 }
165  
166 # finished begin block
167 }
168  
169  
170  
171 # define --tee cli option
172 87 ▶INIT {
Subroutine INIT: called CC=2 SCAR=6.9
173 Mnet::Opts::Cli::define({
174 getopt => 'tee:s',
175 help_tip => 'redirect stdout and stderr to file',
176 help_text => '
177 script stdout and stderr can be redirected to a --tee file
178 use with Mnet::Log to capture output suppressed by --quiet/silent
179 use with Mnet::Batch parent to merge all parent/child outputs
180 refer to perldoc Mnet::Tee for more info
181 ',
182 87 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 76 11 87
183 }
184  
185  
186  
187 7 ▶sub batch_fork {
Subroutine batch_fork: called CC=1 SCAR=0.0
Pod undocumented
188  
189 # Mnet::Tee::test_reset()
190 # purpose: reset accumulated test outputs buffered first line output
191 # note: called after new Mnet::Batch::fork child, to get rid of parent output
192  
193 # reset accumulated test outputs to null
194 7 $Mnet::Tee::test_outputs = "";
195 7 $Mnet::Tee::fh_line1 = undef;
196 7 return;
197 }
198  
199  
200  
201 1 ▶sub file {
Subroutine file: called CC=4 SCAR=14.6
Pod documented
202  
203 =head2 file
204  
205 Mnet::Tee::file($file)
206  
207 The function can be used to have script output written to the specified file,
208 including all output prior to the call. The script will abort if unable to
209 open the new file.
210  
211 =cut
212  
213 # read input filename
214 1 ▶ my $file = shift // croak "missing file arg";
Condition50%1/2
shift() // croak("missing file arg")
l!l
10
MC/DC0%0/1
shift() // croak("missing file arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
215 1 DEBUG("file starting");
216  
217 # abort if unable to open new output file
218 1 DEBUG("file opening file $file");
219 1 ▶ open(my $fh, ">", $file) or FATAL("error writing to file $file, $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or FATAL("error writing to file $file, $!") 1 0 1
220  
221 # close any old output file and use this one going forward
222 1 ▶ if (defined $Mnet::Tee::fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Mnet::Tee::fh) 0 1 1
223 0 DEBUG("file closing old file handle");
224 0 close $Mnet::Tee::fh;
225 }
226  
227 # update file handle used by tie functions in begin block
228 1 DEBUG("file updating file handle");
229 1 $Mnet::Tee::fh = $fh;
230  
231 # write all prior output to the new file
232 1 DEBUG("file saving prior test output");
233 1 syswrite $Mnet::Tee::fh, $Mnet::Tee::test_outputs;
234  
235 # finished file function
236 1 DEBUG("file finished");
237 1 return;
238 }
239  
240  
241  
242 213 ▶sub tee_no_term {
Subroutine tee_no_term: called CC=3 SCAR=11.1
Pod undocumented
243  
244 # Mnet::Tee::tee_no_term($text)
245 # purpose: output text to tee file and test_outputs, but not stdout or stderr
246 # note: this is used from Mnet::Log, for when --quiet or --silent are set
247 # note: refer also to test_pause and test_unpause functions in this module
248  
249 # send text to tee_file and accumulate in test_outputs if no test_paused
250 213 my $text = shift;
251 213 ▶ Mnet::Tee::tee_file($text) if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 213 0 213
252 213 ▶ $Mnet::Tee::test_outputs .= $text if not $Mnet::Tee::test_paused;
Branch: 1 branch
BranchTrueFalseTotal
if not $Mnet::Tee::test_paused 120 93 213
253 }
254  
255  
256  
257 28 ▶sub test_outputs {
Subroutine test_outputs: called CC=1 SCAR=0.0
Pod undocumented
258  
259 # $test_outputs = Mnet::Tee::test_outputs()
260 # purpose: return a string containing all accumulated prior test outputs
261 # $outputs: string containing all prior stdout and stderr test outputs
262  
263 # return all prior stdout and stderr outputs
264 28 return $Mnet::Tee::test_outputs;
265 }
266  
267  
268  
269 1662 ▶sub test_pause {
Subroutine test_pause: called CC=1 SCAR=0.0
Pod undocumented
270  
271 # Mnet::Tee::test_pause()
272 # purpose: stop the accumulation of new test output from stdout and stderr
273 # note: this does not affect output being sent to a file
274  
275 # disable accumulation of captured output
276 1662 $Mnet::Tee::test_paused = 1;
277 }
278  
279  
280  
281 5 ▶sub test_paused {
Subroutine test_paused: called CC=1 SCAR=0.0
Pod undocumented
282  
283 # $test_paused = Mnet::Tee::test_paused()
284 # purpose: returns current pause/pause status
285 # $paused: true if the accumulation of new stdout/stderr test output is paused
286  
287 # return status of stdout and stderr test output accumulation
288 5 return $Mnet::Tee::test_paused;
289 }
290  
291  
292  
293 1658 ▶sub test_unpause {
Subroutine test_unpause: called CC=1 SCAR=0.0
Pod undocumented
294  
295 # Mnet::Tee::test_unpause()
296 # purpose: starts the accumulation of new test output from stdout and stderr
297  
298 # enable accumulation of captured output
299 1658 $Mnet::Tee::test_paused = 0;
300 }
301  
302  
303  
304 =head1 SEE ALSO
305  
306 L<Mnet>
307  
308 L<Mnet::Opts::Cli>
309  
310 L<Mnet::Test>
311  
312 =cut
313  
314 # normal end of package
315 87 1;
316  
« Mnet/Version.pm ↑ Summary Mnet/Log/Conditional.pm »