blib/lib/Devel/Scope.pm

filter: Statementstmt 100% 122 / 122 Branchbran 83.3% 15 / 18 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 14 / 14 Podpod 14.2% 1 / 7 totaltotal 91.4% 161 / 176 CC 17 SCAR 14.2
CC
17
Cov
96%
debug
10.0
validate_env_vars
4.0
validate_env_vars
4.0
CRAP 4.1
↑ Summary
1 package Devel::Scope;
2  
3 9 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Scope::Upper qw( SCOPE );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Term::Colormap qw( colormap print_colored_text colored_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Time::HiRes qw( tv_interval gettimeofday );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 require Exporter;
13 7 our @ISA = qw( Exporter );
14 40 our @EXPORT_OK = qw( debug debug_enable debug_disable );
15  
16 7 our $VERSION = '0.12';
17  
18 7 my %default_config = (
19 'DEVEL_SCOPE_DEPTH' => 0,
20 'DEVEL_SCOPE_MIN_DECIMAL_PLACES' => 10,
21 'DEVEL_SCOPE_TIME_FORMAT' => '%06f',
22 'DEVEL_SCOPE_TIME_LOG_BASE' => 5,
23 'DEVEL_SCOPE_TIME_LOG_OFFSET' => 4,
24 );
25  
26 6 my %config = validate_env_vars();
27  
28 6 my $time_format = $config{'DEVEL_SCOPE_TIME_FORMAT'};
29 6 my $format_total_and_elapsed = "[ $time_format : $time_format ]";
30  
31 6 my $debug_min_elapsed = 10 ** (-1 * $config{'DEVEL_SCOPE_MIN_DECIMAL_PLACES'} );
32 6 my $log_base = log( $config{ 'DEVEL_SCOPE_TIME_LOG_BASE' } );
33  
34 # blue cyan green yellow orange red
35 6 my $colormap = [ 201, 51, 46, 226, 202, 196 ];
36  
37 6 my $start_time = [ gettimeofday ];
38 7 my $tic = $start_time;
39 7 my $last_file = '';
40  
41 7 debug("Using " . __PACKAGE__ . ' with ');
42 7 for my $key (sort keys %config) {
43 31 debug(" " . $key . '=' . $config{$key});
44 }
45 7 debug("[ total seconds, seconds since last debug call ]");
46 7 debug('D-2 means Depth = 2 ( relative to the file or subroutine )');
47 7 debug('L-4 means Line number 4');
48 7 debug('Stars indicate time spent between debug calls:');
49 7 for my $time_level (1..5) {
50 31 my $sec = seconds_for_time_level($time_level);
51 31 my $color_index = min($time_level, $#$colormap);
52 31 my $stars = colored_text($colormap->[$color_index],
53 "(" . ('*'x$time_level) . ")");
54 31 debug("$stars : > $sec seconds");
55 }
56 7 debug("-"x40);
57  
58 190 ▶sub debug {
Subroutine debug: called CC=10 SCAR=23.0
Pod documented
59 190 ▶ return if not defined $ENV{'DEVEL_SCOPE_DEPTH'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"DEVEL_SCOPE_DEPTH"} 35 155 190
60 155 $config{'DEVEL_SCOPE_DEPTH'} = $ENV{'DEVEL_SCOPE_DEPTH'};
61 155 my $toc = [ gettimeofday ];
62  
63 155 my ($message) = join(' ', @_);
64 155 my $depth = SCOPE(1);
65  
66 155 my ($pack0, $file0, $line0) = caller(); # If in main
67 155 ▶ return if ($depth > $config{'DEVEL_SCOPE_DEPTH'}) and ($pack0 ne __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $config{"DEVEL_SCOPE_DEPTH"} and $pack0 ne __PACKAGE__ 26 129 155
Condition100%3/3
$depth > $config{"DEVEL_SCOPE_DEPTH"} and $pack0 ne __PACKAGE__
!ll&&!rl&&r
874226
MC/DC100%2/2
$depth > $config{"DEVEL_SCOPE_DEPTH"} and $pack0 ne __PACKAGE__
$depth > $config{"DEVEL_SCOPE_DEPTH"} $pack0 ne __PACKAGE__
Truth table100%3/3
A and B
A= $depth > $config{"DEVEL_SCOPE_DEPTH"}B= $pack0 ne __PACKAGE__
ABresult
0X0
100
111
68  
69 129 my ($package, $filename, $line, $subroutine) = caller(1);
70 129 ▶ if (defined $subroutine) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subroutine) { } 106 23 129
71 106 ▶ if ($subroutine eq 'main::' or $subroutine eq '(eval)') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subroutine eq 'main::' or $subroutine eq '(eval)') { } 82 24 106
Condition66%2/3
$subroutine eq "main::" or $subroutine eq "(eval)"
l!l&&r!l&&!r
08224
MC/DC50%1/2
$subroutine eq "main::" or $subroutine eq "(eval)"
$subroutine eq "main::" $subroutine eq "(eval)"
Truth table66%2/3
A or B
A= $subroutine eq "main::"B= $subroutine eq "(eval)"
ABresult
1X1
011
000
72 82 $subroutine = '';
73 } else {
74 24 $subroutine .= "()";
75 }
76 106 $subroutine =~ s|^main::||;
77 } else {
78 23 $line = $line0;
79 23 $subroutine = '';
80 23 $filename = $file0;
81 }
82  
83 129 my $elapsed = tv_interval( $tic, $toc );
84 129 ▶ return unless $elapsed >= $debug_min_elapsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $elapsed >= $debug_min_elapsed 0 129 129
85  
86 129 my $total_elapsed = tv_interval( $start_time, $toc );
87 129 my $time_output = sprintf($format_total_and_elapsed,
88 $total_elapsed, $elapsed);
89  
90 # Time level capped at the number of colors in our colormap
91 129 my $time_level = int( $config{'DEVEL_SCOPE_TIME_LOG_OFFSET'}
92 + ( log($elapsed) / $log_base ));
93  
94 129 ▶ if ($filename eq $last_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq $last_file) { } 123 6 129
95 123 $filename = '';
96 } else {
97 6 output("DEVEL_SCOPE Entering $filename \n");
98 6 $last_file = $filename;
99 }
100  
101 129 output("$time_output D-$depth L-$line $subroutine : ");
102 129 ▶ if ($time_level > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_level > 0) 8 121 129
103 # Highligh longer running steps with stars (* = fastish, ***** = slow )
104 8 my $color_index = min($time_level, $#$colormap);
105 8 print_colored_text($colormap->[$color_index],
106 "(" . ('*'x$time_level) . ")");
107 8 print " : ";
108 }
109 129 output("$message\n");
110 129 $tic = [ gettimeofday ];
111 }
112  
113 35 ▶sub seconds_for_time_level {
Subroutine seconds_for_time_level: called CC=1 SCAR=0.0
Pod undocumented
114 35 my ($level) = @_;
115 35 my $offset = $config{'DEVEL_SCOPE_TIME_LOG_OFFSET'};
116 35 return exp( ($level - $offset) * $log_base );
117 }
118  
119 264 ▶sub output {
Subroutine output: called CC=1 SCAR=0.0
Pod undocumented
120 264 my ($msg) = @_;
121  
122 {
123 264 local $| = 1;
124 264 print $msg;
125 }
126 }
127  
128 1 ▶sub debug_disable {
Subroutine debug_disable: called CC=1 SCAR=0.0
Pod undocumented
129 1 my $previous_value = $ENV{'DEVEL_SCOPE_DEPTH'};
130 1 delete $ENV{'DEVEL_SCOPE_DEPTH'};
131 1 delete $config{'DEVEL_SCOPE_DEPTH'};
132 1 return $previous_value;
133 }
134  
135 1 ▶sub debug_enable {
Subroutine debug_enable: called CC=2 SCAR=7.2
Pod undocumented
136 1 my ($depth) = @_;
137 1 ▶ $depth ||= $default_config{'DEVEL_SCOPE_DEPTH'};
Condition33%1/3
$depth ||= $default_config{"DEVEL_SCOPE_DEPTH"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$depth ||= $default_config{"DEVEL_SCOPE_DEPTH"}
$depth $default_config{"DEVEL_SCOPE_DEPTH"}
Truth table33%1/3
A ||= B
A= $depthB= $default_config{"DEVEL_SCOPE_DEPTH"}
ABresult
1X1
011
000
138  
139 1 $ENV{'DEVEL_SCOPE_DEPTH'} = $depth;
140 1 $config{'DEVEL_SCOPE_DEPTH'} = $depth;
141  
142 1 return $depth;
143 }
144  
145 8 ▶sub validate_env_vars {
Subroutine validate_env_vars: called CC=4 SCAR=13.9
Pod undocumented
Pod undocumented
146 8 my $env_prefix = 'DEVEL_SCOPE_';
147 8 my @env_vars_maybe = grep { m|^$env_prefix| } keys %ENV;
148  
149 8 my %config = %default_config;
150 8 for my $env_var (@env_vars_maybe) {
151 7 ▶ if ( not defined $config{$env_var} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $config{$env_var}) { } 2 5 7
if (not defined $config{$env_var}) { } 0 0 0
152 2 print "Invalid " . __PACKAGE__ . " env variable '$env_var'\n";
153 2 print "Possible variable names: [name=default]\n";
154 2 for my $key ( sort keys %config ) {
155 10 print " " . $key . '=' . $config{$key} . "\n";
156 }
157 2 die "\n";
158 } else {
159 5 $config{ $env_var } = $ENV{ $env_var };
160 }
161 }
162  
163 6 return %config;
164 }
165  
166 7 1; # End of Devel::Scope
167  
168 __END__
↑ Summary