File Coverage

blib/lib/Apache/Voodoo/Debug/Log4perl.pm
Criterion Covered Total %
statement 28 145 19.3
branch 4 24 16.6
condition 0 8 0.0
subroutine 7 32 21.8
pod 0 18 0.0
total 39 227 17.1


line stmt bran cond sub pod time code
1             package Apache::Voodoo::Debug::Log4perl;
2              
3             $VERSION = "3.0200";
4              
5 2     2   68665 use strict;
  2         5  
  2         310  
6 2     2   14 use warnings;
  2         5  
  2         78  
7              
8 2     2   11 use base("Apache::Voodoo::Debug::Common");
  2         4  
  2         221  
9              
10 2     2   13 use File::Spec;
  2         4  
  2         48  
11 2     2   2178 use Log::Log4perl;
  2         61840  
  2         12  
12 2     2   100 use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Indent = 1;
  2         3  
  2         3864  
13              
14             $Log::Log4perl::caller_depth = 3;
15              
16             #
17             # Since log4perl wants to use one config file for the whole running perl program (one
18             # call to init), and # ApacheVoodo lets you define logging per application (multiple inits).
19             # We're using a singleton to get around that. We append each config block to a hash and
20             # then init log4perl after the all the apps are loaded. Kinda ugly, but until log4perl supports
21             # multiple configs, then it's what we're stuck with.
22             #
23             our $self;
24              
25             sub new {
26 3     3 0 10 my $class = shift;
27 3         6 my $id = shift;
28 3         7 my $conf = shift;
29              
30 3 100       15 unless (ref($self)) {
31 1         2 $self = {};
32 1         3 $self->{conf} = {};
33 1         4 bless($self,$class);
34             }
35              
36 3 50       21 if (ref($conf) eq "HASH") {
    50          
37 0         0 foreach (keys %{$conf}) {
  0         0  
38 0         0 $self->{conf}->{$_} = $conf->{$_};
39             }
40             }
41             elsif (!ref($conf)) {
42 3         17 $self->{v_file} = $conf;
43             }
44              
45 3         10 return $self;
46             }
47              
48             sub bootstrapped {
49 0     0 0   my $self = shift;
50              
51 0 0         unless (Log::Log4perl->initialized()) {
52 0           my $conf;
53 0 0         if ($self->{v_file}) {
54 0 0         if (open(F,$self->{v_file})) {
55 0           local $/ = undef;
56 0           $conf = ;
57 0           $conf .= "\n";
58 0           close(F);
59             }
60             else {
61 0           warn $!
62             }
63             }
64 0           foreach (keys %{$self->{conf}}) {
  0            
65 0           $conf .= $_ .' = '.$self->{conf}->{$_}."\n";
66             }
67              
68 0           Log::Log4perl->init_once(\$conf);
69             }
70             }
71              
72             sub enabled {
73 0     0 0   return 1;
74             }
75              
76              
77 0     0 0   sub debug { my $self = shift; $self->_get_logger->debug($self->_dumper(@_)); }
  0            
78 0     0 0   sub info { my $self = shift; $self->_get_logger->info( $self->_dumper(@_)); }
  0            
79 0     0 0   sub warn { my $self = shift; $self->_get_logger->warn( $self->_dumper(@_)); }
  0            
80 0     0 0   sub error { my $self = shift; $self->_get_logger->error($self->_dumper(@_)); }
  0            
81 0     0 0   sub exception { my $self = shift; $self->_get_logger->fatal($self->_dumper(@_)); }
  0            
82              
83 0     0 0   sub trace { my $self = shift; $self->_get_logger->trace($self->_dump_trace(@_)); }
  0            
84 0     0 0   sub table { my $self = shift; $self->_get_logger->debug($self->_dump_table(@_)); }
  0            
85              
86 0     0 0   sub return_data { my $self = shift; $self->_get_logger('ReturnData' )->trace($self->_dumper(@_)); }
  0            
87 0     0 0   sub url { my $self = shift; $self->_get_logger('Url' )->trace($self->_dumper(@_)); }
  0            
88 0     0 0   sub status { my $self = shift; $self->_get_logger('Status' )->trace($self->_dumper(@_)); }
  0            
89 0     0 0   sub params { my $self = shift; $self->_get_logger('Params' )->trace($self->_dumper(@_)); }
  0            
90 0     0 0   sub template_conf { my $self = shift; $self->_get_logger('TemplateConf')->trace($self->_dumper(@_)); }
  0            
91 0     0 0   sub session { my $self = shift; $self->_get_logger('Session' )->trace($self->_dumper(@_)); }
  0            
