1 |
dpavlin |
3 |
package WebPAC::Common; |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
dpavlin |
16 |
use Log::Log4perl qw/get_logger :levels/; |
7 |
|
|
use Time::HiRes qw/time/; |
8 |
dpavlin |
3 |
|
9 |
dpavlin |
29 |
# If ture, enable logging debug |
10 |
|
|
my $log_debug = 0; |
11 |
|
|
|
12 |
dpavlin |
3 |
=head1 NAME |
13 |
|
|
|
14 |
|
|
WebPAC::Common - internal methods called from other WebPAC modules |
15 |
|
|
|
16 |
|
|
=head1 VERSION |
17 |
|
|
|
18 |
|
|
Version 0.01 |
19 |
|
|
|
20 |
|
|
=cut |
21 |
|
|
|
22 |
|
|
our $VERSION = '0.01'; |
23 |
|
|
|
24 |
dpavlin |
6 |
=head1 SYNOPSYS |
25 |
dpavlin |
4 |
|
26 |
dpavlin |
6 |
This module defines common functions, and is used as base for other, more |
27 |
|
|
specific modules. |
28 |
|
|
|
29 |
dpavlin |
4 |
=head1 FUNCTIONS |
30 |
|
|
|
31 |
dpavlin |
9 |
=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 |
dpavlin |
3 |
=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 |
dpavlin |
6 |
If no path to configuration file is given, dummy empty configuration |
142 |
dpavlin |
10 |
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 |
dpavlin |
6 |
|
145 |
dpavlin |
29 |
This function will also read C<log_conf> value from current object and try |
146 |
dpavlin |
31 |
to read that as configuration file if it exists, if it doesn't it will |
147 |
|
|
fallback to default C<conf/log.conf>. |
148 |
dpavlin |
29 |
|
149 |
dpavlin |
31 |
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 |
dpavlin |
3 |
=cut |
154 |
|
|
|
155 |
|
|
sub _init_logger { |
156 |
|
|
my $self = shift; |
157 |
|
|
my $file = shift; |
158 |
dpavlin |
29 |
$file ||= $self->{'log_conf'}; |
159 |
|
|
$file = 'conf/log.conf'; |
160 |
|
|
my $name = (caller(2))[3] || caller; |
161 |
dpavlin |
10 |
|
162 |
dpavlin |
31 |
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 |
dpavlin |
10 |
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 |
dpavlin |
31 |
warn "# $name is using debug logger\n" if ($log_debug); |
178 |
dpavlin |
73 |
} elsif ($name =~ m/Test::Exception/o) { |
179 |
dpavlin |
32 |
warn "# disabled logging for Text::Exception\n" if ($log_debug); |
180 |
dpavlin |
73 |
} elsif (-e $file) { |
181 |
dpavlin |
31 |
warn "# $name is using $file logger\n" if ($log_debug); |
182 |
|
|
Log::Log4perl->init($file); |
183 |
dpavlin |
73 |
return 1; |
184 |
dpavlin |
31 |
} else { |
185 |
|
|
warn "# $name is using null logger\n" if ($log_debug); |
186 |
dpavlin |
3 |
} |
187 |
dpavlin |
31 |
Log::Log4perl->init( \$conf ); |
188 |
|
|
|
189 |
|
|
return 1; |
190 |
dpavlin |
3 |
} |
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 |
dpavlin |
29 |
my $name = (caller(2))[3] || caller; |
206 |
|
|
$self->{'_logger_'} ||= $self->_init_logger; |
207 |
dpavlin |
3 |
|
208 |
dpavlin |
29 |
warn "# get_logger( $name )\n" if ($log_debug); |
209 |
|
|
|
210 |
dpavlin |
3 |
return get_logger($name); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
|
214 |
|
|
=head1 LOGGING |
215 |
|
|
|
216 |
|
|
Logging in WebPAC is performed by L<Log::Log4perl> with config file |
217 |
|
|
C<log.conf>. |
218 |
|
|
|
219 |
|
|
Methods defined above have different levels of logging, so |
220 |
|
|
it's descriptions will be useful to turn (mostry B<debug> logging) on |
221 |
|
|
or off to see why WabPAC isn't perforing as you expect it (it might even |
222 |
|
|
be a bug!). |
223 |
|
|
|
224 |
|
|
B<This is different from normal Log4perl behaviour>. To repeat, you can |
225 |
|
|
also use method names, and not only classes (which are just few) |
226 |
|
|
to filter logging. |
227 |
|
|
|
228 |
|
|
|