blib/lib/Plack/Middleware/LogStderr.pm

filter: Statementstmt 90.2% 74 / 82 Branchbran 73.3% 22 / 30 Conditioncond 89.4% 17 / 19 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 90.0% 18 / 20 Podpod 100% 2 / 2 totaltotal 86.5% 142 / 164 CC 24 SCAR 14.3
CC
24
Cov
86%
__run_callback
7.0
prepare_app
6.0
OPEN
6.0
CRAP 4.2
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Plack::Middleware::LogStderr;
4 1 $Plack::Middleware::LogStderr::VERSION = '0.001';
5 # ABSTRACT: Everything printed to STDERR sent to psgix.logger or other logger
6 # KEYWORDS: plack middleware errors logging environment I/O handle stderr
7  
8 1 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Plack::Util::Accessor qw/logger callback tie_callback capture_callback no_tie log_level log_level_capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Capture::Tiny 'capture_stderr';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 19 ▶sub prepare_app {
Subroutine prepare_app: called CC=6 SCAR=17.9
Pod documented
15 19 my $self = shift;
16 19 ▶ $self->{log_level} = $self->log_level || 'error';
Condition100%2/2
$self->log_level || 'error'
l!l
217
MC/DC100%1/1
$self->log_level || 'error'
$self->log_level
Truth table100%2/2
A
A= $self->log_level
Aresult
11
00
17 19 ▶ $self->{log_level_capture} = $self->log_level_capture || $self->{log_level} ;
Condition66%2/3
$self->log_level_capture || $self->{'log_level'}
l!l&&r!l&&!r
1180
MC/DC0%0/2
$self->log_level_capture || $self->{'log_level'}
$self->log_level_capture $self->{'log_level'}
Truth table66%2/3
A || B
A= $self->log_level_captureB= $self->{'log_level'}
ABresult
1X1
011
000
18
19 19 foreach my $cb (qw/logger callback tie_callback capture_callback/){
20 58 ▶ if ($self->$cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$cb) 22 36 58
21 22 ▶ if (not __isa_coderef($self->$cb)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__isa_coderef($self->$cb)) 12 10 22
22 12 die "'$cb' is not a coderef!"
23 }
24 }
25 }
26 }
27  
28 7 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
29 7 my ($self, $env) = @_;
30 7 ▶ my $logger = $self->logger || $env->{'psgix.logger'};
Condition100%3/3
$self->logger || $env->{'psgix.logger'}
l!l&&r!l&&!r
151
MC/DC100%2/2
$self->logger || $env->{'psgix.logger'}
$self->logger $env->{'psgix.logger'}
Truth table100%3/3
A || B
A= $self->loggerB= $env->{'psgix.logger'}
ABresult
1X1
011
000
31  
32 7 ▶ die 'no psgix.logger in $env; cannot send STDERR to it!'
Branch: 1 branch
BranchTrueFalseTotal
unless $logger 1 6 7
33 if not $logger;
34
35 12 ▶ my $stderr_logger = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 12 my $message = shift;
37 12 $message = $self->__run_tie_callback($message);
38 12 $logger->({level => $self->{log_level}, message => $message });
39 6 };
40
41 6 ▶ my ($stderr, @app) = capture_stderr {
Subroutine __ANON__: called CC=5 SCAR=16.4
42 6 my ($app, $err);
43  
44 6 ▶ tie *STDERR, 'Plack::Middleware::LogStderr::Handle2Logger', $stderr_logger
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_tie 4 2 6
45 unless $self->no_tie ;
46  
47 6 eval {
48 6 $app = $self->app->($env);
49 };
50 6 ▶ $err = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
51  
52 6 ▶ untie *STDERR
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_tie 4 2 6
53 unless $self->no_tie ;
54
55 6 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 6 6
56 0 die $@;
57 }
58 6 return $app;
59 6 };
60 6 ▶ if ($stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr) 6 0 6
61 6 $stderr = $self->__run_capture_callback($stderr) ;
62 6 $logger->({level => $self->{log_level_capture}, message => $stderr });
63 }
64
65 6 return $app[0];
66 }
67  
68 18 ▶sub __run_callback {
Subroutine __run_callback: called CC=7 SCAR=19.5
69 18 my ($self, $msg, $extra_cb) = @_;
70 18 ▶ $msg = $self->callback->($msg) if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 9 9 18
71 18 ▶ if ($extra_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_cb) 18 0 18
72 18 ▶ if ($extra_cb eq 'tie' && $self->tie_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_cb eq "tie" and $self->tie_callback) 6 12 18
Condition100%3/3
$extra_cb eq "tie" and $self->tie_callback
!ll&&!rl&&r
666
MC/DC100%2/2
$extra_cb eq "tie" and $self->tie_callback
$extra_cb eq "tie" $self->tie_callback
Truth table100%3/3
A and B
A= $extra_cb eq "tie"B= $self->tie_callback
ABresult
0X0
100
111
73 6 $msg = $self->tie_callback->($msg) ;
74 }
75 18 ▶ if ($extra_cb eq 'capture' && $self->capture_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_cb eq "capture" and $self->capture_callback) 3 15 18
Condition100%3/3
$extra_cb eq "capture" and $self->capture_callback
!ll&&!rl&&r
1233
MC/DC100%2/2
$extra_cb eq "capture" and $self->capture_callback
$extra_cb eq "capture" $self->capture_callback
Truth table100%3/3
A and B
A= $extra_cb eq "capture"B= $self->capture_callback
ABresult
0X0
100
111
76 3 $msg = $self->capture_callback->($msg) ;
77 }
78 }
79 18 return $msg;
80 }
81 6 ▶sub __run_capture_callback {
Subroutine __run_capture_callback: called CC=1 SCAR=0.0
82 6 my ($self, $msg) = @_;
83 6 $msg = $self->__run_callback($msg, 'capture');
84 6 return $msg;
85
86 }
87 12 ▶sub __run_tie_callback {
Subroutine __run_tie_callback: called CC=1 SCAR=0.0
88 12 my ($self, $msg) = @_;
89 12 $msg = $self->__run_callback($msg, 'tie');
90 12 return $msg;
91 }
92  
93 22 ▶sub __isa_coderef {
Subroutine __isa_coderef: called CC=4 SCAR=13.9
94 22 ▶ ref $_[0] eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "CODE" or (builtin::reftype($_[0]) || "") eq "CODE" or overload::Method($_[0], "&{}") 10 12 22
Condition80%4/5
ref $_[0] eq "CODE" or (builtin::reftype($_[0]) || "") eq "CODE"
l!l&&r!l&&!r
10012
builtin::reftype($_[0]) || ''
l!l
48
MC/DC100%2/2
ref $_[0] eq "CODE" or (builtin::reftype($_[0]) || "") eq "CODE"
ref $_[0] eq "CODE" builtin::reftype($_[0])
Truth table100%3/3
A or B
A= ref $_[0] eq "CODE"B= builtin::reftype($_[0])
ABresult
1X1
011
000
95 or (Scalar::Util::reftype($_[0]) || '') eq 'CODE'
96 or overload::Method($_[0], '&{}')
97 }
98  
99 package # hide from PAUSE
100 Plack::Middleware::LogStderr::Handle2Logger;
101 1 our $VERSION = '0.001';
102 # ABSTRACT: Tie File Handle to a logger
103 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 4 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
106 4 my ($pkg, $logger) = @_;
107 4 return bless {logger => $logger}, $pkg;
108 }
109 8 ▶sub PRINT {
Subroutine PRINT: called CC=1 SCAR=0.0
110 8 my ($self, @msg) = @_;
111 8 my $message = join('', @msg);
112 8 $self->{logger}->( $message );
113 }
114 4 ▶sub PRINTF {
Subroutine PRINTF: called CC=1 SCAR=0.0
115 4 my ($self, $fmt, @msg) = @_;
116 4 my $message = sprintf($fmt, @msg);
117 4 $self->{logger}->($message);
118 }
119 ## if something tries to reopen FILEHANDLE just return true -- noop
120 0 ▶sub OPEN {
Subroutine OPEN: not called CC=2 SCAR=17.9
121 0 my ($self) = @_;
122 0 ▶ if (warnings::enabled()) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings::enabled()) 0 0 0
123 0 warnings::warn("open called on tied handle Handle2Logger");
124 }
125 0 return 1;
126 }
127 ## if something tries to set BINMODE -- noop
128 0 ▶sub BINMODE {
Subroutine BINMODE: not called CC=2 SCAR=17.9
129 0 ▶ if (warnings::enabled()) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings::enabled()) 0 0 0
130 0 warnings::warn("binmode called on tied handle Handle2Logger");
131 }
132 0 return undef;
133 }
134  
135  
136 1 1;
137  
138 __END__
↑ Summary