Mnet/Log.pm

filter: Statementstmt 94.8% 166 / 175 Branchbran 90.9% 100 / 110 Conditioncond 80.8% 59 / 73 MC/DCmcdc 64.2% 27 / 42 Subroutinesub 90.9% 30 / 33 Podpod 66.6% 10 / 15 totaltotal 87.5% 392 / 448 CC 84 SCAR 30.2
CC
84
Cov
91%
output
42.1
END
20.4
_sig_handler
9.6
CRAP 20.5
« Mnet/Opts/Cli.pm ↑ Summary Mnet/Batch.pm »
1 package Mnet::Log;
2  
3 =head1 NAME
4  
5 Mnet::Log - Logging, compatible with Log4perl
6  
7 =head1 SYNOPSIS
8  
9 # imports DEBUG, INFO, WARN, and FATAL
10 use Mnet::Log qw( DEBUG INFO WARN FATAL );
11  
12 # options can be set for Mnet::Log objects
13 my $log = Mnet::Log->new();
14  
15 # output to standard out and err is captured
16 syswrite STDOUT, $text;
17 syswrite STDERR, $text;
18  
19 # dbg entries
20 DEBUG($text);
21 $log->debug($text);
22  
23 # inf entries
24 INFO($text);
25 $log->info($text);
26  
27 # WRN entries
28 WARN($text);
29 $log->warn($text);
30  
31 # ERR entries
32 # note that eval warnings are output as normal
33 # evals can use local $SIG{__WARN__} = sub { die @_ };
34 warn $text;
35 die $text;
36  
37 # DIE entries
38 FATAL($text);
39 $log->fatal($text);
40  
41 # first line of first WRN/ERR/DIE entry
42 $text = Mnet::Log::error();
43  
44 =head1 DESCRIPTION
45  
46 Mnet::Log supports generating the following types of log entries
47  
48 dbg stdout detailed info, visible when debug option is set
49 inf stdout normal informational entries intended for users
50 --- stdout reserved for Mnet notices, ignored by --test
51 WRN stderr logged warning entries, execution continues
52 ERR stderr perl die and warn outputs with stack trace
53 DIE stderr logged fatal errors, execution aborts
54  
55 The following options can be used to control log outputs:
56  
57 debug enable dbg log outputs
58 debug-error enable debug error file
59 quiet disable all stdout log outputs
60 silent disable all stdout and stderr log outputs
61  
62 Note that this module also installs __DIE__, __WARN__, INT, and TERM signal
63 handlers, in order to augment the logging of these events. These are made so
64 that compile and eval signals are processed by perl as normal.
65  
66 Note that timestamps and other varying data are filtered out of log outputs
67 when the --record, --replay, or --test cli options are enabled or if the
68 L<Mnet::Log::Test> module has been used.
69  
70 The signal handlers used in this module require perl 5.8 or newer.
71  
72 =head1 METHODS
73  
74 Mnet::Log implements the methods listed below.
75  
76 =cut
77  
78 # required modules
79 # note that Mnet modules should use Mnet::Log::Conditional, not this module
80 # perl 5.8 for signal handling functionality, see man perlipc
81 # update Mnet.pm and this perldoc if changed
82 141 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 141 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 141 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 141 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 141 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
87 141 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 141 ▶use Mnet::Opts;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 141 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 141 ▶use Mnet::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 141 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 # export function names
94 141 our @EXPORT_OK = qw( DEBUG INFO WARN FATAL );
95  
96 # begin block runs before init blocks of other modules
97 141 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.2
98  
99 # autoflush standard output
100 # so that multi-process syswrite lines are not split
101 141 $| = 1;
102  
103 # note start time of script, seconds since epoch, floating point
104 141 our $start_time = Time::HiRes::time();
105  
106 # init global flag to track that first log message was output
107 # this is used to output log entry when script started and finished
108 141 my $first = undef;
109  
110 # init global error flag, user to track first error message
111 # should be set for perl warn and die, and warn and fatal Mnet::Log calls
112 141 my $error = undef;
113  
114 # init global variable used to accumulate --debug-error output
115 # starts off as null, undef disables, refer to disable_debug_error sub
116 141 our $debug_error = "";
117  
118 # declare sub used by SIG handlers to log error and stack trace info
119 12 ▶ sub _sig_handler {
Subroutine _sig_handler: called CC=9 SCAR=22.7
120 12 my ($label, $caller, $error, $sev) = (shift, shift, shift, 7);
121 12 my $opts = Mnet::Opts::Cli::Cache::get({});
122 12 ▶ $sev = 3 if not defined $Mnet::Log::error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mnet::Log::error 9 3 12
123 12 output(undef, "ERR", 3, $caller, "$label, $error");
124 12 ▶ if (not $opts->{quiet} and not $label =~ /^perl (die|warn),\s(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"quiet"} || $label =~ /^perl (die|warn),\s(.*)/) 9 3 12
Condition66%2/3
$opts->{'quiet'} || $label =~ /^perl (die|warn),\s(.*)/
l!l&&r!l&&!r
309
MC/DC50%1/2
$opts->{'quiet'} || $label =~ /^perl (die|warn),\s(.*)/
$opts->{'quiet'} $label =~ /^perl (die|warn),\s(.*)/
Truth table66%2/3
A || B
A= $opts->{'quiet'}B= $label =~ /^perl (die|warn),\s(.*)/
ABresult
1X1
011
000
125 output(undef, "err", $sev, $caller, "$label, $_")
126 9 foreach split(/\n/, Carp::longmess());
127 9 ▶ output(undef,"err",$sev,$caller,"$label, \$! = $!") if $! ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $! ne "" 0 9 9
128 9 ▶ output(undef,"err",$sev,$caller,"$label, \$@ = $@") if $@ ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 9 9
129 9 ▶ output(undef,"err",$sev,$caller,"$label, \$? = $?") if $? ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $? ne "" 9 0 9
130 9 ▶ output(undef,"err",$sev,$caller,"$label, \$^E = $^E") if $^E ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $^E ne "" 0 9 9
131 }
132 }
133  
134 # trap perl die signal, log as error and exit with a failed status
135 # CORE::die handles propogated, compile, and eval warnings
136 # $^S is undef while compiling/parsing, true in eval, false otherwise
137 # exit with error after _sig_handler call to output error and stack trace
138 10 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
139 10 ▶ if (not @_ or not defined $^S or $^S) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ && defined $^S or $^S) { } 4 6 10
Condition83%5/6
not @_ && defined $^S or $^S
l!l&&r!l&&!r
136
@_ && defined $^S
!ll&&!rl&&r
019
MC/DC66%2/3
not @_ && defined $^S or $^S
@_ defined $^S $^S
Truth table75%3/4
not(A && B) or C
A= @_B= defined $^SC= $^S
ABCresult
0XX1
10X1
1111
1100
140 4 CORE::die(@_);
141 } else {
142 6 _sig_handler("perl die", scalar(caller), "@_");
143 }
144 6 exit 1;
145 141 };
146  
147 # trap perl warn signal, log as error and resume execution
148 # CORE::warn handles propogated and compile warnings
149 # $^S is undef while compiling/parsing, true in eval, false otherwise
150 # return after _sig_handler call to output error and stack trace
151 6 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
152 6 ▶ if (not @_ or not defined $^S) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ && defined $^S) { } 0 6 6
Condition33%1/3
@_ && defined $^S
!ll&&!rl&&r
006
MC/DC0%0/2
@_ && defined $^S
@_ defined $^S
Truth table33%1/3
A && B
A= @_B= defined $^S
ABresult
0X0
100
111
153 0 CORE::warn(@_);
154 } else {
155 6 _sig_handler("perl warn", scalar(caller), "@_");
156 }
157 6 return 1;
158 141 };
159  
160 # trap system interrupt signal, log as error and exit with failed status
161 # output a linefeed to stderr after ^C put there by shell
162 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 syswrite STDERR, "\n";
164 0 output(undef, "ERR", 3, scalar(caller), "interrupt signal received");
165 0 exit 1;
166 141 };
167  
168 # trap terminate signal, log as error and exit with failed status
169 0 ▶ $SIG{TERM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 0 output(undef, "ERR", 3, scalar(caller), "terminate signal received");
171 0 exit 1;
172 141 };
173  
174 # begin block finished
175 }
176  
177  
178  
179 # init cli options used by this module
180 140 ▶INIT {
Subroutine INIT: called CC=5 SCAR=16.1
181  
182 # init --debug option
183 Mnet::Opts::Cli::define({
184 getopt => 'debug!',
185 help_tip => 'set to display extra debug log entries',
186 help_text => '
187 note that the --quiet and --silent options override this option
188 use --debug-error for debug error file, see --help debug-error
189 refer also to the Mnet::Opts::Set::Debug pragma module
190 refer to perldoc Mnet::Log for more information
191 ',
192 140 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 118 22 140
193  
194 # init --debug-error option
195 Mnet::Opts::Cli::define({
196 getopt => 'debug-error=s',
197 help_hide => 1,
198 help_tip => 'set for debug file on errors, or stdout',
199 help_text => '
200 set to the path and name of debug file to write after any errors
201 this way you get debug logs for errors, and normal logs otherwise
202 any asterisk in filename will be replaced with timestamp and pid
203 send output to terminal using --debug-error /dev/stdout or stderr
204 refer to perldoc Mnet::Log for more information
205 ',
206 140 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 118 22 140
207  
208 # init --quiet option
209 Mnet::Opts::Cli::define({
210 getopt => 'quiet!',
211 help_tip => 'suppresses terminal stdout logging',
212 help_text => '
213 suppresses Mnet::Log stdout entries, not necessarily other stdout
214 use shell redirection if necessary to suppress all script stdout
215 note that setting this option overrides the --silent option
216 refer also to the Mnet::Opts::Set::Quiet pragma module
217 refer to perldoc Mnet::Log for more information
218 ',
219 140 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 118 22 140
220  
221 # init --silent option
222 Mnet::Opts::Cli::define({
223 getopt => 'silent!',
224 help_tip => 'suppresses stdout and stderr logging',
225 help_text => '
226 suppresses all Mnet::Log output, but not necessarily other output
227 use shell redirection if necessary to suppress all script output
228 note that this option can be overridden by the --quiet option
229 refer also to the Mnet::Opts::Set::Silent pragma module
230 refer to perldoc Mnet::Log for more information
231 ',
232 140 ▶ }) if $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Opts/Cli.pm"} 118 22 140
233  
234 # finished init code block
235 }
236  
237  
238  
239 28 ▶sub new {
Subroutine new: called CC=5 SCAR=17.2
Pod documented
240  
241 =head2 new
242  
243 $log = Mnet::Log->new(\%opts)
244  
245 This class method creates a new Mnet::Log object. The opts hash ref
246 argument is not requried but may be used to override any parsed cli options
247 parsed with the L<Mnet::Opts::Cli> module.
248  
249 The returned object may be used to call other documented methods in this module.
250  
251 The input opts hash ref may contain a log_id key which may be set to a device
252 name or other identifier which will be prepended to all entries made using the
253 returned Mnet::Log object. A warning will be issued if the log_id contains any
254 spaces.
255  
256 Refer to the SYNOPSIS section of this perldoc for more information.
257  
258 =cut
259  
260 # read input class and options hash ref merged with cli options
261 28 ▶ my $class = shift // croak("missing class arg");
Condition50%1/2
shift() // croak("missing class arg")
l!l
280
MC/DC0%0/1
shift() // croak("missing class arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
262 28 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition100%2/2
shift() // {}
l!l
1216
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
263  
264 # croak if log_id contains non-space characters
265 croak("invalid spaces in log_id $opts->{log_id}")
266 28 ▶ if defined $opts->{log_id} and $opts->{log_id} !~ /^\S+$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"log_id"} and not $opts->{"log_id"} =~ /^\S+$/ 0 28 28
Condition33%1/3
defined $opts->{"log_id"} and not $opts->{"log_id"} =~ /^\S+$/
!ll&&!rl&&r
2800
MC/DC0%0/2
defined $opts->{"log_id"} and not $opts->{"log_id"} =~ /^\S+$/
defined $opts->{"log_id"} not $opts->{"log_id"} =~ /^\S+$/
Truth table33%1/3
A and not(B)
A= defined $opts->{"log_id"}B= not $opts->{"log_id"} =~ /^\S+$/
ABresult
0X0
100
111
267  
268 # create log object from options object
269 28 my $self = bless $opts, $class;
270  
271 # finished new method
272 28 return $self;
273 }
274  
275  
276  
277 13 ▶sub batch_fork {
Subroutine batch_fork: called CC=1 SCAR=0.0
Pod undocumented
278  
279 # Mnet::Log::batch_fork($error_reset)
280 # purpose: called to reset start time for forked batch child
281 # $error_reset: used to reset Mnet::Log::error before child executes
282 # note: this is meant to be called from Mnet::Batch::fork only
283  
284 # reset forked child error, start time, debug error, and first log entry
285 13 my $error_reset = shift;
286 13 $Mnet::Log::error = $error_reset;
287 13 $Mnet::Log::start_time = Time::HiRes::time();
288 13 $Mnet::Log::debug_error = "";
289 13 $Mnet::Log::first = 0;
290 }
291  
292  
293  
294 126 ▶sub disable_debug_error {
Subroutine disable_debug_error: called CC=1 SCAR=0.0
Pod undocumented
295  
296 # Mnet::Log::disable_debug_error()
297 # purpose: called from Mnet::Opts::Cli if --debug_error not on command line
298  
299 # set global debug_error to undef to disable, to save memory
300 126 $Mnet::Log::debug_error = undef;
301 }
302  
303  
304 49 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
305  
306 =head2 Mnet::Log::error
307  
308 $error = Mnet::Log::error();
309  
310 This function returns the first line of error text from the perl warn or die
311 commands or Mnet::Log warn or fatal outputs, prefixed by the calling module.
312  
313 A value of undefined is returned if there have not yet been any errors.
314  
315 =cut
316  
317 # return contents of global error flag
318 49 return $Mnet::Log::error;
319 }
320  
321  
322  
323 6440 ▶sub output {
Subroutine output: called CC=42 SCAR=37.4
Pod undocumented
324  
325 # $true = output($self, $prefix, $severity, $caller, $text)
326 # purpose: used by other methods in this module to output Mnet::Log entries
327 # $self: object instance passed from public methods in this module, or undef
328 # $prefix: set to keyword dbg, inf, WRN, ERR, or "---" to bypass Mnet::Tee
329 # $severity: 7=debug, 6=info, 5=notice (no Mnet::Test), 4=warn, 3=error, 2=fatal
330 # $caller: original caller of method or function making log entry
331 # $text: zero or more lines of log text, timestamp/caller/etc will be added
332 # $true: boolean true is always returned, can use like: output() and ...
333  
334 # read args for object and/or text, level, and caller
335 6440 my ($self, $prefix, $severity, $caller) = (shift, shift, shift, shift);
336 6440 ▶ my $text = shift // "undef log text";
Condition50%1/2
shift() // "undef log text"
l!l
64400
MC/DC0%0/1
shift() // "undef log text"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
337 6440 ▶ $text = " " if $text eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "" 0 6440 6440
338  
339 # set self to hash ref of cached cli opts if we weren't called as a method
340 6440 ▶ $self = Mnet::Opts::Cli::Cache::get({}) if not defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 831 5609 6440
341  
342 # output first log entry, honoring current logging options
343 # Mnet::Opts::Set pragmas are in effect until Mnet::Opts::Cli->new call
344 # project scripts should call Mnet::Opts::Cli->new before Mnet::Log calls
345 # notice call for first line bypasses saving in Mnet::Tee::test_outputs
346 # filter pid if Mnet::Log::Test loaded, perhaps by Mnet::Opts::Cli->new
347 6440 ▶ if (not $Mnet::Log::first) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Mnet::Log::first) 149 6291 6440
348 149 $Mnet::Log::first = 1;
349 149 my $script_name = $0;
350 149 $script_name =~ s/^.*\///;
351 149 my $started = "$script_name started";
352 149 ▶ if (not $INC{"Mnet/Log/Test.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"Mnet/Log/Test.pm"}) 4 145 149
353 4 $started .= ", pid $$, ".localtime($Mnet::Log::start_time);
354 }
355 149 ▶ $Mnet::Log::debug_error = undef if not $INC{"Mnet/Opts/Cli.pm"};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"Mnet/Opts/Cli.pm"} 22 127 149
356 149 NOTICE($started);
357 149 output(undef, "dbg", 7, "Mnet::Version", Mnet::Version::info());
358 }
359  
360 # return early for debug entries if no --debug set and debug_error disabled
361 return 1 if $severity > 6
362 and not $self->{debug}
363 6440 ▶ and not defined $Mnet::Log::debug_error;
Branch: 1 branch
BranchTrueFalseTotal
if $severity > 6 and not $self->{"debug"} and not defined $Mnet::Log::debug_error 2890 3550 6440
Condition100%6/6
$severity > 6 and not $self->{"debug"} and not defined $Mnet::Log::debug_error
!ll&&!rl&&r
249710532890
$severity > 6 and not $self->{"debug"}
!ll&&!rl&&r
85316443943
MC/DC100%3/3
$severity > 6 and not $self->{"debug"} and not defined $Mnet::Log::debug_error
$severity > 6 not $self->{"debug"} not defined $Mnet::Log::debug_error
Truth table100%4/4
A and not(B) and not(C)
A= $severity > 6B= not $self->{"debug"}C= not defined $Mnet::Log::debug_error
ABCresult
0XX0
10X0
1100
1111
364  
365 # update global error flag with first line of first error entry
366 3550 ▶ $Mnet::Log::error = "$caller, ".(split(/\n/, $text))[0]
Branch: 1 branch
BranchTrueFalseTotal
if $severity < 5 and not defined $Mnet::Log::error 36 3514 3550
Condition100%3/3
$severity < 5 and not defined $Mnet::Log::error
!ll&&!rl&&r
34645036
MC/DC100%2/2
$severity < 5 and not defined $Mnet::Log::error
$severity < 5 not defined $Mnet::Log::error
Truth table100%3/3
A and not(B)
A= $severity < 5B= not defined $Mnet::Log::error
ABresult
0X0
100
111
367 if $severity < 5 and not defined $Mnet::Log::error;
368  
369 # set hh:mm:ss timestamp for entries as long as --test is not set
370 # timestamps are filtered from output with --test/record/replay cli opts
371 3550 my ($timestamp, $sec, $min, $hr, $mday, $mon, $yr) = ("", localtime());
372 $timestamp = sprintf("%02d:%02d:%02d ", $hr, $min, $sec)
373 if not $INC{"Mnet/Log/Test.pm"} and not $self->{test}
374 3550 ▶ and not $self->{record} and not $self->{replay};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"Mnet/Log/Test.pm"} or $self->{"test"} or $self->{"record"} or $self->{"replay"} 36 3514 3550
Condition66%6/9
$INC{"Mnet/Log/Test.pm"} or $self->{"test"} or $self->{"record"} or $self->{"replay"}
l!l&&r!l&&!r
3514036
$INC{"Mnet/Log/Test.pm"} or $self->{"test"} or $self->{"record"}
l!l&&r!l&&!r
3514036
$INC{"Mnet/Log/Test.pm"} or $self->{"test"}
l!l&&r!l&&!r
3514036
MC/DC25%1/4
$INC{"Mnet/Log/Test.pm"} or $self->{"test"} or $self->{"record"} or $self->{"replay"}
$INC{"Mnet/Log/Test.pm"} $self->{"test"} $self->{"record"} $self->{"replay"}
Truth table40%2/5
A or B or C or D
A= $INC{"Mnet/Log/Test.pm"}B= $self->{"test"}C= $self->{"record"}D= $self->{"replay"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
375  
376 # note identifier for Mnet::Log entries
377 3550 ▶ my $log_id = $self->{log_id} // "-";
Condition100%2/2
$self->{"log_id"} // "-"
l!l
773473
MC/DC100%1/1
$self->{"log_id"} // "-"
$self->{"log_id"}
Truth table100%2/2
A
A= $self->{"log_id"}
Aresult
11
00
378  
379 # loop through lines of text and output log entries
380 3550 foreach my $line (split(/\n/, $text)) {
381  
382 # prepare output log entry for each line of text
383 4565 $line = "${timestamp}$prefix $log_id $caller $line";
384  
385 # accumulate all output log entries to --debug-error buffer
386 4565 ▶ $Mnet::Log::debug_error .= "$line\n" if defined $Mnet::Log::debug_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Mnet::Log::debug_error 2562 2003 4565
387  
388 # stdout sev 7 debug
389 # not included in Mnet::Tee::test_outputs
390 # if --quiet or --silent set then output to --tee file only
391 4565 ▶ if ($severity == 7 and $self->{debug}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($severity == 7 and $self->{'debug'}) { } 1973 2592 4565
elsif ($severity == 6) { } 293 2299 2592
elsif ($severity == 5) { } 474 1825 2299
elsif ($severity < 5) { } 86 1739 1825
Condition100%3/3
$severity == 7 and $self->{"debug"}
!ll&&!rl&&r
85317391973
MC/DC100%2/2
$severity == 7 and $self->{"debug"}
$severity == 7 $self->{"debug"}
Truth table100%3/3
A and B
A= $severity == 7B= $self->{"debug"}
ABresult
0X0
100
111
392 1973 ▶ Mnet::Tee::test_pause() if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 1377 596 1973
393 1973 ▶ if ($self->{silent} or $self->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'silent'} or $self->{'quiet'}) { } 106 1867 1973
Condition100%3/3
$self->{"silent"} or $self->{"quiet"}
l!l&&r!l&&!r
74321867
MC/DC100%2/2
$self->{"silent"} or $self->{"quiet"}
$self->{"silent"} $self->{"quiet"}
Truth table100%3/3
A or B
A= $self->{"silent"}B= $self->{"quiet"}
ABresult
1X1
011
000
394 106 ▶ Mnet::Tee::tee_no_term("$line\n") if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 16 90 106
395 } else {
396 1867 syswrite STDOUT, "$line\n";
397 }
398 1973 ▶ Mnet::Tee::test_unpause() if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 1377 596 1973
399  
400 # stdout sev 6 info
401 # included in Mnet::Tee::test_outputs
402 # if --quiet or --silent set then output to --tee file only
403 } elsif ($severity == 6) {
404 293 ▶ if ($self->{silent} or $self->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'silent'} or $self->{'quiet'}) { } 156 137 293
Condition100%3/3
$self->{"silent"} or $self->{"quiet"}
l!l&&r!l&&!r
20136137
MC/DC100%2/2
$self->{"silent"} or $self->{"quiet"}
$self->{"silent"} $self->{"quiet"}
Truth table100%3/3
A or B
A= $self->{"silent"}B= $self->{"quiet"}
ABresult
1X1
011
000
405 156 ▶ Mnet::Tee::tee_no_term("$line\n") if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 116 40 156
406 } else {
407 137 syswrite STDOUT, "$line\n";
408 }
409  
410 # stdout sev 5 notice
411 # not included in Mnet::Tee::test_outputs
412 # if --quiet or --silent set then output to --tee file only
413 } elsif ($severity == 5) {
414 474 ▶ Mnet::Tee::test_pause() if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 269 205 474
415 474 ▶ if ($self->{silent} or $self->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'silent'} or $self->{'quiet'}) { } 139 335 474
Condition100%3/3
$self->{"silent"} or $self->{"quiet"}
l!l&&r!l&&!r
6574335
MC/DC100%2/2
$self->{"silent"} or $self->{"quiet"}
$self->{"silent"} $self->{"quiet"}
Truth table100%3/3
A or B
A= $self->{"silent"}B= $self->{"quiet"}
ABresult
1X1
011
000
416 139 ▶ Mnet::Tee::tee_no_term("$line\n") if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 77 62 139
417 } else {
418 335 syswrite STDOUT, "$line\n";
419 }
420 474 ▶ Mnet::Tee::test_unpause() if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 269 205 474
421  
422 # stderr sev 4 warn, sev 3 error, and sev 2 fatal
423 # included in Mnet::Tee::test_outputs
424 # if --quiet and perl die/warn error then output original error only
425 # if --silent set then output to --tee file only
426 } elsif ($severity < 5) {
427 86 ▶ $line = $2 if $self->{quiet} and $text =~ /^perl (die|warn),\s(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quiet"} and $text =~ /^perl (die|warn),\s(.*)/ 3 83 86
Condition100%3/3
$self->{"quiet"} and $text =~ /^perl (die|warn),\s(.*)/
!ll&&!rl&&r
67163
MC/DC100%2/2
$self->{"quiet"} and $text =~ /^perl (die|warn),\s(.*)/
$self->{"quiet"} $text =~ /^perl (die|warn),\s(.*)/
Truth table100%3/3
A and B
A= $self->{"quiet"}B= $text =~ /^perl (die|warn),\s(.*)/
ABresult
0X0
100
111
428 86 ▶ if ($self->{silent} and not $self->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'silent'} and not $self->{'quiet'}) { } 14 72 86
Condition100%3/3
$self->{"silent"} and not $self->{"quiet"}
!ll&&!rl&&r
69314
MC/DC100%2/2
$self->{"silent"} and not $self->{"quiet"}
$self->{"silent"} not $self->{"quiet"}
Truth table100%3/3
A and not(B)
A= $self->{"silent"}B= not $self->{"quiet"}
ABresult
0X0
100
111
429 14 ▶ Mnet::Tee::tee_no_term("$line\n") if $INC{"Mnet/Tee.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Tee.pm"} 4 10 14
430 } else {
431 72 syswrite STDERR, "$line\n";
432 }
433 }
434  
435 # continue looping through lines of text
436 }
437  
438 # finished output function
439 3550 return 1;
440 }
441  
442  
443  
444 12 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod documented
445  
446 =head2 debug
447  
448 $log->debug($text)
449  
450 Output a debug entry to stdout with an Mnet::Log prefix of dbg.
451  
452 =cut
453  
454 # call output function
455 12 my ($self, $text) = (shift, shift);
456 12 return $self->output("dbg", 7, scalar(caller), $text);
457 }
458  
459  
460  
461 11 ▶sub info {
Subroutine info: called CC=1 SCAR=0.0
Pod documented
462  
463 =head2 info
464  
465 $log->info($text)
466  
467 Output an info entry to stdout with an Mnet::Log prefix of inf.
468  
469 =cut
470  
471 # call output function
472 11 my ($self, $text) = (shift, shift);
473 11 return $self->output("inf", 6, scalar(caller), $text);
474 }
475  
476  
477  
478 0 ▶sub notice {
Subroutine notice: not called CC=1 SCAR=6.9
Pod undocumented
479  
480 # $self->notice($text)
481 # purpose: output log text to stdout, bypassing Mnet::Test log capture
482 # $text: zero or more lines of log text, timestamp/caller/etc will be added
483  
484 # call output function
485 0 my ($self, $text) = (shift, shift);
486 0 return output($self, "---", 5, scalar(caller), $text);
487 }
488  
489  
490  
491 11 ▶sub warn {
Subroutine warn: called CC=1 SCAR=0.0
Pod documented
492  
493 =head2 warn
494  
495 $log->warn($text)
496  
497 Output a warn entry to stderr with an Mnet::Log prefix of WRN.
498  
499 =cut
500  
501 # call output function
502 11 my ($self, $text) = (shift, shift);
503 11 return $self->output("WRN", 4, scalar(caller), $text);
504 }
505  
506  
507  
508 6 ▶sub fatal {
Subroutine fatal: called CC=2 SCAR=6.9
Pod documented
509  
510 =head2 fatal
511  
512 $log->fatal($text)
513  
514 Output a fatal entry to stderr with an Mnet::log prefix of DIE. Note that calls
515 to fatal are handled in an eval the same as calls to die.
516  
517 =cut
518  
519 # call normal die in an eval, otherwise call output function
520 # $^S is undef while compiling/parsing, true in eval, false otherwise
521 6 my ($self, $text) = (shift, shift);
522 6 ▶ CORE::die("$text\n") if $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S 1 5 6
523 5 $self->output("DIE", 2, scalar(caller), $text);
524 5 exit 1;
525 }
526  
527  
528  
529 =head1 FUNCTIONS
530  
531 Mnet::Log also implements the functions listed below.
532  
533 =cut
534  
535  
536  
537 199 ▶sub DEBUG {
Subroutine DEBUG: called CC=1 SCAR=0.0
Pod documented
538  
539 =head2 DEBUG
540  
541 DEBUG($text)
542  
543 Output a debug entry to stdout with an Mnet::Log prefix of dbg.
544  
545 =cut
546  
547 # call output function
548 199 my $text = shift;
549 199 return output(undef, "dbg", 7, scalar(caller), $text);
550 }
551  
552  
553  
554 12 ▶sub INFO {
Subroutine INFO: called CC=1 SCAR=0.0
Pod documented
555  
556 =head2 INFO
557  
558 INFO($text)
559  
560 Output an info entry to stdout with an Mnet::Log prefix of inf.
561  
562 =cut
563  
564 # call output function
565 12 my $text = shift;
566 12 return output(undef, "inf", 6, scalar(caller), $text);
567 }
568  
569  
570  
571 286 ▶sub NOTICE {
Subroutine NOTICE: called CC=1 SCAR=0.0
Pod undocumented
572  
573 # NOTICE($text)
574 # purpose: output log text to stdout, bypassing Mnet::Test log capture
575 # $text: zero or more lines of log text, timestamp/caller/etc will be added
576  
577 # call output function
578 286 my $text = shift;
579 286 return output(undef, "---", 5, scalar(caller), $text);
580 }
581  
582  
583  
584 9 ▶sub WARN {
Subroutine WARN: called CC=1 SCAR=0.0
Pod documented
585  
586 =head2 WARN
587  
588 WARN($text)
589  
590 Output a warn entry to stderr with an Mnet::Log prefix of WRN.
591  
592 =cut
593  
594 # call output function
595 9 my $text = shift;
596 9 return output(undef, "WRN", 4, scalar(caller), $text);
597 }
598  
599  
600  
601 8 ▶sub FATAL {
Subroutine FATAL: called CC=2 SCAR=6.9
Pod documented
602  
603 =head2 FATAL
604  
605 FATAL($text)
606  
607 Output a fatal entry to stderr with an Mnet::Log prefix of DIE. Note that calls
608 to fatal are handled in an eval the same as calls to die.
609  
610 =cut
611  
612 # call normal die in an eval, otherwise call output function
613 # $^S is undef while compiling/parsing, true in eval, false otherwise
614 8 my $text = shift;
615 8 ▶ CORE::die("$text\n") if ($^S);
Branch: 1 branch
BranchTrueFalseTotal
if $^S 1 7 8
616 7 output(undef, "DIE", 2, scalar(caller), $text);
617 7 exit 1;
618 }
619  
620  
621  
622 # output Mnet::Log finished entry at end of script
623 # output log prefix "---" bypasses Mnet::Test recording of these entries
624 141 ▶END {
Subroutine END: called CC=20 SCAR=30.1
625  
626 # note --debug_error filename, substitute unique timestamp/pid for asterisk
627 # unique timestampt/pid allows for --debug_error used from batch parent
628 141 my $debug_error_file = Mnet::Opts::Cli::Cache::debug_error();
629 141 ▶ if (defined $debug_error_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug_error_file) 1 140 141
630 1 my ($sec, $min, $hr, $mday, $mon, $yr) = localtime();
631 1 $yr += 1900; $mon += 1;
632 1 my $date_stamp = sprintf("%02s-%02s-%02s", $yr, $mon, $mday);
633 1 my $time_stamp = sprintf("%02s:%02s:%02s", $hr, $min, $sec);
634 1 $debug_error_file =~ s/\*/${date_stamp}_${time_stamp}_pid-$$/;
635 }
636  
637 # prepare summary/finished info for last log entry
638 141 my $finished = "no errors";
639 141 ▶ $finished = "exit error status" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 26 115 141
640 141 ▶ $finished = "errors" if defined $Mnet::Log::error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Mnet::Log::error 34 107 141
641 141 my $elapsed = Time::HiRes::time - $Mnet::Log::start_time;
642 141 $elapsed = sprintf("%.3f secs elapsed", $elapsed);
643 141 ▶ $finished .= ", pid $$, $elapsed" if not $INC{"Mnet/Log/Test.pm"};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"Mnet/Log/Test.pm"} 4 137 141
644  
645 # output notice of errors
646 # only if Mnet::Log::error is set, will be text of first error
647 # only if first line was output, meaning logging was enabled/used
648 # only if Mnet::Log::Test was not used to filter varying log outputs
649 # Mnet::Opts::Cli->new loads Mnet::Log::Test if --test/record/replay set
650 NOTICE("detected at least one error, in $Mnet::Log::error")
651 if defined $Mnet::Log::error
652 and $Mnet::Log::first
653 141 ▶ and not $INC{"Mnet/Log/Test.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Mnet::Log::error and $Mnet::Log::first and not $INC{"Mnet/Log/Test.pm"} 0 141 141
Condition66%4/6
defined $Mnet::Log::error and $Mnet::Log::first and not $INC{"Mnet/Log/Test.pm"}
!ll&&!rl&&r
107340
defined $Mnet::Log::error and $Mnet::Log::first
!ll&&!rl&&r
107034
MC/DC0%0/3
defined $Mnet::Log::error and $Mnet::Log::first and not $INC{"Mnet/Log/Test.pm"}
defined $Mnet::Log::error $Mnet::Log::first not $INC{"Mnet/Log/Test.pm"}
Truth table50%2/4
A and B and not(C)
A= defined $Mnet::Log::errorB= $Mnet::Log::firstC= not $INC{"Mnet/Log/Test.pm"}
ABCresult
0XX0
10X0
1100
1111
654  
655 # output notice if we will be writing --debug-error output
656 # this puts notice on screen, in file, and before finished message
657 141 ▶ NOTICE("creating --debug-error $debug_error_file")
Branch: 1 branch
BranchTrueFalseTotal
if $Mnet::Log::error and defined $debug_error_file 1 140 141
Condition100%3/3
$Mnet::Log::error and defined $debug_error_file
!ll&&!rl&&r
107331
MC/DC100%2/2
$Mnet::Log::error and defined $debug_error_file
$Mnet::Log::error defined $debug_error_file
Truth table100%3/3
A and B
A= $Mnet::Log::errorB= defined $debug_error_file
ABresult
0X0
100
111
658 if $Mnet::Log::error and defined $debug_error_file;
659  
660 # output last finished log notice
661 # only if first line was output, meaning logging was enabled/used
662 141 ▶ NOTICE("finished, $finished") if $Mnet::Log::first;
Branch: 1 branch
BranchTrueFalseTotal
if $Mnet::Log::first 136 5 141
663  
664 # output --debug-error file, if there were errors and that opt was set
665 141 ▶ if (defined $Mnet::Log::error and defined $debug_error_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Mnet::Log::error and defined $debug_error_file) 1 140 141
Condition100%3/3
defined $Mnet::Log::error and defined $debug_error_file
!ll&&!rl&&r
107331
MC/DC100%2/2
defined $Mnet::Log::error and defined $debug_error_file
defined $Mnet::Log::error defined $debug_error_file
Truth table100%3/3
A and B
A= defined $Mnet::Log::errorB= defined $debug_error_file
ABresult
0X0
100
111
666 1 ▶ if (open(my $fh, ">", $debug_error_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $debug_error_file) { } 1 0 1
667 1 my ($sep1, $sep2) = ("-" x 14, "-" x 36);
668 1 syswrite $fh, "\n$sep1 DEBUG ERROR OUTPUT STARTING $sep2\n\n";
669 1 ▶ syswrite $fh, $Mnet::Log::debug_error // "<undef>\n";
Condition50%1/2
$Mnet::Log::debug_error // "<undef>\n"
l!l
10
MC/DC0%0/1
$Mnet::Log::debug_error // "<undef>\n"
$Mnet::Log::debug_error
Truth table50%1/2
A
A= $Mnet::Log::debug_error
Aresult
11
00
670 1 syswrite $fh, "\n$sep1 DEBUG ERROR OUTPUT FINISHED $sep2\n\n";
671 1 close $fh;
672 } else {
673 0 WARN("unable to open --debug_error $debug_error_file, $!");
674 }
675 }
676  
677 # call Mnet::Test to process --record and --test cli opt, if loaded
678 # called here so that tests occur after last line of Mnet::Log output
679 # --test diff undef if --replay --test diff was not attempted
680 # --test diff is null for no diff, exit clean even if output had warnings
681 # --test diff is non-null for failed diff, exit with a failed status
682 141 ▶ if ($INC{"Mnet/Test.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Mnet/Test.pm"}) 74 67 141
683 74 my $diff = Mnet::Test::done();
684 72 ▶ if (defined $diff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $diff) 5 67 72
685 5 ▶ exit 0 if not $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 4 1 5
686 1 ▶ exit 1 if $diff;
Branch: 1 branch
BranchTrueFalseTotal
if $diff 1 0 1
687 }
688 }
689  
690 # set failed exit status if any errors were caught by signal handlers
691 134 ▶ exit 1 if defined $Mnet::Log::error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Mnet::Log::error 34 100 134
692  
693 # finished end block
694 }
695  
696  
697  
698 =head1 TESTING
699  
700 When used with the L<Mnet::Test> --record option all stdout and stderr log
701 entry output from this module is captured with the exception of dbg entries.
702  
703 Refer to the L<Mnet::Test> module for more information.
704  
705 =head1 SEE ALSO
706  
707 L<Mnet>
708  
709 L<Mnet::Log::Conditional>
710  
711 L<Mnet::Log::Test>
712  
713 L<Mnet::Opts::Cli>
714  
715 L<Mnet::Opts::Set::Debug>
716  
717 L<Mnet::Opts::Set::Quiet>
718  
719 L<Mnet::Opts::Set::Silent>
720  
721 L<Mnet::Test>
722  
723 L<Mnet::Version>
724  
725 =cut
726  
727 # normal end of package
728 141 1;
729  
« Mnet/Opts/Cli.pm ↑ Summary Mnet/Batch.pm »