1 package WebPAC::Common;
11 use Log::Log4perl qw/get_logger :levels/;
12 use Time::HiRes qw/time/;
13 use Data::Dump qw/dump/;
17 use base qw/Class::Accessor/;
18 __PACKAGE__->mk_accessors( qw/log_debug no_log debug/ );
22 WebPAC::Common - internal methods called from other WebPAC modules
30 our $VERSION = '0.05';
34 This module defines common functions, and is used as base for other, more
37 my $o = WebPAC::Common->new({
49 Generate additional debugging log on C<STDERR>
53 Disable all logging (useful for tests)
57 Use debugging logger which dumps output only yo C<STDERR>
66 Draw progress bar on STDERR.
68 $webpac->progress_bar($current, $max);
77 my $log = $self->_get_logger();
79 $self->{last_pcnt_t} ||= time();
81 $log->logconfess("no current value!") if (! $curr);
82 $log->logconfess("no maximum value!") if (! $max);
86 $log->debug("overflow to $curr");
89 $self->{'last_pcnt'} ||= 1;
90 $self->{'start_t'} ||= time();
92 my $p = int($curr * 100 / $max) || 1;
95 if ($p < $self->{'last_pcnt'}) {
96 $self->{'last_pcnt'} = $p;
97 $self->{'start_t'} = time();
102 if ($p != $self->{'last_pcnt'} || ( $t - $self->{last_pcnt_t} ) > 2 ) {
104 my $rate = ($curr / ($t - $self->{'start_t'} || 1));
105 my $eta = ($max-$curr) / ($rate || 1);
106 printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta));
107 $self->{'last_pcnt'} = $p;
108 $self->{'last_curr'} = $curr;
109 $self->{last_pcnt_t} = $t;
111 print STDERR "\n" if ($p == 100);
116 Format time (in seconds) for display.
118 print $webpac->fmt_time(time());
120 This method is called by L<progress_bar> to display remaining time.
130 my ($ss,$mm,$hh) = gmtime($t);
131 $out .= "${hh}h" if ($hh);
132 $out .= sprintf("%02d:%02d", $mm,$ss);
133 $out .= " " if ($hh == 0);
139 Fill in variable names by values
141 print $webpac->fill_in( 'foo = $foo bar = $bar',
142 foo => 42, bar => 11,
150 my $format = shift || die "no format?";
153 foreach my $n ( keys %$d ) {
154 $format =~ s/\$\Q$n\E/$d->{$n}/gs;
157 die "unknown variables in '$format' input data = ", dump( $d ) if ( $format =~ m/\$\w+/ );
168 my $path = $self->var_path('data_dir', 'data_file', ... );
178 # $abs_path = abs_path( $0 );
179 # $abs_path =~ s!/WebPAC/Common\.pm!!;
180 $abs_path = '/data/webpac2';
183 return File::Spec->catfile($abs_path, 'var', @_);
186 =head1 EXPORTED NETHODS
190 my @array = force_array( $ref, sub {
191 warn "reference is undefined!";
197 my ( $what, $error ) = @_;
199 if ( ref( $what ) eq 'ARRAY' ) {
200 @result = @{ $what };
201 } elsif ( defined $what ) {
204 $error->() if ref($error) eq 'CODE';
210 =head1 INTERNAL METHODS
212 Here is a quick list of internal methods, mostly useful to turn debugging
213 on them (see L<LOGGING> below for explanation).
219 Internal function to eval code without C<strict 'subs'>.
226 my $code = shift || return;
228 my $log = $self->_get_logger();
231 my $ret = eval $code;
233 $log->error("problem with eval code [$code]: $@");
236 $log->debug("eval: ",$code," [",$ret,"]");
238 return $ret || undef;
243 This function will init C<Log::Log4perl> using provided configuration file.
245 $webpac->_init_logger('/path/to/log.conf');
247 If no path to configuration file is given, dummy empty configuration
248 will be created. If any mode which inherits from this one is called
249 with C<debug> flag, it will turn logging to debug level.
251 This function will also read C<log_conf> value from current object and try
252 to read that as configuration file if it exists, if it doesn't it will
253 fallback to default C<conf/log.conf>.
255 You can disable all logging by adding C<no_log> to constructor of WebPAC
256 object. Object in C<Test::Exception> class will disable logging
264 $file ||= $self->{'log_conf'};
265 $file = 'conf/log.conf';
266 my $name = (caller(2))[3] || caller;
270 warn "# $name disabled logging\n" if $self->log_debug;
271 $Log::Log4perl::Config::CONFIG_INTEGRITY_CHECK = 0;
272 } elsif ($self->debug) {
273 $conf = << '_log4perl_';
275 log4perl.rootLogger=INFO, SCREEN
277 log4perl.logger.WebPAC.=DEBUG
279 log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
280 log4perl.appender.SCREEN.layout=PatternLayout
281 log4perl.appender.SCREEN.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n
284 warn "# $name is using debug logger\n" if $self->log_debug;
285 } elsif ($name =~ m/Test::Exception/o) {
286 warn "# disabled logging for Text::Exception\n" if $self->log_debug;
288 warn "# $name is using $file logger\n" if $self->log_debug;
289 Log::Log4perl->init($file);
292 warn "# $name is using null logger\n" if $self->log_debug;
294 Log::Log4perl->init( \$conf );
302 Get C<Log::Log4perl> object with a twist: domains are defined for each
305 my $log = $webpac->_get_logger();
314 my $name = (caller(1))[3] || caller;
318 if ( $self->log_debug ) {
320 my $s = (caller($_))[3];
321 $f .= "#### $_ >> $s\n" if ($s);
325 $self->{'_logger_'} ||= $self->_init_logger;
327 my $log = get_logger( $name );
328 warn "# get_logger( $name ) level ", $log->level, "\n$f" if ($self->log_debug && !defined($_logger_seen->{$name}));
329 $_logger_seen->{$name}++;
336 Quick cludge to make logging object available to scripts which
337 use webpac line this:
339 my $log = _new WebPAC::Common()->_get_logger();
346 bless($self, $class);
348 $self ? return $self : return undef;
353 Logging in WebPAC is performed by L<Log::Log4perl> with config file
356 Methods defined above have different levels of logging, so
357 it's descriptions will be useful to turn (mostry B<debug> logging) on
358 or off to see why WabPAC isn't perforing as you expect it (it might even
361 B<This is different from normal Log4perl behaviour>. To repeat, you can
362 also use method names, and not only classes (which are just few)