lib/Apache/TestTrace.pm

filter: Statementstmt 75.6% 56 / 74 Branchbran 21.4% 3 / 14 Conditioncond 17.6% 3 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 58.8% 10 / 17 Podpod 0.0% 0 / 4 totaltotal 53.7% 72 / 134 CC 13 SCAR 21.3
CC
13
Cov
59%
trace_level
30.0
prefix
12.0
__ANON__
6.0
CRAP 8.4
« blib/lib/Apache/TestCommonPost.pm ↑ Summary blib/lib/Apache/TestMB.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 package Apache::TestTrace;
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw(@Levels @Utils @Level_subs @Util_subs
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 @ISA @EXPORT $VERSION $Level $LogFH);
24  
25 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 @Levels = qw(emerg alert crit error warning notice info debug);
27 1 @Utils = qw(todo);
28 1 @Level_subs = map {($_, "${_}_mark", "${_}_sub")} (@Levels);
29 1 @Util_subs = map {($_, "${_}_mark", "${_}_sub")} (@Utils);
30 }
31  
32 1 @ISA = qw(Exporter);
33 1 @EXPORT = (@Level_subs);
34 1 $VERSION = '0.01';
35 1 ▶use subs (@Level_subs, @Util_subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # default settings overrideable by users
38 1 $Level = undef;
39 1 $LogFH = \*STDERR;
40  
41 # private data
42 1 ▶use constant COLOR => ($ENV{APACHE_TEST_COLOR} && -t STDOUT) ? 1 : 0;
Subroutine BEGIN: called CC=3 SCAR=12.5
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'APACHE_TEST_COLOR'} && -t STDOUT ? : 0 1 1
Condition33%1/3
$ENV{"APACHE_TEST_COLOR"} and -t STDOUT
!ll&&!rl&&r
100
MC/DC0%0/2
$ENV{"APACHE_TEST_COLOR"} and -t STDOUT
$ENV{"APACHE_TEST_COLOR"} -t STDOUT
Truth table33%1/3
A and B
A= $ENV{"APACHE_TEST_COLOR"}B= -t STDOUT
ABresult
0X0
100
111
43 1 ▶use constant HAS_COLOR => eval {
Subroutine BEGIN: called CC=3 SCAR=11.8
44 #XXX: another way to color WINFU terms?
45 1 ▶ !(grep { $^O eq $_ } qw(MSWin32 cygwin NetWare)) and
Condition40%2/5
!grep({$^O eq $_;} 'MSWin32', 'cygwin', 'NetWare') && 0 && require Term::ANSIColor
!ll&&!rl&&r
100
not grep({$^O eq $_;} "MSWin32", "cygwin", "NetWare") and 0
!ll
01
MC/DC0%0/2
!grep({$^O eq $_;} 'MSWin32', 'cygwin', 'NetWare') && 0 && require Term::ANSIColor
not grep({$^O eq $_;} "MSWin32", "cygwin", "NetWare") require Term::ANSIColor
Truth table0%0/3
not(A) && B
A= not grep({$^O eq $_;} "MSWin32", "cygwin", "NetWare")B= require Term::ANSIColor
ABresult
0X0
100
111
46 COLOR and require Term::ANSIColor;
47 1 };
48 1 ▶use constant HAS_DUMPER => eval { require Data::Dumper; };
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # emerg => 1, alert => 2, crit => 3, ...
51 1 my %levels; @levels{@Levels} = 1..@Levels;
52 1 $levels{todo} = $levels{debug};
53 1 my $default_level = 'info'; # to prevent user typos
54  
55 1 my %colors = ();
56  
57 1 if (HAS_COLOR) {
58 %colors = (
59 emerg => 'bold white on_blue',
60 alert => 'bold blue on_yellow',
61 crit => 'reverse',
62 error => 'bold red',
63 warning => 'yellow',
64 notice => 'green',
65 info => 'cyan',
66 debug => 'magenta',
67 reset => 'reset',
68 todo => 'underline',
69 );
70  
71 $Term::ANSIColor::AUTORESET = 1;
72  
73 for (keys %colors) {
74 $colors{$_} = Term::ANSIColor::color($colors{$_});
75 }
76 }
77  
78 *expand = HAS_DUMPER ?
79 0 ▶ sub { map { ref $_ ? Data::Dumper::Dumper($_) : $_ } @_ } :
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
80 0 ▶ sub { @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
81  
82 0 ▶sub prefix {
Subroutine prefix: not called CC=3 SCAR=24.8
Pod undocumented
83 0 my $prefix = shift;
84  
85 0 ▶ if ($prefix eq 'mark') {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix eq 'mark') { } 0 0 0
elsif ($prefix eq 'sub') { } 0 0 0
86 0 return join(":", (caller(3))[1..2]) . " : ";
87 }
88 elsif ($prefix eq 'sub') {
89 0 return (caller(3))[3] . " : ";
90 }
91 else {
92 0 return '';
93 }
94 }
95  
96 0 ▶sub c_trace {
Subroutine c_trace: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ($level, $prefix_type) = (shift, shift);
98 0 my $prefix = prefix($prefix_type);
99 print $LogFH
100 0 map { "$colors{$level}$prefix$_$colors{reset}\n"}
101 grep defined($_), expand(@_);
102 }
103  
104 0 ▶sub nc_trace {
Subroutine nc_trace: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($level, $prefix_type) = (shift, shift);
106 0 my $prefix = prefix($prefix_type);
107 print $LogFH
108 0 map { sprintf "[%7s] %s%s\n", $level, $prefix, $_ }
109 grep defined($_), expand(@_);
110 }
111  
112 {
113 1 my $trace = HAS_COLOR ? \&c_trace : \&nc_trace;
114 1 my @prefices = ('', 'mark', 'sub');
115 # if the level is sufficiently high, enable the tracing for a
116 # given level otherwise assign NOP
117 1 for my $level (@Levels, @Utils) {
118 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 9 for my $prefix (@prefices) {
120 27 ▶ my $func = $prefix ? "${level}_$prefix" : $level;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 18 9 27
121 0 ▶ *$func = sub { $trace->($level, $prefix, @_)
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 ▶ if trace_level() >= $levels{$level};
Branch: 1 branch
BranchTrueFalseTotal
if trace_level() >= $levels{$level} 0 0 0
123 27 };
124 }
125 }
126 }
127  
128 0 ▶sub trace_level {
Subroutine trace_level: not called CC=5 SCAR=34.0
Pod undocumented
129 # overriden by user/-trace
130 (defined $Level && $levels{$Level}) ||
131 # or overriden by env var
132 (exists $ENV{APACHE_TEST_TRACE_LEVEL} &&
133 $levels{$ENV{APACHE_TEST_TRACE_LEVEL}}) ||
134 # or default
135 0 ▶ $levels{$default_level};
Branch: 1 branch
BranchTrueFalseTotal
defined $Apache::TestTrace::Level and $levels{$Apache::TestTrace::Level} or exists $ENV{"APACHE_TEST_TRACE_LEVEL"} and $levels{$ENV{"APACHE_TEST_TRACE_LEVEL"}} or $levels{$default_level} 0 0 0
Condition0%0/9
defined $Apache::TestTrace::Level and $levels{$Apache::TestTrace::Level} or exists $ENV{"APACHE_TEST_TRACE_LEVEL"} and $levels{$ENV{"APACHE_TEST_TRACE_LEVEL"}}
l!l&&r!l&&!r
000
defined $Apache::TestTrace::Level and $levels{$Apache::TestTrace::Level}
!ll&&!rl&&r
000
exists $ENV{"APACHE_TEST_TRACE_LEVEL"} and $levels{$ENV{"APACHE_TEST_TRACE_LEVEL"}}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $Apache::TestTrace::Level and $levels{$Apache::TestTrace::Level} or exists $ENV{"APACHE_TEST_TRACE_LEVEL"} and $levels{$ENV{"APACHE_TEST_TRACE_LEVEL"}}
defined $Apache::TestTrace::Level $levels{$Apache::TestTrace::Level} exists $ENV{"APACHE_TEST_TRACE_LEVEL"} $levels{$ENV{"APACHE_TEST_TRACE_LEVEL"}}
Truth table0%0/7
A and B or C and D
A= defined $Apache::TestTrace::LevelB= $levels{$Apache::TestTrace::Level}C= exists $ENV{"APACHE_TEST_TRACE_LEVEL"}D= $levels{$ENV{"APACHE_TEST_TRACE_LEVEL"}}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
136 }
137  
138 1 1;
139 __END__
« blib/lib/Apache/TestCommonPost.pm ↑ Summary blib/lib/Apache/TestMB.pm »