1 |
package WebPAC::Common; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Log::Log4perl qw/get_logger :levels/; |
7 |
use Time::HiRes qw/time/; |
8 |
|
9 |
# If ture, enable logging debug |
10 |
my $log_debug = 0; |
11 |
|
12 |
=head1 NAME |
13 |
|
14 |
WebPAC::Common - internal methods called from other WebPAC modules |
15 |
|
16 |
=head1 VERSION |
17 |
|
18 |
Version 0.02 |
19 |
|
20 |
=cut |
21 |
|
22 |
our $VERSION = '0.02'; |
23 |
|
24 |
=head1 SYNOPSYS |
25 |
|
26 |
This module defines common functions, and is used as base for other, more |
27 |
specific modules. |
28 |
|
29 |
=head1 FUNCTIONS |
30 |
|
31 |
=head2 progress_bar |
32 |
|
33 |
Draw progress bar on STDERR. |
34 |
|
35 |
$webpac->progress_bar($current, $max); |
36 |
|
37 |
=cut |
38 |
|
39 |
sub progress_bar { |
40 |
my $self = shift; |
41 |
|
42 |
my ($curr,$max) = @_; |
43 |
|
44 |
my $log = $self->_get_logger(); |
45 |
|
46 |
$log->logconfess("no current value!") if (! $curr); |
47 |
$log->logconfess("no maximum value!") if (! $max); |
48 |
|
49 |
if ($curr > $max) { |
50 |
$max = $curr; |
51 |
$log->debug("overflow to $curr"); |
52 |
} |
53 |
|
54 |
$self->{'last_pcnt'} ||= 1; |
55 |
$self->{'start_t'} ||= time(); |
56 |
|
57 |
my $p = int($curr * 100 / $max) || 1; |
58 |
|
59 |
# reset on re-run |
60 |
if ($p < $self->{'last_pcnt'}) { |
61 |
$self->{'last_pcnt'} = $p; |
62 |
$self->{'start_t'} = time(); |
63 |
} |
64 |
|
65 |
if ($p != $self->{'last_pcnt'}) { |
66 |
|
67 |
my $t = time(); |
68 |
my $rate = ($curr / ($t - $self->{'start_t'} || 1)); |
69 |
my $eta = ($max-$curr) / ($rate || 1); |
70 |
printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta)); |
71 |
$self->{'last_pcnt'} = $p; |
72 |
$self->{'last_curr'} = $curr; |
73 |
} |
74 |
print STDERR "\n" if ($p == 100); |
75 |
} |
76 |
|
77 |
=head2 fmt_time |
78 |
|
79 |
Format time (in seconds) for display. |
80 |
|
81 |
print $webpac->fmt_time(time()); |
82 |
|
83 |
This method is called by L<progress_bar> to display remaining time. |
84 |
|
85 |
=cut |
86 |
|
87 |
sub fmt_time { |
88 |
my $self = shift; |
89 |
|
90 |
my $t = shift || 0; |
91 |
my $out = ""; |
92 |
|
93 |
my ($ss,$mm,$hh) = gmtime($t); |
94 |
$out .= "${hh}h" if ($hh); |
95 |
$out .= sprintf("%02d:%02d", $mm,$ss); |
96 |
$out .= " " if ($hh == 0); |
97 |
return $out; |
98 |
} |
99 |
|
100 |
# |
101 |
# |
102 |
# |
103 |
|
104 |
=head1 INTERNAL METHODS |
105 |
|
106 |
Here is a quick list of internal methods, mostly useful to turn debugging |
107 |
on them (see L<LOGGING> below for explanation). |
108 |
|
109 |
=cut |
110 |
|
111 |
=head2 _eval |
112 |
|
113 |
Internal function to eval code without C<strict 'subs'>. |
114 |
|
115 |
=cut |
116 |
|
117 |
sub _eval { |
118 |
my $self = shift; |
119 |
|
120 |
my $code = shift || return; |
121 |
|
122 |
my $log = $self->_get_logger(); |
123 |
|
124 |
no strict 'subs'; |
125 |
my $ret = eval $code; |
126 |
if ($@) { |
127 |
$log->error("problem with eval code [$code]: $@"); |
128 |
} |
129 |
|
130 |
$log->debug("eval: ",$code," [",$ret,"]"); |
131 |
|
132 |
return $ret || undef; |
133 |
} |
134 |
|
135 |
=head2 _init_logger |
136 |
|
137 |
This function will init C<Log::Log4perl> using provided configuration file. |
138 |
|
139 |
$webpac->_init_logger('/path/to/log.conf'); |
140 |
|
141 |
If no path to configuration file is given, dummy empty configuration |
142 |
will be created. If any mode which inherits from this one is called |
143 |
with C<debug> flag, it will turn logging to debug level. |
144 |
|
145 |
This function will also read C<log_conf> value from current object and try |
146 |
to read that as configuration file if it exists, if it doesn't it will |
147 |
fallback to default C<conf/log.conf>. |
148 |
|
149 |
You can disable all logging by adding C<no_log> to constructor of WebPAC |
150 |
object. Object in C<Test::Exception> class will disable logging |
151 |
automatically. |
152 |
|
153 |
=cut |
154 |
|
155 |
sub _init_logger { |
156 |
my $self = shift; |
157 |
my $file = shift; |
158 |
$file ||= $self->{'log_conf'}; |
159 |
$file = 'conf/log.conf'; |
160 |
my $name = (caller(2))[3] || caller; |
161 |
|
162 |
my $conf = q( ); |
163 |
if ($self->{'no_log'}) { |
164 |
warn "# $name disabled logging\n" if ($log_debug); |
165 |
} elsif ($self->{'debug'}) { |
166 |
$conf = << '_log4perl_'; |
167 |
|
168 |
log4perl.rootLogger=INFO, SCREEN |
169 |
|
170 |
log4perl.logger.WebPAC.=DEBUG |
171 |
|
172 |
log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen |
173 |
log4perl.appender.SCREEN.layout=PatternLayout |
174 |
log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n |
175 |
|
176 |
_log4perl_ |
177 |
warn "# $name is using debug logger\n" if ($log_debug); |
178 |
} elsif ($name =~ m/Test::Exception/o) { |
179 |
warn "# disabled logging for Text::Exception\n" if ($log_debug); |
180 |
} elsif (-e $file) { |
181 |
warn "# $name is using $file logger\n" if ($log_debug); |
182 |
Log::Log4perl->init($file); |
183 |
return 1; |
184 |
} else { |
185 |
warn "# $name is using null logger\n" if ($log_debug); |
186 |
} |
187 |
Log::Log4perl->init( \$conf ); |
188 |
|
189 |
return 1; |
190 |
} |
191 |
|
192 |
|
193 |
=head2 _get_logger |
194 |
|
195 |
Get C<Log::Log4perl> object with a twist: domains are defined for each |
196 |
method |
197 |
|
198 |
my $log = $webpac->_get_logger(); |
199 |
|
200 |
=cut |
201 |
|
202 |
sub _get_logger { |
203 |
my $self = shift; |
204 |
|
205 |
my $name = (caller(2))[3] || caller; |
206 |
$self->{'_logger_'} ||= $self->_init_logger; |
207 |
|
208 |
my $log = get_logger( $name ); |
209 |
warn "# get_logger( $name ) level ", $log->level, "\n" if ($log_debug); |
210 |
return $log; |
211 |
} |
212 |
|
213 |
|
214 |
=head2 _log |
215 |
|
216 |
Quick cludge to make logging object available to scripts which |
217 |
use webpac line this: |
218 |
|
219 |
my $log = _new WebPAC::Common()->_get_logger(); |
220 |
|
221 |
=cut |
222 |
|
223 |
sub _new { |
224 |
my $class = shift; |
225 |
my $self = {@_}; |
226 |
bless($self, $class); |
227 |
|
228 |
$self ? return $self : return undef; |
229 |
} |
230 |
|
231 |
=head1 LOGGING |
232 |
|
233 |
Logging in WebPAC is performed by L<Log::Log4perl> with config file |
234 |
C<log.conf>. |
235 |
|
236 |
Methods defined above have different levels of logging, so |
237 |
it's descriptions will be useful to turn (mostry B<debug> logging) on |
238 |
or off to see why WabPAC isn't perforing as you expect it (it might even |
239 |
be a bug!). |
240 |
|
241 |
B<This is different from normal Log4perl behaviour>. To repeat, you can |
242 |
also use method names, and not only classes (which are just few) |
243 |
to filter logging. |
244 |
|
245 |
|