| 1 | ###############r################################### | ||||||||||||
| 2 | package Log::Log4perl::Level; | ||||||||||||
| 3 | ################################################## | ||||||||||||
| 4 | |||||||||||||
| 5 | 55 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 55 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 55 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 55 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | # log4j, for whatever reason, puts 0 as all and MAXINT as OFF. | ||||||||||||
| 11 | # this seems less optimal, as more logging would imply a higher | ||||||||||||
| 12 | # level. But oh well. Probably some brokenness that has persisted. :) | ||||||||||||
| 13 | 55 | ▶ | use constant ALL_INT => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 55 | ▶ | use constant TRACE_INT => 5000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 55 | ▶ | use constant DEBUG_INT => 10000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 55 | ▶ | use constant INFO_INT => 20000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 55 | ▶ | use constant WARN_INT => 30000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 55 | ▶ | use constant ERROR_INT => 40000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 55 | ▶ | use constant FATAL_INT => 50000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 55 | ▶ | use constant OFF_INT => (2 ** 31) - 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | |||||||||||||
| 22 | 55 | ▶ | no strict qw(refs); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 55 | ▶ | use vars qw(%PRIORITY %LEVELS %SYSLOG %L4P_TO_LD); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 55 | %PRIORITY = (); # unless (%PRIORITY); | |||||||||||
| 26 | 55 | ▶ | %LEVELS = () unless (%LEVELS); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 55 | ▶ | %SYSLOG = () unless (%SYSLOG); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 55 | ▶ | %L4P_TO_LD = () unless (%L4P_TO_LD); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | |||||||||||||
| 30 | 471 | ▶ | sub add_priority { | ||||||||||
|
Subroutine
add_priority: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 31 | 471 | my ($prio, $intval, $syslog, $log_dispatch_level) = @_; | |||||||||||
| 32 | 471 | $prio = uc($prio); # just in case; | |||||||||||
| 33 | |||||||||||||
| 34 | 471 | $PRIORITY{$prio} = $intval; | |||||||||||
| 35 | 471 | $LEVELS{$intval} = $prio; | |||||||||||
| 36 | |||||||||||||
| 37 | # Set up the mapping between Log4perl integer levels and | ||||||||||||
| 38 | # Log::Dispatch levels | ||||||||||||
| 39 | # Note: Log::Dispatch uses the following levels: | ||||||||||||
| 40 | # 0 debug | ||||||||||||
| 41 | # 1 info | ||||||||||||
| 42 | # 2 notice | ||||||||||||
| 43 | # 3 warning | ||||||||||||
| 44 | # 4 error | ||||||||||||
| 45 | # 5 critical | ||||||||||||
| 46 | # 6 alert | ||||||||||||
| 47 | # 7 emergency | ||||||||||||
| 48 | |||||||||||||
| 49 | # The equivalent Log::Dispatch level is optional, set it to | ||||||||||||
| 50 | # the highest value (7=emerg) if it's not provided. | ||||||||||||
| 51 | 471 | ▶ | $log_dispatch_level = 7 unless defined $log_dispatch_level; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | |||||||||||||
| 53 | 471 | $L4P_TO_LD{$prio} = $log_dispatch_level; | |||||||||||
| 54 | |||||||||||||
| 55 | 471 | ▶ | $SYSLOG{$prio} = $syslog if defined($syslog); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | # create the basic priorities | ||||||||||||
| 59 | 55 | add_priority("OFF", OFF_INT, -1, 7); | |||||||||||
| 60 | 55 | add_priority("FATAL", FATAL_INT, 0, 7); | |||||||||||
| 61 | 55 | add_priority("ERROR", ERROR_INT, 3, 4); | |||||||||||
| 62 | 55 | add_priority("WARN", WARN_INT, 4, 3); | |||||||||||
| 63 | 55 | add_priority("INFO", INFO_INT, 6, 1); | |||||||||||
| 64 | 55 | add_priority("DEBUG", DEBUG_INT, 7, 0); | |||||||||||
| 65 | 55 | add_priority("TRACE", TRACE_INT, 8, 0); | |||||||||||
| 66 | 55 | add_priority("ALL", ALL_INT, 8, 0); | |||||||||||
| 67 | |||||||||||||
| 68 | # we often sort numerically, so a helper func for readability | ||||||||||||
| 69 | 43803 | ▶ | sub numerically {$a <=> $b} | ||||||||||
|
Subroutine
numerically: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 70 | |||||||||||||
| 71 | ########################################### | ||||||||||||
| 72 | 448 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||
| 73 | ########################################### | ||||||||||||
| 74 | 448 | my($class, $namespace) = @_; | |||||||||||
| 75 | |||||||||||||
| 76 | 448 | ▶ | if(defined $namespace) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | # Export $OFF, $FATAL, $ERROR etc. to | ||||||||||||
| 78 | # the given namespace | ||||||||||||
| 79 | 2 | ▶ | $namespace .= "::" unless $namespace =~ /::$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | } else { | ||||||||||||
| 81 | # Export $OFF, $FATAL, $ERROR etc. to | ||||||||||||
| 82 | # the caller's namespace | ||||||||||||
| 83 | 446 | $namespace = caller(0) . "::"; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 448 | for my $key (keys %PRIORITY) { | |||||||||||
| 87 | 3584 | my $name = "$namespace$key"; | |||||||||||
| 88 | 3584 | my $value = $PRIORITY{$key}; | |||||||||||
| 89 | 3584 | *{"$name"} = \$value; | |||||||||||
| 90 | 3584 | my $nameint = "$namespace${key}_INT"; | |||||||||||
| 91 | 3584 | my $func = uc($key) . "_INT"; | |||||||||||
| 92 | 3584 | *{"$nameint"} = \&$func; | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | ################################################## | ||||||||||||
| 97 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 98 | ################################################## | ||||||||||||
| 99 | # We don't need any of this class nonsense | ||||||||||||
| 100 | # in Perl, because we won't allow subclassing | ||||||||||||
| 101 | # from this. We're optimizing for raw speed. | ||||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | ################################################## | ||||||||||||
| 105 | 193 | ▶ | sub to_priority { | ||||||||||
|
Subroutine
to_priority: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 106 | # changes a level name string to a priority numeric | ||||||||||||
| 107 | ################################################## | ||||||||||||
| 108 | 193 | my($string) = @_; | |||||||||||
| 109 | |||||||||||||
| 110 | 193 | ▶ | if(exists $PRIORITY{$string}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 192 | return $PRIORITY{$string}; | |||||||||||
| 112 | }else{ | ||||||||||||
| 113 | 1 | croak "level '$string' is not a valid error level (".join ('|', keys %PRIORITY),')'; | |||||||||||
| 114 | } | ||||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | ################################################## | ||||||||||||
| 118 | 40 | ▶ | sub to_level { | ||||||||||
|
Subroutine
to_level: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||
| 119 | # changes a priority numeric constant to a level name string | ||||||||||||
| 120 | ################################################## | ||||||||||||
| 121 | 40 | my ($priority) = @_; | |||||||||||
| 122 | 40 | ▶ | if (exists $LEVELS{$priority}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 40 | return $LEVELS{$priority} | |||||||||||
| 124 | }else { | ||||||||||||
| 125 | 0 | croak("priority '$priority' is not a valid error level number (", | |||||||||||
| 126 | join("|", sort numerically keys %LEVELS), " | ||||||||||||
| 127 | )"); | ||||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | ################################################## | ||||||||||||
| 133 | 0 | ▶ | sub to_LogDispatch_string { | ||||||||||
|
Subroutine
to_LogDispatch_string: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 134 | # translates into strings that Log::Dispatch recognizes | ||||||||||||
| 135 | ################################################## | ||||||||||||
| 136 | 0 | my($priority) = @_; | |||||||||||
| 137 | |||||||||||||
| 138 | 0 | ▶ | confess "do what? no priority?" unless defined $priority; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | |||||||||||||
| 140 | 0 | my $string; | |||||||||||
| 141 | |||||||||||||
| 142 | 0 | ▶ | if(exists $LEVELS{$priority}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 0 | $string = $LEVELS{$priority}; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | # Log::Dispatch idiosyncrasies | ||||||||||||
| 147 | 0 | ▶ | if($priority == $PRIORITY{WARN}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 0 | $string = "WARNING"; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | ▶ | if($priority == $PRIORITY{FATAL}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | 0 | $string = "EMERGENCY"; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 0 | return $string; | |||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | ################################################### | ||||||||||||
| 159 | 199 | ▶ | sub is_valid { | ||||||||||
|
Subroutine
is_valid: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 160 | ################################################### | ||||||||||||
| 161 | 199 | my $q = shift; | |||||||||||
| 162 | |||||||||||||
| 163 | 199 | ▶ | if ($q =~ /[A-Z]/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | 3 | return exists $PRIORITY{$q}; | |||||||||||
| 165 | }else{ | ||||||||||||
| 166 | 196 | return $LEVELS{$q}; | |||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | } | ||||||||||||
| 170 | |||||||||||||
| 171 | 4 | ▶ | sub get_higher_level { | ||||||||||
|
Subroutine
get_higher_level: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 172 | 4 | my ($old_priority, $delta) = @_; | |||||||||||
| 173 | |||||||||||||
| 174 | 4 | ▶ | $delta ||= 1; | ||||||||||
|
Condition50%1/2
$delta ||= 1
MC/DC0%0/1
$delta ||= 1
$delta
Truth table50%1/2
A
A= $delta
| |||||||||||||
| 175 | |||||||||||||
| 176 | 4 | my $new_priority = 0; | |||||||||||
| 177 | |||||||||||||
| 178 | 4 | foreach (1..$delta){ | |||||||||||
| 179 | #so the list is TRACE, DEBUG, INFO, WARN, ERROR, FATAL | ||||||||||||
| 180 | # but remember, the numbers go in reverse order! | ||||||||||||
| 181 | 153 | foreach my $p (sort numerically keys %LEVELS){ | |||||||||||
| 182 | 5194 | ▶ | if ($p > $old_priority) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 41 | $new_priority = $p; | |||||||||||
| 184 | 41 | last; | |||||||||||
| 185 | } | ||||||||||||
| 186 | } | ||||||||||||
| 187 | 153 | $old_priority = $new_priority; | |||||||||||
| 188 | } | ||||||||||||
| 189 | 4 | return $new_priority; | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | 40 | ▶ | sub get_lower_level { | ||||||||||
|
Subroutine
get_lower_level: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 193 | 40 | my ($old_priority, $delta) = @_; | |||||||||||
| 194 | |||||||||||||
| 195 | 40 | ▶ | $delta ||= 1; | ||||||||||
|
Condition50%1/2
$delta ||= 1
MC/DC0%0/1
$delta ||= 1
$delta
Truth table50%1/2
A
A= $delta
| |||||||||||||
| 196 | |||||||||||||
| 197 | 40 | my $new_priority = 0; | |||||||||||
| 198 | |||||||||||||
| 199 | 40 | foreach (1..$delta){ | |||||||||||
| 200 | #so the list is FATAL, ERROR, WARN, INFO, DEBUG, TRACE | ||||||||||||
| 201 | # but remember, the numbers go in reverse order! | ||||||||||||
| 202 | 140 | foreach my $p (reverse sort numerically keys %LEVELS){ | |||||||||||
| 203 | 3774 | ▶ | if ($p < $old_priority) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 204 | 76 | $new_priority = $p; | |||||||||||
| 205 | 76 | last; | |||||||||||
| 206 | } | ||||||||||||
| 207 | } | ||||||||||||
| 208 | 140 | $old_priority = $new_priority; | |||||||||||
| 209 | } | ||||||||||||
| 210 | 40 | return $new_priority; | |||||||||||
| 211 | } | ||||||||||||
| 212 | |||||||||||||
| 213 | 8313 | ▶ | sub isGreaterOrEqual { | ||||||||||
|
Subroutine
isGreaterOrEqual: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 214 | 8313 | my $lval = shift; | |||||||||||
| 215 | 8313 | my $rval = shift; | |||||||||||
| 216 | |||||||||||||
| 217 | # in theory, we should check if the above really ARE valid levels. | ||||||||||||
| 218 | # but we just use numeric comparison, since they aren't really classes. | ||||||||||||
| 219 | |||||||||||||
| 220 | # oh, yeah, and 'cuz level ints go from 0 .. N with 0 being highest, | ||||||||||||
| 221 | # these are reversed. | ||||||||||||
| 222 | 8313 | return $lval <= $rval; | |||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | ###################################################################### | ||||||||||||
| 226 | # | ||||||||||||
| 227 | # since the integer representation of levels is reversed from what | ||||||||||||
| 228 | # we normally want, we don't want to use < and >... instead, we | ||||||||||||
| 229 | # want to use this comparison function | ||||||||||||
| 230 | |||||||||||||
| 231 | |||||||||||||
| 232 | 55 | 1; | |||||||||||
| 233 | |||||||||||||
| 234 | __END__ | ||||||||||||