blib/lib/App/CELL/Status.pm

filter: Statementstmt 89.5% 120 / 134 Branchbran 75.7% 50 / 66 Conditioncond 61.1% 11 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 96.1% 25 / 26 Podpod 100% 12 / 12 totaltotal 83.5% 223 / 267 CC 42 SCAR 18.8
CC
42
Cov
83%
dump
16.9
new
11.6
expurgate
5.1
CRAP 6.5
« blib/lib/App/CELL/Message.pm ↑ Summary blib/lib/App/CELL/Util.pm »
1 # *************************************************************************
2 # Copyright (c) 2014-2020, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32  
33 package App::CELL::Status;
34  
35 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 18 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 18 ▶use App::CELL::Log qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 18 ▶use App::CELL::Util qw( stringify_args );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 18 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 18 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
44 18 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 18 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47  
48  
49 =head1 NAME
50  
51 App::CELL::Status - class for return value objects
52  
53  
54  
55 =head1 SYNOPSIS
56  
57 use App::CELL::Status;
58  
59 # simplest usage
60 my $status = App::CELL::Status->ok;
61 print "ok" if ( $status->ok );
62 $status = App::CELL::Status->not_ok;
63 print "NOT ok" if ( $status->not_ok );
64  
65 # as a return value: in the caller
66 my $status = $XYZ( ... );
67 return $status if not $status->ok; # handle failure
68 my $payload = $status->payload; # handle success
69  
70  
71  
72 =head1 INHERITANCE
73  
74 This module inherits from C<App::CELL::Message>
75  
76 =cut
77  
78 18 ▶use parent qw( App::CELL::Message );
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80  
81  
82 =head1 DESCRIPTION
83  
84 An App::CELL::Status object is a reference to a hash containing some or
85 all of the following keys (attributes):
86  
87 =over
88  
89 =item C<level> - the status level (see L</new>, below)
90  
91 =item C<message> - message explaining the status
92  
93 =item C<caller> - an array reference containing the three-item list
94 generated by the C<caller> function
95  
96 =back
97  
98 The typical use cases for this object are:
99  
100 =over
101  
102 =item As a return value from a function call
103  
104 =item To trigger a higher-severity log message
105  
106 =back
107  
108 All calls to C<< App::CELL::Status->new >> with a status other than OK
109 trigger a log message.
110  
111  
112  
113 =head1 PUBLIC METHODS
114  
115 This module provides the following public methods:
116  
117  
118  
119 =head2 new
120
121 Construct a status object and trigger a log message if the level is anything
122 other than "OK". Always returns a status object. If no level is specified, the
123 level will be 'ERR'. If no code is given, the code will be undefined (I think).
124  
125 =cut
126  
127 206 ▶sub new {
Subroutine new: called CC=11 SCAR=24.5
Pod documented
128 206 my ( $class, @ARGS ) = @_;
129 206 my %ARGS = @ARGS;
130 206 my $self;
131  
132 # default to ERR level
133 206 ▶ unless ( defined $ARGS{level} and grep { $ARGS{level} eq $_ } $log->permitted_levels ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ARGS{"level"} and grep {$ARGS{"level"} eq $_;} $App::CELL::Status::log->permitted_levels) 1 205 206
Condition66%2/3
defined $ARGS{"level"} and grep {$ARGS{"level"} eq $_;} $App::CELL::Status::log->permitted_levels
!ll&&!rl&&r
01205
MC/DC50%1/2
defined $ARGS{"level"} and grep {$ARGS{"level"} eq $_;} $App::CELL::Status::log->permitted_levels
defined $ARGS{"level"} grep {$ARGS{"level"} eq $_;} $App::CELL::Status::log->permitted_levels
Truth table66%2/3
A and B
A= defined $ARGS{"level"}B= grep {$ARGS{"level"} eq $_;} $App::CELL::Status::log->permitted_levels
ABresult
0X0
100
111
134 1 $ARGS{level} = 'ERR';
135 }
136  
137 # if caller array not given, create it
138 206 ▶ if ( not $ARGS{caller} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ARGS{"caller"}) 124 82 206
139 124 $ARGS{caller} = [ CORE::caller() ];
140 }
141  
142 206 ▶ $ARGS{args} = [] if not defined( $ARGS{args} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ARGS{"args"} 172 34 206
143 206 $ARGS{called_from_status} = 1;
144  
145 206 ▶ if ( $ARGS{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{"code"}) 69 137 206
146 # App::CELL::Message->new returns a status object
147 69 my $status = $class->SUPER::new( %ARGS );
148 69 ▶ if ( $status->ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->ok) { } 69 0 69
149 69 my $parent = $status->payload;
150 69 $ARGS{msgobj} = $parent;
151 69 $ARGS{code} = $parent->code;
152 69 $ARGS{text} = $parent->text;
153 } else {
154 0 $ARGS{code} = $status->code;
155 0 ▶ if ( $ARGS{args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{'args'}) { } 0 0 0
156 0 $ARGS{text} = $status->text . stringify_args( $ARGS{args} );
157 } else {
158 0 $ARGS{text} = $status->text;
159 }
160 }
161 }
162  
163 # bless into objecthood
164 206 $self = bless \%ARGS, 'App::CELL::Status';
165  
166 # Log the message
167 206 ▶ $log->status_obj( $self, cell => ( $ARGS{cell} || 0 ) ) if ( $ARGS{level} ne 'OK' and $ARGS{level} ne 'NOT_OK' );
Branch: 1 branch
BranchTrueFalseTotal
if $ARGS{"level"} ne "OK" and $ARGS{"level"} ne "NOT_OK" 57 149 206
Condition100%5/5
$ARGS{"level"} ne "OK" and $ARGS{"level"} ne "NOT_OK"
!ll&&!rl&&r
147257
$ARGS{'cell'} || 0
l!l
1641
MC/DC100%2/2
$ARGS{"level"} ne "OK" and $ARGS{"level"} ne "NOT_OK"
$ARGS{"level"} ne "OK" $ARGS{"level"} ne "NOT_OK"
MC/DC100%1/1
$ARGS{'cell'} || 0
$ARGS{'cell'}
Truth table100%3/3
A and B
A= $ARGS{"level"} ne "OK"B= $ARGS{"level"} ne "NOT_OK"
ABresult
0X0
100
111
Truth table100%2/2
A
A= $ARGS{'cell'}
Aresult
11
00
168  
169 # return the created object
170 206 return $self;
171 }
172  
173  
174 =head2 dump
175  
176 Dump an existing status object. Takes: PARAMHASH. Parameter 'to' determines
177 destination, which can be 'string' (default), 'log' or 'fd'.
178  
179 # dump object to string
180 my $dump_str = $status->dump();
181 $dump_str = $status->dump( to => 'string' );
182  
183 # dump object to log
184 $status->dump( to => 'log' );
185  
186 # dump object to file descriptor
187 $status->dump( fd => STDOUT );
188 $status->dump( to => 'fd', fd => \*STDOUT );
189  
190 Always returns a true value.
191  
192 =cut
193  
194 6 ▶sub dump {
Subroutine dump: called CC=11 SCAR=28.3
Pod documented
195 6 my $self = shift;
196 6 my ( %ARGS ) = validate( @_, { 'to' => 0, 'fd' => 0 } );
197 6 my ( $action, $fh );
198 6 ▶ if ( not %ARGS ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not %ARGS) { } 2 4 6
elsif (exists $ARGS{'to'}) { } 2 2 4
elsif (exists $ARGS{'fd'}) { } 2 0 2
199 2 $action = 'string';
200 } elsif ( exists $ARGS{'to'} ) {
201 2 ▶ if ( $ARGS{'to'} eq 'string' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ARGS{'to'} eq 'string') { } 0 2 2
elsif ($ARGS{'to'} eq 'log') { } 2 0 2
elsif ($ARGS{'to'} eq 'fd' and exists $ARGS{'fd'}) { } 0 0 0
202 0 $action = 'string';
203 } elsif ( $ARGS{'to'} eq 'log' ) {
204 2 $action = 'log';
205 ▶ } elsif ( $ARGS{'to'} eq 'fd' and exists $ARGS{'fd'} ) {
Condition0%0/3
$ARGS{"to"} eq "fd" and exists $ARGS{"fd"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ARGS{"to"} eq "fd" and exists $ARGS{"fd"}
$ARGS{"to"} eq "fd" exists $ARGS{"fd"}
Truth table0%0/3
A and B
A= $ARGS{"to"} eq "fd"B= exists $ARGS{"fd"}
ABresult
0X0
100
111
206 0 $action = 'fd';
207 0 $fh = $ARGS{'fd'};
208 } else {
209 0 die "App::CELL->Status->dump() doing nothing (bad arguments)";
210 }
211 } elsif ( exists $ARGS{'fd'} ) {
212 2 $action = 'fd';
213 2 $fh = $ARGS{'fd'};
214 } else {
215 0 die "App::CELL->Status->dump() doing nothing (bad arguments)";
216 }
217 6 ▶ if ( $action eq "string" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'string') { } 2 4 6
elsif ($action eq 'log') { } 2 2 4
elsif ($action eq 'fd') { } 2 0 2
218 2 return _prep_dump_string(
219 level => $self->level,
220 code => $self->code,
221 text => $self->text,
222 );
223 } elsif ( $action eq "log" ) {
224 2 $log->status_obj( $self );
225 } elsif ( $action eq "fd" ) {
226 2 print $fh _prep_dump_string(
227 level => $self->level,
228 code => $self->code,
229 text => $self->text,
230 ), "\n";
231 } else {
232 0 die "App::CELL->Status->dump() doing nothing (bad things happening)";
233 }
234  
235 4 return 1;
236 }
237  
238  
239 4 ▶sub _prep_dump_string {
Subroutine _prep_dump_string: called CC=3 SCAR=11.6
240 4 my %ARGS = validate( @_, {
241 'level' => 1,
242 'code' => 0,
243 'text' => 1,
244 } );
245  
246 4 my $prepped_string = "$ARGS{'level'}: ";
247 4 ▶ if ( $ARGS{'code'} and $ARGS{'code'} ne $ARGS{'text'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{"code"} and $ARGS{"code"} ne $ARGS{"text"}) 4 0 4
Condition33%1/3
$ARGS{"code"} and $ARGS{"code"} ne $ARGS{"text"}
!ll&&!rl&&r
004
MC/DC0%0/2
$ARGS{"code"} and $ARGS{"code"} ne $ARGS{"text"}
$ARGS{"code"} $ARGS{"code"} ne $ARGS{"text"}
Truth table33%1/3
A and B
A= $ARGS{"code"}B= $ARGS{"code"} ne $ARGS{"text"}
ABresult
0X0
100
111
248 4 $prepped_string .= "($ARGS{'code'}) ";
249 }
250 4 $prepped_string .= "$ARGS{'text'}";
251  
252 4 return $prepped_string;
253 }
254  
255  
256 =head2 ok
257  
258 If the first argument is blessed, assume we're being called as an
259 instance method: return true if status is OK, false otherwise.
260  
261 Otherwise, assume we're being called as a class method: return a
262 new OK status object with optional payload (optional parameter to the
263 method call, must be a scalar).
264  
265 =cut
266  
267 152 ▶sub ok {
Subroutine ok: called CC=4 SCAR=13.9
Pod documented
268  
269 152 my ( $self, $payload ) = @_;
270 152 my $ARGS = {};
271  
272 152 ▶ if ( blessed $self )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 116 36 152
273 { # instance method
274 116 ▶ return 1 if ( $self->level eq 'OK' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->level eq "OK" 93 23 116
275 23 return 0;
276  
277 }
278 36 $ARGS->{level} = 'OK';
279 36 ▶ $ARGS->{payload} = $payload if $payload;
Branch: 1 branch
BranchTrueFalseTotal
if $payload 7 29 36
280 36 $ARGS->{caller} = [ CORE::caller() ];
281 36 return bless $ARGS, __PACKAGE__;
282 }
283  
284  
285 =head2 not_ok
286  
287 Similar method to 'ok', except it handles 'NOT_OK' status.
288  
289 When called as an instance method, returns a true value if the status level
290 is anything other than 'OK'. Otherwise false.
291  
292 When called as a class method, returns a 'NOT_OK' status object.
293 Optionally, a payload can be supplied as an argument.
294  
295 =cut
296  
297 40 ▶sub not_ok {
Subroutine not_ok: called CC=4 SCAR=13.9
Pod documented
298  
299 40 my ( $self, $payload ) = @_;
300 40 my $ARGS = {};
301  
302 40 ▶ if ( blessed $self )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 38 2 40
303 { # instance method
304 38 ▶ return 1 if $self->{level} ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"level"} ne "OK" 14 24 38
305 24 return 0;
306 }
307 2 $ARGS->{level} = 'NOT_OK';
308 2 ▶ $ARGS->{payload} = $payload if $payload;
Branch: 1 branch
BranchTrueFalseTotal
if $payload 1 1 2
309 2 $ARGS->{caller} = [ CORE::caller() ];
310 2 return bless $ARGS, __PACKAGE__;
311 }
312  
313  
314 =head2 level
315  
316 Accessor method, returns level of status object in ALL-CAPS. All status
317 objects must have a level attribute.
318  
319 =cut
320  
321 219 ▶sub level {
Subroutine level: called CC=2 SCAR=6.9
Pod documented
322 219 my $self = shift;
323 219 ▶ $self->{'level'} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 218 219
324 219 return $self->{'level'};
325 }
326  
327  
328 =head2 code
329  
330 Accesor method, returns code of status object, or "C<< <NONE> >>" if none
331 present.
332  
333 =cut
334  
335 83 ▶sub code {
Subroutine code: called CC=3 SCAR=11.0
Pod documented
336 83 my $self = shift;
337 83 ▶ $self->{'code'} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 82 83
338 83 ▶ return $self->{'code'} || "<NONE>";
Condition100%2/2
$self->{'code'} || '<NONE>'
l!l
6716
MC/DC100%1/1
$self->{'code'} || '<NONE>'
$self->{'code'}
Truth table100%2/2
A
A= $self->{'code'}
Aresult
11
00
339 }
340
341  
342 =head2 args
343  
344 Accessor method - returns value of the 'args' property.
345  
346 =cut
347  
348 5 ▶sub args {
Subroutine args: called CC=2 SCAR=6.9
Pod documented
349 5 my $self = shift;
350 5 ▶ $self->{'args'} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 4 5
351 5 return $self->{'args'};
352 }
353  
354  
355 =head2 text
356  
357 Accessor method, returns text of status object, or the code if no text
358 present. If neither code nor text are present, returns "C<< <NONE> >>"
359  
360 =cut
361  
362 68 ▶sub text {
Subroutine text: called CC=2 SCAR=6.9
Pod documented
363 68 ▶ return $_[0]->{text} if $_[0]->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"text"} 62 6 68
364 6 return $_[0]->code;
365 }
366  
367  
368 =head2 caller
369  
370 Accessor method. Returns array reference containing output of C<caller>
371 function associated with this status object, or C<[]> if not present.
372  
373 =cut
374  
375 63 ▶sub caller { return $_[0]->{caller} || []; }
Subroutine caller: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$_[0]{'caller'} || []
l!l
630
MC/DC0%0/1
$_[0]{'caller'} || []
$_[0]{'caller'}
Truth table50%1/2
A
A= $_[0]{'caller'}
Aresult
11
00
376  
377  
378 =head2 payload
379  
380 When called with no arguments, acts like an accessor method.
381 When called with a scalar argument, either adds that as the payload or
382 changes the payload to that.
383  
384 Logs a warning if an existing payload is changed.
385  
386 Returns the (new) payload or undef.
387  
388 =cut
389  
390 139 ▶sub payload {
Subroutine payload: called CC=3 SCAR=15.1
Pod documented
391 139 my ( $self, $new_payload ) = @_;
392 139 ▶ if ( defined $new_payload ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_payload) 0 139 139
393 $log->warn( "Changing payload of status object. Old payload was " .
394 0 ▶ "->$self->{payload}<-", cell => 1 ) if $self->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"payload"} 0 0 0
395 0 $self->{payload} = $new_payload;
396 }
397 139 return $self->{payload};
398 }
399  
400  
401 =head2 msgobj
402  
403 Accessor method (returns the parent message object)
404  
405 =cut
406  
407 2 ▶sub msgobj {
Subroutine msgobj: called CC=2 SCAR=8.1
Pod documented
408 2 my $self = $_[0];
409  
410 2 ▶ return $self->{msgobj} if exists $self->{msgobj};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"msgobj"} 2 0 2
411 0 return; # returns undef in scalar context
412 }
413  
414  
415 =head2 expurgate
416  
417 Make a deep copy of the status object, unbless it, and remove certain
418 attributes deemed "extraneous".
419  
420 =cut
421  
422 1 ▶sub expurgate {
Subroutine expurgate: called CC=5 SCAR=16.3
Pod documented
423 1 my ( $self ) = @_;
424 1 ▶ return unless blessed( $self );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 1 1
425  
426 1 my ( $clone, $status );
427 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
428 1 $clone = dclone( $self );
429 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
430 0 $status = __PACKAGE__->new(
431 level => 'CRIT',
432 code => $_,
433 );
434 1 };
435 1 ▶ return $status->expurgate if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 1 1
436 1 my $udc;
437
438 1 foreach my $key ( keys %$clone ) {
439 7 ▶ next if grep { $key eq $_ } ( 'args', 'called_from_status', 'caller', 'msgobj' );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$key eq $_;} "args", "called_from_status", "caller", "msgobj" 4 3 7
440 3 $udc->{$key} = $clone->{$key};
441 }
442
443 1 return $udc;
444 }
445  
446  
447 18 1;
« blib/lib/App/CELL/Message.pm ↑ Summary blib/lib/App/CELL/Util.pm »