XUL/Logging.pm

filter: Statementstmt 90.6% 136 / 150 Branchbran 75.0% 54 / 72 Conditioncond 68.8% 31 / 45 MC/DCmcdc 40.0% 10 / 25 Subroutinesub 95.0% 19 / 20 Podpod 33.3% 4 / 12 totaltotal 78.4% 254 / 324 CC 57 SCAR 20.0
CC
57
Cov
83%
default
13.8
new
10.5
dispatch
9.2
CRAP 7.4
« XUL/Encode.pm ↑ Summary XUL/Controler.pm »
1 package POE::XUL::Logging;
2 # $Id: Logging.pm 1566 2010-11-03 03:13:32Z fil $
3 # Copyright Philip Gwyn 2007-2010. All rights reserved.
4 #
5 # Handle logging features for the application
6 #
7  
8 20 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use Scalar::Util qw( reftype blessed openhandle );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 20 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 20 our $VERSION = '0.0600';
18  
19 20 require Exporter;
20 20 our @ISA = qw( Exporter );
21  
22 20 our @EXPORT_OK = qw( xwarn xlog xdebug xcarp xcarp2 );
23 20 our @EXPORT = @EXPORT_OK;
24  
25 20 our $SINGLETON;
26  
27 # To interface with log4perl
28 20 my %type2level = (
29 DEBUG => 10000,
30 LOG => 20000,
31 REQ => 20000,
32 WARN => 30000,
33 SETUP => 42000
34 );
35  
36 ############################################################
37 sub new
38 {
39 6 ▶ my( $package, $args, $log_root ) = @_;
Subroutine new: called CC=10 SCAR=23.5
Pod undocumented
40  
41 6 my $rt = reftype $args;
42 6 ▶ if( $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 6 0 6
43 6 ▶ if( !$rt or $rt eq 'CODE' or $rt eq 'HASH' or blessed $args ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $rt or $rt eq 'CODE' or $rt eq 'HASH' or builtin::blessed($args)) { } 5 1 6
elsif ($rt eq 'ARRAY') { } 1 0 1
Condition88%8/9
not $rt or $rt eq "CODE" or $rt eq "HASH" or builtin::blessed($args)
l!l&&r!l&&!r
411
not $rt or $rt eq "CODE" or $rt eq "HASH"
l!l&&r!l&&!r
132
not $rt or $rt eq "CODE"
l!l&&r!l&&!r
105
MC/DC75%3/4
not $rt or $rt eq "CODE" or $rt eq "HASH" or builtin::blessed($args)
not $rt $rt eq "CODE" $rt eq "HASH" builtin::blessed($args)
Truth table80%4/5
not(A) or B or C or D
A= not $rtB= $rt eq "CODE"C= $rt eq "HASH"D= builtin::blessed($args)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
44 # ok
45 }
46 elsif( $rt eq 'ARRAY' ) {
47 1 ▶ if( 2 > @$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 > @$args) 0 1 1
48 0 croak "logging parameter must have at least 2 elements";
49 }
50 }
51 else {
52 0 croak "logging parameter must be a CODE ref, ARRAY ref, scalar or a log4perl object";
53 }
54 }
55  
56 6 my $self = $SINGLETON =
57 bless { logger=>$args, log_root=>$log_root }, $package;
58  
59 6 ▶ if( $rt and $rt eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt and $rt eq "HASH") 3 3 6
Condition100%3/3
$rt and $rt eq "HASH"
!ll&&!rl&&r
123
MC/DC100%2/2
$rt and $rt eq "HASH"
$rt $rt eq "HASH"
Truth table100%3/3
A and B
A= $rtB= $rt eq "HASH"
ABresult
0X0
100
111
60 3 $self->{logger} = $args->{logger};
61 3 $self->{access_log} = $args->{access_log};
62 3 $self->{error_log} = $args->{error_log};
63 3 $self->__init_apps( $args->{apps} );
64 }
65  
66 6 return $SINGLETON;
67 }
68  
69 sub __init_apps
70 {
71 3 ▶ my( $self, $apps ) = @_;
Subroutine __init_apps: called CC=8 SCAR=21.4
72 3 ▶ unless( $apps ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($apps) 2 1 3
73 2 $self->{apps} = [];
74 2 return;
75 }
76 1 ▶ unless( ref $apps ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $apps) { } 1 0 1
(do { "ARRAY" eq ref $apps }) 0 1 1
77 0 $apps = { $apps=>$apps };
78 }
79 0 elsif( 'ARRAY' eq ref $apps ) {
80 0 my %A;
81 0 @A{@$apps} = @$apps;
82 0 $apps = \%A;
83 }
84 1 $self->{apps} = [];
85 1 ▶ while( my( $app, $def ) = each %$apps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$apps) 2 1 3
86 2 push @{ $self->{apps} }, $app;
87 2 foreach my $t ( qw( access error ) ) {
88 4 my $log = "${t}_log";
89 4 my $file;
90 4 ▶ unless( ref $def ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $def) { } 2 2 4
if (do { $def->{$log} }) { } 1 1 2
91 2 $file = File::Spec->catfile( $def, $log );
92 }
93 elsif( $def->{$log} ) {
94 1 $file = $def->{$log};
95 }
96 else {
97 1 $file = File::Spec->catfile( $app, $log );
98 }
99 4 $self->{"$app-$t-log"} = $file;
100 }
101 }
102 1 return;
103 }
104  
105 ############################################################
106 sub setup
107 {
108 5 ▶ my( $self ) = @_;
Subroutine setup: called CC=2 SCAR=6.9
Pod undocumented
109  
110 5 ▶ $self->{logger} ||= \&default_sub;
Condition100%2/2
$self->{"logger"} ||= \&POE::XUL::Logging::default_sub
l!l
41
MC/DC100%1/1
$self->{"logger"} ||= \&POE::XUL::Logging::default_sub
$self->{"logger"}
Truth table100%2/2
A
A= $self->{"logger"}
Aresult
11
00
111 $self->dispatch( { type => 'SETUP',
112 directory => $self->{log_root}
113 5 } );
114 }
115  
116 ############################################################
117 # Dispatch the exception
118 sub dispatch
119 {
120 31 ▶ my( $self, $exception ) = @_;
Subroutine dispatch: called CC=9 SCAR=22.2
Pod undocumented
121 31 ▶ $self = $SINGLETON unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 31 31
122  
123 31 ▶ $exception = { message => $exception, type => 'LOG' }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $exception 0 31 31
124 unless ref $exception;
125  
126 31 my $rt = reftype $self->{logger};
127 31 ▶ if( blessed $self->{logger} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($self->{'logger'})) { } 6 25 31
elsif (not $rt) { } 5 20 25
elsif ($rt eq 'ARRAY') { } 5 15 20
elsif ($rt eq 'CODE') { } 15 0 15
128 6 ▶ return if $exception->{type} eq 'SETUP';
Branch: 1 branch
BranchTrueFalseTotal
if $exception->{"type"} eq "SETUP" 1 5 6
129 5 my $lvl = $type2level{ $exception->{type} };
130 5 ▶ $lvl ||= $type2level{ 'LOG' };
Condition66%2/3
$lvl ||= $type2level{"LOG"}
l!l&&r!l&&!r
410
MC/DC0%0/2
$lvl ||= $type2level{"LOG"}
$lvl $type2level{"LOG"}
Truth table66%2/3
A ||= B
A= $lvlB= $type2level{"LOG"}
ABresult
1X1
011
000
131 5 $self->{logger}->log( $lvl, $exception->{message} );
132 }
133 elsif( not $rt ) {
134 5 $POE::Kernel::poe_kernel->call( $self->{logger}, 'log', $exception );
135 }
136 elsif( $rt eq 'ARRAY' ) {
137 # warn "POE logger @{ $self->{logger} }";
138 5 $POE::Kernel::poe_kernel->call( @{ $self->{logger} }, $exception );
139 }
140 elsif( $rt eq 'CODE' ) {
141 15 $self->{logger}->( $exception );
142 }
143 }
144  
145 ############################################################
146 sub default_sub
147 {
148 9 ▶ my( $ex ) = @_;
Subroutine default_sub: called CC=5 SCAR=16.2
Pod undocumented
149 9 ▶ $ex->{type} ||= '';
Condition50%1/2
$ex->{"type"} ||= ""
l!l
90
MC/DC0%0/1
$ex->{"type"} ||= ""
$ex->{"type"}
Truth table50%1/2
A
A= $ex->{"type"}
Aresult
11
00
150  
151 9 ▶ if( $ex->{type} eq 'SETUP' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ex->{"type"} eq "SETUP") 1 8 9
152 1 $SINGLETON->default_setup;
153 1 return;
154 }
155  
156 8 ▶ if( $ex->{type} and $ex->{type} ne 'REQ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ex->{"type"} and $ex->{"type"} ne "REQ") 6 2 8
Condition66%2/3
$ex->{"type"} and $ex->{"type"} ne "REQ"
!ll&&!rl&&r
026
MC/DC50%1/2
$ex->{"type"} and $ex->{"type"} ne "REQ"
$ex->{"type"} $ex->{"type"} ne "REQ"
Truth table66%2/3
A and B
A= $ex->{"type"}B= $ex->{"type"} ne "REQ"
ABresult
0X0
100
111
157 6 $ex->{message} = "$ex->{type} $ex->{message}";
158 }
159 8 $SINGLETON->default( @_ )
160 }
161  
162  
163 ############################################################
164 sub default_setup
165 {
166 1 ▶ my( $self ) = @_;
Subroutine default_setup: called CC=5 SCAR=16.4
Pod undocumented
167 1 ▶ $self = $SINGLETON unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 1 1
168  
169 1 $self->{stderr_fh} = $self->open_file( qw( error_log error_log ) );
170 1 $self->{error_fh} = $self->{stderr_fh};
171 1 $self->{log_fh} = $self->open_file( qw( access_log access_log ) );
172 1 $self->{access_fh} = $self->{log_fh};
173 1 foreach my $app ( @{ $self->{apps} } ) {
174 2 foreach my $t ( qw( error access ) ) {
175 4 ▶ if( $self->{"$app-$t-log"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"$app-$t-log"}) { } 4 0 4
176 4 $self->{"$app-$t-fh"} =
177 $self->open_file( "$app-$t-log", "$app/${t}_log" );
178 }
179 else {
180 0 $self->{"$app-$t-fh"} = $self->{"${t}_fh"};
181 }
182 }
183 }
184 }
185  
186 ############################################################
187 sub open_file
188 {
189 6 ▶ my( $self, $key, $name ) = @_;
Subroutine open_file: called CC=6 SCAR=18.6
Pod undocumented
190  
191 6 my $file = $self->{$key};
192 6 ▶ $file ||= File::Spec->catfile( $self->{log_root}, $name );
Condition33%1/3
$file ||= "File::Spec"->catfile($self->{"log_root"}, $name)
l!l&&r!l&&!r
600
MC/DC0%0/2
$file ||= "File::Spec"->catfile($self->{"log_root"}, $name)
$file "File::Spec"->catfile($self->{"log_root"}, $name)
Truth table33%1/3
A ||= B
A= $fileB= "File::Spec"->catfile($self->{"log_root"}, $name)
ABresult
1X1
011
000
193 6 ▶ unless( File::Spec->file_name_is_absolute( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 4 2 6
194 4 $file = File::Spec->catfile( $self->{log_root}, $file );
195 4 $self->{$key} = $file;
196 }
197  
198 6 my( $vol, $dir, $f ) = File::Spec->splitpath( $file );
199  
200 6 ▶ if( $dir and not -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and not -d $dir) 3 3 6
Condition66%2/3
$dir and not -d $dir
!ll&&!rl&&r
033
MC/DC50%1/2
$dir and not -d $dir
$dir not -d $dir
Truth table66%2/3
A and not(B)
A= $dirB= not -d $dir
ABresult
0X0
100
111
201 3 File::Path::mkpath( [ $dir ], 0, 0750 );
202 }
203  
204 6 my $fh = IO::File->new;
205 6 ▶ unless( $fh->open(">> $file") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open(">> $file")) 0 6 6
206 0 warn "AUGH $file: $!";
207 0 die "Unable to create log file $file: $!";
208 }
209 6 $fh->autoflush(1);
210 6 return $fh;
211 }
212  
213  
214  
215 ############################################################
216 sub default
217 {
218 8 ▶ my( $self, $exception ) = @_;
Subroutine default: called CC=12 SCAR=26.2
Pod undocumented
219 8 ▶ $self = $SINGLETON unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 8 8
220  
221 8 ▶ my $type = $exception->{type}||'';
Condition50%1/2
$exception->{'type'} || ''
l!l
80
MC/DC0%0/1
$exception->{'type'} || ''
$exception->{'type'}
Truth table50%1/2
A
A= $exception->{'type'}
Aresult
11
00
222 8 my $msg = $exception->{message};
223 8 ▶ $msg = '' unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 8 8
224 8 ▶ $msg =~ s/\n+$// if $exception->{location};
Branch: 1 branch
BranchTrueFalseTotal
if $exception->{"location"} 1 7 8
225 8 ▶ if( $msg !~ /\n$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg =~ /\n$/) 6 2 8
226 $msg .= " at $exception->{caller}[1] line $exception->{caller}[2]"
227 6 ▶ if $exception->{caller};
Branch: 1 branch
BranchTrueFalseTotal
if $exception->{"caller"} 6 0 6
228 6 $msg .= "\n";
229 }
230
231 8 ▶ my $app = $self->{app}||'THERE-IS-NO-APP';
Condition100%2/2
$self->{'app'} || 'THERE-IS-NO-APP'
l!l
44
MC/DC100%1/1
$self->{'app'} || 'THERE-IS-NO-APP'
$self->{'app'}
Truth table100%2/2
A
A= $self->{'app'}
Aresult
11
00
232 8 ▶ my $t = $type eq 'REQ' ? 'access' : 'error';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'REQ' ? : 2 6 8
233 8 ▶ my $fh = $self->{"$app-$t-fh"} || $self->{"${t}_fh"} || $self->{stderr_fh};
Condition50%3/6
$$self{"$app-$t-fh"} || $$self{"${t}_fh"} || $self->{'stderr_fh'}
l!l&&r!l&&!r
800
$$self{"$app-$t-fh"} or $$self{"${t}_fh"}
l!l&&r!l&&!r
440
MC/DC0%0/3
$$self{"$app-$t-fh"} || $$self{"${t}_fh"} || $self->{'stderr_fh'}
$$self{"$app-$t-fh"} $$self{"${t}_fh"} $self->{'stderr_fh'}
Truth table50%2/4
A or B || C
A= $$self{"$app-$t-fh"}B= $$self{"${t}_fh"}C= $self->{'stderr_fh'}
ABCresult
1XX1
01X1
0011
0000
234 8 ▶ if( $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) { } 8 0 8
235 8 $fh->print( $msg );
236 }
237 else {
238 0 print STDERR $msg;
239 }
240 }
241  
242  
243  
244 ############################################################
245 sub __mk_exception
246 {
247 22 ▶ my( $package, $type, $level, @msg ) = @_;
Subroutine __mk_exception: called CC=2 SCAR=7.0
248  
249 22 local $, = $,;
250 22 ▶ $, = '' unless defined $,;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $, 22 0 22
251 return {
252 type => $type,
253 22 message => join( $,, grep {defined} @msg),
254 caller => [ caller( $level ) ]
255 };
256 }
257  
258 sub xdebug
259 {
260 5 ▶ return carp join( '' , @_ ) unless $SINGLETON;
Subroutine xdebug: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $POE::XUL::Logging::SINGLETON 0 5 5
261 5 $SINGLETON->dispatch( $SINGLETON->__mk_exception( 'DEBUG', 1, @_ ) );
262 }
263  
264 sub xwarn
265 {
266 7 ▶ return carp join( '' , @_ ) unless $SINGLETON;
Subroutine xwarn: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $POE::XUL::Logging::SINGLETON 0 7 7
267 7 $SINGLETON->dispatch( $SINGLETON->__mk_exception( 'WARN', 1, @_ ) );
268 }
269  
270 sub xcarp
271 {
272 5 ▶ my $ex = $SINGLETON->__mk_exception( 'WARN', 2, @_ );
Subroutine xcarp: called CC=1 SCAR=0.0
Pod documented
273 5 $ex->{location} = 1;
274 5 $SINGLETON->dispatch( $ex );
275 }
276  
277 sub xcarp2
278 {
279 0 ▶ my $ex = $SINGLETON->__mk_exception( 'WARN', 3, @_ );
Subroutine xcarp2: not called CC=1 SCAR=6.9
Pod undocumented
280 0 $ex->{location} = 1;
281 0 $SINGLETON->dispatch( $ex );
282 }
283  
284 sub xlog
285 {
286 9 ▶ my $ex;
Subroutine xlog: called CC=5 SCAR=16.4
Pod documented
287 9 ▶ if( 1==@_ and 'HASH' eq ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_ and 'HASH' eq ref $_[0]) { } 4 5 9
Condition66%2/3
1 == @_ and "HASH" eq ref $_[0]
!ll&&!rl&&r
054
MC/DC50%1/2
1 == @_ and "HASH" eq ref $_[0]
1 == @_ "HASH" eq ref $_[0]
Truth table66%2/3
A and B
A= 1 == @_B= "HASH" eq ref $_[0]
ABresult
0X0
100
111
288 4 $ex = $_[0];
289 4 ▶ $ex->{type} ||= 'LOG';
Condition50%1/2
$ex->{"type"} ||= "LOG"
l!l
40
MC/DC0%0/1
$ex->{"type"} ||= "LOG"
$ex->{"type"}
Truth table50%1/2
A
A= $ex->{"type"}
Aresult
11
00
290 4 ▶ $ex->{caller} ||= [ caller( 0 ) ];
Condition50%1/2
$ex->{"caller"} ||= [caller 0]
l!l
04
MC/DC0%0/1
$ex->{"caller"} ||= [caller 0]
$ex->{"caller"}
Truth table50%1/2
A
A= $ex->{"caller"}
Aresult
11
00
291 }
292 else {
293 5 $ex = $SINGLETON->__mk_exception( 'LOG', 1, @_ );
294 }
295 9 $SINGLETON->dispatch( $ex );
296 }
297  
298  
299  
300  
301 20 1;
302  
303 __DATA__
« XUL/Encode.pm ↑ Summary XUL/Controler.pm »