| 1 | package Log::Log4perl::Appender::TestBuffer; | ||||||||||||||||||||
| 2 | 30 | our @ISA = qw(Log::Log4perl::Appender); | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | ################################################## | ||||||||||||||||||||
| 5 | # Log dispatcher writing to a string buffer | ||||||||||||||||||||
| 6 | # For testing. | ||||||||||||||||||||
| 7 | # This is like having a Log::Log4perl::Appender::TestBuffer | ||||||||||||||||||||
| 8 | ################################################## | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 30 | our %POPULATION = (); | |||||||||||||||||||
| 11 | 30 | our $LOG_PRIORITY = 0; | |||||||||||||||||||
| 12 | 30 | our $DESTROY_MESSAGES = ""; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | ################################################## | ||||||||||||||||||||
| 15 | 110 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 16 | ################################################## | ||||||||||||||||||||
| 17 | 110 | my $proto = shift; | |||||||||||||||||||
| 18 | 110 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 19 | 110 | my %params = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 110 | my $self = { | |||||||||||||||||||
| 22 | name => "unknown name", | ||||||||||||||||||||
| 23 | %params, | ||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 110 | bless $self, $class; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 110 | ▶ | $self->{stderr} = exists $params{stderr} ? $params{stderr} : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 110 | $self->{buffer} = ""; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 110 | $POPULATION{$self->{name}} = $self; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 110 | return $self; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | ################################################## | ||||||||||||||||||||
| 37 | 201 | ▶ | sub log { | ||||||||||||||||||
|
Subroutine
log: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||
| 38 | ################################################## | ||||||||||||||||||||
| 39 | 201 | my $self = shift; | |||||||||||||||||||
| 40 | 201 | my %params = @_; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 201 | ▶ | if( !defined $params{level} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | die "No level defined in log() call of " . __PACKAGE__; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 201 | ▶ | $self->{buffer} .= "[$params{level}]: " if $LOG_PRIORITY; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 201 | $self->{buffer} .= $params{message}; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | ########################################### | ||||||||||||||||||||
| 50 | 2 | ▶ | sub clear { | ||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 51 | ########################################### | ||||||||||||||||||||
| 52 | 2 | my($self) = @_; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 2 | $self->{buffer} = ""; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | ################################################## | ||||||||||||||||||||
| 58 | 292 | ▶ | sub buffer { | ||||||||||||||||||
|
Subroutine
buffer: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 59 | ################################################## | ||||||||||||||||||||
| 60 | 292 | my($self, $new) = @_; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 292 | ▶ | if(defined $new) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 98 | $self->{buffer} = $new; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 292 | return $self->{buffer}; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | ################################################## | ||||||||||||||||||||
| 70 | 63 | ▶ | sub reset { | ||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 71 | ################################################## | ||||||||||||||||||||
| 72 | 63 | my($self) = @_; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 63 | %POPULATION = (); | |||||||||||||||||||
| 75 | 63 | $self->{buffer} = ""; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | ################################################## | ||||||||||||||||||||
| 79 | 70 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 80 | ################################################## | ||||||||||||||||||||
| 81 | 70 | my($self) = @_; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 70 | $DESTROY_MESSAGES .= __PACKAGE__ . " destroyed"; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | #this delete() along with &reset() above was causing | ||||||||||||||||||||
| 86 | #Attempt to free unreferenced scalar at | ||||||||||||||||||||
| 87 | #blib/lib/Log/Log4perl/TestBuffer.pm line 69. | ||||||||||||||||||||
| 88 | #delete $POPULATION{$self->name}; | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | ################################################## | ||||||||||||||||||||
| 92 | 92 | ▶ | sub by_name { | ||||||||||||||||||
|
Subroutine
by_name: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 93 | ################################################## | ||||||||||||||||||||
| 94 | 92 | my($self, $name) = @_; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # Return a TestBuffer by appender name. This is useful if | ||||||||||||||||||||
| 97 | # test buffers are created behind our back (e.g. via the | ||||||||||||||||||||
| 98 | # Log4perl config file) and later on we want to | ||||||||||||||||||||
| 99 | # retrieve an instance to query its content. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 92 | ▶ | die "No name given" unless defined $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 92 | return $POPULATION{$name}; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 30 | 1; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||