| 1 | ################################################## | ||||||||||
| 2 | package Log::Log4perl::Filter::Boolean; | ||||||||||
| 3 | ################################################## | ||||||||||
| 4 | |||||||||||
| 5 | 54 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 54 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 54 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 54 | ▶ | use Log::Log4perl::Level; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 54 | ▶ | use Log::Log4perl::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 54 | ▶ | use constant _INTERNAL_DEBUG => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 54 | ▶ | use base qw(Log::Log4perl::Filter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | ################################################## | ||||||||||
| 18 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | ################################################## | ||||||||||
| 20 | 3 | my ($class, %options) = @_; | |||||||||
| 21 | |||||||||||
| 22 | 3 | my $self = { params => {}, | |||||||||
| 23 | %options, | ||||||||||
| 24 | }; | ||||||||||
| 25 | |||||||||||
| 26 | 3 | bless $self, $class; | |||||||||
| 27 | |||||||||||
| 28 | 3 | print "Compiling '$options{logic}'\n" if _INTERNAL_DEBUG; | |||||||||
| 29 | |||||||||||
| 30 | # Set up meta-decider for later | ||||||||||
| 31 | 3 | $self->compile_logic($options{logic}); | |||||||||
| 32 | |||||||||||
| 33 | 3 | return $self; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | ################################################## | ||||||||||
| 37 | 7 | ▶ | sub ok { | ||||||||
|
Subroutine
ok: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 38 | ################################################## | ||||||||||
| 39 | 7 | my ($self, %p) = @_; | |||||||||
| 40 | |||||||||||
| 41 | 7 | return $self->eval_logic(\%p); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | ################################################## | ||||||||||
| 45 | 3 | ▶ | sub compile_logic { | ||||||||
|
Subroutine
compile_logic: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||
| 46 | ################################################## | ||||||||||
| 47 | 3 | my ($self, $logic) = @_; | |||||||||
| 48 | |||||||||||
| 49 | # Extract Filter placeholders in logic as defined | ||||||||||
| 50 | # in configuration file. | ||||||||||
| 51 | 3 | ▶ | while($logic =~ /([\w_-]+)/g) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | # Get the corresponding filter object | ||||||||||
| 53 | 7 | my $filter = Log::Log4perl::Filter::by_name($1); | |||||||||
| 54 | 7 | ▶ | die "Filter $1 required by Boolean filter, but not defined" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | unless $filter; | ||||||||||
| 56 | |||||||||||
| 57 | 7 | $self->{params}->{$1} = $filter; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # Fabricate a parameter list: A1/A2/A3 => $A1, $A2, $A3 | ||||||||||
| 61 | 3 | my $plist = join ', ', map { '$' . $_ } keys %{$self->{params}}; | |||||||||
| 62 | |||||||||||
| 63 | # Replace all the (dollar-less) placeholders in the code with | ||||||||||
| 64 | # calls to their respective coderefs. | ||||||||||
| 65 | 3 | $logic =~ s/([\w_-]+)/\&\$$1/g; | |||||||||
| 66 | |||||||||||
| 67 | # Set up the meta decider, which transforms the config file | ||||||||||
| 68 | # logic into compiled perl code | ||||||||||
| 69 | 3 | my $func = <<EOT; | |||||||||
| 70 | sub { | ||||||||||
| 71 | my($plist) = \@_; | ||||||||||
| 72 | $logic; | ||||||||||
| 73 | } | ||||||||||
| 74 | EOT | ||||||||||
| 75 | |||||||||||
| 76 | 3 | print "func=$func\n" if _INTERNAL_DEBUG; | |||||||||
| 77 | |||||||||||
| 78 | 3 | my $eval_func = eval $func; | |||||||||
| 79 | |||||||||||
| 80 | 3 | ▶ | if(! $eval_func) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | die "Syntax error in Boolean filter logic: $eval_func"; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 3 | $self->{eval_func} = $eval_func; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | ################################################## | ||||||||||
| 88 | 7 | ▶ | sub eval_logic { | ||||||||
|
Subroutine
eval_logic: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 89 | ################################################## | ||||||||||
| 90 | 7 | my($self, $p) = @_; | |||||||||
| 91 | |||||||||||
| 92 | 7 | my @plist = (); | |||||||||
| 93 | |||||||||||
| 94 | # Eval the results of all filters referenced | ||||||||||
| 95 | # in the code (although the order of keys is | ||||||||||
| 96 | # not predictable, it is consistent :) | ||||||||||
| 97 | 7 | for my $param (keys %{$self->{params}}) { | |||||||||
| 98 | # Pass a coderef as a param that will run the filter's ok method and | ||||||||||
| 99 | # return a 1 or 0. | ||||||||||
| 100 | 19 | print "Passing filter $param\n" if _INTERNAL_DEBUG; | |||||||||
| 101 | 10 | ▶ | push(@plist, sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 102 | 10 | ▶ | return $self->{params}->{$param}->ok(%$p) ? 1 : 0 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 19 | }); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # Now pipe the parameters into the canned function, | ||||||||||
| 107 | # have it evaluate the logic and return the final | ||||||||||
| 108 | # decision | ||||||||||
| 109 | 7 | print "Passing in (", join(', ', @plist), ")\n" if _INTERNAL_DEBUG; | |||||||||
| 110 | 7 | return $self->{eval_func}->(@plist); | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 54 | 1; | |||||||||
| 114 | |||||||||||
| 115 | __END__ | ||||||||||