lib/Catalyst/Log.pm

filter: Statementstmt 86.3% 95 / 110 Branchbran 84.6% 22 / 26 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 80.9% 17 / 21 Podpod 100% 5 / 5 totaltotal 82.9% 151 / 182 CC 23 SCAR 10.5
CC
23
Cov
84%
_log
6.0
disable
6.0
_dump
6.0
CRAP 2.9
« script/catalyst.pl ↑ Summary lib/Catalyst/Middleware/Stash.pm »
1 package Catalyst::Log;
2  
3 169 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 169 with 'MooseX::Emulate::Class::Accessor::Fast';
5  
6 169 ▶use Data::Dump;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 169 ▶use Moose::Util 'find_meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 169 ▶use Carp qw/ cluck /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 169 our %LEVELS = (); # Levels stored as bit field, ergo debug = 1, warn = 2 etc
11 169 our %LEVEL_MATCH = (); # Stored as additive, thus debug = 31, warn = 30 etc
12  
13 169 has level => (is => 'rw');
14 169 has _body => (is => 'rw');
15 169 has abort => (is => 'rw');
16 170 ▶has autoflush => (is => 'rw', default => sub {1});
Subroutine __ANON__: called CC=1 SCAR=0.0
17 169 has _psgi_logger => (is => 'rw', predicate => '_has_psgi_logger', clearer => '_clear_psgi_logger');
18 169 has _psgi_errors => (is => 'rw', predicate => '_has_psgi_errors', clearer => '_clear_psgi_errors');
19  
20 96 ▶sub clear_psgi {
Subroutine clear_psgi: called CC=1 SCAR=0.0
Pod documented
21 96 my $self = shift;
22 96 $self->_clear_psgi_logger;
23 96 $self->_clear_psgi_errors;
24 }
25  
26 897 ▶sub psgienv {
Subroutine psgienv: called CC=3 SCAR=11.0
Pod documented
27 897 my ($self, $env) = @_;
28  
29 897 ▶ $self->_psgi_logger($env->{'psgix.logger'}) if $env->{'psgix.logger'};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"psgix.logger"} 1 896 897
30 897 ▶ $self->_psgi_errors($env->{'psgi.errors'}) if $env->{'psgi.errors'};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"psgi.errors"} 888 9 897
31 }
32  
33  
34 {
35 169 my @levels = qw[ debug info warn error fatal ];
36  
37 169 my $meta = find_meta(__PACKAGE__);
38 169 my $summed_level = 0;
39 169 ▶ for ( my $i = $#levels ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 845 169 1014
40  
41 845 my $name = $levels[$i];
42  
43 845 my $level = 1 << $i;
44 845 $summed_level |= $level;
45  
46 845 $LEVELS{$name} = $level;
47 845 $LEVEL_MATCH{$name} = $summed_level;
48  
49 6 ▶ $meta->add_method($name, sub {
Subroutine warn: called CC=2 SCAR=6.9
Subroutine error: called CC=2 SCAR=6.9
50 6 my $self = shift;
51  
52 6 ▶ if ( $self->level & $level ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->level & $level) 3 3 6
if ($self->level & $level) 30 151 181
53 3 $self->_log( $name, @_ );
54 }
55 845 });
56  
57 0 ▶ $meta->add_method("is_$name", sub {
Subroutine is_info: not called CC=1 SCAR=1.2
Subroutine is_error: called CC=1 SCAR=1.2
58 0 my $self = shift;
59 0 return $self->level & $level;
60 845 });;
61 }
62 }
63  
64 170 ▶around new => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
65 170 my $orig = shift;
66 170 my $class = shift;
67 170 my $self = $class->$orig;
68  
69 170 ▶ $self->levels( scalar(@_) ? @_ : keys %LEVELS );
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 21 149 170
70  
71 170 return $self;
72 169 };
73  
74 170 ▶sub levels {
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
75 170 my ( $self, @levels ) = @_;
76 170 $self->level(0);
77 170 $self->enable(@levels);
78 }
79  
80 170 ▶sub enable {
Subroutine enable: called CC=2 SCAR=6.9
Pod documented
81 170 my ( $self, @levels ) = @_;
82 170 my $level = $self->level;
83 170 for(map { $LEVEL_MATCH{$_} } @levels){
84 769 $level |= $_;
85 }
86 170 $self->level($level);
87 }
88  
89 0 ▶sub disable {
Subroutine disable: not called CC=2 SCAR=17.9
Pod documented
90 0 my ( $self, @levels ) = @_;
91 0 my $level = $self->level;
92 0 for(map { $LEVELS{$_} } @levels){
93 0 $level &= ~$_;
94 }
95 0 $self->level($level);
96 }
97  
98 169 our $HAS_DUMPED;
99 0 ▶sub _dump {
Subroutine _dump: not called CC=2 SCAR=17.9
100 0 my $self = shift;
101 0 ▶ unless ($HAS_DUMPED++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Catalyst::Log::HAS_DUMPED++) 0 0 0
102 0 cluck("Catalyst::Log::_dump is deprecated and will be removed. Please change to using your own Dumper.\n");
103 }
104 0 $self->info( Data::Dump::dump(@_) );
105 }
106  
107 33 ▶sub _log {
Subroutine _log: called CC=6 SCAR=18.0
108 33 my $self = shift;
109 33 my $level = shift;
110 33 my $message = join( "\n", @_ );
111 33 ▶ if ($self->can('_has_psgi_logger') and $self->_has_psgi_logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('_has_psgi_logger') and $self->_has_psgi_logger) { } 1 32 33
Condition66%2/3
$self->can("_has_psgi_logger") and $self->_has_psgi_logger
!ll&&!rl&&r
0321
MC/DC50%1/2
$self->can("_has_psgi_logger") and $self->_has_psgi_logger
$self->can("_has_psgi_logger") $self->_has_psgi_logger
Truth table66%2/3
A and B
A= $self->can("_has_psgi_logger")B= $self->_has_psgi_logger
ABresult
0X0
100
111
112 1 $self->_psgi_logger->({
113 level => $level,
114 message => $message,
115 });
116 } else {
117 32 ▶ $message .= "\n" unless $message =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\n$/ 24 8 32
118 32 my $body = $self->_body;
119 32 $body .= sprintf( "[%s] %s", $level, $message );
120 32 $self->_body($body);
121 }
122 33 ▶ if( $self->autoflush && !$self->abort ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoflush and not $self->abort) 31 2 33
Condition66%2/3
$self->autoflush and not $self->abort
!ll&&!rl&&r
2031
MC/DC50%1/2
$self->autoflush and not $self->abort
$self->autoflush not $self->abort
Truth table66%2/3
A and not(B)
A= $self->autoflushB= not $self->abort
ABresult
0X0
100
111
123 31 $self->_flush;
124 }
125 33 return 1;
126 }
127  
128 1070 ▶sub _flush {
Subroutine _flush: called CC=3 SCAR=11.0
129 1070 my $self = shift;
130 1070 ▶ if ( $self->abort || !$self->_body ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->abort or not $self->_body) { } 1038 32 1070
Condition66%2/3
$self->abort or not $self->_body
l!l&&r!l&&!r
0103832
MC/DC50%1/2
$self->abort or not $self->_body
$self->abort not $self->_body
Truth table66%2/3
A or not(B)
A= $self->abortB= not $self->_body
ABresult
1X1
011
000
131 1038 $self->abort(undef);
132 }
133 else {
134 32 $self->_send_to_log( $self->_body );
135 }
136 1070 $self->_body(undef);
137 }
138  
139 20 ▶sub _send_to_log {
Subroutine _send_to_log: called CC=3 SCAR=11.0
140 20 my $self = shift;
141 20 ▶ if ($self->can('_has_psgi_errors') and $self->_has_psgi_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('_has_psgi_errors') and $self->_has_psgi_errors) { } 8 12 20
Condition66%2/3
$self->can("_has_psgi_errors") and $self->_has_psgi_errors
!ll&&!rl&&r
0128
MC/DC50%1/2
$self->can("_has_psgi_errors") and $self->_has_psgi_errors
$self->can("_has_psgi_errors") $self->_has_psgi_errors
Truth table66%2/3
A and B
A= $self->can("_has_psgi_errors")B= $self->_has_psgi_errors
ABresult
0X0
100
111
142 8 $self->_psgi_errors->print(@_);
143 } else {
144 12 binmode STDERR, ":utf8";
145 12 print STDERR @_;
146 }
147 }
148  
149 # 5.7 compat code.
150 # Alias _body to body, add a before modifier to warn..
151 169 my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time.
152 169 $meta->add_method('body', $meta->get_method('_body'));
153 169 my %package_hash; # Only warn once per method, per package.
154 # I haven't provided a way to disable them, patches welcome.
155 0 ▶$meta->add_before_method_modifier('body', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
156 0 my $class = blessed(shift);
157 0 ▶ $package_hash{$class}++ || do {
Branch: 1 branch
BranchTrueFalseTotal
$package_hash{$class}++ or do { CORE::warn "Class $class is calling the deprecated method Catalyst::Log->body method,\n" . 'this will be removed in Catalyst 5.81' } 0 0 0
158 0 warn("Class $class is calling the deprecated method Catalyst::Log->body method,\n"
159 . "this will be removed in Catalyst 5.81");
160 };
161 169 });
162 # End 5.70 backwards compatibility hacks.
163  
164 169 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 169 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
166  
167 169 1;
168  
169 __END__
« script/catalyst.pl ↑ Summary lib/Catalyst/Middleware/Stash.pm »