blib/lib/oEdtk/logger2.pm

filter: Statementstmt 64.4% 69 / 107 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 79.1% 19 / 24 Podpod 20.0% 1 / 5 totaltotal 44.2% 89 / 201 CC 23 SCAR 52.2
CC
23
Cov
44%
logger
420.0
logger_help
12.0
logger_rc_list
6.0
CRAP 184.2
« blib/lib/oEdtk/Outmngr.pm ↑ Summary blib/lib/oEdtk/libXls.pm »
1 package oEdtk::logger; ## no critic (Modules::RequireFilenameMatchesPackage)
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); #backward compatibility for Perls under 5.6
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = 2.1129;
10 1 @ISA = qw(Exporter);
11 1 @EXPORT =
12 qw(logger logger_help logger_rc_list *LOGGER_OUT set_warn_to_logger set_logger_level _LEVEL_ _HELP_ _FAIL_ _SUCCESS_ _EXEC_ _WARN_ _INFO_ _FOLLW_ _CONTXT_ _DEBUG_);
13 1 @EXPORT_OK = qw($VERSION $LOGGERLEVEL $WARN2LOGGER);
14  
15 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #reset $! to a "clean" or "neutral" value that depends on the platform
20 1 ▶ use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 STDERR->clearerr(); # Reset $! for the STDERR handler
22 }
23  
24 #usage:
25 #use logger qw(:DEFAULT);
26 # logger (_LEVEL_, "message");
27  
28 {
29 1 our $LOGGERLEVEL = 1;
30 1 our $WARN2LOGGER = 0;
31 1 *LOGGER_OUT = *STDERR; # why messages don't bubble up into tracking?
32  
33 1 ▶ use constant _LEVEL_ => -2; # in case of misuse
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶ use constant _HELP_ => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶ use constant _FAIL_ => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶ use constant _SUCCESS_ => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶ use constant _EXEC_ => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶ use constant _WARN_ => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶ use constant _INFO_ => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶ use constant _FOLLW_ => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶ use constant _DEBUG_ => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶ use constant _CONTXT_ => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 my %hLETTER_LEVEL = (
45 H => _HELP_, # -2
46 F => _FAIL_, # -1
47 E => _FAIL_, # -1
48 S => _SUCCESS_, # 0
49 X => _EXEC_, # 1
50 W => _WARN_, # 4
51 A => _WARN_, # 4
52 I => _INFO_, # 5
53 P => _INFO_, # 5
54 L => _FOLLW_, # 6
55 C => _CONTXT_, # 8
56 D => _DEBUG_, # 7
57 U => _CONTXT_, # 8
58 );
59  
60 1 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 my $PROG = basename( $0, "" );
62  
63 1 my @label_LEVEL = (
64 'SUCCESS', 'EXEC', 'ND_2', 'ND_3', 'WARN', 'INFO',
65 'FOLLOW', 'CONTEXT', 'DEBUG', 'ND', 'ND'
66 ); #keep lasts ND
67 1 $label_LEVEL[_FAIL_] = 'FAILED';
68 1 $label_LEVEL[_HELP_] = 'HELP';
69  
70 0 ▶ sub set_logger_level {
Subroutine set_logger_level: not called CC=1 SCAR=6.9
Pod undocumented
71 0 $LOGGERLEVEL = shift;
72 0 1;
73 }
74  
75 0 ▶ sub set_warn_to_logger() {
Subroutine set_warn_to_logger: not called CC=1 SCAR=6.9
Pod undocumented
76 0 $WARN2LOGGER = 1;
77 0 1;
78 }
79  
80 0 ▶ sub logger {
Subroutine logger: not called CC=20 SCAR=60.4
Pod documented
81 0 my ( $messLevel, $logMess, $modifier, @rcLogger ) = @_;
82  
83 # Normalization: letter -> integer level
84 0 ▶ if ( defined $messLevel && $messLevel =~ /^([A-Z])$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $messLevel and $messLevel =~ /^([A-Z])$/i) 0 0 0
Condition0%0/3
defined $messLevel and $messLevel =~ /^([A-Z])$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $messLevel and $messLevel =~ /^([A-Z])$/i
defined $messLevel $messLevel =~ /^([A-Z])$/i
Truth table0%0/3
A and B
A= defined $messLevelB= $messLevel =~ /^([A-Z])$/i
ABresult
0X0
100
111
85 0 my $key = uc($messLevel);
86 0 ▶ if ( exists $hLETTER_LEVEL{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hLETTER_LEVEL{$key}) { } 0 0 0
87 0 $messLevel = $hLETTER_LEVEL{$key};
88 }
89 else {
90 0 $messLevel = _LEVEL_; # misuse level
91 0 logger_help(0);
92 }
93 }
94  
95 0 ▶ if ( defined $modifier && $modifier =~ /^([A-Z])$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $modifier and $modifier =~ /^([A-Z])$/i) 0 0 0
Condition0%0/3
defined $modifier and $modifier =~ /^([A-Z])$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $modifier and $modifier =~ /^([A-Z])$/i
defined $modifier $modifier =~ /^([A-Z])$/i
Truth table0%0/3
A and B
A= defined $modifierB= $modifier =~ /^([A-Z])$/i
ABresult
0X0
100
111
96 0 ▶ $modifier = $hLETTER_LEVEL{ uc($modifier) } // $LOGGERLEVEL;
Condition0%0/3
$hLETTER_LEVEL{uc $modifier} // $oEdtk::logger::LOGGERLEVEL
l!l&&r!l&&!r
000
MC/DC0%0/2
$hLETTER_LEVEL{uc $modifier} // $oEdtk::logger::LOGGERLEVEL
$hLETTER_LEVEL{uc $modifier} $oEdtk::logger::LOGGERLEVEL
Truth table0%0/3
A // B
A= $hLETTER_LEVEL{uc $modifier}B= $oEdtk::logger::LOGGERLEVEL
ABresult
1X1
011
000
97 }
98 0 ▶ $LOGGERLEVEL = ( $modifier || $LOGGERLEVEL );
Condition0%0/3
$modifier || $oEdtk::logger::LOGGERLEVEL
l!l&&r!l&&!r
000
MC/DC0%0/2
$modifier || $oEdtk::logger::LOGGERLEVEL
$modifier $oEdtk::logger::LOGGERLEVEL
Truth table0%0/3
A || B
A= $modifierB= $oEdtk::logger::LOGGERLEVEL
ABresult
1X1
011
000
99  
100 # IF THE MESSAGE IS OUTSIDE THE DEFINED LIMITS, WE DON'T WASTE TIME, WE EXIT
101 0 ▶ if ( $messLevel > $LOGGERLEVEL && $messLevel ne _FAIL_ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($messLevel > $oEdtk::logger::LOGGERLEVEL and $messLevel ne -1) { } 0 0 0
elsif ($messLevel == -1) { } 0 0 0
elsif ($messLevel == -2) { } 0 0 0
Condition0%0/3
$messLevel > $oEdtk::logger::LOGGERLEVEL and $messLevel ne -1
!ll&&!rl&&r
000
MC/DC0%0/2
$messLevel > $oEdtk::logger::LOGGERLEVEL and $messLevel ne -1
$messLevel > $oEdtk::logger::LOGGERLEVEL $messLevel ne -1
Truth table0%0/3
A and B
A= $messLevel > $oEdtk::logger::LOGGERLEVELB= $messLevel ne -1
ABresult
0X0
100
111
102 0 return $messLevel;
103  
104 # IF THE MESSAGE IS A FAIL AND THERE IS NO STANDARD RETURN CODE, WE SET
105 # THE CODE 41 UNKNOWN ERROR 41
106 }
107 elsif ( $messLevel == _FAIL_ ) {
108 0 ▶ if ( !($!) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($!) 0 0 0
109 0 $! = 41;
110 }
111 0 $rcLogger[0] = sprintf( "%0.3d", $! );
112  
113 # FLUSH TO SYNCHRONIZE MESSAGES
114 0 $| = 1;
115  
116 }
117 elsif ( $messLevel == _HELP_ ) {
118 0 &logger_help();
119 0 exit _SUCCESS_;
120  
121 }
122 else {
123 0 $rcLogger[0] = "000";
124 }
125  
126 0 $rcLogger[1] = sprintf( "\>%s|", ( strftime "%Y-%m-%d %H:%M:%S", localtime ) );
127 0 ▶ $rcLogger[2] = sprintf(
Condition0%0/4
$label_LEVEL[$messLevel] || 'ND'
l!l
00
$. || 0
l!l
00
MC/DC0%0/1
$label_LEVEL[$messLevel] || 'ND'
$label_LEVEL[$messLevel]
MC/DC0%0/1
$. || 0
$.
Truth table0%0/2
A
A= $label_LEVEL[$messLevel]
Aresult
11
00
Truth table0%0/2
A
A= $.
Aresult
11
00
128 " %-7s|FH-%07d|PID-%s|last RC: %0.3d|",
129 $label_LEVEL[$messLevel] || 'ND',
130 ( $. || 0 ),
131 $$, $!
132 );
133 0 $rcLogger[3] = sprintf( " %s|last RC: %0.3d-%s", $logMess, $!, $! );
134  
135 0 ▶ if ( $messLevel <= $LOGGERLEVEL && $messLevel > _FAIL_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messLevel <= $oEdtk::logger::LOGGERLEVEL and $messLevel > -1) 0 0 0
Condition0%0/3
$messLevel <= $oEdtk::logger::LOGGERLEVEL and $messLevel > -1
!ll&&!rl&&r
000
MC/DC0%0/2
$messLevel <= $oEdtk::logger::LOGGERLEVEL and $messLevel > -1
$messLevel <= $oEdtk::logger::LOGGERLEVEL $messLevel > -1
Truth table0%0/3
A and B
A= $messLevel <= $oEdtk::logger::LOGGERLEVELB= $messLevel > -1
ABresult
0X0
100
111
136 0 print LOGGER_OUT join( '', @rcLogger ) . "\n";
137  
138 # } elsif ($messLevel == _FAIL_) {
139 # print $rcLogger[0] . $rcLogger[1] ."\n";
140 }
141  
142 0 ▶ if ( $messLevel == _FAIL_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messLevel == -1) 0 0 0
143  
144 # For die: we return a single string that contains everything
145 0 ▶ return wantarray ? @rcLogger : join( '', @rcLogger );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
146 }
147  
148 0 ▶ return wantarray ? @rcLogger : $rcLogger[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
149 }
150  
151 0 ▶ sub logger_help {
Subroutine logger_help: not called CC=3 SCAR=24.8
Pod undocumented
152 0 ▶ my $call_rc_list = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
153 0 print STDOUT <<"EOF";
154 HELP FOR LOGGER USE, AND RETURN CODES LIST.
155  
156 Examples :
157 &logger(_HELP_);
158 &logger(xxxxxx, "message" [, _DEBUG_]);
159 &logger(xxxxxx, "message" [, n]);
160  
161 Example for logging a fatal error :
162 open (IN, "<\$filename") or die &logger(_FAIL_, "can't open <\$filename>");
163  
164 \$LOGGERLEVEL defines the sensitivity level for messages
165 \$LOGGERLEVEL possible values :
166 _FAIL_ => level -1, fatal error, logger will look for the system error to report
167 _SUCCESS_ => level 0, treatment successfull
168 _EXEC_ => level 1, information message for evaluation
169 _WARN_ => level 4, WARN message
170 _INFO_ => level 5, message for significant information
171 _FOLLW_ => level 6, message for tracked information
172 _CONTXT_ => level 7, message for contextual information
173 _DEBUG_ => level 8, full debug level, general message for debug
174 _HELP_ => This message for information
175  
176 Messages are filtered depend on \$LOGGERLEVEL preset.
177 Third parameter is used to change \$LOGGERLEVEL value.
178 \$LOGGERLEVEL is currently set to $LOGGERLEVEL.
179  
180 EOF
181  
182 0 ▶ logger_rc_list() if ($call_rc_list);
Branch: 1 branch
BranchTrueFalseTotal
if $call_rc_list 0 0 0
183 }
184  
185 0 ▶ sub logger_rc_list() {
Subroutine logger_rc_list: not called CC=2 SCAR=17.9
Pod undocumented
186 0 print "LIST OF APPLICATION RETURN CODES :\n";
187 0 printf( "%0.3d: %s, %s\n", _SUCCESS_, $label_LEVEL[_SUCCESS_], "$PROG success" );
188 0 ▶ for ( $! = 1 ; $! <= 133 ; $!++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! <= 133) 0 0 0
189  
190 # SYSTEM ERRORS
191 #$. is the current line number on the current filehandle
192 #$! current errno value
193 #$@ last error captured by eval
194 #$0 perl script name
195 0 printf( "%0.3d: %s\n", $!, $! );
196 }
197 }
198  
199 }
200 1 1;
« blib/lib/oEdtk/Outmngr.pm ↑ Summary blib/lib/oEdtk/libXls.pm »