| 1 | ###################################################################### | ||||||||||||||||||||||||||||
| 2 | # Buffer.pm -- 2004, Mike Schilli <m@perlmeister.com> | ||||||||||||||||||||||||||||
| 3 | ###################################################################### | ||||||||||||||||||||||||||||
| 4 | # Composite appender buffering messages until a trigger condition is met. | ||||||||||||||||||||||||||||
| 5 | ###################################################################### | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | ########################################### | ||||||||||||||||||||||||||||
| 8 | package Log::Log4perl::Appender::Buffer; | ||||||||||||||||||||||||||||
| 9 | ########################################### | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | our @ISA = qw(Log::Log4perl::Appender); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '1.53'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ########################################### | ||||||||||||||||||||||||||||
| 19 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | ########################################### | ||||||||||||||||||||||||||||
| 21 | 4 | my($class, %options) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | my $self = { | |||||||||||||||||||||||||||
| 24 | appender=> undef, | ||||||||||||||||||||||||||||
| 25 | buffer => [], | ||||||||||||||||||||||||||||
| 26 | options => { | ||||||||||||||||||||||||||||
| 27 | max_messages => undef, | ||||||||||||||||||||||||||||
| 28 | trigger => undef, | ||||||||||||||||||||||||||||
| 29 | trigger_level => undef, | ||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | level => 0, | ||||||||||||||||||||||||||||
| 32 | %options, | ||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | if($self->{trigger_level}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $self->{trigger} = level_trigger($self->{trigger_level}); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Pass back the appender to be synchronized as a dependency | ||||||||||||||||||||||||||||
| 40 | # to the configuration file parser | ||||||||||||||||||||||||||||
| 41 | 4 | push @{$options{l4p_depends_on}}, $self->{appender}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Run our post_init method in the configurator after | ||||||||||||||||||||||||||||
| 44 | # all appenders have been defined to make sure the | ||||||||||||||||||||||||||||
| 45 | # appender we're playing 'dam' for really exists | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | push @{$options{l4p_post_config_subs}}, sub { $self->post_init() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | ########################################### | ||||||||||||||||||||||||||||
| 52 | 4 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=5 SCAR=17.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | ########################################### | ||||||||||||||||||||||||||||
| 54 | 4 | my($self, %params) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | local $Log::Log4perl::caller_depth = | |||||||||||||||||||||||||||
| 57 | $Log::Log4perl::caller_depth + 2; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Do we need to discard a message because there's already | ||||||||||||||||||||||||||||
| 60 | # max_size messages in the buffer? | ||||||||||||||||||||||||||||
| 61 | 4 | ▶ | if(defined $self->{max_messages} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"max_messages"} and @{$self->{"buffer"};} == $self->{"max_messages"}
MC/DC0%0/2
defined $self->{"max_messages"} and @{$self->{"buffer"};} == $self->{"max_messages"}
defined $self->{"max_messages"}
@{$self->{"buffer"};} == $self->{"max_messages"}
Truth table33%1/3
A and B
A= defined $self->{"max_messages"}B= @{$self->{"buffer"};} == $self->{"max_messages"}
| |||||||||||||||||||||||||||||
| 62 | 0 | @{$self->{buffer}} == $self->{max_messages}) { | |||||||||||||||||||||||||||
| 63 | 0 | shift @{$self->{buffer}}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | # Ask the appender to save a cached message in $cache | ||||||||||||||||||||||||||||
| 66 | $self->{app}->SUPER::log(\%params, | ||||||||||||||||||||||||||||
| 67 | $params{log4p_category}, | ||||||||||||||||||||||||||||
| 68 | 4 | $params{log4p_level}, \my $cache); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Save it in the appender's message buffer, but only if | ||||||||||||||||||||||||||||
| 71 | # it hasn't been suppressed by an appender threshold | ||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if( defined $cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | push @{ $self->{buffer} }, $cache; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | $self->flush() if $self->{trigger}->($self, \%params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ########################################### | ||||||||||||||||||||||||||||
| 80 | 5 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | ########################################### | ||||||||||||||||||||||||||||
| 82 | 5 | my($self) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Flush pending messages if we have any | ||||||||||||||||||||||||||||
| 85 | 5 | for my $cache (@{$self->{buffer}}) { | |||||||||||||||||||||||||||
| 86 | 3 | $self->{app}->SUPER::log_cached($cache); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Empty buffer | ||||||||||||||||||||||||||||
| 90 | 5 | $self->{buffer} = []; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | ########################################### | ||||||||||||||||||||||||||||
| 94 | 4 | ▶ | sub post_init { | ||||||||||||||||||||||||||
|
Subroutine
post_init: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | ########################################### | ||||||||||||||||||||||||||||
| 96 | 4 | my($self) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | if(! exists $self->{appender}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | die "No appender defined for " . __PACKAGE__; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | my $appenders = Log::Log4perl->appenders(); | |||||||||||||||||||||||||||
| 103 | 4 | my $appender = Log::Log4perl->appenders()->{$self->{appender}}; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | if(! defined $appender) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | die "Appender $self->{appender} not defined (yet) when " . | |||||||||||||||||||||||||||
| 107 | __PACKAGE__ . " needed it"; | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | $self->{app} = $appender; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ########################################### | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub level_trigger { | ||||||||||||||||||||||||||
|
Subroutine
level_trigger: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | ########################################### | ||||||||||||||||||||||||||||
| 116 | 0 | my($level) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # closure holding $level | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 0 | my($self, $params) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | return Log::Log4perl::Level::to_priority( | ||||||||||||||||||||||||||||
| 123 | 0 | $params->{log4p_level}) >= | |||||||||||||||||||||||||||
| 124 | Log::Log4perl::Level::to_priority($level); | ||||||||||||||||||||||||||||
| 125 | 0 | }; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | ########################################### | ||||||||||||||||||||||||||||
| 129 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | ########################################### | ||||||||||||||||||||||||||||
| 131 | 2 | my($self) = @_; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||