Progress.pm

filter: Statementstmt 84.6% 77 / 91 Branchbran 68.0% 34 / 50 Conditioncond 73.0% 19 / 26 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 86.9% 20 / 23 Podpod 87.5% 7 / 8 totaltotal 76.8% 163 / 212 CC 35 SCAR 17.3
CC
35
Cov
78%
inc
12.0
at
9.1
_build__writer
8.0
CRAP 5.7
« Progress/RenderTTY.pm ↑ Summary
1 package Log::Progress;
2 6 $Log::Progress::VERSION = '0.13';
3 6 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use IO::Handle; # for 'autoflush'
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Conveniently write progress messages to logger or file handle
9  
10  
11 11 ▶has to => ( is => 'rw', isa => \&_assert_valid_output, default => sub { \*STDERR },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 22 ▶ trigger => sub { delete $_[0]{_writer} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
13 1284 ▶sub squelch {
Subroutine squelch: called CC=2 SCAR=8.4
Pod documented
14 1284 my $self= shift;
15 0 ▶ if (@_) { $self->_squelch(shift); $self->_calc_precision_squelch() }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1284 1284
16 1284 $self->{squelch};
17 }
18 1295 ▶sub precision {
Subroutine precision: called CC=2 SCAR=8.4
Pod documented
19 1295 my $self= shift;
20 0 ▶ if (@_) { $self->_precision(shift); $self->_calc_precision_squelch() }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1295 1295
21 1295 $self->{precision};
22 }
23 26 ▶has step_id => ( is => 'rw', default => sub { $ENV{PROGRESS_STEP_ID} },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 7 ▶ trigger => sub { delete $_[0]{_writer} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 6 has current => ( is => 'rw' );
27 6 has total => ( is => 'rw' );
28  
29 6 has _writer => ( is => 'lazy' );
30 6 has _squelch => ( is => 'rw', init_arg => 'squelch' );
31 6 has _precision => ( is => 'rw', init_arg => 'precision' );
32 6 has _last_progress => ( is => 'rw' );
33  
34 33 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
35 33 shift->_calc_precision_squelch();
36 }
37  
38 33 ▶sub _calc_precision_squelch {
Subroutine _calc_precision_squelch: called CC=5 SCAR=16.1
39 33 my $self= shift;
40 33 my $squelch= $self->_squelch;
41 33 my $precision= $self->_precision;
42 33 ▶ if (!defined $squelch && !defined $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $squelch || defined $precision) { } 16 17 33
Condition100%3/3
defined $squelch || defined $precision
l!l&&r!l&&!r
13416
MC/DC100%2/2
defined $squelch || defined $precision
defined $squelch defined $precision
Truth table100%3/3
A || B
A= defined $squelchB= defined $precision
ABresult
1X1
011
000
43 16 $squelch= .01;
44 16 $precision= 2;
45 } else {
46 # calculation for digit length of number of steps
47 17 ▶ defined $precision or $precision= int(log(1/$squelch)/log(10) + .99999);
Branch: 1 branch
BranchTrueFalseTotal
defined $precision or $precision = int log(1 / $squelch) / "2.30258509299405" + "0.99999" 4 13 17
48 17 ▶ defined $squelch or $squelch= 1/(10**$precision);
Branch: 1 branch
BranchTrueFalseTotal
defined $squelch or $squelch = 1 / 10 ** $precision 13 4 17
49 }
50 33 $self->{squelch}= $squelch;
51 33 $self->{precision}= $precision;
52 }
53  
54 33 ▶sub _assert_valid_output {
Subroutine _assert_valid_output: called CC=6 SCAR=18.7
55 33 my $to= shift;
56 33 my $type= ref $to;
57 33 ▶ $type && (
Branch: 1 branch
BranchTrueFalseTotal
$type and $type eq "GLOB" || $type eq "CODE" || $type->can("print") || $type->can("info") or die "$to is not a file handle, logger object, or code reference" 33 0 33
Condition75%9/12
$type and $type eq "GLOB" || $type eq "CODE" || $type->can("print") || $type->can("info")
!ll&&!rl&&r
0033
$type eq "GLOB" or $type eq "CODE" or $type->can("print") or $type->can("info")
l!l&&r!l&&!r
3210
$type eq "GLOB" or $type eq "CODE" or $type->can("print")
l!l&&r!l&&!r
20121
$type eq "GLOB" or $type eq "CODE"
l!l&&r!l&&!r
12813
MC/DC0%0/5
$type and $type eq "GLOB" || $type eq "CODE" || $type->can("print") || $type->can("info")
$type $type eq "GLOB" $type eq "CODE" $type->can("print") $type->can("info")
Truth table66%4/6
A and B or C or D or E
A= $typeB= $type eq "GLOB"C= $type eq "CODE"D= $type->can("print")E= $type->can("info")
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
58 $type eq 'GLOB'
59 or $type eq 'CODE'
60 or $type->can('print')
61 or $type->can('info')
62 ) or die "$to is not a file handle, logger object, or code reference";
63 }
64  
65 17 ▶sub _build__writer {
Subroutine _build__writer: called CC=8 SCAR=20.8
66 17 my $self= shift;
67
68 17 ▶ my $prefix= "progress: ".(defined $self->step_id? $self->step_id.' ' : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->step_id ? : 9 8 17
69 17 my $to= $self->to;
70 17 my $type= ref $to;
71 17 ▶ $to->autoflush(1) if $type eq 'GLOB' or $type->can('autoflush');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "GLOB" or $type->can("autoflush") 9 8 17
Condition100%3/3
$type eq "GLOB" or $type->can("autoflush")
l!l&&r!l&&!r
278
MC/DC100%2/2
$type eq "GLOB" or $type->can("autoflush")
$type eq "GLOB" $type->can("autoflush")
Truth table100%3/3
A or B
A= $type eq "GLOB"B= $type->can("autoflush")
ABresult
1X1
011
000
72 2 ▶ return ($type eq 'GLOB')? sub { print $to $prefix.join('', @_)."\n"; }
Subroutine __ANON__: called CC=1 SCAR=0.0
73 24 ▶ : ($type eq 'CODE')? sub { $to->($prefix.join('', @_)); }
Subroutine __ANON__: called CC=1 SCAR=0.0
74 118 ▶ : ($type->can('print'))? sub { $to->print($prefix.join('', @_)."\n"); }
Subroutine __ANON__: called CC=1 SCAR=0.0
75 1 ▶ : ($type->can('info'))? sub { $to->info($prefix.join('', @_)); }
Subroutine __ANON__: called CC=1 SCAR=0.0
76 17 ▶ : die "unhandled case";
Branch: 4 branches
BranchTrueFalseTotal
$type eq 'GLOB' ? : 2 15 17
$type eq 'CODE' ? : 7 8 15
$type->can('print') ? : 7 1 8
$type->can('info') ? : 1 0 1
77 }
78  
79  
80 1281 ▶sub at {
Subroutine at: called CC=9 SCAR=22.1
Pod documented
81 1281 my ($self, $current, $total, $message)= @_;
82 1281 ▶ if (defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $total) { } 123 1158 1281
83 123 $self->total($total);
84 } else {
85 1158 $total= $self->total;
86 1158 ▶ $total= 1 unless defined $total;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $total 1158 0 1158
87 }
88 1281 $self->current($current);
89 1281 ▶ my $progress= $total? $current / $total : 0;
Branch: 1 branch
BranchTrueFalseTotal
$total ? : 1281 0 1281
90 1281 my $sq= $self->squelch;
91 1281 my $formatted= sprintf("%.*f", $self->precision, int($progress/$sq + .0000000001)*$sq);
92 1281 ▶ return if defined $self->_last_progress
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_last_progress and abs($formatted - $self->_last_progress) + "1e-10" < $sq 1143 138 1281
Condition100%3/3
defined $self->_last_progress and abs($formatted - $self->_last_progress) + "1e-10" < $sq
!ll&&!rl&&r
121261143
MC/DC100%2/2
defined $self->_last_progress and abs($formatted - $self->_last_progress) + "1e-10" < $sq
defined $self->_last_progress abs($formatted - $self->_last_progress) + "1e-10" < $sq
Truth table100%3/3
A and B
A= defined $self->_last_progressB= abs($formatted - $self->_last_progress) + "1e-10" < $sq
ABresult
0X0
100
111
93 and abs($formatted - $self->_last_progress)+.0000000001 < $sq;
94 138 $self->_last_progress($formatted);
95 138 ▶ if ($total != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total != 1) 101 37 138
96 101 ▶ $formatted= (int($current) == $current)? "$current/$total"
Branch: 1 branch
BranchTrueFalseTotal
int $current == $current ? : 101 0 101
97 : sprintf("%.*f/%d", $self->precision, $current, $total);
98 }
99 138 ▶ $self->_writer->($formatted . ($message? " - $message":''));
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 12 126 138
100 }
101  
102 # backward compatibility with version <= 0.03
103 0 ▶sub progress {
Subroutine progress: not called CC=2 SCAR=17.9
Pod documented
104 0 my ($self, $current, $total, $message)= @_;
105 0 ▶ $total= 1 unless defined $total;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $total 0 0 0
106 0 $self->at($current, $total, $message);
107 }
108  
109  
110 0 ▶sub inc {
Subroutine inc: not called CC=3 SCAR=24.8
Pod documented
111 0 my ($self, $offset, $message)= @_;
112 0 ▶ $offset= 1 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
113 0 ▶ $self->at(($self->current || 0) + $offset, undef, $message);
Condition0%0/2
$self->current || 0
l!l
00
MC/DC0%0/1
$self->current || 0
$self->current
Truth table0%0/2
A
A= $self->current
Aresult
11
00
114 }
115  
116  
117 0 ▶sub data {
Subroutine data: not called CC=2 SCAR=17.9
Pod documented
118 0 my ($self, $data)= @_;
119 0 ▶ ref $data eq 'HASH' or croak "data must be a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or croak("data must be a hashref") 0 0 0
120 0 $self->_writer->(JSON->new->ascii->encode($data));
121 }
122  
123  
124 7 ▶sub substep {
Subroutine substep: called CC=5 SCAR=18.1
Pod documented
125 7 my ($self, $step_id, $step_contribution, $title)= @_;
126 7 ▶ length $title or croak "sub-step title is required";
Branch: 1 branch
BranchTrueFalseTotal
length $title or croak("sub-step title is required") 7 0 7
127
128 7 ▶ $step_id= $self->step_id . '.' . $step_id
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->step_id and length $self->step_id 0 7 7
Condition33%1/3
defined $self->step_id and length $self->step_id
!ll&&!rl&&r
700
MC/DC0%0/2
defined $self->step_id and length $self->step_id
defined $self->step_id length $self->step_id
Truth table33%1/3
A and B
A= defined $self->step_idB= length $self->step_id
ABresult
0X0
100
111
129 if defined $self->step_id and length $self->step_id;
130
131 7 my $sub_progress= ref($self)->new(
132 to => $self->to,
133 squelch => $self->_squelch,
134 precision => $self->_precision,
135 step_id => $step_id,
136 );
137
138 7 ▶ if ($step_contribution) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step_contribution) { } 7 0 7
139 7 $sub_progress->_writer->(sprintf("(%.*f) %s", $self->precision, $step_contribution, $title));
140 } else {
141 0 $sub_progress->_writer->("- $title");
142 }
143
144 7 return $sub_progress;
145 }
146  
147 6 1;
148  
149 __END__
« Progress/RenderTTY.pm ↑ Summary