Mnet/Log/Conditional.pm

filter: Statementstmt 100% 68 / 68 Branchbran 91.6% 22 / 24 Conditioncond 54.5% 6 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 17 / 17 Podpod 81.8% 9 / 11 totaltotal 89.7% 123 / 137 CC 18 SCAR 9.9
CC
18
Cov
93%
new
5.6
fatal
4.1
warn
3.0
CRAP 2.7
« Mnet/Tee.pm ↑ Summary Mnet/Opts/Set.pm »
1 package Mnet::Log::Conditional;
2  
3 =head1 NAME
4  
5 Mnet::Log::Conditional - Use Mnet::Log if Mnet::Log is loaded
6  
7 =head1 SYNOPSIS
8  
9 use Mnet::Log::Conditional qw( DEBUG INFO WARN FATAL );
10  
11 # nothing will happen unless Mnet::Log was loaded
12 INFO("starting");
13  
14 # errors will still always go to stderr
15 WARN("error");
16 FATAL("abort");
17  
18 my $log = Mnet::Log::Conditional->new($opts);
19 $log->DEBUG("object oriented interace");
20  
21 =head1 DESCRIPTION
22  
23 Mnet::Log::Conditional can be called to output log entries only if the
24 L<Mnet::Log> module has already been otherwise loaded.
25  
26 This is used by other L<Mnet> modules for logging, so that they generate
27 log output only if the calling script is using the L<Mnet::Log> module. Users
28 who create custom modules may want to do the same thing.
29  
30 Refer to L<Mnet::Log> for more information.
31  
32 =head1 METHODS
33  
34 Mnet::Log::Conditional implements the methods listed below.
35  
36 =cut
37  
38 # required modules
39 # modules below can't import from this module due to Exporter catch-22,
40 # symbols aren't available for export until import has a chance to run,
41 # workaround is call with path, example: Mnet::Log::Conditional::INFO()
42 181 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 181 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 181 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 181 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
46 181 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 181 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # export function names
50 181 our @EXPORT_OK = qw( DEBUG INFO NOTICE WARN FATAL );
51  
52  
53  
54 414 ▶sub new {
Subroutine new: called CC=5 SCAR=17.2
Pod documented
55  
56 =head2 new
57  
58 $log = Mnet::Log::Conditional->new(\%opts)
59  
60 This class method creates a new Mnet::Log::Conditional object. The opts hash
61 ref argument is not requried but may be used to override any parsed cli options
62 parsed with the L<Mnet::Opts::Cli> module.
63  
64 The returned object may be used to call other documented functions and methods
65 in this module, which will call the L<Mnet::Log> module if it is loaded.
66  
67 Refer to the new method in perldoc L<Mnet::Log> for more information.
68  
69 =cut
70  
71 # read input class and options hash ref merged with cli options
72 414 ▶ my $class = shift // croak("missing class arg");
Condition50%1/2
shift() // croak("missing class arg")
l!l
4140
MC/DC0%0/1
shift() // croak("missing class arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
73 414 ▶ my $opts = Mnet::Opts::Cli::Cache::get(shift // {});
Condition100%2/2
shift() // {}
l!l
253161
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
74  
75 # warn if log_id contains non-space characters
76 croak("invalid log_id $opts->{log_id}")
77 414 ▶ 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 414 414
Condition33%1/3
defined $opts->{"log_id"} and not $opts->{"log_id"} =~ /^\S+$/
!ll&&!rl&&r
41400
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
78  
79 # create log object from options object
80 414 my $self = bless $opts, $class;
81  
82 # finished new method
83 414 return $self;
84 }
85  
86  
87  
88 5362 ▶sub debug {
Subroutine debug: called CC=2 SCAR=6.9
Pod documented
89  
90 =head2 debug
91  
92 $log->debug($text)
93  
94 Output a debug entry using the L<Mnet::Log> module, if loaed.
95  
96 =cut
97  
98 # call Mnet::Log::output if loaded or return
99 5362 my ($self, $text) = (shift, shift);
100 return Mnet::Log::output($self, "dbg", 7, scalar(caller), $text)
101 5362 ▶ if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 5198 164 5362
102 164 return 1;
103 }
104  
105  
106  
107 311 ▶sub info {
Subroutine info: called CC=2 SCAR=6.9
Pod documented
108  
109 =head2 info
110  
111 $log->info($text)
112  
113 Output an info entry using the L<Mnet::Log> module, if loaed.
114  
115 =cut
116  
117 # call Mnet::Log::output if loaded or return
118 311 my ($self, $text) = (shift, shift);
119 return Mnet::Log::output($self, "inf", 6, scalar(caller), $text)
120 311 ▶ if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 269 42 311
121 42 return 1;
122 }
123  
124  
125  
126 100 ▶sub notice {
Subroutine notice: called CC=2 SCAR=6.9
Pod undocumented
127  
128 # $self->notice($text)
129 # purpose: output notice using Mnet::Log if loaded, otherwise nothing happens
130  
131 # call Mnet::Log::output if loaded or return;
132 100 my ($self, $text) = (shift, shift);
133 return Mnet::Log::output($self, "---", 5, scalar(caller), $text)
134 100 ▶ if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 97 3 100
135 3 return 1;
136 }
137  
138  
139  
140 2 ▶sub warn {
Subroutine warn: called CC=3 SCAR=11.0
Pod documented
141  
142 =head2 warn
143  
144 $log->warn($text)
145  
146 Output a warn entry using the L<Mnet::Log> module, if loaed.
147  
148 =cut
149  
150 # call Mnet::Log::output if loaded or warn
151 2 my ($self, $text) = (shift, shift);
152 2 ▶ if ($INC{"Mnet/Log.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Mnet/Log.pm'}) { } 1 1 2
153 1 Mnet::Log::output($self, "WRN", 4, scalar(caller), $text);
154 } else {
155 1 $text =~ s/\n*$//;
156 1 ▶ my $log_id = $self->{log_id} // "-";
Condition50%1/2
$self->{"log_id"} // "-"
l!l
01
MC/DC0%0/1
$self->{"log_id"} // "-"
$self->{"log_id"}
Truth table50%1/2
A
A= $self->{"log_id"}
Aresult
11
00
157 1 CORE::warn("WRN $log_id " . scalar(caller) . " $text\n");
158 }
159 2 return 1;
160 }
161  
162  
163  
164 7 ▶sub fatal {
Subroutine fatal: called CC=4 SCAR=14.0
Pod documented
165  
166 =head2 fatal
167  
168 $log->fatal($text)
169  
170 Output a fatal entry using the L<Mnet::Log> module, if loaded.
171  
172 =cut
173  
174 # call Mnet::Log output if loaded or die
175 # $^S is undef while compiling/parsing, true in eval, false otherwise
176 7 my ($self, $text) = (shift, shift);
177 7 ▶ if ($INC{"Mnet/Log.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Mnet/Log.pm'}) { } 5 2 7
178 5 ▶ CORE::die("$text\n") if $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 5 5
179 5 Mnet::Log::output($self, "DIE", 2, scalar(caller), $text);
180 } else {
181 2 ▶ my $log_id = $self->{log_id} // "-";
Condition50%1/2
$self->{"log_id"} // "-"
l!l
02
MC/DC0%0/1
$self->{"log_id"} // "-"
$self->{"log_id"}
Truth table50%1/2
A
A= $self->{"log_id"}
Aresult
11
00
182 2 CORE::die("DIE $log_id " . scalar(caller) . " $text\n");
183 }
184 5 exit 1;
185 }
186  
187  
188  
189 =head1 FUNCTIONS
190  
191 Mnet::Log::Conditional also implements the functions listed below.
192  
193 =cut
194  
195  
196  
197 26 ▶sub DEBUG {
Subroutine DEBUG: called CC=2 SCAR=6.9
Pod documented
198  
199 =head2 DEBUG
200  
201 DEBUG($text)
202  
203 Output a debug entry using the L<Mnet::Log> module, if loaed.
204  
205 =cut
206  
207 # call Mnet::Log::output if loaded or return;
208 26 my $text = shift;
209 return Mnet::Log::output(undef, "dbg", 7, scalar(caller), $text)
210 26 ▶ if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 25 1 26
211 1 return 1;
212 }
213  
214  
215  
216 2 ▶sub INFO {
Subroutine INFO: called CC=2 SCAR=6.9
Pod documented
217  
218 =head2 INFO
219  
220 INFO($text)
221  
222 Output an info entry using the L<Mnet::Log> module, if loaed.
223  
224 =cut
225  
226 # call Mnet::Log::output if loaded or return;
227 2 my $text = shift;
228 return Mnet::Log::output(undef, "inf", 6, scalar(caller), $text)
229 2 ▶ if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 1 1 2
230 1 return 1;
231 }
232  
233  
234  
235 92 ▶sub NOTICE {
Subroutine NOTICE: called CC=2 SCAR=6.9
Pod undocumented
236  
237 # NOTICE($text)
238 # purpose: output notice using Mnet::Log if loaded, otherwise nothing happens
239  
240 # call Mnet::Log::output if loaded or return;
241 92 my $text = shift;
242 return Mnet::Log::output(undef, "---", 5, scalar(caller), $text)
243 92 ▶ if $INC{"Mnet/Log.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} 91 1 92
244 1 return 1;
245 }
246  
247  
248  
249 5 ▶sub WARN {
Subroutine WARN: called CC=2 SCAR=6.9
Pod documented
250  
251 =head2 WARN
252  
253 WARN($text)
254  
255 Output a warn entry using the L<Mnet::Log> module, if loaed.
256  
257 =cut
258  
259 # call Mnet::Log::output if loaded or warn
260 5 my $text = shift;
261 5 ▶ if ($INC{"Mnet/Log.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Mnet/Log.pm'}) { } 3 2 5
262 3 Mnet::Log::output(undef, "WRN", 4, scalar(caller), $text);
263 } else {
264 2 $text =~ s/\n*$//;
265 2 CORE::warn("WRN - " . scalar(caller) . " $text\n");
266 }
267 5 return 1;
268 }
269  
270  
271  
272 5 ▶sub FATAL {
Subroutine FATAL: called CC=2 SCAR=6.9
Pod documented
273  
274 =head2 FATAL
275  
276 FATAL($text)
277  
278 Output a fatal entry using the L<Mnet::Log> module, if loaed.
279  
280 =cut
281  
282 # call Mnet::Log::output if loaded or die
283 5 my $text = shift;
284 5 ▶ if ($INC{"Mnet/Log.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Mnet/Log.pm'}) { } 3 2 5
285 3 Mnet::Log::output(undef, "DIE", 2, scalar(caller), $text);
286 } else {
287 2 CORE::die("DIE - " . scalar(caller) . " $text\n");
288 }
289 3 exit 1;
290 }
291  
292  
293  
294 =head1 SEE ALSO
295  
296 L<Mnet>
297  
298 L<Mnet::Log>
299  
300 =cut
301  
302 # normal end of package
303 181 1;
304  
« Mnet/Tee.pm ↑ Summary Mnet/Opts/Set.pm »