blib/lib/App/CELL/Log.pm

filter: Statementstmt 85.2% 98 / 115 Branchbran 66.1% 41 / 62 Conditioncond 87.5% 21 / 24 MC/DCmcdc 78.5% 11 / 14 Subroutinesub 83.3% 15 / 18 Podpod 100% 6 / 6 totaltotal 80.3% 192 / 239 CC 40 SCAR 23.3
CC
40
Cov
80%
AUTOLOAD
18.2
init
13.4
_sanitize_level
12.0
CRAP 10.3
« blib/lib/App/CELL/Load.pm ↑ Summary blib/lib/App/CELL/Message.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::Log;
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 # IMPORTANT: this module must not depend on any other CELL modules
40 # except possibly App::CELL::Util
41 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 18 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 18 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46  
47  
48 =head1 NAME
49  
50 App::CELL::Log - the Logging part of CELL
51  
52  
53  
54 =head1 SYNOPSIS
55  
56 use App::CELL::Log qw( $log );
57  
58 # set up logging for application FooBar -- need only be done once
59 $log->init( ident => 'FooBar' );
60  
61 # do not suppess 'trace' and 'debug' messages
62 $log->init( debug_mode => 1 );
63  
64 # do not append filename and line number of caller
65 $log->init( show_caller => 0 );
66  
67 # log messages at different log levels
68 my $level = 'warn' # can be any of the levels provided by Log::Any
69 $log->$level ( "Foobar log message" );
70  
71 # the following App::CELL-specific levels are supported as well
72 $log->ok ( "Info-level message prefixed with 'OK: '");
73 $log->not_ok ( "Info-level message prefixed with 'NOT_OK: '");
74  
75 # by default, the caller's filename and line number are appended
76 # to suppress this for an individual log message:
77 $log->debug ( "Debug-level message", suppress_caller => 1 );
78  
79 # Log a status object (happens automatically when object is
80 # constructed)
81 $log->status_obj( $status_obj );
82  
83 # Log a message object
84 $log->message_obj( $message_obj );
85  
86  
87  
88 =head1 EXPORTS
89  
90 This module provides the following exports:
91  
92 =over
93  
94 =item C<$log> - App::CELL::Log singleton
95  
96 =back
97  
98 =cut
99  
100 18 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
101 18 our @EXPORT_OK = qw( $log );
102  
103  
104  
105 =head1 PACKAGE VARIABLES
106  
107 =over
108  
109 =item C<$ident> - the name of our application
110  
111 =item C<$show_caller> - boolean value, determines if caller information is
112 displayed in log messages
113  
114 =item C<$debug_mode> - boolean value, determines if we display debug
115 messages
116  
117 =item C<$log> - App::CELL::Log singleton object
118  
119 =item C<$log_any_obj> - Log::Any singleton object
120  
121 =item C<@permitted_levels> - list of permissible log levels
122  
123 =back
124  
125 =cut
126  
127 18 our $debug_mode = 0;
128 18 our $ident = 'CELLtest';
129 18 our $show_caller = 1;
130 18 our $log = bless {}, __PACKAGE__;
131 18 our $log_any_obj;
132 18 our @permitted_levels = qw( OK NOT_OK TRACE DEBUG INFO INFORM NOTICE
133 WARN WARNING ERR ERROR CRIT CRITICAL FATAL EMERGENCY );
134 18 our $AUTOLOAD;
135  
136  
137  
138 =head1 DESCRIPTION
139  
140 App::CELL's logs using L<Log::Any>. This C<App::CELL::Log> module exists
141 to: (1) provide documentation, (2) store the logging category (C<$ident>),
142 (3) store the L<Log::Any> log object, (4) provide convenience functions for
143 logging 'OK' and 'NOT_OK' statuses.
144  
145  
146  
147 =head1 METHODS
148  
149  
150 =head2 debug_mode
151  
152 If argument provided, set the $debug_mode package variable.
153 If no argument, simply return the current debug-mode setting.
154 Examples:
155  
156 $log->debug_mode(0); # turn debug mode off
157 $log->debug_mode(1); # turn debug mode on
158 print "Debug mode is on\n" if $log->debug_mode;
159  
160 =cut
161  
162 2 ▶sub debug_mode {
Subroutine debug_mode: called CC=2 SCAR=8.1
Pod documented
163 2 my ( $self, @ARGS ) = @_;
164 2 ▶ return $debug_mode = $ARGS[0] if @ARGS;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGS 2 0 2
165 0 return $debug_mode;
166 }
167  
168  
169 =head2 ident
170  
171 Set the $ident package variable and the Log::Any category
172  
173 =cut
174  
175 0 ▶sub ident {
Subroutine ident: not called CC=1 SCAR=6.9
Pod documented
176 0 my $self = shift;
177 0 $ident = shift;
178 0 return $log_any_obj = Log::Any->get_logger(category => $ident);
179 }
180  
181  
182 =head2 show_caller
183  
184 Set the $show_caller package variable
185  
186 =cut
187  
188 2 ▶sub show_caller { return $show_caller = $_[1]; }
Subroutine show_caller: called CC=1 SCAR=0.0
Pod documented
189  
190  
191 =head2 permitted_levels
192  
193 Access the C<@permitted_levels> package variable.
194  
195 =cut
196  
197 206 ▶sub permitted_levels { return @permitted_levels };
Subroutine permitted_levels: called CC=1 SCAR=0.0
Pod documented
198  
199  
200 =head2 init
201  
202 Initializes (or reconfigures) the logger. Although in most cases folks will
203 want to call this in order to set C<ident>, it is not required for logging
204 to work. See L<App::CELL::Guide> for instructions on how to log with
205 L<App::CELL>.
206  
207 Takes PARAMHASH as argument. Recognized parameters:
208  
209 =over
210  
211 =item C<ident> -- (i.e., category) string, e.g. 'FooBar' for
212 the FooBar application, or 'CELLtest' if none given
213  
214 =item C<show_caller> -- sets the C<$show_caller> package variable (see
215 above)
216  
217 =item C<debug_mode> -- sets the C<$debug_mode> package variable (see above)
218  
219 =back
220  
221 Always returns 1.
222  
223 =cut
224  
225 18 ▶sub init {
Subroutine init: called CC=10 SCAR=25.9
Pod documented
226 18 my ( $self, %ARGS ) = @_;
227  
228 # process 'ident'
229 18 ▶ if ( defined( $ARGS{ident} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ARGS{'ident'}) { } 15 3 18
230 15 ▶ if ( $ARGS{ident} eq $ident and $ident ne 'CELLtest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{'ident'} eq $App::CELL::Log::ident and $App::CELL::Log::ident ne 'CELLtest') { } 0 15 15
Condition33%1/3
$ARGS{"ident"} eq $App::CELL::Log::ident and $App::CELL::Log::ident ne "CELLtest"
!ll&&!rl&&r
0150
MC/DC0%0/2
$ARGS{"ident"} eq $App::CELL::Log::ident and $App::CELL::Log::ident ne "CELLtest"
$ARGS{"ident"} eq $App::CELL::Log::ident $App::CELL::Log::ident ne "CELLtest"
Truth table33%1/3
A and B
A= $ARGS{"ident"} eq $App::CELL::Log::identB= $App::CELL::Log::ident ne "CELLtest"
ABresult
0X0
100
111
231 0 $log->info( "Logging already configured", cell => 1 );
232 } else {
233 15 $ident = $ARGS{ident};
234 15 $log_any_obj = Log::Any->get_logger(category => $ident);
235 }
236 } else {
237 3 $ident = 'CELLtest';
238 3 $log_any_obj = Log::Any->get_logger(category => $ident);
239 }
240  
241 # process 'debug_mode' argument
242 18 ▶ if ( exists( $ARGS{debug_mode} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ARGS{"debug_mode"}) 8 10 18
243 8 ▶ $debug_mode = 1 if $ARGS{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $ARGS{"debug_mode"} 7 1 8
244 8 ▶ $debug_mode = 0 if not $ARGS{debug_mode};
Branch: 1 branch
BranchTrueFalseTotal
unless $ARGS{"debug_mode"} 1 7 8
245 }
246 #$log->info( "debug_mode is $debug_mode", cell => 1 );
247
248 # process 'show_caller'
249 18 ▶ if ( exists( $ARGS{show_caller} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ARGS{"show_caller"}) 0 18 18
250 0 ▶ $show_caller = 1 if $ARGS{show_caller};
Branch: 1 branch
BranchTrueFalseTotal
if $ARGS{"show_caller"} 0 0 0
251 0 ▶ $show_caller = 0 if not $ARGS{show_caller};
Branch: 1 branch
BranchTrueFalseTotal
unless $ARGS{"show_caller"} 0 0 0
252 }
253  
254 18 return 1;
255 }
256  
257  
258 =head2 DESTROY
259  
260 For some reason, Perl 5.012 seems to want a DESTROY method
261  
262 =cut
263  
264 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
265 0 my $self = shift;
266 0 ▶ $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::DESTROY") 0 0 0
267 }
268  
269  
270 =head2 AUTOLOAD
271  
272 Call Log::Any methods after some pre-processing
273  
274 =cut
275  
276 956 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=18 SCAR=29.0
277
278 956 my ( $class, $msg_text, @ARGS ) = @_;
279 956 my $method = $AUTOLOAD;
280 956 $method =~ s/.*:://;
281  
282 # if method is not in permitted_levels, pass through to Log::Any
283 # directly
284 956 ▶ if ( not grep { $_ =~ m/$method/i } @permitted_levels ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ =~ /$method/iu;} @App::CELL::Log::permitted_levels) 19 937 956
285 19 return $log_any_obj->$method( $msg_text, @ARGS );
286 }
287  
288 # we are logging a message
289 937 my %ARGS;
290 937 ▶ %ARGS = @ARGS if @ARGS % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGS % 2 == 0 937 0 937
291 937 my ( $file, $line );
292 937 my ( $level, $text );
293 937 my $method_uc = uc $method;
294 937 ▶ if ( $method_uc eq 'OK' or $method_uc eq 'NOT_OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_uc eq 'OK' or $method_uc eq 'NOT_OK') { } 2 935 937
Condition100%3/3
$method_uc eq "OK" or $method_uc eq "NOT_OK"
l!l&&r!l&&!r
11935
MC/DC100%2/2
$method_uc eq "OK" or $method_uc eq "NOT_OK"
$method_uc eq "OK" $method_uc eq "NOT_OK"
Truth table100%3/3
A or B
A= $method_uc eq "OK"B= $method_uc eq "NOT_OK"
ABresult
1X1
011
000
295 2 $level = $method_uc;
296 2 $method_uc = 'INFO';
297 2 $method = 'info';
298 } else {
299 935 $level = $method_uc;
300 }
301 937 my $method_lc = lc $method;
302  
303 # determine what caller info will be displayed, if any
304 937 ▶ if ( %ARGS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%ARGS) { } 824 113 937
305 824 ▶ if ( $ARGS{caller} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ARGS{'caller'}) { } 59 765 824
elsif ($ARGS{'suppress_caller'}) { } 108 657 765
306 59 ( undef, $file, $line ) = @{ $ARGS{caller} };
307 } elsif ( $ARGS{suppress_caller} ) {
308 108 ( $file, $line ) = ( '', '' );
309 } else {
310 657 ( undef, $file, $line ) = caller;
311 }
312 } else {
313 113 ( undef, $file, $line ) = caller;
314 }
315  
316 # if this is a CELL internal debug message, continue only if
317 # the CELL_DEBUG_MODE environment variable exists and is true
318 937 ▶ if ( $ARGS{'cell'} and ( $method_lc eq 'debug' or $method_lc eq 'trace') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{"cell"} and $method_lc eq "debug" || $method_lc eq "trace") 723 214 937
Condition100%6/6
$ARGS{"cell"} and $method_lc eq "debug" || $method_lc eq "trace"
!ll&&!rl&&r
11599723
$method_lc eq "debug" or $method_lc eq "trace"
l!l&&r!l&&!r
722199
MC/DC100%3/3
$ARGS{"cell"} and $method_lc eq "debug" || $method_lc eq "trace"
$ARGS{"cell"} $method_lc eq "debug" $method_lc eq "trace"
Truth table100%4/4
A and B or C
A= $ARGS{"cell"}B= $method_lc eq "debug"C= $method_lc eq "trace"
ABCresult
0XX0
1000
11X1
1011
319 723 ▶ return unless $ENV{'CELL_DEBUG_MODE'};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"CELL_DEBUG_MODE"} 723 0 723
320 }
321  
322 214 ▶ $log->init( ident => $ident ) if not $log_any_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::CELL::Log::log_any_obj 0 214 214
323 214 ▶ die "No Log::Any object!" if not $log_any_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::CELL::Log::log_any_obj 0 214 214
324 214 ▶ return if not $debug_mode and ( $method_lc eq 'debug' or $method_lc eq 'trace' );
Branch: 1 branch
BranchTrueFalseTotal
if not $App::CELL::Log::debug_mode and $method_lc eq "debug" || $method_lc eq "trace" 35 179 214
Condition100%6/6
not $App::CELL::Log::debug_mode and $method_lc eq "debug" || $method_lc eq "trace"
!ll&&!rl&&r
6411535
$method_lc eq "debug" or $method_lc eq "trace"
l!l&&r!l&&!r
341115
MC/DC100%3/3
not $App::CELL::Log::debug_mode and $method_lc eq "debug" || $method_lc eq "trace"
not $App::CELL::Log::debug_mode $method_lc eq "debug" $method_lc eq "trace"
Truth table100%4/4
not(A) and B or C
A= not $App::CELL::Log::debug_modeB= $method_lc eq "debug"C= $method_lc eq "trace"
ABCresult
0XX0
1000
11X1
1011
325 179 ▶ if ( not $msg_text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg_text) 0 179 179
326 0 $msg_text = "<NO_TEXT>"
327 }
328 179 $log_any_obj->$method_lc( _assemble_log_message( "$level: $msg_text", $file, $line ) );
329 179 return;
330 }
331  
332  
333 =head2 status_obj
334  
335 Take a status object and log it.
336  
337 =cut
338  
339 59 ▶sub status_obj {
Subroutine status_obj: called CC=6 SCAR=18.0
Pod documented
340 59 my ( $self, $status_obj, $cell ) = @_;
341 59 my ( $level, $code, $text, $caller, %ARGS );
342 59 $level = $status_obj->level;
343 59 $code = $status_obj->code;
344 59 $text = $status_obj->text;
345 59 $caller = $status_obj->caller;
346 59 ▶ $ARGS{caller} = $caller if $caller;
Branch: 1 branch
BranchTrueFalseTotal
if $caller 59 0 59
347 59 ▶ $ARGS{cell} = $cell if $cell;
Branch: 1 branch
BranchTrueFalseTotal
if $cell 57 2 59
348 59 ▶ if ( $code ne $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne $text) 38 21 59
349 38 $text = "($code) $text"
350 }
351 59 ▶ $text = "<STATUS OBJECT WITHOUT TEXT OR CODE>" if not $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 59 59
352 #( $level, $text ) = _sanitize_level( $level, $text );
353  
354 59 ▶ $log->init( ident => $ident ) if not $log_any_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::CELL::Log::log_any_obj 0 59 59
355 59 return $log->$level( $text, %ARGS );
356 }
357  
358  
359 #=head2 msg
360 #
361 #Take a message object and log it.
362 #
363 #=cut
364 #
365 #sub msg {
366 # my ( $self, $msgobj, @ARGS ) = @_;
367 # return if not blessed( $msgobj );
368 # $log->init( ident => $ident ) if not $log_any_obj;
369 # my $level = $msgobj->level;
370 # my $text = $msgobj->text;
371 #}
372  
373  
374 0 ▶sub _sanitize_level {
Subroutine _sanitize_level: not called CC=3 SCAR=24.8
375 0 my ( $level, $msg_text ) = @_;
376 0 ▶ if ( $level eq 'OK' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($level eq 'OK') { } 0 0 0
elsif ($level eq 'NOT_OK') { } 0 0 0
377 0 $level = 'INFO';
378 0 $msg_text = "OK: " . $msg_text;
379 } elsif ( $level eq 'NOT_OK' ) {
380 0 $level = 'INFO';
381 0 $msg_text = "NOT_OK: " . $msg_text;
382 }
383 0 return ( lc $level, $msg_text );
384 }
385  
386 179 ▶sub _assemble_log_message {
Subroutine _assemble_log_message: called CC=5 SCAR=16.1
387 179 my ( $message, $file, $line ) = @_;
388  
389 179 ▶ if ( $file and File::Spec->file_name_is_absolute( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and "File::Spec"->file_name_is_absolute($file)) 65 114 179
Condition100%3/3
$file and "File::Spec"->file_name_is_absolute($file)
!ll&&!rl&&r
1210265
MC/DC100%2/2
$file and "File::Spec"->file_name_is_absolute($file)
$file "File::Spec"->file_name_is_absolute($file)
Truth table100%3/3
A and B
A= $fileB= "File::Spec"->file_name_is_absolute($file)
ABresult
0X0
100
111
390 65 ( undef, undef, $file ) = File::Spec->splitpath( $file );
391 }
392  
393 179 ▶ return "$message at $file line $line" if $show_caller and $file;
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Log::show_caller and $file 167 12 179
Condition66%2/3
$App::CELL::Log::show_caller and $file
!ll&&!rl&&r
012167
MC/DC50%1/2
$App::CELL::Log::show_caller and $file
$App::CELL::Log::show_caller $file
Truth table66%2/3
A and B
A= $App::CELL::Log::show_callerB= $file
ABresult
0X0
100
111
394  
395 12 return $message;
396 }
397  
398 18 1;
« blib/lib/App/CELL/Load.pm ↑ Summary blib/lib/App/CELL/Message.pm »