Weixin/Log.pm

filter: Statementstmt 35.3% 41 / 116 Branchbran 2.0% 1 / 48 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 30.3% 10 / 33 Podpod 47.0% 8 / 17 totaltotal 23.4% 60 / 256 CC 40 SCAR 41.7
CC
40
Cov
22%
append
240.0
_format
56.0
colored
42.0
CRAP 64.9
« Weixin/Model/Remote/_webwxverifyuser.pm ↑ Summary Weixin/Plugin/Translation.pm »
1 package Mojo::Weixin::Log;
2 1 ▶use base qw(Mojo::Base Mojo::EventEmitter Mojo::Log);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶BEGIN{
Subroutine BEGIN: called CC=2 SCAR=7.1
10 1 eval{require Term::ANSIColor};
11 1 ▶ $Mojo::Weixin::Log::is_support_color = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 1 0 1
12 }
13 11 ▶sub has { Mojo::Base::attr(__PACKAGE__, @_) };
Subroutine has: called CC=1 SCAR=0.0
Pod documented
14
15 0 ▶has format => sub { \&_format };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 0 ▶has handle => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
17 # STDERR
18 0 ▶ return \*STDERR unless my $path = shift->path;
Branch: 1 branch
BranchTrueFalseTotal
unless my $path = (shift())->path 0 0 0
19 # File
20 0 ▶ croak qq{Can't open log file "$path": $!} unless open my $file, '>>', $path;
Branch: 1 branch
BranchTrueFalseTotal
unless open my $file, ">>", $path 0 0 0
21 0 return $file;
22 1 };
23 0 ▶has history => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 1 has level => 'debug';
25 1 has head => '';
26 1 has encoding => undef;
27 1 has unicode_support => 1;
28 1 has disable_color => 0;
29 1 has console_output => 0;
30 1 has max_history_size => 10;
31 1 has 'path';
32
33 # Supported log levels
34 1 my $LEVEL = {debug => 1, info => 2, msg=>3, warn => 4, error => 5, fatal => 6};
35 0 ▶sub _format {
Subroutine _format: not called CC=7 SCAR=40.3
36 0 my ($time, $level, @lines) = @_;
37 0 ▶ my %opt = ref $lines[0] eq "HASH"?%{shift @lines}:();
Branch: 1 branch
BranchTrueFalseTotal
ref $lines[0] eq 'HASH' ? : 0 0 0
38 0 ▶ $time = $opt{time} if defined $opt{time};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"time"} 0 0 0
39 0 ▶ $time = $time?POSIX::strftime('[%y/%m/%d %H:%M:%S]',localtime($time)):"";
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 0 0 0
40 my $log = {
41 head => $opt{head} // "",
42 head_color => $opt{head_color},
43 'time' => $time,
44 time_color => $opt{time_color},
45 level => $opt{level} // $level,
46 level_color => $opt{level_color},
47 title => defined $opt{title}?"$opt{title} ":"",
48 title_color => $opt{title_color},
49 content => [split /\n/,join "",@lines],
50 content_color=> $opt{content_color},
51 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'title'} ? : 0 0 0
Condition0%0/5
$opt{"head"} // ""
l!l
00
$opt{"level"} // $level
l!l&&r!l&&!r
000
MC/DC0%0/1
$opt{"head"} // ""
$opt{"head"}
MC/DC0%0/2
$opt{"level"} // $level
$opt{"level"} $level
Truth table0%0/2
A
A= $opt{"head"}
Aresult
11
00
Truth table0%0/3
A // B
A= $opt{"level"}B= $level
ABresult
1X1
011
000
52 0 return $log;
53 }
54 0 ▶sub colored {
Subroutine colored: not called CC=6 SCAR=37.4
Pod undocumented
55 #black red green yellow blue magenta cyan white
56 0 my $self = shift;
57 0 ▶ return $_[0] if (!$_[0] or !$_[1] or $self->disable_color or !$Mojo::Weixin::Log::is_support_color);
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0] && $_[1] or $self->disable_color or not $is_support_color 0 0 0
Condition0%0/9
not $_[0] && $_[1] or $self->disable_color or not $is_support_color
l!l&&r!l&&!r
000
not $_[0] && $_[1] or $self->disable_color
l!l&&r!l&&!r
000
$_[0] && $_[1]
!ll&&!rl&&r
000
MC/DC0%0/4
not $_[0] && $_[1] or $self->disable_color or not $is_support_color
$_[0] $_[1] $self->disable_color not $is_support_color
Truth table0%0/5
not(A && B) or C or not(D)
A= $_[0]B= $_[1]C= $self->disable_colorD= not $is_support_color
ABCDresult
0XXX1
10XX1
111X1
11011
11000
58 0 ▶ return Term::ANSIColor::colored(@_) if $Mojo::Weixin::Log::is_support_color;
Branch: 1 branch
BranchTrueFalseTotal
if $is_support_color 0 0 0
59 }
60 0 ▶sub reform_encoding{
Subroutine reform_encoding: not called CC=6 SCAR=33.9
Pod undocumented
61 0 my $self = shift;
62 0 my $log = shift;
63 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 0 my $msg ;
65 0 ▶ if($self->unicode_support and Encode::is_utf8($log)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unicode_support and &Encode::is_utf8($log)) { } 0 0 0
Condition0%0/3
$self->unicode_support and &Encode::is_utf8($log)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->unicode_support and &Encode::is_utf8($log)
$self->unicode_support &Encode::is_utf8($log)
Truth table0%0/3
A and B
A= $self->unicode_supportB= &Encode::is_utf8($log)
ABresult
0X0
100
111
66 0 ▶ $msg = encode($self->encoding || console_out,$log);
Condition0%0/2
$self->encoding || 'console_out'
l!l
00
MC/DC0%0/1
$self->encoding || 'console_out'
$self->encoding
Truth table0%0/2
A
A= $self->encoding
Aresult
11
00
67 }
68 else{
69 0 ▶ if($self->encoding =~/^utf-?8$/i ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->encoding =~ /^utf-?8$/i) { } 0 0 0
70 0 $msg = $log;
71 }
72 else{
73 0 ▶ $msg = encode($self->encoding || console_out,decode("utf8",$log));
Condition0%0/2
$self->encoding || 'console_out'
l!l
00
MC/DC0%0/1
$self->encoding || 'console_out'
$self->encoding
Truth table0%0/2
A
A= $self->encoding
Aresult
11
00
74 }
75 }
76 0 return $msg;
77 }
78 0 ▶sub append {
Subroutine append: not called CC=15 SCAR=54.8
Pod documented
79 0 my ($self,$log) = @_;
80 0 ▶ return unless my $handle = $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
unless my $handle = $self->handle 0 0 0
81 0 flock $handle, LOCK_EX;
82 0 $log->{$_} = $self->reform_encoding($log->{$_}) for(qw(head level title ));
83 0 $_ = $self->reform_encoding($_) for @{$log->{content}};
84 0 ▶ if( -t $handle){
Branch: 1 branch
BranchTrueFalseTotal
if (-t $handle) { } 0 0 0
85 0 my $color_msg;
86 0 for(@{$log->{content}}){
87 $color_msg .= $self->colored($log->{head},$log->{head_color})
88 . $self->colored($log->{time},$log->{time_color})
89 . " "
90 . ( $log->{level}?"[".$self->colored($log->{level},$log->{level_color})."]":"" )
91 . " "
92 . $self->colored($log->{title},$log->{title_color})
93 . $self->colored($_,$log->{content_color})
94 0 ▶ . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$log->{'level'} ? : 0 0 0
95 }
96 0 ▶ $handle->print($color_msg) or croak "Can't write to log: $!";
Branch: 1 branch
BranchTrueFalseTotal
$handle->print($color_msg) or croak("Can't write to log: $!") 0 0 0
97 }
98 else{
99 0 my $msg;
100 0 for(@{$log->{content}}){
101 $msg .= $log->{head}
102 . $log->{time}
103 . " "
104 . ($log->{level}?"[$log->{level}]":"")
105 . " "
106 . $log->{title}
107 0 ▶ . $_
Branch: 1 branch
BranchTrueFalseTotal
$log->{'level'} ? : 0 0 0
108 . "\n";
109 }
110 0 ▶ $handle->print($msg) or croak "Can't write to log: $!";
Branch: 1 branch
BranchTrueFalseTotal
$handle->print($msg) or croak("Can't write to log: $!") 0 0 0
111 0 ▶ if($self->console_output and -t STDOUT){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->console_output and -t STDOUT) 0 0 0
Condition0%0/3
$self->console_output and -t STDOUT
!ll&&!rl&&r
000
MC/DC0%0/2
$self->console_output and -t STDOUT
$self->console_output -t STDOUT
Truth table0%0/3
A and B
A= $self->console_outputB= -t STDOUT
ABresult
0X0
100
111
112 0 my $color_msg;
113 0 for(@{$log->{content}}){
114 $color_msg .= $self->colored($log->{head},$log->{head_color})
115 . $self->colored($log->{time},$log->{time_color})
116 . " "
117 . ( $log->{level}?"[".$self->colored($log->{level},$log->{level_color})."]":"" )
118 . " "
119 . $self->colored($log->{title},$log->{title_color})
120 . $self->colored($_,$log->{content_color})
121 0 ▶ . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$log->{'level'} ? : 0 0 0
122 }
123 0 print STDERR $color_msg;#or croak "Can't write to log: $!"
124 }
125 }
126 0 flock $handle, LOCK_UN;
127 }
128
129 0 ▶sub debug { shift->_log(debug => @_) }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
130 0 ▶sub error { shift->_log(error => @_) }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
131 0 ▶sub fatal { shift->_log(fatal => @_) }
Subroutine fatal: not called CC=1 SCAR=6.9
Pod documented
132 0 ▶sub info { shift->_log(info => @_) }
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
133 0 ▶sub warn { shift->_log(warn => @_) }
Subroutine warn: not called CC=1 SCAR=6.9
Pod documented
134 0 ▶sub msg { shift->_log(msg => @_) }
Subroutine msg: not called CC=1 SCAR=6.9
Pod undocumented
135
136 0 ▶sub is_debug { shift->_now('debug') }
Subroutine is_debug: not called CC=1 SCAR=6.9
Pod undocumented
137 0 ▶sub is_error { shift->_now('error') }
Subroutine is_error: not called CC=1 SCAR=6.9
Pod undocumented
138 0 ▶sub is_info { shift->_now('info') }
Subroutine is_info: not called CC=1 SCAR=6.9
Pod undocumented
139 0 ▶sub is_warn { shift->_now('warn') }
Subroutine is_warn: not called CC=1 SCAR=6.9
Pod undocumented
140 0 ▶sub is_msg { shift->_now('msg') }
Subroutine is_msg: not called CC=1 SCAR=6.9
Pod undocumented
141 0 ▶sub is_fatal { shift->_now('fatal') }
Subroutine is_fatal: not called CC=1 SCAR=6.9
Pod undocumented
142
143 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
144 0 my $self = shift->SUPER::new(@_);
145 0 $self->on(message => \&_message);
146 0 return $self;
147 }
148
149 0 ▶sub _log { shift->emit('message', shift, ref $_[0] eq 'CODE' ? $_[0]() : @_) }
Subroutine _log: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 0 0 0
150
151 0 ▶sub _message {
Subroutine _message: not called CC=5 SCAR=34.0
152 0 my ($self, $level) = (shift, shift);
153
154 0 ▶ return unless $self->_now($level);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_now($level) 0 0 0
155
156 0 my $max = $self->max_history_size;
157 0 my $history = $self->history;
158 0 ▶ if(ref $_[0] eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
159 0 ▶ $_[0]{head} = $self->head if not defined $_[0]{head};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"head"} 0 0 0
160 }
161 else{
162 0 unshift @_,{head=>$self->head};
163 }
164 0 push @$history, my $msg = [time, $level, @_];
165 0 ▶ shift @$history while @$history > $max;
Branch: 1 branch
BranchTrueFalseTotal
@$history > $max and do { shift @$history } 0 0 0
166  
167 0 $self->append($self->format->(@$msg));
168 }
169
170 0 ▶sub _now { $LEVEL->{pop()} >= $LEVEL->{$ENV{MOJO_LOG_LEVEL} || shift->level} }
Subroutine _now: not called CC=2 SCAR=17.9
Condition0%0/3
$ENV{'MOJO_LOG_LEVEL'} || (shift())->level
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'MOJO_LOG_LEVEL'} || (shift())->level
$ENV{'MOJO_LOG_LEVEL'} (shift())->level
Truth table0%0/3
A || B
A= $ENV{'MOJO_LOG_LEVEL'}B= (shift())->level
ABresult
1X1
011
000
171
172 1 1;
« Weixin/Model/Remote/_webwxverifyuser.pm ↑ Summary Weixin/Plugin/Translation.pm »