blib/lib/Catalyst/Plugin/Log/Dispatch.pm

filter: Statementstmt 19.4% 27 / 139 Branchbran 3.9% 3 / 76 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 56.2% 9 / 16 Podpod n/a totaltotal 14.3% 39 / 272 CC 51 SCAR 60.8
CC
51
Cov
12%
setup
930.0
__ANON__
72.0
__ANON__
56.0
CRAP 439.1
↑ Summary
1 package Catalyst::Plugin::Log::Dispatch;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.121';
7  
8 #use base 'Catalyst::Base';
9 1 ▶use vars qw/$HasTimePiece $HasTimeHiRes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=15.2
13 1 ▶ Log::Dispatch::Config->use or warn "$@\nIt moves without using Log::Dispatch::Config.\n";
Branch: 1 branch
BranchTrueFalseTotal
"Log::Dispatch::Config"->use or warn "$@\nIt moves without using Log::Dispatch::Config.\n" 0 1 1
14 1 ▶ $HasTimeHiRes = 1 if( Time::HiRes->use(qw/tv_interval/) );
Branch: 1 branch
BranchTrueFalseTotal
if "Time::HiRes"->use("tv_interval") 1 0 1
15 1 ▶ $HasTimePiece = 1 if( Time::Piece->use );
Branch: 1 branch
BranchTrueFalseTotal
if "Time::Piece"->use 1 0 1
16 };
17 $Catalyst::Plugin::Log::Dispatch::CallerDepth = 0;
18  
19 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 # Module implementation here
23  
24 0 ▶sub setup {
Subroutine setup: not called CC=30 SCAR=68.4
25 0 ▶ if( $Catalyst::VERSION >= 5.8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::VERSION >= 5.8) { } 0 0 0
26 0 ▶ MRO::Compat->use or die "can not use MRO::Compat : $@\n";
Branch: 1 branch
BranchTrueFalseTotal
"MRO::Compat"->use or die "can not use MRO::Compat : $@\n" 0 0 0
27 }
28 else {
29 0 ▶ NEXT->use or die "can not use NEXT : $@\n";
Branch: 1 branch
BranchTrueFalseTotal
"NEXT"->use or die "can not use NEXT : $@\n" 0 0 0
30 }
31 0 my $c = shift;
32 0 my $old_log = undef;
33 0 ▶ if ( $c->log and ref( $c->log ) eq 'Catalyst::Log' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->log and ref $c->log eq "Catalyst::Log") 0 0 0
Condition0%0/3
$c->log and ref $c->log eq "Catalyst::Log"
!ll&&!rl&&r
000
MC/DC0%0/2
$c->log and ref $c->log eq "Catalyst::Log"
$c->log ref $c->log eq "Catalyst::Log"
Truth table0%0/3
A and B
A= $c->logB= ref $c->log eq "Catalyst::Log"
ABresult
0X0
100
111
34 0 $old_log = $c->log;
35 }
36 0 $c->log( Catalyst::Plugin::Log::Dispatch::Backend->new );
37
38 #Make it an array with one element if its a hashref
39 0 ▶ if (ref ( $c->config->{'Log::Dispatch'} ) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->config->{"Log::Dispatch"} eq "HASH") 0 0 0
40 0 $c->config->{'Log::Dispatch'} = [ $c->config->{'Log::Dispatch'} ];
41 }
42
43 0 ▶ unless ( ref( $c->config->{'Log::Dispatch'} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $c->config->{"Log::Dispatch"} eq "ARRAY") 0 0 0
44 push(
45 0 @{ $c->config->{'Log::Dispatch'} },
46 { class => 'STDOUT',
47 name => 'default',
48 min_level => 'debug',
49 format => '[%p] %m%n'
50 }
51 );
52  
53 }
54 0 foreach my $tlogc ( @{ $c->config->{'Log::Dispatch'} } ) {
55 0 my %logc = %{$tlogc};
56 0 ▶ if ( $logc{'class'} eq 'STDOUT' or $logc{'class'} eq 'STDERR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logc{"class"} eq "STDOUT" or $logc{"class"} eq "STDERR") 0 0 0
Condition0%0/3
$logc{"class"} eq "STDOUT" or $logc{"class"} eq "STDERR"
l!l&&r!l&&!r
000
MC/DC0%0/2
$logc{"class"} eq "STDOUT" or $logc{"class"} eq "STDERR"
$logc{"class"} eq "STDOUT" $logc{"class"} eq "STDERR"
Truth table0%0/3
A or B
A= $logc{"class"} eq "STDOUT"B= $logc{"class"} eq "STDERR"
ABresult
1X1
011
000
57 0 my $io = IO::Handle->new;
58 0 $io->fdopen( fileno( $logc{'class'} ), 'w' );
59 0 $logc{'class'} = 'Handle';
60 0 $logc{'handle'} = $io;
61 }
62 0 my $class = sprintf( "Log::Dispatch::%s", $logc{'class'} );
63 0 delete $logc{'class'};
64 0 ▶ $logc{'callbacks'} = [$logc{'callbacks'}] if(ref($logc{'callbacks'}) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $logc{"callbacks"} eq "CODE" 0 0 0
65
66 0 ▶ if(exists $logc{'format'} and defined $Log::Dispatch::Config::CallerDepth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $logc{"format"} and defined $Log::Dispatch::Config::CallerDepth) 0 0 0
Condition0%0/3
exists $logc{"format"} and defined $Log::Dispatch::Config::CallerDepth
!ll&&!rl&&r
000
MC/DC0%0/2
exists $logc{"format"} and defined $Log::Dispatch::Config::CallerDepth
exists $logc{"format"} defined $Log::Dispatch::Config::CallerDepth
Truth table0%0/3
A and B
A= exists $logc{"format"}B= defined $Log::Dispatch::Config::CallerDepth
ABresult
0X0
100
111
67 0 my $callbacks = Log::Dispatch::Config->format_to_cb($logc{'format'},0);
68 0 ▶ if(defined $callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callbacks) 0 0 0
69 0 ▶ $logc{'callbacks'} = [] unless($logc{'callbacks'});
Branch: 1 branch
BranchTrueFalseTotal
unless $logc{"callbacks"} 0 0 0
70 0 push(@{$logc{'callbacks'}}, $callbacks);
71 }
72 }
73 0 ▶ if( exists $logc{'format_o'} and length( $logc{'format_o'} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $logc{'format_o'} and length $logc{'format_o'}) { } 0 0 0
elsif (not $logc{'callbacks'}) { } 0 0 0
Condition0%0/3
exists $logc{"format_o"} and length $logc{"format_o"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $logc{"format_o"} and length $logc{"format_o"}
exists $logc{"format_o"} length $logc{"format_o"}
Truth table0%0/3
A and B
A= exists $logc{"format_o"}B= length $logc{"format_o"}
ABresult
0X0
100
111
74 0 my $callbacks = Catalyst::Plugin::Log::Dispatch->_format_to_cb_o($logc{'format_o'},0);
75 0 ▶ if(defined $callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callbacks) 0 0 0
76 0 ▶ $logc{'callbacks'} = [] unless($logc{'callbacks'});
Branch: 1 branch
BranchTrueFalseTotal
unless $logc{"callbacks"} 0 0 0
77 0 push(@{$logc{'callbacks'}}, $callbacks);
78 }
79 }
80 elsif(!$logc{'callbacks'}) {
81 0 ▶ $logc{'callbacks'} = sub { my %p = @_; return "$p{message}\n"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 }
83 0 ▶ $class->use or die "$@";
Branch: 1 branch
BranchTrueFalseTotal
$class->use or die "$@" 0 0 0
84 0 my $logb = $class->new(%logc);
85 0 ▶ $logb->{rtf} = $logc{real_time_flush} || 0;
Condition0%0/2
$logc{'real_time_flush'} || 0
l!l
00
MC/DC0%0/1
$logc{'real_time_flush'} || 0
$logc{'real_time_flush'}
Truth table0%0/2
A
A= $logc{'real_time_flush'}
Aresult
11
00
86 0 $c->log->add( $logb );
87 }
88
89 0 ▶ if ($old_log && defined __log_dispatch_get_body( $old_log ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_log and defined __log_dispatch_get_body($old_log)) 0 0 0
Condition0%0/3
$old_log and defined __log_dispatch_get_body($old_log)
!ll&&!rl&&r
000
MC/DC0%0/2
$old_log and defined __log_dispatch_get_body($old_log)
$old_log defined __log_dispatch_get_body($old_log)
Truth table0%0/3
A and B
A= $old_logB= defined __log_dispatch_get_body($old_log)
ABresult
0X0
100
111
90 0 my @old_logs;
91 0 foreach my $line ( split /\n/, __log_dispatch_get_body( $old_log ) ) {
92 0 ▶ if ( $line =~ /^\[(\w+)] (.+)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[(\w+)] (.+)$/) { } 0 0 0
elsif ($line =~ /^\[(\w{3} \w{3}[ ]{1,2}\d{1,2}[ ]{1,2}\d{1,2}:\d{2}:\d{2} \d{4})\] \[catalyst\] \[(\w+)\] (.+)$/) { } 0 0 0
93 0 push( @old_logs, { level => $1, msg => [$2] } );
94 }
95 elsif( $line =~ /^\[(\w{3} \w{3}[ ]{1,2}\d{1,2}[ ]{1,2}\d{1,2}:\d{2}:\d{2} \d{4})\] \[catalyst\] \[(\w+)\] (.+)$/ ) {
96 0 push( @old_logs, { level => $2, msg => [$3] } );
97 }
98 else {
99 0 push( @{ $old_logs[-1]->{'msg'} }, $line );
100 }
101 }
102 0 foreach my $line (@old_logs) {
103 0 my $level = $line->{'level'};
104 0 $c->log->$level( join( "\n", @{ $line->{'msg'} } ) );
105 }
106 }
107 0 ▶ if( $Catalyst::VERSION >= 5.8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::VERSION >= 5.8) { } 0 0 0
108 0 return $c->maybe::next::method( @_ );
109 }
110 else {
111 0 $c->NEXT::setup(@_);
112 }
113 }
114  
115  
116 0 ▶sub __log_dispatch_get_body {
Subroutine __log_dispatch_get_body: not called CC=2 SCAR=17.9
117 0 my $log = shift;
118 0 ▶ return $Catalyst::VERSION >= 5.8 ? $log->_body : $log->body;
Branch: 1 branch
BranchTrueFalseTotal
$Catalyst::VERSION >= 5.8 ? : 0 0 0
119 }
120 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 # copy and paste from Log::Dispatch::Config
122 # please teach a cool method.
123 0 ▶sub _format_to_cb_o {
Subroutine _format_to_cb_o: not called CC=3 SCAR=24.8
124 0 my($class, $format, $stack) = @_;
125 0 ▶ return undef unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
126
127 # caller() called only when necessary
128 0 my $needs_caller = $format =~ /%[FLP]/;
129 0 ▶ if( $HasTimeHiRes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::Plugin::Log::Dispatch::HasTimeHiRes) { } 0 0 0
130 0 ▶ return sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
131 0 my %p = @_;
132 0 $p{p} = delete $p{level};
133 0 $p{m} = delete $p{message};
134 0 $p{n} = "\n";
135 0 $p{'%'} = '%';
136 0 $p{i} = $$;
137 0 ▶ if ($needs_caller) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_caller) 0 0 0
138 0 my $depth = 0;
139 0 ▶ $depth++ while caller($depth) =~ /^Catalyst::Plugin::Log::Dispatch/;
Branch: 1 branch
BranchTrueFalseTotal
caller($depth) =~ /^Catalyst::Plugin::Log::Dispatch/ and do { ++$depth } 0 0 0
140 0 $depth += $Catalyst::Plugin::Log::Dispatch::CallerDepth;
141 0 @p{qw(P F L)} = caller($depth);
142 }
143
144 0 my ($t,$ms) = Time::HiRes::gettimeofday();
145 0 $ms = sprintf('%06d', $ms);
146 0 my $log = $format;
147 0 $log =~ s{
148 (%d(?:{(.*?)})?)| # $1: datetime $2: datetime fmt
149 (%MS)| # $3: milli second
150 (?:%([%pmFLPni])) # $4: others
151 }{
152 0 ▶ if ($1 && $2) {
Branch: 4 branches
BranchTrueFalseTotal
if ($1 and $2) { } 0 0 0
elsif ($1) { } 0 0 0
elsif ($3) { } 0 0 0
elsif ($4) { } 0 0 0
Condition0%0/3
$1 and $2
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $2
$1 $2
Truth table0%0/3
A and B
A= $1B= $2
ABresult
0X0
100
111
153 0 _strftime_o($2,$t);
154 }
155 elsif ($1) {
156 0 scalar localtime;
157 }
158 elsif ($3) {
159 0 $ms;
160 }
161 elsif ($4) {
162 0 $p{$4};
163 }
164 }egx;
165 0 return $log;
166 0 };
167 }
168 else {
169 0 ▶ return sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
170 0 my %p = @_;
171 0 $p{p} = delete $p{level};
172 0 $p{m} = delete $p{message};
173 0 $p{n} = "\n";
174 0 $p{'%'} = '%';
175 0 $p{i} = $$;
176 0 ▶ if ($needs_caller) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_caller) 0 0 0
177 0 my $depth = 0;
178 0 ▶ $depth++ while caller($depth) =~ /^Catalyst::Plugin::Log::Dispatch/;
Branch: 1 branch
BranchTrueFalseTotal
caller($depth) =~ /^Catalyst::Plugin::Log::Dispatch/ and do { ++$depth } 0 0 0
179 0 $depth += $Catalyst::Plugin::Log::Dispatch::CallerDepth;
180 0 @p{qw(P F L)} = caller($depth);
181 }
182
183 0 my $log = $format;
184 0 $log =~ s{
185 (%d(?:{(.*?)})?)| # $1: datetime $2: datetime fmt
186 (?:%([%pmFLPn])) # $3: others
187 }{
188 0 ▶ if ($1 && $2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 and $2) { } 0 0 0
elsif ($1) { } 0 0 0
elsif ($3) { } 0 0 0
Condition0%0/3
$1 and $2
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $2
$1 $2
Truth table0%0/3
A and B
A= $1B= $2
ABresult
0X0
100
111
189 0 _strftime_o($2);
190 }
191 elsif ($1) {
192 0 scalar localtime;
193 }
194 elsif ($3) {
195 0 $p{$3};
196 }
197 }egx;
198 0 return $log;
199 0 };
200 }
201 }
202  
203 0 ▶sub _strftime_o {
Subroutine _strftime_o: not called CC=3 SCAR=24.8
204 0 my $fmt = shift;
205 0 ▶ my $time = shift || time;
Condition0%0/2
shift() || time
l!l
00
MC/DC0%0/1
shift() || time
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
206 0 ▶ if ($HasTimePiece) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Catalyst::Plugin::Log::Dispatch::HasTimePiece) { } 0 0 0
207 0 return Time::Piece->new($time)->strftime($fmt);
208 } else {
209 0 require POSIX;
210 0 return POSIX::strftime($fmt, localtime($time));
211 }
212 }
213  
214  
215 1;
216  
217 package Catalyst::Plugin::Log::Dispatch::Backend;
218  
219 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 1 ▶use base qw/Log::Dispatch Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
222  
223 use Time::HiRes qw/gettimeofday/;
224 use Data::Dump;
225 use Data::Dumper;
226  
227 {
228 foreach my $l (qw/debug info warn error fatal/) {
229 my $name = $l;
230 $name = 'warning' if ( $name eq 'warn' );
231 $name = 'critical' if ( $name eq 'fatal' );
232  
233 no strict 'refs';
234 *{"is_${l}"} = sub {
235 my $self = shift;
236 return $self->level_is_valid($name);
237 };
238  
239 *{"$l"} = sub {
240 my $self = shift;
241 my %p = (level => $name,
242 message => "@_");
243 local $Log::Dispatch::Config::CallerDepth += 1;
244 local $Catalyst::Plugin::Log::Dispatch::CallerDepth += 3;
245 if( keys( %{ $self->{outputs} } ) ) {
246 foreach (keys %{ $self->{outputs} }) {
247 my %h = %p;
248 $h{name} = $_;
249 if( $self->{outputs}->{$_}->{rtf} ) {
250 $self->{outputs}->{$_}->log(%h);
251 }
252 else {
253 $h{message} = $self->{outputs}->{$_}->_apply_callbacks(%h)
254 if($self->{outputs}->{$_}->{callbacks});
255 push(@{$self->_body}, \%h);
256 }
257 }
258 }
259 else {
260 push(@{$self->_body}, \%p);
261 }
262 };
263 }
264 }
265  
266 sub new {
267 my $pkg = shift;
268 my $this = $pkg->SUPER::new(@_);
269 $this->mk_accessors(qw/abort _body/);
270 $this->_body([]);
271 return $this;
272 }
273  
274  
275 sub dumper {
276 my $self = shift;
277 return $self->debug( Data::Dumper::Dumper(@_) );
278 }
279  
280 sub _dump {
281 my $self = shift;
282 return $self->debug( Data::Dump::dump(@_) );
283 }
284  
285 sub level_is_valid {
286 my $self = shift;
287 return 0 if ( $self->abort );
288 return $self->SUPER::level_is_valid(@_);
289 }
290  
291 sub _flush {
292 my $self = shift;
293 if ( $self->abort || !(scalar @{$self->_body})) {
294 $self->abort(undef);
295 }
296 else {
297 foreach my $p (@{$self->_body}) {
298 local $self->{outputs}->{$p->{name}}->{callbacks} = undef;
299 $self->{outputs}->{$p->{name}}->log(%{$p});
300 }
301 }
302 $self->_body([]);
303 }
304  
305  
306 1; # Magic true value required at end of module
307 __END__
↑ Summary