/[webpac2]/trunk/lib/WebPAC/Common.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Common.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 924 - (hide annotations)
Wed Oct 31 00:26:45 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 6833 byte(s)
 r1394@llin:  dpavlin | 2007-10-31 01:26:46 +0100
 add new (exported by default) function force_array
 used all over the place

1 dpavlin 3 package WebPAC::Common;
2 dpavlin 924 use Exporter 'import';
3     @EXPORT = qw/
4     force_array
5     /;
6 dpavlin 3
7     use warnings;
8     use strict;
9    
10 dpavlin 16 use Log::Log4perl qw/get_logger :levels/;
11     use Time::HiRes qw/time/;
12 dpavlin 856 use Data::Dump qw/dump/;
13 dpavlin 887 use File::Spec;
14 dpavlin 3
15 dpavlin 887 use base qw/Class::Accessor/;
16     __PACKAGE__->mk_accessors( qw/log_debug no_log debug/ );
17 dpavlin 29
18 dpavlin 3 =head1 NAME
19    
20     WebPAC::Common - internal methods called from other WebPAC modules
21    
22     =head1 VERSION
23    
24 dpavlin 887 Version 0.05
25 dpavlin 3
26     =cut
27    
28 dpavlin 887 our $VERSION = '0.05';
29 dpavlin 3
30 dpavlin 6 =head1 SYNOPSYS
31 dpavlin 4
32 dpavlin 6 This module defines common functions, and is used as base for other, more
33     specific modules.
34    
35 dpavlin 887 my $o = WebPAC::Common->new({
36     log_debug => 1,
37     no_log => 1,
38     debug => 1,
39     });
40    
41     Options:
42    
43     =over 20
44    
45     =item log_debug
46    
47     Generate additional debugging log on C<STDERR>
48    
49     =item no_log
50    
51     Disable all logging (useful for tests)
52    
53     =item debug
54    
55     Use debugging logger which dumps output only yo C<STDERR>
56    
57     =back
58    
59    
60 dpavlin 4 =head1 FUNCTIONS
61    
62 dpavlin 9 =head2 progress_bar
63    
64     Draw progress bar on STDERR.
65    
66     $webpac->progress_bar($current, $max);
67    
68     =cut
69    
70     sub progress_bar {
71     my $self = shift;
72    
73     my ($curr,$max) = @_;
74    
75     my $log = $self->_get_logger();
76    
77 dpavlin 763 $self->{last_pcnt_t} ||= time();
78    
79 dpavlin 9 $log->logconfess("no current value!") if (! $curr);
80     $log->logconfess("no maximum value!") if (! $max);
81    
82     if ($curr > $max) {
83     $max = $curr;
84     $log->debug("overflow to $curr");
85     }
86    
87     $self->{'last_pcnt'} ||= 1;
88     $self->{'start_t'} ||= time();
89    
90     my $p = int($curr * 100 / $max) || 1;
91    
92     # reset on re-run
93     if ($p < $self->{'last_pcnt'}) {
94     $self->{'last_pcnt'} = $p;
95     $self->{'start_t'} = time();
96     }
97    
98 dpavlin 763 my $t = time();
99 dpavlin 9
100 dpavlin 763 if ($p != $self->{'last_pcnt'} || ( $t - $self->{last_pcnt_t} ) > 2 ) {
101    
102 dpavlin 9 my $rate = ($curr / ($t - $self->{'start_t'} || 1));
103     my $eta = ($max-$curr) / ($rate || 1);
104     printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
105     $self->{'last_pcnt'} = $p;
106     $self->{'last_curr'} = $curr;
107 dpavlin 763 $self->{last_pcnt_t} = $t;
108 dpavlin 9 }
109     print STDERR "\n" if ($p == 100);
110     }
111    
112     =head2 fmt_time
113    
114     Format time (in seconds) for display.
115    
116     print $webpac->fmt_time(time());
117    
118     This method is called by L<progress_bar> to display remaining time.
119    
120     =cut
121    
122     sub fmt_time {
123     my $self = shift;
124    
125     my $t = shift || 0;
126     my $out = "";
127    
128     my ($ss,$mm,$hh) = gmtime($t);
129     $out .= "${hh}h" if ($hh);
130     $out .= sprintf("%02d:%02d", $mm,$ss);
131     $out .= " " if ($hh == 0);
132     return $out;
133     }
134    
135 dpavlin 856 =head2 fill_in
136    
137     Fill in variable names by values
138    
139     print $webpac->fill_in( 'foo = $foo bar = $bar',
140     foo => 42, bar => 11,
141     );
142    
143     =cut
144    
145     sub fill_in {
146     my $self = shift;
147    
148     my $format = shift || die "no format?";
149     my $d = {@_};
150    
151     foreach my $n ( keys %$d ) {
152     $format =~ s/\$\Q$n\E/$d->{$n}/gs;
153     }
154    
155     die "unknown variables in '$format' input data = ", dump( $d ) if ( $format =~ m/\$\w+/ );
156    
157     return $format;
158     }
159    
160 dpavlin 9 #
161     #
162     #
163    
164 dpavlin 887 =head2 var_path
165    
166     my $path = $self->var_path('data_dir', 'data_file', ... );
167    
168     =cut
169    
170     sub var_path {
171     my $self = shift;
172    
173     return File::Spec->catfile('var', @_);
174     }
175    
176 dpavlin 924 =head1 EXPORTED NETHODS
177 dpavlin 887
178 dpavlin 924 =head2 force_array
179    
180     my @array = force_array( $ref, sub {
181     warn "reference is undefined!";
182     });
183    
184     =cut
185    
186     sub force_array {
187     my ( $what, $error ) = @_;
188     my @result;
189     if ( ref( $what ) eq 'ARRAY' ) {
190     @result = @{ $what };
191     } elsif ( defined $what ) {
192     @result = ( $what );
193     } else {
194     $error->() if ref($error) eq 'CODE';
195     }
196     return @result;
197     }
198    
199    
200 dpavlin 3 =head1 INTERNAL METHODS
201    
202     Here is a quick list of internal methods, mostly useful to turn debugging
203     on them (see L<LOGGING> below for explanation).
204    
205     =cut
206    
207     =head2 _eval
208    
209     Internal function to eval code without C<strict 'subs'>.
210    
211     =cut
212    
213     sub _eval {
214     my $self = shift;
215    
216     my $code = shift || return;
217    
218     my $log = $self->_get_logger();
219    
220     no strict 'subs';
221     my $ret = eval $code;
222     if ($@) {
223     $log->error("problem with eval code [$code]: $@");
224     }
225    
226     $log->debug("eval: ",$code," [",$ret,"]");
227    
228     return $ret || undef;
229     }
230    
231     =head2 _init_logger
232    
233     This function will init C<Log::Log4perl> using provided configuration file.
234    
235     $webpac->_init_logger('/path/to/log.conf');
236    
237 dpavlin 6 If no path to configuration file is given, dummy empty configuration
238 dpavlin 10 will be created. If any mode which inherits from this one is called
239     with C<debug> flag, it will turn logging to debug level.
240 dpavlin 6
241 dpavlin 29 This function will also read C<log_conf> value from current object and try
242 dpavlin 31 to read that as configuration file if it exists, if it doesn't it will
243     fallback to default C<conf/log.conf>.
244 dpavlin 29
245 dpavlin 31 You can disable all logging by adding C<no_log> to constructor of WebPAC
246     object. Object in C<Test::Exception> class will disable logging
247     automatically.
248    
249 dpavlin 3 =cut
250    
251     sub _init_logger {
252     my $self = shift;
253     my $file = shift;
254 dpavlin 29 $file ||= $self->{'log_conf'};
255     $file = 'conf/log.conf';
256     my $name = (caller(2))[3] || caller;
257 dpavlin 10
258 dpavlin 31 my $conf = q( );
259 dpavlin 887 if ($self->no_log) {
260     warn "# $name disabled logging\n" if $self->log_debug;
261     } elsif ($self->debug) {
262 dpavlin 31 $conf = << '_log4perl_';
263    
264 dpavlin 10 log4perl.rootLogger=INFO, SCREEN
265    
266     log4perl.logger.WebPAC.=DEBUG
267    
268     log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
269     log4perl.appender.SCREEN.layout=PatternLayout
270     log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
271    
272     _log4perl_
273 dpavlin 887 warn "# $name is using debug logger\n" if $self->log_debug;
274 dpavlin 73 } elsif ($name =~ m/Test::Exception/o) {
275 dpavlin 887 warn "# disabled logging for Text::Exception\n" if $self->log_debug;
276 dpavlin 73 } elsif (-e $file) {
277 dpavlin 887 warn "# $name is using $file logger\n" if $self->log_debug;
278 dpavlin 31 Log::Log4perl->init($file);
279 dpavlin 73 return 1;
280 dpavlin 31 } else {
281 dpavlin 887 warn "# $name is using null logger\n" if $self->log_debug;
282 dpavlin 3 }
283 dpavlin 31 Log::Log4perl->init( \$conf );
284    
285     return 1;
286 dpavlin 3 }
287    
288    
289     =head2 _get_logger
290    
291     Get C<Log::Log4perl> object with a twist: domains are defined for each
292     method
293    
294     my $log = $webpac->_get_logger();
295    
296     =cut
297    
298 dpavlin 632 my $_logger_seen;
299    
300 dpavlin 3 sub _get_logger {
301     my $self = shift;
302    
303 dpavlin 633 my $name = (caller(1))[3] || caller;
304    
305     # make name full
306     my $f = '';
307 dpavlin 887 if ( $self->log_debug ) {
308 dpavlin 633 foreach ( 0 .. 5 ) {
309     my $s = (caller($_))[3];
310     $f .= "#### $_ >> $s\n" if ($s);
311     }
312     }
313    
314 dpavlin 29 $self->{'_logger_'} ||= $self->_init_logger;
315 dpavlin 3
316 dpavlin 363 my $log = get_logger( $name );
317 dpavlin 887 warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($self->log_debug && !defined($_logger_seen->{$name}));
318 dpavlin 632 $_logger_seen->{$name}++;
319 dpavlin 363 return $log;
320 dpavlin 3 }
321    
322    
323 dpavlin 254 =head2 _log
324    
325     Quick cludge to make logging object available to scripts which
326     use webpac line this:
327    
328     my $log = _new WebPAC::Common()->_get_logger();
329    
330     =cut
331    
332     sub _new {
333 dpavlin 594 my $class = shift;
334     my $self = {@_};
335 dpavlin 254 bless($self, $class);
336    
337     $self ? return $self : return undef;
338     }
339    
340 dpavlin 3 =head1 LOGGING
341    
342     Logging in WebPAC is performed by L<Log::Log4perl> with config file
343     C<log.conf>.
344    
345     Methods defined above have different levels of logging, so
346     it's descriptions will be useful to turn (mostry B<debug> logging) on
347     or off to see why WabPAC isn't perforing as you expect it (it might even
348     be a bug!).
349    
350     B<This is different from normal Log4perl behaviour>. To repeat, you can
351     also use method names, and not only classes (which are just few)
352     to filter logging.
353    
354 dpavlin 632 =cut
355 dpavlin 3
356 dpavlin 632 1;

  ViewVC Help
Powered by ViewVC 1.1.26