| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Log::Progress; |
|
2
|
|
|
|
|
|
|
$Log::Progress::VERSION = '0.12'; |
|
3
|
6
|
|
|
6
|
|
7756
|
use Moo 2; |
|
|
6
|
|
|
|
|
35359
|
|
|
|
6
|
|
|
|
|
40
|
|
|
4
|
6
|
|
|
6
|
|
5458
|
use Carp; |
|
|
6
|
|
|
|
|
15
|
|
|
|
6
|
|
|
|
|
349
|
|
|
5
|
6
|
|
|
6
|
|
1707
|
use IO::Handle; # for 'autoflush' |
|
|
6
|
|
|
|
|
19221
|
|
|
|
6
|
|
|
|
|
299
|
|
|
6
|
6
|
|
|
6
|
|
2121
|
use JSON; |
|
|
6
|
|
|
|
|
25472
|
|
|
|
6
|
|
|
|
|
38
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# ABSTRACT: Conveniently write progress messages to logger or file handle |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has to => ( is => 'rw', isa => \&_assert_valid_output, default => sub { \*STDERR }, |
|
12
|
|
|
|
|
|
|
trigger => sub { delete $_[0]{_writer} } ); |
|
13
|
|
|
|
|
|
|
sub squelch { |
|
14
|
1284
|
|
|
1284
|
1
|
1817
|
my $self= shift; |
|
15
|
1284
|
50
|
|
|
|
2568
|
if (@_) { $self->_squelch(shift); $self->_calc_precision_squelch() } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
16
|
1284
|
|
|
|
|
2541
|
$self->{squelch}; |
|
17
|
|
|
|
|
|
|
} |
|
18
|
|
|
|
|
|
|
sub precision { |
|
19
|
1295
|
|
|
1295
|
1
|
1911
|
my $self= shift; |
|
20
|
1295
|
50
|
|
|
|
2346
|
if (@_) { $self->_precision(shift); $self->_calc_precision_squelch() } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
21
|
1295
|
|
|
|
|
6404
|
$self->{precision}; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
has step_id => ( is => 'rw', default => sub { $ENV{PROGRESS_STEP_ID} }, |
|
24
|
|
|
|
|
|
|
trigger => sub { delete $_[0]{_writer} } ); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has current => ( is => 'rw' ); |
|
27
|
|
|
|
|
|
|
has total => ( is => 'rw' ); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has _writer => ( is => 'lazy' ); |
|
30
|
|
|
|
|
|
|
has _squelch => ( is => 'rw', init_arg => 'squelch' ); |
|
31
|
|
|
|
|
|
|
has _precision => ( is => 'rw', init_arg => 'precision' ); |
|
32
|
|
|
|
|
|
|
has _last_progress => ( is => 'rw' ); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub BUILD { |
|
35
|
33
|
|
|
33
|
0
|
281
|
shift->_calc_precision_squelch(); |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _calc_precision_squelch { |
|
39
|
33
|
|
|
33
|
|
53
|
my $self= shift; |
|
40
|
33
|
|
|
|
|
106
|
my $squelch= $self->_squelch; |
|
41
|
33
|
|
|
|
|
67
|
my $precision= $self->_precision; |
|
42
|
33
|
100
|
100
|
|
|
185
|
if (!defined $squelch && !defined $precision) { |
|
43
|
16
|
|
|
|
|
31
|
$squelch= .01; |
|
44
|
16
|
|
|
|
|
19
|
$precision= 2; |
|
45
|
|
|
|
|
|
|
} else { |
|
46
|
|
|
|
|
|
|
# calculation for digit length of number of steps |
|
47
|
17
|
100
|
|
|
|
76
|
defined $precision or $precision= int(log(1/$squelch)/log(10) + .99999); |
|
48
|
17
|
100
|
|
|
|
48
|
defined $squelch or $squelch= 1/(10**$precision); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
33
|
|
|
|
|
93
|
$self->{squelch}= $squelch; |
|
51
|
33
|
|
|
|
|
360
|
$self->{precision}= $precision; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _assert_valid_output { |
|
55
|
33
|
|
|
33
|
|
767
|
my $to= shift; |
|
56
|
33
|
|
|
|
|
96
|
my $type= ref $to; |
|
57
|
33
|
50
|
66
|
|
|
1040
|
$type && ( |
|
|
|
|
66
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub _build__writer { |
|
66
|
17
|
|
|
17
|
|
208
|
my $self= shift; |
|
67
|
|
|
|
|
|
|
|
|
68
|
17
|
100
|
|
|
|
284
|
my $prefix= "progress: ".(defined $self->step_id? $self->step_id.' ' : ''); |
|
69
|
17
|
|
|
|
|
603
|
my $to= $self->to; |
|
70
|
17
|
|
|
|
|
115
|
my $type= ref $to; |
|
71
|
17
|
100
|
100
|
|
|
403
|
$to->autoflush(1) if $type eq 'GLOB' or $type->can('autoflush'); |
|
72
|
2
|
|
|
2
|
|
13
|
return ($type eq 'GLOB')? sub { print $to $prefix.join('', @_)."\n"; } |
|
73
|
24
|
|
|
24
|
|
203
|
: ($type eq 'CODE')? sub { $to->($prefix.join('', @_)); } |
|
74
|
118
|
|
|
118
|
|
2448
|
: ($type->can('print'))? sub { $to->print($prefix.join('', @_)."\n"); } |
|
75
|
1
|
|
|
1
|
|
6
|
: ($type->can('info'))? sub { $to->info($prefix.join('', @_)); } |
|
76
|
17
|
50
|
|
|
|
1227
|
: die "unhandled case"; |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub at { |
|
81
|
1281
|
|
|
1281
|
1
|
3426480
|
my ($self, $current, $total, $message)= @_; |
|
82
|
1281
|
100
|
|
|
|
2632
|
if (defined $total) { |
|
83
|
123
|
|
|
|
|
1034
|
$self->total($total); |
|
84
|
|
|
|
|
|
|
} else { |
|
85
|
1158
|
|
|
|
|
1747
|
$total= $self->total; |
|
86
|
1158
|
50
|
|
|
|
2175
|
$total= 1 unless defined $total; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
1281
|
|
|
|
|
2857
|
$self->current($current); |
|
89
|
1281
|
50
|
|
|
|
2746
|
my $progress= $total? $current / $total : 0; |
|
90
|
1281
|
|
|
|
|
2280
|
my $sq= $self->squelch; |
|
91
|
1281
|
|
|
|
|
2374
|
my $formatted= sprintf("%.*f", $self->precision, int($progress/$sq + .0000000001)*$sq); |
|
92
|
1281
|
100
|
100
|
|
|
7212
|
return if defined $self->_last_progress |
|
93
|
|
|
|
|
|
|
and abs($formatted - $self->_last_progress)+.0000000001 < $sq; |
|
94
|
138
|
|
|
|
|
675
|
$self->_last_progress($formatted); |
|
95
|
138
|
100
|
|
|
|
410
|
if ($total != 1) { |
|
96
|
101
|
50
|
|
|
|
492
|
$formatted= (int($current) == $current)? "$current/$total" |
|
97
|
|
|
|
|
|
|
: sprintf("%.*f/%d", $self->precision, $current, $total); |
|
98
|
|
|
|
|
|
|
} |
|
99
|
138
|
100
|
|
|
|
6988
|
$self->_writer->($formatted . ($message? " - $message":'')); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# backward compatibility with version <= 0.03 |
|
103
|
|
|
|
|
|
|
sub progress { |
|
104
|
0
|
|
|
0
|
1
|
0
|
my ($self, $current, $total, $message)= @_; |
|
105
|
0
|
0
|
|
|
|
0
|
$total= 1 unless defined $total; |
|
106
|
0
|
|
|
|
|
0
|
$self->at($current, $total, $message); |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub inc { |
|
111
|
0
|
|
|
0
|
1
|
0
|
my ($self, $offset, $message)= @_; |
|
112
|
0
|
0
|
|
|
|
0
|
$offset= 1 unless defined $offset; |
|
113
|
0
|
|
0
|
|
|
0
|
$self->at(($self->current || 0) + $offset, undef, $message); |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub data { |
|
118
|
0
|
|
|
0
|
1
|
0
|
my ($self, $data)= @_; |
|
119
|
0
|
0
|
|
|
|
0
|
ref $data eq 'HASH' or croak "data must be a hashref"; |
|
120
|
0
|
|
|
|
|
0
|
$self->_writer->(JSON->new->ascii->encode($data)); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub substep { |
|
125
|
7
|
|
|
7
|
1
|
127
|
my ($self, $step_id, $step_contribution, $title)= @_; |
|
126
|
7
|
50
|
|
|
|
37
|
length $title or croak "sub-step title is required"; |
|
127
|
|
|
|
|
|
|
|
|
128
|
7
|
50
|
33
|
|
|
175
|
$step_id= $self->step_id . '.' . $step_id |
|
129
|
|
|
|
|
|
|
if defined $self->step_id and length $self->step_id; |
|
130
|
|
|
|
|
|
|
|
|
131
|
7
|
|
|
|
|
164
|
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
|
50
|
|
|
|
19
|
if ($step_contribution) { |
|
139
|
7
|
|
|
|
|
41
|
$sub_progress->_writer->(sprintf("(%.*f) %s", $self->precision, $step_contribution, $title)); |
|
140
|
|
|
|
|
|
|
} else { |
|
141
|
0
|
|
|
|
|
0
|
$sub_progress->_writer->("- $title"); |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
7
|
|
|
|
|
301
|
return $sub_progress; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
__END__ |