| 1 | ################################################## | ||||||||||
| 2 | package Log::Log4perl::Filter; | ||||||||||
| 3 | ################################################## | ||||||||||
| 4 | |||||||||||
| 5 | 54 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 54 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 54 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 54 | ▶ | use Log::Log4perl::Level; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 54 | ▶ | use Log::Log4perl::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 54 | ▶ | use constant _INTERNAL_DEBUG => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 54 | our %FILTERS_DEFINED = (); | |||||||||
| 15 | |||||||||||
| 16 | ################################################## | ||||||||||
| 17 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 18 | ################################################## | ||||||||||
| 19 | 4 | my($class, $name, $action) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 4 | print "Creating filter $name\n" if _INTERNAL_DEBUG; | |||||||||
| 22 | |||||||||||
| 23 | 4 | my $self = { name => $name }; | |||||||||
| 24 | 4 | bless $self, $class; | |||||||||
| 25 | |||||||||||
| 26 | 4 | ▶ | if(ref($action) eq "CODE") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | # it's a code ref | ||||||||||
| 28 | 4 | $self->{ok} = $action; | |||||||||
| 29 | } else { | ||||||||||
| 30 | # it's something else | ||||||||||
| 31 | 0 | die "Code for ($name/$action) not properly defined"; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 4 | return $self; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | ################################################## | ||||||||||
| 38 | 19 | ▶ | sub register { # Register a filter by name | ||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | # (Passed on to subclasses) | ||||||||||
| 40 | ################################################## | ||||||||||
| 41 | 19 | my($self) = @_; | |||||||||
| 42 | |||||||||||
| 43 | 19 | by_name($self->{name}, $self); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | ################################################## | ||||||||||
| 47 | 38 | ▶ | sub by_name { # Get/Set a filter object by name | ||||||||
|
Subroutine
by_name: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 48 | ################################################## | ||||||||||
| 49 | 38 | my($name, $value) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 38 | ▶ | if(defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 19 | $FILTERS_DEFINED{$name} = $value; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 38 | ▶ | if(exists $FILTERS_DEFINED{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 38 | return $FILTERS_DEFINED{$name}; | |||||||||
| 57 | } else { | ||||||||||
| 58 | 0 | return undef; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | ################################################## | ||||||||||
| 63 | 243 | ▶ | sub reset { | ||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 64 | ################################################## | ||||||||||
| 65 | 243 | %FILTERS_DEFINED = (); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | ################################################## | ||||||||||
| 69 | 7 | ▶ | sub ok { | ||||||||
|
Subroutine
ok: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 70 | ################################################## | ||||||||||
| 71 | 7 | my($self, %p) = @_; | |||||||||
| 72 | |||||||||||
| 73 | 7 | print "Calling $self->{name}'s ok method\n" if _INTERNAL_DEBUG; | |||||||||
| 74 | |||||||||||
| 75 | # Force filter classes to define their own | ||||||||||
| 76 | # ok(). Exempt are only sub {..} ok functions, | ||||||||||
| 77 | # defined in the conf file. | ||||||||||
| 78 | die "This is to be overridden by the filter" unless | ||||||||||
| 79 | 7 | ▶ | defined $self->{ok}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | # What should we set the message in $_ to? The most logical | ||||||||||
| 82 | # approach seems to be to concat all parts together. If some | ||||||||||
| 83 | # filter wants to dissect the parts, it still can examine %p, | ||||||||||
| 84 | # which gets passed to the subroutine and contains the chunks | ||||||||||
| 85 | # in $p{message}. | ||||||||||
| 86 | # Split because of CVS | ||||||||||
| 87 | local($_) = join $ | ||||||||||
| 88 | 7 | Log::Log4perl::JOIN_MSG_ARRAY_CHAR, @{$p{message}}; | |||||||||
| 89 | 7 | print "\$_ is '$_'\n" if _INTERNAL_DEBUG; | |||||||||
| 90 | |||||||||||
| 91 | 7 | my $decision = $self->{ok}->(%p); | |||||||||
| 92 | |||||||||||
| 93 | 7 | print "$self->{name}'s ok'ed: ", | |||||||||
| 94 | ($decision ? "yes" : "no"), "\n" if _INTERNAL_DEBUG; | ||||||||||
| 95 | |||||||||||
| 96 | 7 | return $decision; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 54 | 1; | |||||||||
| 100 | |||||||||||
| 101 | __END__ | ||||||||||