Log4perl/Appender/Limit.pm

filter: Statementstmt 85.9% 61 / 71 Branchbran 57.1% 16 / 28 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 83.3% 10 / 12 Podpod 16.6% 1 / 6 totaltotal 75.7% 103 / 136 CC 20 SCAR 19.7
CC
20
Cov
78%
log
12.2
restore
12.0
save
6.0
CRAP 7.2
« Log4perl/JavaMap/JDBCAppender.pm ↑ Summary Log4perl/Logger.pm »
1 ######################################################################
2 # Limit.pm -- 2003, Mike Schilli <m@perlmeister.com>
3 ######################################################################
4 # Special composite appender limiting the number of messages relayed
5 # to its appender(s).
6 ######################################################################
7  
8 ###########################################
9 package Log::Log4perl::Appender::Limit;
10 ###########################################
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our @ISA = qw(Log::Log4perl::Appender);
17  
18 1 our $VERSION = '1.53';
19  
20 ###########################################
21 8 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
22 ###########################################
23 8 my($class, %options) = @_;
24  
25 8 my $self = {
26 max_until_flushed => undef,
27 max_until_discarded => undef,
28 appender_method_on_flush
29 => undef,
30 appender => undef,
31 accumulate => 1,
32 persistent => undef,
33 block_period => 3600,
34 buffer => [],
35 %options,
36 };
37  
38 # Pass back the appender to be limited as a dependency
39 # to the configuration file parser
40 8 push @{$options{l4p_depends_on}}, $self->{appender};
41  
42 # Run our post_init method in the configurator after
43 # all appenders have been defined to make sure the
44 # appenders we're connecting to really exist.
45 8 ▶ push @{$options{l4p_post_config_subs}}, sub { $self->post_init() };
Subroutine __ANON__: called CC=1 SCAR=0.0
46  
47 8 bless $self, $class;
48  
49 8 ▶ if(defined $self->{persistent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"persistent"}) 0 8 8
50 0 $self->restore();
51 }
52  
53 8 return $self;
54 }
55  
56 ###########################################
57 32 ▶sub log {
Subroutine log: called CC=12 SCAR=25.0
Pod undocumented
58 ###########################################
59 32 my($self, %params) = @_;
60
61 32 local $Log::Log4perl::caller_depth =
62 $Log::Log4perl::caller_depth + 2;
63  
64 # Check if message needs to be discarded
65 32 my $discard = 0;
66 32 ▶ if(defined $self->{max_until_discarded} and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"max_until_discarded"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1) 22 10 32
Condition66%2/3
defined $self->{"max_until_discarded"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
!ll&&!rl&&r
10022
MC/DC50%1/2
defined $self->{"max_until_discarded"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
defined $self->{"max_until_discarded"} scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
Truth table66%2/3
A and B
A= defined $self->{"max_until_discarded"}B= scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
ABresult
0X0
100
111
67 22 scalar @{$self->{buffer}} >= $self->{max_until_discarded} - 1) {
68 22 $discard = 1;
69 }
70  
71 # Check if we need to flush
72 32 my $flush = 0;
73 32 ▶ if(defined $self->{max_until_flushed} and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"max_until_flushed"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1) 2 30 32
Condition100%3/3
defined $self->{"max_until_flushed"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
!ll&&!rl&&r
2642
MC/DC100%2/2
defined $self->{"max_until_flushed"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
defined $self->{"max_until_flushed"} scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
Truth table100%3/3
A and B
A= defined $self->{"max_until_flushed"}B= scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
ABresult
0X0
100
111
74 6 scalar @{$self->{buffer}} >= $self->{max_until_flushed} - 1) {
75 2 $flush = 1;
76 }
77  
78 32 ▶ if(!$flush and
Branch: 1 branch
BranchTrueFalseTotal
if (not $flush and (exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time)) 23 9 32
Condition83%5/6
not $flush and (exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time)
!ll&&!rl&&r
2723
exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time
!ll&&!rl&&r
7023
MC/DC66%2/3
not $flush and (exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time)
not $flush exists $self->{"sent_last"} $self->{"sent_last"} + $self->{"block_period"} > time
Truth table75%3/4
not(A) and B and C
A= not $flushB= exists $self->{"sent_last"}C= $self->{"sent_last"} + $self->{"block_period"} > time
ABCresult
0XX0
10X0
1100
1111
79 (exists $self->{sent_last} and
80 $self->{sent_last} + $self->{block_period} > time()
81 )
82 ) {
83 # Message needs to be blocked for now.
84 23 ▶ return if $discard;
Branch: 1 branch
BranchTrueFalseTotal
if $discard 20 3 23
85  
86 # Ask the appender to save a cached message in $cache
87 $self->{app}->SUPER::log(\%params,
88 $params{log4p_category},
89 3 $params{log4p_level}, \my $cache);
90  
91 # Save message and other parameters
92 3 ▶ push @{$self->{buffer}}, $cache if $self->{accumulate};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"accumulate"} 3 0 3
93  
94 3 ▶ $self->save() if $self->{persistent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"persistent"} 0 3 3
95  
96 3 return;
97 }
98  
99 # Relay all messages we got to the SUPER class, which needs to render the
100 # messages according to the appender's layout, first.
101  
102 # Log pending messages if we have any
103 9 $self->flush();
104  
105 # Log current message as well
106 $self->{app}->SUPER::log(\%params,
107 $params{log4p_category},
108 9 $params{log4p_level});
109  
110 9 $self->{sent_last} = time();
111  
112 # We need to store the timestamp persistently, if requested
113 9 ▶ $self->save() if $self->{persistent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"persistent"} 0 9 9
114 }
115  
116 ###########################################
117 8 ▶sub post_init {
Subroutine post_init: called CC=3 SCAR=12.0
Pod undocumented
118 ###########################################
119 8 my($self) = @_;
120  
121 8 ▶ if(! exists $self->{appender}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"appender"}) 0 8 8
122 0 die "No appender defined for " . __PACKAGE__;
123 }
124  
125 8 my $appenders = Log::Log4perl->appenders();
126 8 my $appender = Log::Log4perl->appenders()->{$self->{appender}};
127  
128 8 ▶ if(! defined $appender) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $appender) 0 8 8
129 0 die "Appender $self->{appender} not defined (yet) when " .
130 __PACKAGE__ . " needed it";
131 }
132  
133 8 $self->{app} = $appender;
134 }
135  
136 ###########################################
137 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod undocumented
138 ###########################################
139 0 my($self) = @_;
140  
141 0 my $pdata = [$self->{buffer}, $self->{sent_last}];
142  
143 # Save the buffer if we're in persistent mode
144 store $pdata, $self->{persistent} or
145 0 ▶ die "Cannot save messages in $self->{persistent} ($!)";
Branch: 1 branch
BranchTrueFalseTotal
store($pdata, $self->{"persistent"}) or die qq[Cannot save messages in $self->{"persistent"} ($!)] 0 0 0
146 }
147  
148 ###########################################
149 0 ▶sub restore {
Subroutine restore: not called CC=3 SCAR=24.8
Pod undocumented
150 ###########################################
151 0 my($self) = @_;
152  
153 0 ▶ if(-f $self->{persistent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"persistent"}) 0 0 0
154 my $pdata = retrieve $self->{persistent} or
155 0 ▶ die "Cannot retrieve messages from $self->{persistent} ($!)";
Branch: 1 branch
BranchTrueFalseTotal
my $pdata = retrieve($self->{"persistent"}) or die qq[Cannot retrieve messages from $self->{"persistent"} ($!)] 0 0 0
156 0 ($self->{buffer}, $self->{sent_last}) = @$pdata;
157 }
158 }
159  
160 ###########################################
161 14 ▶sub flush {
Subroutine flush: called CC=3 SCAR=11.0
Pod undocumented
162 ###########################################
163 14 my($self) = @_;
164  
165 # Log pending messages if we have any
166 14 for(@{$self->{buffer}}) {
167 3 $self->{app}->SUPER::log_cached($_);
168 }
169  
170 # call flush() on the attached appender if so desired.
171 14 ▶ if( $self->{appender_method_on_flush} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"appender_method_on_flush"}) 2 12 14
172 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
173 2 my $method = $self->{appender_method_on_flush};
174 2 $self->{app}->$method();
175 }
176  
177 # Empty buffer
178 14 $self->{buffer} = [];
179 }
180  
181 ###########################################
182 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
183 ###########################################
184 1 my($self) = @_;
185  
186 }
187  
188 1 1;
189  
190 __END__
« Log4perl/JavaMap/JDBCAppender.pm ↑ Summary Log4perl/Logger.pm »