File Coverage

blib/lib/Log/Log4perl/Appender/TestBuffer.pm
Criterion Covered Total %
statement 28 29 96.5
branch 7 10 70.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 6 16.6
total 44 55 80.0


line stmt bran cond sub pod time code
1             our @ISA = qw(Log::Log4perl::Appender);
2              
3             ##################################################
4             # Log dispatcher writing to a string buffer
5             # For testing.
6             # This is like having a Log::Log4perl::Appender::TestBuffer
7             ##################################################
8              
9             our %POPULATION = ();
10             our $LOG_PRIORITY = 0;
11             our $DESTROY_MESSAGES = "";
12              
13             ##################################################
14             ##################################################
15             my $proto = shift;
16             my $class = ref $proto || $proto;
17 130     130 1 241 my %params = @_;
18 130   33     496  
19 130         404 my $self = {
20             name => "unknown name",
21 130         552 %params,
22             };
23              
24             bless $self, $class;
25              
26 130         303 $self->{stderr} = exists $params{stderr} ? $params{stderr} : 1;
27             $self->{buffer} = "";
28 130 50       460  
29 130         243 $POPULATION{$self->{name}} = $self;
30              
31 130         362 return $self;
32             }
33 130         539  
34             ##################################################
35             ##################################################
36             my $self = shift;
37             my %params = @_;
38              
39 298     298 0 448 if( !defined $params{level} ) {
40 298         971 die "No level defined in log() call of " . __PACKAGE__;
41             }
42 298 50       671 $self->{buffer} .= "[$params{level}]: " if $LOG_PRIORITY;
43 0         0 $self->{buffer} .= $params{message};
44             }
45 298 100       576  
46 298         961 ###########################################
47             ###########################################
48             my($self) = @_;
49              
50             $self->{buffer} = "";
51             }
52 3     3 0 546  
53             ##################################################
54 3         9 ##################################################
55             my($self, $new) = @_;
56              
57             if(defined $new) {
58             $self->{buffer} = $new;
59             }
60 387     387 0 4074  
61             return $self->{buffer};
62 387 100       739 }
63 127         206  
64             ##################################################
65             ##################################################
66 387         2210 my($self) = @_;
67              
68             %POPULATION = ();
69             $self->{buffer} = "";
70             }
71              
72 65     65 0 10798 ##################################################
73             ##################################################
74 65         133 my($self) = @_;
75 65         259  
76             $DESTROY_MESSAGES .= __PACKAGE__ . " destroyed";
77              
78             #this delete() along with &reset() above was causing
79             #Attempt to free unreferenced scalar at
80             #blib/lib/Log/Log4perl/TestBuffer.pm line 69.
81 77     77   4248 #delete $POPULATION{$self->name};
82             }
83 77         872  
84             ##################################################
85             ##################################################
86             my($self, $name) = @_;
87              
88             # Return a TestBuffer by appender name. This is useful if
89             # test buffers are created behind our back (e.g. via the
90             # Log4perl config file) and later on we want to
91             # retrieve an instance to query its content.
92              
93             die "No name given" unless defined $name;
94 113     113 0 2336  
95             return $POPULATION{$name};
96              
97             }
98              
99             1;
100              
101 113 50       252  
102             =encoding utf8
103 113         379  
104             =head1 NAME
105              
106             Log::Log4perl::Appender::TestBuffer - Appender class for testing
107              
108             =head1 SYNOPSIS
109              
110             use Log::Log4perl::Appender::TestBuffer;
111              
112             my $appender = Log::Log4perl::Appender::TestBuffer->new(
113             name => 'mybuffer',
114             );
115              
116             # Append to the buffer
117             $appender->log(
118             level = > 'alert',
119             message => "I'm searching the city for sci-fi wasabi\n"
120             );
121              
122             # Retrieve the result
123             my $result = $appender->buffer();
124              
125             # Clear the buffer to the empty string
126             $appender->clear();
127              
128             =head1 DESCRIPTION
129              
130             This class is used for internal testing of C<Log::Log4perl>. It
131             is a C<Log::Dispatch>-style appender, which writes to a buffer
132             in memory, from where actual results can be easily retrieved later
133             to compare with expected results.
134              
135             Every buffer created is stored in an internal global array, and can
136             later be referenced by name:
137              
138             my $app = Log::Log4perl::Appender::TestBuffer->by_name("mybuffer");
139              
140             retrieves the appender object of a previously created buffer "mybuffer".
141             To reset this global array and have it forget all of the previously
142             created testbuffer appenders (external references to those appenders
143             nonwithstanding), use
144              
145             Log::Log4perl::Appender::TestBuffer->reset();
146              
147             =head1 SEE ALSO
148              
149             =head1 LICENSE
150              
151             Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt>
152             and Kevin Goess E<lt>cpan@goess.orgE<gt>.
153              
154             This library is free software; you can redistribute it and/or modify
155             it under the same terms as Perl itself.
156              
157             =head1 AUTHOR
158              
159             Please contribute patches to the project on Github:
160              
161             http://github.com/mschilli/log4perl
162              
163             Send bug reports or requests for enhancements to the authors via our
164              
165             MAILING LIST (questions, bug reports, suggestions/patches):
166             log4perl-devel@lists.sourceforge.net
167              
168             Authors (please contact them via the list above, not directly):
169             Mike Schilli <m@perlmeister.com>,
170             Kevin Goess <cpan@goess.org>
171              
172             Contributors (in alphabetical order):
173             Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton
174             Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony
175             Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy
176             Grundman, Paul Harrington, Alexander Hartmaier David Hull,
177             Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter,
178             Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope,
179             Lars Thegler, David Viner, Mac Yang.
180