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