92              
93             sub mark {
94 0     0 0   my $self = shift;
95              
96 0           push(@{$self->{profile}},[@_]);
  0            
97             }
98              
99             sub shutdown {
100 0     0 0   my $self = shift;
101              
102 0           my @d = @{$self->{profile}};
  0            
103 0           my $last = $#d;
104 0 0         if ($last > 0) {
105 0           my $total_time = $d[$last]->[0] - $d[0]->[0];
106              
107 0           my @return = map {
108 0           [
109             sprintf("%.5f", $d[$_]->[0] - $d[$_-1]->[0]),
110             sprintf("%5.2f%%",($d[$_]->[0] - $d[$_-1]->[0])/$total_time*100),
111             $d[$_]->[1]
112             ]
113             } (1 .. $last);
114              
115 0           unshift(@return, [
116             sprintf("%.5f",$total_time),
117             'percent',
118             'message'
119             ]);
120              
121 0           my $logger = $self->_get_logger("Profile");
122 0           $logger->debug($self->_dump_table("Profile",\@return));
123             }
124              
125 0           delete $self->{profile};
126             }
127              
128             sub _dumper {
129 0     0     my $self = shift;
130 0           my @data = @_;
131             return sub {
132 0 0 0 0     if (scalar(@data) > 1 || ref($data[0])) {
133             # if there's more than one item, or the item we have is a reference
134             # then we need to serialize it.
135 0           return Dumper \@data;
136             }
137             else {
138 0           return $data[0];
139             }
140 0           };
141             }
142              
143             sub _get_logger {
144 0     0     my $self = shift;
145 0           my $section = shift;
146              
147 0 0         if ($section) {
148 0           return Log::Log4perl->get_logger("Apache::Voodoo::".$section);
149             }
150             else {
151 0           my @stack = $self->stack_trace();
152 0 0         if (scalar(@stack)) {
153 0           return Log::Log4perl->get_logger($stack[-1]->{class});
154             }
155             else {
156 0           return Log::Log4perl->get_logger("Apache::Voodoo");
157             }
158             }
159             }
160              
161             sub _dump_table {
162 0     0     my $s = shift;
163 0           my @data = @_;
164              
165             return sub {
166 0     0     my $self = $s;
167 0           my $name = "Table";
168 0 0         if (scalar(@data) > 1) {
169 0           $name = shift @data;
170             }
171              
172 0           return "\n$name\n" . $self->_mk_table(@{$data[0]});
  0            
173 0           };
174             }
175              
176             sub _dump_trace {
177 0     0     my $s = shift;
178 0           my $n = shift;
179 0           my $t = [$s->stack_trace()];
180              
181             return sub {
182 0     0     my $self = $s;
183 0           my $trace = $t;
184              
185 0   0       my $name = ($n || "Trace");
186 0           my @data = map {
187 0           [
188             $_->{class},
189             $_->{function},
190             $_->{line},
191             ]
192 0           } @{$trace};
193              
194 0           unshift(@data,['Class','Subroutine','Line']);
195 0           return "\n$name\n".$self->_mk_table(@data);
196 0           };
197             }
198              
199             sub _mk_table {
200 0     0     my $self = shift;
201 0           my @data = @_;
202              
203 0           my @col;
204             # find the widest element in each column
205 0           foreach my $row (@data) {
206 0           for (my $i=0; $i < scalar(@{$row}); $i++) {
  0            
207 0 0 0       if (!defined($col[$i]) || length($row->[$i]) > $col[$i]) {
208 0           $col[$i] = length($row->[$i]);
209             }
210             }
211             }
212              
213 0           my $t_width = 2; # "| "
214 0           foreach (@col) {
215 0           $t_width += $_ + 3; # " | "
216             }
217 0           $t_width -= 1; # "| " -> "|"
218              
219 0           my @return;
220 0           push(@return,'-' x $t_width);
221 0           foreach my $row (@data) {
222 0           my $line = "| ";
223 0           for (my $i=0; $i < scalar(@{$row}); $i++) {
  0            
224 0           $line .= sprintf("%-".$col[$i]."s",$row->[$i]) . " | ";
225             }
226 0           $line =~ s/ $//;
227 0           push (@return,$line);
228 0           push(@return,'-' x $t_width);
229             }
230 0           return join("\n",@return);
231             }
232              
233             1;
234              
235             ################################################################################
236             # Copyright (c) 2005-2010 Steven Edwards (maverick@smurfbane.org).
237             # All rights reserved.
238             #
239             # You may use and distribute Apache::Voodoo under the terms described in the
240             # LICENSE file include in this package. The summary is it's a legalese version
241             # of the Artistic License :)
242             #
243             ################################################################################