Paranoid/Log.pm

filter: Statementstmt 83.2% 184 / 221 Branchbran 71.5% 63 / 88 Conditioncond 48.7% 20 / 41 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 96.5% 28 / 29 Podpod 100% 4 / 4 totaltotal 74.9% 302 / 403 CC 67 SCAR 28.7
CC
67
Cov
76%
plverbosity
66.7
_loadModule
25.0
startLogger
21.1
CRAP 17.7
« Paranoid/Filesystem.pm ↑ Summary Paranoid/Input.pm »
1 # Paranoid::Log -- Log support for paranoid programs
2 #
3 # $Id: lib/Paranoid/Log.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Log;
33  
34 17 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 17 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 17 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 17 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 17 ▶use Paranoid::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 17 ▶use Paranoid::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 17 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
45  
46 17 @EXPORT = qw(
47 PL_DEBUG PL_INFO PL_NOTICE PL_WARN
48 PL_ERR PL_CRIT PL_ALERT PL_EMERG
49 PL_EQ PL_NE PL_GE PL_LE
50 startLogger stopLogger plog plverbosity
51 );
52 17 @EXPORT_OK = (@EXPORT);
53 17 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
54  
55 17 ▶use constant PL_DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 17 ▶use constant PL_INFO => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 17 ▶use constant PL_NOTICE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 17 ▶use constant PL_WARN => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 17 ▶use constant PL_ERR => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 17 ▶use constant PL_CRIT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 17 ▶use constant PL_ALERT => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 17 ▶use constant PL_EMERG => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 17 ▶use constant PL_EQ => '=';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 17 ▶use constant PL_NE => '!';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 17 ▶use constant PL_GE => '+';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 17 ▶use constant PL_LE => '-';
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 17 ▶use constant PL_LREF => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 17 ▶use constant PL_AREF => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 17 ▶use constant PL_DREF => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 17 our @_scopes = ( PL_EQ, PL_NE, PL_GE, PL_LE );
74 17 our @_levels = (
75 PL_DEBUG, PL_INFO, PL_NOTICE, PL_WARN,
76 PL_ERR, PL_CRIT, PL_ALERT, PL_EMERG,
77 );
78  
79 #####################################################################
80 #
81 # Module code follows
82 #
83 #####################################################################
84  
85 {
86  
87 17 my %loaded = (); # module => loaded (boolean)
88 17 my %msubs = (); # module => log sub ref
89 17 my @dist; # modules to distribute to by log level
90  
91 # This has consists of the name/array key/value pairs. Each associated
92 # array consists of the following entries:
93 # [ $sref, $level, $scope, \%mopts ].
94 17 my %loggers = ();
95  
96 35 ▶ sub _loadModule {
Subroutine _loadModule: called CC=18 SCAR=32.2
97  
98 # Purpose: Loads the requested module if it hasn't been already.
99 # Attempts to first load the module as a name relative to
100 # Paranoid::Log, otherwise by itself.
101 # Returns: True (1) if load was successful,
102 # False (0) if there are any errors
103 # Usage: $rv = _loadModule($module);
104  
105 35 my $module = shift;
106 35 my $mname = $module;
107 35 my ( $sref, $aref, $dref, $rv );
108  
109 35 subPreamble( PDLEVEL2, '$', $module );
110  
111 # Was module already loaded (or a load attempted)?
112 35 ▶ if ( exists $loaded{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loaded{$module}) { } 18 17 35
113  
114 # Yep, so return module status
115 18 $rv = $loaded{$module};
116  
117 } else {
118  
119 # Nope, so let's try to load it.
120 #
121 # Is the module name taint-safe?
122 17 ▶ if ( detaint( $mname, 'filename' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&detaint(\$mname, 'filename')) { } 17 0 17
123  
124 # Yep, so try to load relative to Paranoid::Log
125 17 ▶ $rv =
Branch: 4 branches
BranchTrueFalseTotal
$mname eq 'Stderr' ? : 1 16 17
$mname eq 'Stdout' ? : 0 16 16
$mname eq 'PDebug' ? : 1 15 16
loadModule("Paranoid::Log::$mname", '') && eval "Paranoid::Log::${mname}::init();" && eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;" && eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;" && eval "\$dref = \\&Paranoid::Log::${mname}::delLogger;" ? : 14 1 15
Condition66%8/12
loadModule("Paranoid::Log::$mname", "") and eval "Paranoid::Log::${mname}::init();" and eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;" and eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;" and eval "\$dref = \\&Paranoid::Log::${mname}::delLogger;"
!ll&&!rl&&r
1014
loadModule("Paranoid::Log::$mname", "") and eval "Paranoid::Log::${mname}::init();" and eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;" and eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;"
!ll&&!rl&&r
1014
loadModule("Paranoid::Log::$mname", "") and eval "Paranoid::Log::${mname}::init();" and eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;"
!ll&&!rl&&r
1014
loadModule("Paranoid::Log::$mname", "") and eval "Paranoid::Log::${mname}::init();"
!ll&&!rl&&r
1014
MC/DC20%1/5
loadModule("Paranoid::Log::$mname", "") and eval "Paranoid::Log::${mname}::init();" and eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;" and eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;" and eval "\$dref = \\&Paranoid::Log::${mname}::delLogger;"
loadModule("Paranoid::Log::$mname", "") eval "Paranoid::Log::${mname}::init();" eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;" eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;" eval "\$dref = \\&Paranoid::Log::${mname}::delLogger;"
Truth table33%2/6
A and B and C and D and E
A= loadModule("Paranoid::Log::$mname", "")B= eval "Paranoid::Log::${mname}::init();"C= eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;"D= eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;"E= eval "\$dref = \\&Paranoid::Log::${mname}::delLogger;"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
126 $mname eq 'Stderr' ? 1
127 : $mname eq 'Stdout' ? 1
128 : $mname eq 'PDebug' ? 1
129 : loadModule( "Paranoid::Log::$mname", '' )
130 && eval "Paranoid::Log::${mname}::init();"
131 && eval "\$sref = \\&Paranoid::Log::${mname}::logMsg;"
132 && eval "\$aref = \\&Paranoid::Log::${mname}::addLogger;"
133 && eval "\$dref = \\&Paranoid::Log::${mname}::delLogger;"
134 ? 1
135 : 0;
136  
137 # If that failed, try to load it directly
138 17 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 1 16 17
139 1 ▶ $rv =
Branch: 1 branch
BranchTrueFalseTotal
loadModule($mname, '') && eval "${mname}::init();" && eval "\$sref = \\&${mname}::logMsg;" && eval "\$aref = \\&${mname}::addLogger;" && eval "\$dref = \\&${mname}::delLogger;" ? : 0 1 1
Condition33%4/12
loadModule($mname, "") and eval "${mname}::init();" and eval "\$sref = \\&${mname}::logMsg;" and eval "\$aref = \\&${mname}::addLogger;" and eval "\$dref = \\&${mname}::delLogger;"
!ll&&!rl&&r
100
loadModule($mname, "") and eval "${mname}::init();" and eval "\$sref = \\&${mname}::logMsg;" and eval "\$aref = \\&${mname}::addLogger;"
!ll&&!rl&&r
100
loadModule($mname, "") and eval "${mname}::init();" and eval "\$sref = \\&${mname}::logMsg;"
!ll&&!rl&&r
100
loadModule($mname, "") and eval "${mname}::init();"
!ll&&!rl&&r
100
MC/DC0%0/5
loadModule($mname, "") and eval "${mname}::init();" and eval "\$sref = \\&${mname}::logMsg;" and eval "\$aref = \\&${mname}::addLogger;" and eval "\$dref = \\&${mname}::delLogger;"
loadModule($mname, "") eval "${mname}::init();" eval "\$sref = \\&${mname}::logMsg;" eval "\$aref = \\&${mname}::addLogger;" eval "\$dref = \\&${mname}::delLogger;"
Truth table16%1/6
A and B and C and D and E
A= loadModule($mname, "")B= eval "${mname}::init();"C= eval "\$sref = \\&${mname}::logMsg;"D= eval "\$aref = \\&${mname}::addLogger;"E= eval "\$dref = \\&${mname}::delLogger;"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
140 loadModule( $mname, '' )
141 && eval "${mname}::init();"
142 && eval "\$sref = \\&${mname}::logMsg;"
143 && eval "\$aref = \\&${mname}::addLogger;"
144 && eval "\$dref = \\&${mname}::delLogger;"
145 ? 1
146 : 0;
147 }
148  
149 # Cache & report the results
150 17 $loaded{$module} = $rv;
151 17 $msubs{$module} = [ $sref, $aref, $dref ];
152 17 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 16 1 17
153 16 pdebug( 'successfully loaded log module for %s',
154 PDLEVEL3, $module );
155 } else {
156 1 Paranoid::ERROR =
157 pdebug( 'failed to load log module for %s',
158 PDLEVEL1, $module );
159 }
160  
161 } else {
162  
163 # Module name failed detainting -- report
164 0 Paranoid::ERROR =
165 pdebug( 'failed to detaint module name', PDLEVEL1 );
166 0 $rv = 0;
167 }
168 }
169  
170 35 subPostamble( PDLEVEL2, '$', $rv );
171  
172 35 return $rv;
173 }
174  
175 28 ▶ sub _updateDist {
Subroutine _updateDist: called CC=11 SCAR=24.2
176  
177 # Purpose: Registers logging handles at the appropriate log levels
178 # Returns: Boolean
179 # Usage: $rv = _updateDist();
180  
181 28 my ( $logger, $level, $scope );
182  
183 # Purge @dist and reinitialize
184 28 foreach ( PL_DEBUG .. PL_EMERG ) { $dist[$_] = [] }
185  
186 # Set up the distribution list
187 28 foreach $logger ( keys %loggers ) {
188 26 ( $level, $scope ) = @{ $loggers{$logger} }{qw(severity scope)};
189 26 ▶ if ( $scope eq PL_EQ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($scope eq '=') { } 16 10 26
elsif ($scope eq '+') { } 7 3 10
elsif ($scope eq '-') { } 0 3 3
190 16 push @{ $dist[$level] }, $logger;
191 } elsif ( $scope eq PL_GE ) {
192 7 foreach ( $level .. PL_EMERG ) {
193 41 push @{ $dist[$_] }, $logger;
194 }
195 } elsif ( $scope eq PL_LE ) {
196 0 foreach ( PL_DEBUG .. $level ) {
197 0 push @{ $dist[$_] }, $logger;
198 }
199 } else {
200 3 foreach ( PL_DEBUG .. PL_EMERG ) {
201 24 ▶ push @{ $dist[$_] }, $logger if $level != $_;
Branch: 1 branch
BranchTrueFalseTotal
if $level != $_ 21 3 24
202 }
203 }
204 }
205  
206 # Report distribution list
207 28 foreach $level ( PL_DEBUG .. PL_EMERG ) {
208 224 pdebug( '%s: %s', PDLEVEL3, $level, @{ $dist[$level] } );
209 }
210  
211 28 return 1;
212 }
213  
214 35 ▶ sub startLogger {
Subroutine startLogger: called CC=17 SCAR=30.5
Pod documented
215  
216 # Purpose: Adds a named logger to our loggers hash.
217 # Returns: True (1) if successful,
218 # False (0) if there are any errors
219 # Usage: $rv = startLogger($name, $mech, $level, $scope, { %mopts });
220  
221 35 my $name = shift;
222 35 my $mech = shift;
223 35 my $level = shift;
224 35 my $scope = shift;
225 35 my $mopts = shift;
226 35 my $rv = 1;
227  
228 35 subPreamble( PDLEVEL3, '$$$$\%', $name, $mech, $level, $scope,
229 $mopts );
230  
231 # Set defaults for optional arguments that were left undefined
232 35 ▶ $level = PL_NOTICE unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 1 34 35
233 35 ▶ $scope = PL_GE unless defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scope 1 34 35
234  
235 # This is totally unnecessary, but we'll set PDebug to reflect
236 # how it actually operations in case anyone is looking at the
237 # distribution map
238 35 ▶ $level = PL_DEBUG if $mech eq 'PDebug';
Branch: 1 branch
BranchTrueFalseTotal
if $mech eq "PDebug" 1 34 35
239  
240 # Make sure this is a valid named logger
241 35 ▶ unless ( defined $name and length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length $name) 0 35 35
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
242 0 Paranoid::ERROR =
243 pdebug( 'invalid log name specified: %s', PDLEVEL1, $name );
244 0 $rv = 0;
245 }
246  
247 # Validate log level
248 35 ▶ unless ( scalar grep { $_ == $level } @_levels ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({$_ == $level;} @Paranoid::Log::_levels)) 0 35 35
249 0 Paranoid::ERROR =
250 pdebug( 'invalid log level specified: %s', PDLEVEL1, $level );
251 0 $rv = 0;
252 }
253  
254 # Validate scope
255 35 ▶ unless ( scalar grep { $_ eq $scope } @_scopes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({$_ eq $scope;} @Paranoid::Log::_scopes)) 0 35 35
256 0 Paranoid::ERROR =
257 pdebug( 'invalid log scope specified: %s', PDLEVEL1, $scope );
258 0 $rv = 0;
259 }
260  
261 # Make sure the module can be loaded if the log level was valid
262 35 ▶ $rv = _loadModule($mech) if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 35 0 35
263  
264 # Make sure the log entry is unique
265 35 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 34 1 35
266 34 ▶ if ( exists $loggers{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loggers{$name}) { } 0 34 34
267 0 Paranoid::ERROR = pdebug( 'a logger for %s already exists',
268 PDLEVEL1, $name );
269 0 $rv = 0;
270 } else {
271 34 ▶ $mopts = {}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mopts and ref $mopts eq "HASH" 16 18 34
Condition66%2/3
defined $mopts and ref $mopts eq "HASH"
!ll&&!rl&&r
16018
MC/DC50%1/2
defined $mopts and ref $mopts eq "HASH"
defined $mopts ref $mopts eq "HASH"
Truth table66%2/3
A and B
A= defined $moptsB= ref $mopts eq "HASH"
ABresult
0X0
100
111
272 unless defined $mopts and ref $mopts eq 'HASH';
273 34 $loggers{$name} = {
274 name => $name,
275 mechanism => $mech,
276 severity => $level,
277 scope => $scope,
278 options => {%$mopts} };
279 $rv =
280 $mech eq 'Stderr' ? 1
281 : $mech eq 'Stdout' ? 1
282 : $mech eq 'PDebug' ? 1
283 34 ▶ : &{ $msubs{$mech}[PL_AREF] }( %{ $loggers{$name} } );
Branch: 3 branches
BranchTrueFalseTotal
$mech eq 'Stderr' ? : 1 33 34
$mech eq 'Stdout' ? : 0 33 33
$mech eq 'PDebug' ? : 1 32 33
284 34 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 22 12 34
285 22 _updateDist();
286 } else {
287 12 delete $loggers{$name};
288 }
289 }
290 }
291  
292 35 subPostamble( PDLEVEL3, '$', $rv );
293  
294 35 return $rv;
295 }
296  
297 18 ▶ sub stopLogger {
Subroutine stopLogger: called CC=6 SCAR=18.0
Pod documented
298  
299 # Purpose: Deletes a named logger from the hash.
300 # Returns: True (1)
301 # Usage: _delLogger($name);
302  
303 18 my $name = shift;
304 18 my $rv = 1;
305  
306 18 pdebug( 'deleting %s logger', PDLEVEL3, $name );
307 18 ▶ if ( exists $loggers{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $loggers{$name}) 6 12 18
308 6 ▶ unless ( $loggers{$name}{mechanism} eq 'Stderr'
Branch: 1 branch
BranchTrueFalseTotal
unless ($loggers{$name}{"mechanism"} eq "Stderr" or $loggers{$name}{"mechanism"} eq "Stdout" or $loggers{$name}{"mechanism"} eq "PDebug") 5 1 6
Condition66%4/6
$loggers{$name}{"mechanism"} eq "Stderr" or $loggers{$name}{"mechanism"} eq "Stdout" or $loggers{$name}{"mechanism"} eq "PDebug"
l!l&&r!l&&!r
105
$loggers{$name}{"mechanism"} eq "Stderr" or $loggers{$name}{"mechanism"} eq "Stdout"
l!l&&r!l&&!r
105
MC/DC33%1/3
$loggers{$name}{"mechanism"} eq "Stderr" or $loggers{$name}{"mechanism"} eq "Stdout" or $loggers{$name}{"mechanism"} eq "PDebug"
$loggers{$name}{"mechanism"} eq "Stderr" $loggers{$name}{"mechanism"} eq "Stdout" $loggers{$name}{"mechanism"} eq "PDebug"
Truth table50%2/4
A or B or C
A= $loggers{$name}{"mechanism"} eq "Stderr"B= $loggers{$name}{"mechanism"} eq "Stdout"C= $loggers{$name}{"mechanism"} eq "PDebug"
ABCresult
1XX1
01X1
0011
0000
309 or $loggers{$name}{mechanism} eq 'Stdout'
310 or $loggers{$name}{mechanism} eq 'PDebug' ) {
311 $rv =
312 5 &{ $msubs{ $loggers{$name}{mechanism} }[PL_DREF] }(
313 5 %{ $loggers{$name} } );
314 }
315 6 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 6 0 6
316 6 delete $loggers{$name};
317 6 _updateDist();
318 }
319 }
320  
321 18 return $rv;
322 }
323  
324 665 ▶ sub plog {
Subroutine plog: called CC=12 SCAR=25.3
Pod documented
325  
326 # Purpose: Logs the message to all facilities registered at that level
327 # Returns: True (1) if the message was succesfully logged,
328 # False (0) if there are any errors
329 # Usage: $rv = plog($severity, $message);
330 # Usage: $rv = plog($severity, $message, @pdebugvals);
331  
332 665 my $level = shift;
333 665 my $message = shift;
334 665 my @margs = @_;
335 665 my $rv = 1;
336 665 my %record = (
337 severity => $level,
338 message => $message,
339 );
340 665 my ( $logger, $sref, $plevel );
341  
342 665 subPreamble( PDLEVEL1, '$$;@', $level, $message, @margs );
343  
344 # Validate level and message
345 $rv = 0
346 unless defined $message
347 665 ▶ and scalar grep { $_ == $level } @_levels;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and scalar grep({$_ == $level;} @Paranoid::Log::_levels) 0 665 665
Condition50%1/2
defined $message and scalar grep({$_ == $level;} @Paranoid::Log::_levels)
!ll
0665
MC/DC0%0/1
defined $message and scalar grep({$_ == $level;} @Paranoid::Log::_levels)
defined $message
Truth table50%1/2
A
A= defined $message
Aresult
00
11
348  
349 665 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 665 0 665
350  
351 # Trim leading/trailing whitespace and line terminators
352 665 $message =~ s/^[\s\r\n]+//s;
353 665 $message =~ s/[\s\r\n]+$//s;
354  
355 # First, if PDebug was enabled, we'll preprocess messages through
356 # pdebug. *Every* message gets passed since pdebug has its own
357 # mechanism to decide what to display
358 665 ▶ if ( grep { $loggers{$_}{mechanism} eq 'PDebug' } keys %loggers )
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$loggers{$_}{"mechanism"} eq "PDebug";} keys %loggers) 30 635 665
359 {
360  
361 # Paranoid::Debug uses an escalating scale of verbosity while
362 # this module uses an escalating scale of severity. We can
363 # equate them in an inverse relationship, but we'll also need
364 # to increment the output value since pdebug equates 0 as
365 # disabled.
366 #
367 # Finally, we'll also make it a negative number to
368 # signal pdebug to dive deeper into the call stack to find the
369 # true originator of the message. Otherwise, it would report
370 # plog as the originator, which is less than helpful.
371 30 $plevel = ( ( $level ^ 7 ) + 1 ) * -1;
372  
373 # Send it to pdebug, but save the output
374 30 $message = pdebug( $message, $plevel, @margs );
375  
376 # Substitute the processed output if we had any substitution
377 # values passed at all
378 30 ▶ $record{message} = $message if scalar @margs;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @margs 10 20 30
379  
380 }
381  
382 # Iterate over the @dist level
383 665 ▶ if ( defined $dist[$level] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dist[$level]) 653 12 665
384  
385 # Iterate over each logger
386 653 foreach $logger ( @{ $dist[$level] } ) {
387 677 ▶ next if $loggers{$logger}{mechanism} eq 'PDebug';
Branch: 1 branch
BranchTrueFalseTotal
if $loggers{$logger}{"mechanism"} eq "PDebug" 30 647 677
388  
389 647 ▶ if ( $loggers{$logger}{mechanism} eq 'Stderr' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loggers{$logger}{'mechanism'} eq 'Stderr') { } 1 646 647
elsif ($loggers{$logger}{'mechanism'} eq 'Stdout') { } 0 646 646
390  
391 # Special handling for STDERR
392 1 $rv = pderror($message);
393  
394 } elsif ( $loggers{$logger}{mechanism} eq 'Stdout' ) {
395  
396 # Special handling for STDOUT
397 0 $rv = print STDOUT "$message\n";
398  
399 } else {
400  
401 # Get the sub ref for the logger
402 $sref =
403 646 $msubs{ $loggers{$logger}{mechanism} }[PL_LREF];
404 $rv =
405 defined $sref
406 646 ▶ ? &$sref( %{ $loggers{$logger} }, %record )
Branch: 1 branch
BranchTrueFalseTotal
defined $sref ? : 646 0 646
407 : 0;
408 }
409 }
410 }
411  
412 } else {
413  
414 # Report error
415 0 Paranoid::ERROR = pdebug( 'invalid log level(%s) or message(%s)',
416 PDLEVEL1, $level, $message );
417 0 $rv = 0;
418 }
419  
420 665 subPostamble( PDLEVEL1, '$', $rv );
421  
422 665 return $rv;
423 }
424 }
425  
426 17 ▶sub plverbosity {
Subroutine plverbosity: not called CC=8 SCAR=42.0
Pod documented
427  
428 # Purpose: Sets Stdout/Stderr verbosity according to passed level.
429 # Supports levels 1 - 3, with 4 being the most verbose
430 # Returns: Boolean
431 # Usage: $rv = plverbosity($level);
432  
433 0 my $level = shift;
434 0 my $max = 3;
435 0 my $outidx = PL_NOTICE;
436 0 my $erridx = PL_CRIT;
437 0 my $rv = 1;
438  
439 0 subPreamble( PDLEVEL3, '$', $level );
440  
441 # Make sure a positive integer was passed
442 0 ▶ $rv = 0 unless $level > -1;
Branch: 1 branch
BranchTrueFalseTotal
unless $level > -1 0 0 0
443  
444 # Cap $level
445 0 ▶ $level = $max if $level > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $level > $max 0 0 0
446  
447 # First, stop any current logging
448 0 foreach ( 0 .. 7 ) {
449 0 ▶ stopLogger( $_ < 3 ? "Stdout$_" : "Stderr$_" );
Branch: 1 branch
BranchTrueFalseTotal
$_ < 3 ? : 0 0 0
450 }
451  
452 # Always enable PL_EMERG/PL_ALERT
453 0 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 0 0 0
454 0 startLogger( "Stderr6", 'Stderr', PL_ALERT, PL_EQ );
455 0 startLogger( "Stderr7", 'Stderr', PL_EMERG, PL_EQ );
456 }
457  
458 # Enable what's been asked
459 0 ▶ while ( $rv and $level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $level) 0 0 0
Condition0%0/3
$rv and $level
!ll&&!rl&&r
000
MC/DC0%0/2
$rv and $level
$rv $level
Truth table0%0/3
A and B
A= $rvB= $level
ABresult
0X0
100
111
460  
461 # Start the levels
462 0 startLogger( "Stdout$outidx", 'Stdout', $outidx, PL_EQ );
463 0 startLogger( "Stderr$erridx", 'Stderr', $erridx, PL_EQ );
464  
465 # Decrement the counters
466 0 $outidx--;
467 0 $erridx--;
468 0 $level--;
469 }
470  
471 0 subPostamble( PDLEVEL3, '$', $rv );
472  
473 0 return $rv;
474 }
475  
476 17 1;
477  
478 __END__
« Paranoid/Filesystem.pm ↑ Summary Paranoid/Input.pm »